protected void exitRule(ParseTreeListener listener, RuleNode r) { ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext(); ctx.exitRule(listener); listener.exitEveryRule(ctx); GlobalInfo info = GlobalInfo.getInstance(); info.exitRuleSetup(); }
public static Interval getSourceInterval(@NonNull ParseTree context) { Parameters.notNull("context", context); if (context instanceof TerminalNode) { TerminalNode terminalNode = (TerminalNode) context; Token token = terminalNode.getSymbol(); return new Interval(token.getStartIndex(), token.getStopIndex()); } else if (context instanceof RuleNode) { RuleNode ruleNode = (RuleNode) context; RuleContext ruleContext = ruleNode.getRuleContext(); if (ruleContext instanceof ParserRuleContext) { return getSourceInterval((ParserRuleContext) ruleContext); } else { Token startSymbol = getStartSymbol(context); Token stopSymbol = getStopSymbol(context); if (startSymbol == null || stopSymbol == null) { return Interval.INVALID; } return new Interval(startSymbol.getStartIndex(), stopSymbol.getStopIndex()); } } else { return Interval.INVALID; } }
@CheckForNull public static RuleNode findAncestor(@NonNull ParseTree tree, int ruleIndex) { for (ParseTree current = tree; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; } RuleNode ruleNode = (RuleNode) current; if (ruleNode.getRuleContext().getRuleIndex() == ruleIndex) { return ruleNode; } } return null; }
@CheckForNull public static <ContextClass> ContextClass findAncestor( @NonNull ParseTree tree, @NonNull Class<ContextClass> nodeType) { for (ParseTree current = tree; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; } RuleNode ruleNode = (RuleNode) current; RuleContext ruleContext = ruleNode.getRuleContext(); if (nodeType.isInstance(ruleContext)) { return nodeType.cast(ruleContext); } } return null; }