Exemplo n.º 1
0
  private void findAnonymousContexts(Start ast) {

    ast.apply(
        new DepthFirstAdapter() {

          private final Grammar grammar = Grammar.this;

          @Override
          public void inALexerContext(ALexerContext node) {

            if (node.getName() == null) {
              if (Grammar.this.globalAnonymousContext != null) {
                throw new InternalException(
                    "globalAnonymousContext should not have been created yet");
              }

              Grammar.this.globalAnonymousContext = new Context(this.grammar, node);
            }
          }

          @Override
          public void inAParserContext(AParserContext node) {

            if (node.getName() == null) {
              if (Grammar.this.globalAnonymousContext == null) {
                Grammar.this.globalAnonymousContext = new Context(this.grammar, node);
              } else {
                Grammar.this.globalAnonymousContext.addDeclaration(node);
              }
            }
          }
        });
  }
Exemplo n.º 2
0
  private void fillTreeNameSpace(Start ast) {

    ast.apply(
        new DepthFirstAdapter() {

          private final Grammar grammar = Grammar.this;

          private final TreeNameSpace treeNameSpace = this.grammar.treeNameSpace;

          @Override
          public void inATreeProduction(ATreeProduction node) {

            Tree.TreeProduction treeProduction = new Tree.TreeProduction(node, this.grammar);

            this.treeNameSpace.add(treeProduction);
            Grammar.this.tree.addProduction(treeProduction);
          }
        });
  }
Exemplo n.º 3
0
  private void findInlineExpressions(Start ast) {

    ast.apply(new DeclarationFinder.InlineExpressionsFinder(this));
  }
Exemplo n.º 4
0
  private void fillGlobalNameSpace(Start ast) {

    // add all top-level names, excluding tree names

    ast.apply(
        new DepthFirstAdapter() {

          private final Grammar grammar = Grammar.this;

          private final NameSpace globalNameSpace = this.grammar.globalNameSpace;

          private boolean inLexer;

          private Context currentContext = Grammar.this.globalAnonymousContext;

          @Override
          public void inAGrammar(AGrammar node) {

            this.grammar.declaration = node;
            this.globalNameSpace.add(this.grammar);
          }

          @Override
          public void inANamedExpression(ANamedExpression node) {

            LexerExpression.NamedExpression namedExpression =
                new LexerExpression.NamedExpression(node, this.grammar);

            this.globalNameSpace.add(namedExpression);
            Grammar.this.lexer.addNamedExpression(namedExpression);
          }

          @Override
          public void inALexerContext(ALexerContext node) {

            if (node.getName() != null) {
              Context namedContext = new Context(this.grammar, node);
              this.globalNameSpace.add(namedContext);
              Grammar.this.namedContexts.add(namedContext);
            }
          }

          @Override
          public void inARoot(ARoot node) {

            Grammar.this.parser.addRootDeclaration(node);
          }

          @Override
          public void inAParserContext(AParserContext node) {

            if (node.getName() != null) {
              String name = node.getName().getText();
              Context namedContext = this.globalNameSpace.getNamedContext(name);

              if (namedContext != null) {
                namedContext.addDeclaration(node);
              } else {
                namedContext = new Context(this.grammar, node);
                this.globalNameSpace.add(namedContext);
                Grammar.this.namedContexts.add(namedContext);
              }

              this.currentContext = namedContext;
            }
          }

          @Override
          public void outAParserContext(AParserContext node) {

            this.currentContext = Grammar.this.globalAnonymousContext;
          }

          @Override
          public void inAParserProduction(AParserProduction node) {

            Parser.ParserProduction parserProduction =
                Parser.ParserProduction.newParserProduction(
                    node, this.currentContext, this.grammar);

            this.globalNameSpace.add(parserProduction);
            Grammar.this.parser.addProduction(parserProduction);
          }

          @Override
          public void inALexer(ALexer node) {

            this.inLexer = true;
          }

          @Override
          public void outALexer(ALexer node) {

            this.inLexer = false;
          }

          @Override
          public void inASelector(ASelector node) {

            Selector selector;

            if (this.inLexer) {
              selector = new Selector.LexerSelector(node, this.grammar);
              Grammar.this.lexer.addSelector((Selector.LexerSelector) selector);
            } else {
              selector = new Selector.ParserSelector(node, this.grammar);
              Grammar.this.parser.addSelector((Selector.ParserSelector) selector);
            }

            for (Selector.Selection selection : selector.getSelections()) {
              this.globalNameSpace.add(selection);
            }

            this.globalNameSpace.add(selector);
          }

          @Override
          public void inAInvestigator(AInvestigator node) {

            if (this.inLexer) {
              Investigator.LexerInvestigator investigator =
                  new Investigator.LexerInvestigator(node, this.grammar);
              this.globalNameSpace.add(investigator);

              Grammar.this.lexer.addInvestigator(investigator);
            } else {
              Investigator.ParserInvestigator investigator =
                  new Investigator.ParserInvestigator(node, this.grammar);

              this.globalNameSpace.add(investigator);
              Grammar.this.parser.addInvestigator(investigator);
            }
          }
        });
  }