Package org.antlr.v4.automata

Examples of org.antlr.v4.automata.TailEpsilonRemover


    LoopEndState end = newState(LoopEndState.class, starAST);
    StarLoopbackState loop = newState(StarLoopbackState.class, starAST);
    entry.loopBackState = loop;
    end.loopBackState = loop;

    BlockAST blkAST = (BlockAST)starAST.getChild(0);
    if ( ((QuantifierAST)starAST).isGreedy() ) {
      if (expectNonGreedy(blkAST)) {
        g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, starAST.getToken(), starAST.getToken().getText());
      }
View Full Code Here


    AltAST altTree = (AltAST)originalAltTree.dupTree();
    String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null;

    String label = null;
    boolean isListLabel = false;
    GrammarAST lrlabel = stripLeftRecursion(altTree);
    if ( lrlabel!=null ) {
      label = lrlabel.getText();
      isListLabel = lrlabel.getParent().getType() == PLUS_ASSIGN;
      leftRecursiveRuleRefLabels.add(new Pair<GrammarAST,String>(lrlabel,altLabel));
    }

    stripAltLabel(altTree);
View Full Code Here

    AltAST altTree = (AltAST)originalAltTree.dupTree();
    String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null;

    String label = null;
    boolean isListLabel = false;
    GrammarAST lrlabel = stripLeftRecursion(altTree);
    if ( lrlabel!=null ) {
      label = lrlabel.getText();
      isListLabel = lrlabel.getParent().getType() == PLUS_ASSIGN;
      leftRecursiveRuleRefLabels.add(new Pair<GrammarAST,String>(lrlabel,altLabel));
    }
    stripAltLabel(altTree);
    String altText = text(altTree);
    altText = altText.trim();
View Full Code Here

    for (GrammarAST x : outerAltRuleRefs) {
      RuleRefAST rref = (RuleRefAST)x;
      boolean recursive = rref.getText().equals(ruleName);
      boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size()-1);
      if ( recursive && rightmost ) {
        GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, ""+prec));
        rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
      }
    }
    return t;
  }
View Full Code Here

   * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*)))
   * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*)))
   */
  public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) {
    if ( t==null ) return false;
    GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK);
    if ( blk==null ) return false;
    int n = blk.getChildren().size();
    for (int i = 0; i < n; i++) {
      GrammarAST alt = (GrammarAST)blk.getChildren().get(i);
      Tree first = alt.getChild(0);
      if ( first==null ) continue;
      if (first.getType() == ELEMENT_OPTIONS) {
        first = alt.getChild(1);
        if (first == null) {
          continue;
        }
      }
      if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true;
View Full Code Here

    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

  // 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

      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

        {
          elementOptions.append("tokenIndex=").append(tok.getTokenIndex());
        }

        if ( node instanceof GrammarASTWithOptions ) {
          GrammarASTWithOptions o = (GrammarASTWithOptions)node;
          for (Map.Entry<String, GrammarAST> entry : o.getOptions().entrySet()) {
            if (elementOptions.length() > 0) {
              elementOptions.append(',');
            }

            elementOptions.append(entry.getKey());
View Full Code Here

  public AltAST addPrecedenceArgToRules(AltAST t, int prec) {
    if ( t==null ) return null;
    // get all top-level rule refs from ALT
    List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF));
    for (GrammarAST x : outerAltRuleRefs) {
      RuleRefAST rref = (RuleRefAST)x;
      boolean recursive = rref.getText().equals(ruleName);
      boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size()-1);
      if ( recursive && rightmost ) {
        GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, ""+prec));
        rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
      }
    }
    return t;
  }
View Full Code Here

TOP

Related Classes of org.antlr.v4.automata.TailEpsilonRemover

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.