public static void AugmentTokensWithOriginalPosition(Grammar g, GrammarAST tree) { if (tree == null) return; IList<GrammarAST> optionsSubTrees = tree.GetNodesWithType(ANTLRParser.ELEMENT_OPTIONS); for (int i = 0; i < optionsSubTrees.Count; i++) { GrammarAST t = optionsSubTrees[i]; CommonTree elWithOpt = (CommonTree)t.Parent; if (elWithOpt is GrammarASTWithOptions) { IDictionary<string, GrammarAST> options = ((GrammarASTWithOptions)elWithOpt).GetOptions(); if (options.ContainsKey(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME)) { GrammarToken newTok = new GrammarToken(g, elWithOpt.Token); newTok.originalTokenIndex = int.Parse(options[LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME].Text); elWithOpt.Token = newTok; GrammarAST originalNode = g.ast.GetNodeWithTokenIndex(newTok.TokenIndex); if (originalNode != null) { // update the AST node start/stop index to match the values // of the corresponding node in the original parse tree. elWithOpt.TokenStartIndex = originalNode.TokenStartIndex; elWithOpt.TokenStopIndex = originalNode.TokenStopIndex; } else { // the original AST node could not be located by index; // make sure to assign valid values for the start/stop // index so toTokenString will not throw exceptions. elWithOpt.TokenStartIndex = newTok.TokenIndex; elWithOpt.TokenStopIndex = newTok.TokenIndex; } } } } }