예제 #1
0
 protected void exitRule(ParseTreeListener listener, RuleNode r) {
   ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
   ctx.exitRule(listener);
   listener.exitEveryRule(ctx);
   GlobalInfo info = GlobalInfo.getInstance();
   info.exitRuleSetup();
 }
예제 #2
0
  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;
    }
  }
예제 #3
0
  @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;
  }
예제 #4
0
  @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;
  }