Example #1
0
 public ATN createATN() {
   _createATN(g.rules.values());
   atn.maxTokenType = g.getMaxTokenType();
   addRuleFollowLinks();
   addEOFTransitionToStartRules();
   return atn;
 }
  @NotNull
  @Override
  public ATN createATN() {
    _createATN(g.rules.values());
    assert atn.maxTokenType == g.getMaxTokenType();
    addRuleFollowLinks();
    addEOFTransitionToStartRules();
    ATNOptimizer.optimize(g, atn);

    for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonClosureBlocks) {
      LL1Analyzer analyzer = new LL1Analyzer(atn);
      if (analyzer.LOOK(pair.b, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
        ErrorType errorType =
            pair.a instanceof LeftRecursiveRule
                ? ErrorType.EPSILON_LR_FOLLOW
                : ErrorType.EPSILON_CLOSURE;
        g.tool.errMgr.grammarError(
            errorType, g.fileName, ((GrammarAST) pair.a.ast.getChild(0)).getToken(), pair.a.name);
      }
    }

    optionalCheck:
    for (Triple<Rule, ATNState, ATNState> pair : preventEpsilonOptionalBlocks) {
      int bypassCount = 0;
      for (int i = 0; i < pair.b.getNumberOfTransitions(); i++) {
        ATNState startState = pair.b.transition(i).target;
        if (startState == pair.c) {
          bypassCount++;
          continue;
        }

        LL1Analyzer analyzer = new LL1Analyzer(atn);
        if (analyzer.LOOK(startState, pair.c, null).contains(org.antlr.v4.runtime.Token.EPSILON)) {
          g.tool.errMgr.grammarError(
              ErrorType.EPSILON_OPTIONAL,
              g.fileName,
              ((GrammarAST) pair.a.ast.getChild(0)).getToken(),
              pair.a.name);
          continue optionalCheck;
        }
      }

      if (bypassCount != 1) {
        throw new UnsupportedOperationException(
            "Expected optional block with exactly 1 bypass alternative.");
      }
    }

    return atn;
  }