Example #1
0
 public static void toParseTree(StringBuilder sb, int indent, ParseNode node) {
   for (int x = 0; x < indent; x++) {
     sb.append("  ");
   }
   sb.append("( ");
   sb.append(node.getRule());
   sb.append("\n");
   for (Object child : node._children) {
     if (child instanceof ParseNode) {
       toParseTree(sb, indent + 1, (ParseNode) child);
     } else if (child instanceof Token) {
       Token token = (Token) child;
       if (token.getChannel() != token.HIDDEN_CHANNEL) {
         for (int x = 0; x < indent + 1; x++) {
           sb.append("  ");
         }
         sb.append(token.getType());
         sb.append(":");
         sb.append(token.getText());
         sb.append("\n");
       }
     }
   }
   for (int x = 0; x < indent; x++) {
     sb.append("  ");
   }
   sb.append(")");
   sb.append("\n");
 }
Example #2
0
  private static void find(
      boolean first,
      ParseNode parent,
      ArrayList<String> query,
      int nth,
      boolean seeking,
      ArrayList<Object> result) {
    String spot = query.get(nth);
    if ("**".equals(spot)) {
      find(first, parent, query, nth + 1, true, result);
    }
    for (Object child : parent._children) {
      boolean wildcard = "*".equals(spot);
      boolean tokenQuery = Character.isUpperCase(spot.charAt(0));
      boolean nodeQuery = Character.isLowerCase(spot.charAt(0));

      if (child instanceof Token && (wildcard || tokenQuery)) {
        Token token = (Token) child;
        int type = getLexType(spot);
        if (token.getType() == type) {
          result.add(token);
        }
      } else if (child instanceof ParseNode && (wildcard || nodeQuery)) {
        ParseNode childNode = (ParseNode) child;
        if (wildcard || childNode.getRule().equals(spot)) {
          if (nth + 1 < query.size()) {
            find(first, childNode, query, nth + 1, false, result);
          } else {
            result.add(childNode);
          }
        }
        //				else
        else if (seeking) {
          find(first, childNode, query, nth, true, result);
        }
      }

      if (first && result.size() > 0) break;
    }
  }