Examples of GrammarAST


Examples of org.antlr.v4.tool.ast.GrammarAST

    createRuleStartAndStopATNStates();

    GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
    for (Rule r : rules) {
      // find rule's block
      GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
      CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
      ATNBuilder b = new ATNBuilder(nodes,this);
      try {
        setCurrentRuleName(r.name);
        Handle h = b.ruleBlock(null);
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

  // TODO: this strips the tree properly, but since text()
  // uses the start of stop token index and gets text from that
  // ineffectively ignores this routine.
  public GrammarAST stripLeftRecursion(GrammarAST altAST) {
    GrammarAST lrlabel=null;
    GrammarAST first = (GrammarAST)altAST.getChild(0);
    int leftRecurRuleIndex = 0;
    if ( first.getType() == ELEMENT_OPTIONS ) {
      first = (GrammarAST)altAST.getChild(1);
      leftRecurRuleIndex = 1;
    }
    Tree rref = first.getChild(1); // if label=rule
    if ( (first.getType()==RULE_REF && first.getText().equals(ruleName)) ||
       (rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName)) )
    {
      if ( first.getType()==ASSIGN || first.getType()==PLUS_ASSIGN ) lrlabel = (GrammarAST)first.getChild(0);
      // remove rule ref (first child unless options present)
      altAST.deleteChild(leftRecurRuleIndex);
      // reset index so it prints properly (sets token range of
      // ALT to start to right of left recur rule we deleted)
      GrammarAST newFirstChild = (GrammarAST)altAST.getChild(leftRecurRuleIndex);
      altAST.setTokenStartIndex(newFirstChild.getTokenStartIndex());
    }
    return lrlabel;
  }
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

      Token tok = tokenStream.get(i);

      StringBuilder elementOptions = new StringBuilder();
      if (!noOptions.contains(i)) {
        GrammarAST node = t.getNodeWithTokenIndex(tok.getTokenIndex());
        if ( node!=null &&
           (tok.getType()==TOKEN_REF ||
            tok.getType()==STRING_LITERAL ||
            tok.getType()==RULE_REF) )
        {
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

  public static void augmentTokensWithOriginalPosition(final Grammar g, GrammarAST tree) {
    if ( tree==null ) return;

    List<GrammarAST> optionsSubTrees = tree.getNodesWithType(ANTLRParser.ELEMENT_OPTIONS);
    for (int i = 0; i < optionsSubTrees.size(); i++) {
      GrammarAST t = optionsSubTrees.get(i);
      CommonTree elWithOpt = t.parent;
      if ( elWithOpt instanceof GrammarASTWithOptions ) {
        Map<String, GrammarAST> options = ((GrammarASTWithOptions) elWithOpt).getOptions();
        if ( options.containsKey(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME) ) {
          GrammarToken newTok = new GrammarToken(g, elWithOpt.getToken());
          newTok.originalTokenIndex = Integer.valueOf(options.get(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME).getText());
          elWithOpt.token = newTok;

          GrammarAST originalNode = g.ast.getNodeWithTokenIndex(newTok.getTokenIndex());
          if (originalNode != null) {
            // update the AST node start/stop index to match the values
            // of the corresponding node in the original parse tree.
            elWithOpt.setTokenStartIndex(originalNode.getTokenStartIndex());
            elWithOpt.setTokenStopIndex(originalNode.getTokenStopIndex());
          }
          else {
            // the original AST node could not be located by index;
            // make sure to assign valid values for the start/stop
            // index so toTokenString will not throw exceptions.
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

   */
  public void integrateImportedGrammars(Grammar rootGrammar) {
    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])));
      }

      List<GrammarAST> all_actionRoots = new ArrayList<GrammarAST>();
      List<GrammarAST> imp_actionRoots = imp.ast.getAllChildrenWithType(ANTLRParser.AT);
      if ( actionRoots!=null ) all_actionRoots.addAll(actionRoots);
      all_actionRoots.addAll(imp_actionRoots);

      // COPY ACTIONS
      if ( imp_actionRoots!=null ) {
        DoubleKeyMap<String, String, GrammarAST> namedActions =
          new DoubleKeyMap<String, String, GrammarAST>();

        rootGrammar.tool.log("grammar", "imported actions: "+imp_actionRoots);
        for (GrammarAST at : all_actionRoots) {
          String scopeName = rootGrammar.getDefaultActionScope();
          GrammarAST scope, name, action;
          if ( at.getChildCount()>2 ) { // must have a scope
            scope = (GrammarAST)at.getChild(0);
            scopeName = scope.getText();
            name = (GrammarAST)at.getChild(1);
            action = (GrammarAST)at.getChild(2);
          }
          else {
            name = (GrammarAST)at.getChild(0);
            action = (GrammarAST)at.getChild(1);
          }
          GrammarAST prevAction = namedActions.get(scopeName, name.getText());
          if ( prevAction==null ) {
            namedActions.put(scopeName, name.getText(), action);
          }
          else {
            if ( prevAction.g == at.g ) {
              rootGrammar.tool.errMgr.grammarError(ErrorType.ACTION_REDEFINITION,
                        at.g.fileName, name.token, name.getText());
            }
            else {
              String s1 = prevAction.getText();
              s1 = s1.substring(1, s1.length()-1);
              String s2 = action.getText();
              s2 = s2.substring(1, s2.length()-1);
              String combinedAction = "{"+s1 + '\n'+ s2+"}";
              prevAction.token.setText(combinedAction);
            }
          }
        }
        // at this point, we have complete list of combined actions,
        // some of which are already living in root grammar.
        // Merge in any actions not in root grammar into root's tree.
        for (String scopeName : namedActions.keySet()) {
          for (String name : namedActions.keySet(scopeName)) {
            GrammarAST action = namedActions.get(scopeName, name);
            rootGrammar.tool.log("grammar", action.g.name+" "+scopeName+":"+name+"="+action.getText());
            if ( action.g != rootGrammar ) {
              root.insertChild(1, action.getParent());
            }
          }
        }
      }

      // COPY RULES
      List<GrammarAST> rules = imp.ast.getNodesWithType(ANTLRParser.RULE);
      if ( rules!=null ) {
        for (GrammarAST r : rules) {
          rootGrammar.tool.log("grammar", "imported rule: "+r.toStringTree());
          String name = r.getChild(0).getText();
          boolean rootAlreadyHasRule = rootRuleNames.contains(name);
          if ( !rootAlreadyHasRule ) {
            RULES.addChild(r); // merge in if not overridden
            rootRuleNames.add(name);
          }
        }
      }

      GrammarAST optionsRoot = (GrammarAST)imp.ast.getFirstChildWithType(ANTLRParser.OPTIONS);
      if ( optionsRoot!=null ) {
        // suppress the warning if the options match the options specified
        // in the root grammar
        // https://github.com/antlr/antlr4/issues/707

View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

    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 );
    }

    // Will track 'if' from IF : 'if' ; rules to avoid defining new token for 'if'
    List<Pair<GrammarAST,GrammarAST>> litAliases =
      Grammar.getStringLiteralAliasesFromLexerRules(lexerAST);

    Set<String> stringLiterals = combinedGrammar.getStringLiterals();
    // add strings from combined grammar (and imported grammars) into lexer
    // put them first as they are keywords; must resolve ambigs to these rules
//    tool.log("grammar", "strings from parser: "+stringLiterals);
    int insertIndex = 0;
    nextLit:
    for (String lit : stringLiterals) {
      // if lexer already has a rule for literal, continue
      if ( litAliases!=null ) {
        for (Pair<GrammarAST,GrammarAST> pair : litAliases) {
          GrammarAST litAST = pair.b;
          if ( lit.equals(litAST.getText()) ) continue nextLit;
        }
      }
      // create for each literal: (RULE <uniquename> (BLOCK (ALT <lit>))
      String rname = combinedGrammar.getStringLiteralLexerRuleName(lit);
      // can't use wizard; need special node types
      GrammarAST litRule = new RuleAST(ANTLRParser.RULE);
      BlockAST blk = new BlockAST(ANTLRParser.BLOCK);
      AltAST alt = new AltAST(ANTLRParser.ALT);
      TerminalAST slit = new TerminalAST(new CommonToken(ANTLRParser.STRING_LITERAL, lit));
      alt.addChild(slit);
      blk.addChild(alt);
      CommonToken idToken = new CommonToken(ANTLRParser.TOKEN_REF, rname);
      litRule.addChild(new TerminalAST(idToken));
      litRule.addChild(blk);
      lexerRulesRoot.insertChild(insertIndex, litRule);
//      lexerRulesRoot.getChildren().add(0, litRule);
      lexerRulesRoot.freshenParentAndChildIndexes(); // reset indexes and set litRule parent

      // next literal will be added after the one just added
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

   * this label. Unlabeled alternatives are not included in the result.
   */
  public Map<String, List<Pair<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Pair<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Pair<Integer, AltAST>>>();
    for (int i=1; i<=numberOfAlts; i++) {
      GrammarAST altLabel = alt[i].ast.altLabel;
      if ( altLabel!=null ) {
        List<Pair<Integer, AltAST>> list = labels.get(altLabel.getText());
        if (list == null) {
          list = new ArrayList<Pair<Integer, AltAST>>();
          labels.put(altLabel.getText(), list);
        }

        list.add(new Pair<Integer, AltAST>(i, alt[i].ast));
      }
    }
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

  }

  public List<AltAST> getUnlabeledAltASTs() {
    List<AltAST> alts = new ArrayList<AltAST>();
    for (int i=1; i<=numberOfAlts; i++) {
      GrammarAST altLabel = alt[i].ast.altLabel;
      if ( altLabel==null ) alts.add(alt[i].ast);
    }
    if ( alts.isEmpty() ) return null;
    return alts;
  }
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

    CodeGenerator gen = factory.getGenerator();
    // Since we delete x=lr, we have to manually add decls for all labels
    // on left-recur refs to proper structs
    for (Pair<GrammarAST,String> pair : r.leftRecursiveRuleRefLabels) {
      GrammarAST idAST = pair.a;
      String altLabel = pair.b;
      String label = idAST.getText();
      GrammarAST rrefAST = (GrammarAST)idAST.getParent().getChild(1);
      if ( rrefAST.getType() == ANTLRParser.RULE_REF ) {
        Rule targetRule = factory.getGrammar().getRule(rrefAST.getText());
        String ctxName = gen.getTarget().getRuleFunctionContextStructName(targetRule);
        RuleContextDecl d;
        if (idAST.getParent().getType() == ANTLRParser.ASSIGN) {
          d = new RuleContextDecl(factory, label, ctxName);
        }
View Full Code Here

Examples of org.antlr.v4.tool.ast.GrammarAST

    assert ast.atnState != null
      && ast.atnState.getNumberOfTransitions() == 1
      && ast.atnState.transition(0) instanceof AbstractPredicateTransition;

    GrammarAST failNode = ast.getOptionAST("fail");
    CodeGenerator gen = factory.getGenerator();
    predicate = ast.getText();
    if (predicate.startsWith("{") && predicate.endsWith("}?")) {
      predicate = predicate.substring(1, predicate.length() - 2);
    }
    predicate = gen.getTarget().getTargetStringLiteralFromString(predicate);

    if ( failNode==null ) return;

    if ( failNode instanceof ActionAST ) {
      ActionAST failActionNode = (ActionAST)failNode;
      RuleFunction rf = factory.getCurrentRuleFunction();
      failChunks = ActionTranslator.translateAction(factory, rf,
                              failActionNode.token,
                              failActionNode);
    }
    else {
      msg = gen.getTarget().getTargetStringLiteralFromANTLRStringLiteral(gen,
                                      failNode.getText(),
                                      true);
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.