public static void _findAllNodes(IParseTree t, int index, bool findTokens, IList <IParseTree> nodes) { // check this node (the root) first if (findTokens && t is ITerminalNode) { ITerminalNode tnode = (ITerminalNode)t; if (tnode.Symbol.Type == index) { nodes.Add(t); } } else { if (!findTokens && t is ParserRuleContext) { ParserRuleContext ctx = (ParserRuleContext)t; if (ctx.GetRuleIndex() == index) { nodes.Add(t); } } } // check children for (int i = 0; i < t.ChildCount; i++) { _findAllNodes(t.GetChild(i), index, findTokens, nodes); } }
public override ICollection <IParseTree> Evaluate(IParseTree t) { // return all children of t that match nodeName IList <IParseTree> nodes = new List <IParseTree>(); foreach (ITree c in Trees.GetChildren(t)) { if (c is ParserRuleContext) { ParserRuleContext ctx = (ParserRuleContext)c; if ((ctx.GetRuleIndex() == ruleIndex && !invert) || (ctx.GetRuleIndex() != ruleIndex && invert)) { nodes.Add(ctx); } } } return(nodes); }
public void ExitEveryRule(ParserRuleContext ctx) { checksum.Update(EXIT_RULE); updateChecksum(checksum, ctx.GetRuleIndex()); updateChecksum(checksum, ctx.Stop); }
public void EnterEveryRule(ParserRuleContext ctx) { checksum.Update(ENTER_RULE); updateChecksum(checksum, ctx.GetRuleIndex()); updateChecksum(checksum, ctx.Start); }
protected internal virtual IParseTree MatchImpl(IParseTree tree, IParseTree patternTree, MultiMap <string, IParseTree> labels) { if (tree == null) { throw new ArgumentException("tree cannot be null"); } if (patternTree == null) { throw new ArgumentException("patternTree cannot be null"); } // x and <ID>, x and y, or x and x; or could be mismatched types if (tree is ITerminalNode && patternTree is ITerminalNode) { ITerminalNode t1 = (ITerminalNode)tree; ITerminalNode t2 = (ITerminalNode)patternTree; IParseTree mismatchedNode = null; // both are tokens and they have same type if (t1.Symbol.Type == t2.Symbol.Type) { if (t2.Symbol is TokenTagToken) { // x and <ID> TokenTagToken tokenTagToken = (TokenTagToken)t2.Symbol; // track label->list-of-nodes for both token name and label (if any) labels.Map(tokenTagToken.GetTokenName(), tree); if (tokenTagToken.GetLabel() != null) { labels.Map(tokenTagToken.GetLabel(), tree); } } else { if (t1.GetText().Equals(t2.GetText())) { } else { // x and x // x and y if (mismatchedNode == null) { mismatchedNode = t1; } } } } else { if (mismatchedNode == null) { mismatchedNode = t1; } } return(mismatchedNode); } if (tree is ParserRuleContext && patternTree is ParserRuleContext) { ParserRuleContext r1 = (ParserRuleContext)tree; ParserRuleContext r2 = (ParserRuleContext)patternTree; IParseTree mismatchedNode = null; // (expr ...) and <expr> RuleTagToken ruleTagToken = GetRuleTagToken(r2); if (ruleTagToken != null) { ParseTreeMatch m = null; if (r1.GetRuleIndex() == r2.GetRuleIndex()) { // track label->list-of-nodes for both rule name and label (if any) labels.Map(ruleTagToken.GetRuleName(), tree); if (ruleTagToken.GetLabel() != null) { labels.Map(ruleTagToken.GetLabel(), tree); } } else { if (mismatchedNode == null) { mismatchedNode = r1; } } return(mismatchedNode); } // (expr ...) and (expr ...) if (r1.ChildCount != r2.ChildCount) { if (mismatchedNode == null) { mismatchedNode = r1; } return(mismatchedNode); } int n = r1.ChildCount; for (int i = 0; i < n; i++) { IParseTree childMatch = MatchImpl(r1.GetChild(i), patternTree.GetChild(i), labels); if (childMatch != null) { return(childMatch); } } return(mismatchedNode); } // if nodes aren't both tokens or both rule nodes, can't match return(tree); }