Package org.antlr.v4.parse

Examples of org.antlr.v4.parse.GrammarASTAdaptor


     *
     *  Parameter must be a token.
     *  todo: do we want?
     */
    public void expandParameterizedLoops(GrammarAST root) {
        TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
        v.visit(root, new TreeVisitorAction() {
            @Override
            public Object pre(Object t) {
                if ( ((GrammarAST)t).getType() == 3 ) {
                    return expandParameterizedLoop((GrammarAST)t);
View Full Code Here


    /** Utility visitor that sets grammar ptr in each node */
  public static void setGrammarPtr(final Grammar g, GrammarAST tree) {
    if ( tree==null ) return;
    // ensure each node has pointer to surrounding grammar
    TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
    v.visit(tree, new TreeVisitorAction() {
      @Override
      public Object pre(Object t) { ((GrammarAST)t).g = g; return t; }
      @Override
      public Object post(Object t) { return t; }
View Full Code Here

    List<Grammar> imports = rootGrammar.getAllImportedGrammars();
    if ( imports==null ) return;

    GrammarAST root = rootGrammar.ast;
    GrammarAST id = (GrammarAST) root.getChild(0);
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(id.token.getInputStream());

     GrammarAST tokensRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.TOKENS_SPEC);

    List<GrammarAST> actionRoots = root.getNodesWithType(ANTLRParser.AT);

    // Compute list of rules in root grammar and ensure we have a RULES node
    GrammarAST RULES = (GrammarAST)root.getFirstChildWithType(ANTLRParser.RULES);
    Set<String> rootRuleNames = new HashSet<String>();
    if ( RULES==null ) { // no rules in root, make RULES node, hook in
      RULES = (GrammarAST)adaptor.create(ANTLRParser.RULES, "RULES");
      RULES.g = rootGrammar;
      root.addChild(RULES);
    }
    else {
      // make list of rules we have in root grammar
      List<GrammarAST> rootRules = RULES.getNodesWithType(ANTLRParser.RULE);
      for (GrammarAST r : rootRules) rootRuleNames.add(r.getChild(0).getText());
    }

    for (Grammar imp : imports) {
      // COPY TOKENS
      GrammarAST imp_tokensRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.TOKENS_SPEC);
      if ( imp_tokensRoot!=null ) {
        rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren());
        if ( tokensRoot==null ) {
          tokensRoot = (GrammarAST)adaptor.create(ANTLRParser.TOKENS_SPEC, "TOKENS");
          tokensRoot.g = rootGrammar;
          root.insertChild(1, tokensRoot); // ^(GRAMMAR ID TOKENS...)
        }
        tokensRoot.addChildren(Arrays.asList(imp_tokensRoot.getChildren().toArray(new Tree[0])));
      }
View Full Code Here

   *                adding to lexer to avoid "who's ur daddy" issues
   */
  public GrammarRootAST extractImplicitLexer(Grammar combinedGrammar) {
    GrammarRootAST combinedAST = combinedGrammar.ast;
    //tool.log("grammar", "before="+combinedAST.toStringTree());
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(combinedAST.token.getInputStream());
    GrammarAST[] elements = combinedAST.getChildren().toArray(new GrammarAST[0]);

    // MAKE A GRAMMAR ROOT and ID
    String lexerName = combinedAST.getChild(0).getText()+"Lexer";
    GrammarRootAST lexerAST =
        new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream);
    lexerAST.grammarType = ANTLRParser.LEXER;
    lexerAST.token.setInputStream(combinedAST.token.getInputStream());
    lexerAST.addChild((GrammarAST)adaptor.create(ANTLRParser.ID, lexerName));

    // COPY OPTIONS
    GrammarAST optionsRoot =
      (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS);
    if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) {
      GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
      lexerAST.addChild(lexerOptionsRoot);
      GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);
      for (GrammarAST o : options) {
        String optionName = o.getChild(0).getText();
        if ( Grammar.lexerOptions.contains(optionName) &&
           !Grammar.doNotCopyOptionsToLexer.contains(optionName) )
        {
          GrammarAST optionTree = (GrammarAST)adaptor.dupTree(o);
          lexerOptionsRoot.addChild(optionTree);
          lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1));
        }
      }
    }

    // COPY all named actions, but only move those with lexer:: scope
    List<GrammarAST> actionsWeMoved = new ArrayList<GrammarAST>();
    for (GrammarAST e : elements) {
      if ( e.getType()==ANTLRParser.AT ) {
        lexerAST.addChild((Tree)adaptor.dupTree(e));
        if ( e.getChild(0).getText().equals("lexer") ) {
          actionsWeMoved.add(e);
        }
      }
    }

    for (GrammarAST r : actionsWeMoved) {
      combinedAST.deleteChild( r );
    }

    GrammarAST combinedRulesRoot =
      (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.RULES);
    if ( combinedRulesRoot==null ) return lexerAST;

    // MOVE lexer rules

    GrammarAST lexerRulesRoot =
      (GrammarAST)adaptor.create(ANTLRParser.RULES, "RULES");
    lexerAST.addChild(lexerRulesRoot);
    List<GrammarAST> rulesWeMoved = new ArrayList<GrammarAST>();
    GrammarASTWithOptions[] rules;
    if (combinedRulesRoot.getChildCount() > 0) {
      rules = combinedRulesRoot.getChildren().toArray(new GrammarASTWithOptions[0]);
    }
    else {
      rules = new GrammarASTWithOptions[0];
    }

    for (GrammarASTWithOptions r : rules) {
      String ruleName = r.getChild(0).getText();
      if (Grammar.isTokenName(ruleName)) {
        lexerRulesRoot.addChild((Tree)adaptor.dupTree(r));
        rulesWeMoved.add(r);
      }
    }
    for (GrammarAST r : rulesWeMoved) {
      combinedRulesRoot.deleteChild( r );
View Full Code Here

  }

  /** Given list of X and r refs in alt, compute how many of each there are */
  protected FrequencySet<String> getElementFrequenciesForAlt(AltAST ast) {
    try {
      ElementFrequenciesVisitor visitor = new ElementFrequenciesVisitor(new CommonTreeNodeStream(new GrammarASTAdaptor(), ast));
      visitor.outerAlternative();
      if (visitor.frequencies.size() != 1) {
        factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR);
        return new FrequencySet<String>();
      }
View Full Code Here

    }

  public GrammarAST dupTree() {
    GrammarAST t = this;
    CharStream input = this.token.getInputStream();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
    return (GrammarAST)adaptor.dupTree(t);
  }
View Full Code Here

    return (GrammarAST)adaptor.dupTree(t);
  }

  public String toTokenString() {
    CharStream input = this.token.getInputStream();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
    CommonTreeNodeStream nodes =
      new CommonTreeNodeStream(adaptor, this);
    StringBuilder buf = new StringBuilder();
    GrammarAST o = (GrammarAST)nodes.LT(1);
    int type = adaptor.getType(o);
    while ( type!=Token.EOF ) {
      buf.append(" ");
      buf.append(o.getText());
      nodes.consume();
      o = (GrammarAST)nodes.LT(1);
      type = adaptor.getType(o);
    }
    return buf.toString();
  }
View Full Code Here

    return parse("<string>", new ANTLRStringStream(grammar));
  }

  public GrammarRootAST parse(String fileName, CharStream in) {
    try {
      GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
      ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      lexer.tokens = tokens;
      ToolANTLRParser p = new ToolANTLRParser(tokens, this);
      p.setTreeAdaptor(adaptor);
View Full Code Here

    for (GrammarAST r : ast.getNodesWithType(ANTLRParser.RULE_REF)) {
      if ( r.getParent().getType()==ANTLRParser.RULE ) continue; // must be rule def
      if ( ((GrammarASTWithOptions)r).getOptionString(PRECEDENCE_OPTION_NAME) != null ) continue; // already has arg; must be in rewritten rule
      if ( leftRecursiveRuleNames.contains(r.getText()) ) {
        // found ref to recursive rule not already rewritten with arg
        ((GrammarASTWithOptions)r).setOption(PRECEDENCE_OPTION_NAME, (GrammarAST)new GrammarASTAdaptor().create(ANTLRParser.INT, "0"));
      }
    }
  }
View Full Code Here

    return true;
  }

  public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
View Full Code Here

TOP

Related Classes of org.antlr.v4.parse.GrammarASTAdaptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.