예제 #1
0
 public void walk(ParseTreeListener listener, ParseTree t) {
   if (t instanceof ErrorNode) {
     listener.visitErrorNode((ErrorNode) t);
     return;
   } else if (t instanceof TerminalNode) {
     listener.visitTerminal((TerminalNode) t);
     return;
   }
   GlobalInfo info = GlobalInfo.getInstance();
   info.setNotVisitNodes(new ArrayList<Integer>());
   RuleNode r = (RuleNode) t;
   enterRule(listener, r);
   int n = r.getChildCount();
   for (int i = 0; i < n; i++) {
     Boolean found = false;
     for (int j = 0; j < info.getNotVisitNodes().size(); ++j) {
       if (info.getNotVisitNodes().get(j).equals(i)) {
         found = true;
         break;
       }
     }
     if (!found) {
       List<Integer> aux = info.getNotVisitNodes();
       info.setNotVisitNodes(new ArrayList<Integer>());
       walk(listener, r.getChild(i));
       info.setNotVisitNodes(aux);
     }
   }
   exitRule(listener, r);
   info.setNotVisitNodes(new ArrayList<Integer>());
 }
예제 #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
 protected void exitRule(ParseTreeListener listener, RuleNode r) {
   ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
   ctx.exitRule(listener);
   listener.exitEveryRule(ctx);
   GlobalInfo info = GlobalInfo.getInstance();
   info.exitRuleSetup();
 }
예제 #5
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;
  }