/** For testing; builds trees, does sem anal */ public Grammar(string fileName, string grammarText, Grammar tokenVocabSource, [Nullable] ANTLRToolListener listener) { this.text = grammarText; this.fileName = fileName; this.tool = new AntlrTool(); this.tool.AddListener(listener); Antlr.Runtime.ANTLRStringStream @in = new Antlr.Runtime.ANTLRStringStream(grammarText); @in.name = fileName; this.ast = tool.Parse(fileName, @in); if (ast == null) { throw new NotSupportedException(); } if (ast.tokenStream == null) { throw new InvalidOperationException("expected ast to have a token stream"); } this.tokenStream = ast.tokenStream; this.originalTokenStream = this.tokenStream; // ensure each node has pointer to surrounding grammar Antlr.Runtime.Tree.TreeVisitor v = new Antlr.Runtime.Tree.TreeVisitor(new GrammarASTAdaptor()); v.Visit(ast, new SetPointersAction(this)); InitTokenSymbolTables(); if (tokenVocabSource != null) { ImportVocab(tokenVocabSource); } tool.Process(this, false); }
public Grammar(AntlrTool tool, [NotNull] GrammarRootAST ast) { if (ast == null) { throw new ArgumentNullException(nameof(ast)); } if (ast.tokenStream == null) { throw new ArgumentException("ast must have a token stream", nameof(ast)); } this.tool = tool; this.ast = ast; this.name = (ast.GetChild(0)).Text; this.tokenStream = ast.tokenStream; this.originalTokenStream = this.tokenStream; InitTokenSymbolTables(); }