/** Find and replace * ID*[','] with ID (',' ID)* * ID+[','] with ID (',' ID)+ * (x {action} y)+[','] with x {action} y (',' x {action} y)+ * * Parameter must be a token. * todo: do we want? */ public virtual void ExpandParameterizedLoops(GrammarAST root) { TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); Antlr.Runtime.Misc.Func<object, object> preAction = t => { if (((GrammarAST)t).Type == 3) { return ExpandParameterizedLoop((GrammarAST)t); } return t; }; Antlr.Runtime.Misc.Func<object, object> postAction = t => t; v.Visit(root, new TreeVisitorAction(preAction, postAction)); }
/** Find and replace * ID*[','] with ID (',' ID)* * ID+[','] with ID (',' ID)+ * (x {action} y)+[','] with x {action} y (',' x {action} y)+ * * Parameter must be a token. * todo: do we want? */ public virtual void ExpandParameterizedLoops(GrammarAST root) { TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); Antlr.Runtime.Misc.Func <object, object> preAction = t => { if (((GrammarAST)t).Type == 3) { return(ExpandParameterizedLoop((GrammarAST)t)); } return(t); }; Antlr.Runtime.Misc.Func <object, object> postAction = t => t; v.Visit(root, new TreeVisitorAction(preAction, postAction)); }
/** Utility visitor that sets grammar ptr in each node */ public static void SetGrammarPtr(Grammar g, GrammarAST tree) { if (tree == null) { return; } // ensure each node has pointer to surrounding grammar Antlr.Runtime.Misc.Func <object, object> preAction = t => { ((GrammarAST)t).g = g; return(t); }; Antlr.Runtime.Misc.Func <object, object> postAction = t => t; TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); v.Visit(tree, new TreeVisitorAction(preAction, postAction)); }
/** 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); }
/** Utility visitor that sets grammar ptr in each node */ public static void SetGrammarPtr(Grammar g, GrammarAST tree) { if (tree == null) return; // ensure each node has pointer to surrounding grammar Antlr.Runtime.Misc.Func<object, object> preAction = t => { ((GrammarAST)t).g = g; return t; }; Antlr.Runtime.Misc.Func<object, object> postAction = t => t; TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); v.Visit(tree, new TreeVisitorAction(preAction, postAction)); }