Example #1
0
 // TODO (sam): should we allow this to throw an exception instead of returning null?
 @Nullable
 public <T extends ATNState> T newState(@NotNull Class<T> nodeType, GrammarAST node) {
   try {
     Constructor<T> ctor = nodeType.getConstructor();
     T s = ctor.newInstance();
     if (currentRule == null) s.setRuleIndex(-1);
     else s.setRuleIndex(currentRule.index);
     atn.addState(s);
     return s;
   } catch (Exception e) {
     ErrorManager.internalError("can't create ATN node: " + nodeType.getName(), e);
   }
   return null;
 }
  protected void _createATN(@NotNull Collection<Rule> rules) {
    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);
        rule(r.ast, r.name, h);
      } catch (RecognitionException re) {
        ErrorManager.fatalInternalError("bad grammar AST structure", re);
      }
    }
  }