Example #1
0
  /** For testing; builds trees, does sem anal */
  public Grammar(String fileName, String grammarText, @Nullable ANTLRToolListener listener)
      throws org.antlr.runtime.RecognitionException {
    this.text = grammarText;
    this.fileName = fileName;
    this.tool = new Tool();
    this.tool.addListener(listener);
    org.antlr.runtime.ANTLRStringStream in = new org.antlr.runtime.ANTLRStringStream(grammarText);
    in.name = fileName;

    this.ast = tool.load(in);
    if (ast == null) return;

    // ensure each node has pointer to surrounding grammar
    final Grammar thiz = this;
    TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
    v.visit(
        ast,
        new TreeVisitorAction() {
          @Override
          public Object pre(Object t) {
            ((GrammarAST) t).g = thiz;
            return t;
          }

          @Override
          public Object post(Object t) {
            return t;
          }
        });
    initTokenSymbolTables();

    tool.process(this, false);
  }
Example #2
0
  /** For testing; builds trees, does sem anal */
  public Grammar(
      String fileName,
      String grammarText,
      Grammar tokenVocabSource,
      @Nullable ANTLRToolListener listener)
      throws org.antlr.runtime.RecognitionException {
    this.text = grammarText;
    this.fileName = fileName;
    this.tool = new Tool();
    this.tool.addListener(listener);
    org.antlr.runtime.ANTLRStringStream in = new org.antlr.runtime.ANTLRStringStream(grammarText);
    in.name = fileName;

    this.ast = tool.parse(fileName, in);
    if (ast == null) {
      throw new UnsupportedOperationException();
    }

    if (ast.tokenStream == null) {
      throw new IllegalStateException("expected ast to have a token stream");
    }

    this.tokenStream = ast.tokenStream;

    // ensure each node has pointer to surrounding grammar
    final Grammar thiz = this;
    org.antlr.runtime.tree.TreeVisitor v =
        new org.antlr.runtime.tree.TreeVisitor(new GrammarASTAdaptor());
    v.visit(
        ast,
        new org.antlr.runtime.tree.TreeVisitorAction() {
          @Override
          public Object pre(Object t) {
            ((GrammarAST) t).g = thiz;
            return t;
          }

          @Override
          public Object post(Object t) {
            return t;
          }
        });
    initTokenSymbolTables();

    if (tokenVocabSource != null) {
      importVocab(tokenVocabSource);
    }

    tool.process(this, false);
  }