private static void print(StringBuilder builder, Tree node, String prefix, boolean isTail) {
    final String name =
        node.getText() == null ? "" : node.getText().replaceAll("\r\n|\r|\n", "\u23CE");

    builder
        .append(prefix)
        .append(isTail ? "└─ " : "├─ ") //
        .append(String.format("%04d", node.getLine())) //
        .append(':')
        .append(String.format("%04d", node.getCharPositionInLine()))
        .append(' ') //
        .append(String.format("%02d", node.getType()))
        .append(": ")
        .append(name) //
        .append("\n");

    for (int i = 0; i < (node.getChildCount() - 1); i++) {
      CommonTreePrinter.print(
          builder, node.getChild(i), prefix + (isTail ? "    " : "│   "), false);
    }

    if (node.getChildCount() >= 1) {
      CommonTreePrinter.print(
          builder,
          node.getChild(node.getChildCount() - 1),
          prefix + (isTail ? "    " : "│   "),
          true);
    }
  }
  /**
   * Parse the given property name returning a Property instance for the property.
   *
   * @param propertyName is the property name to parse
   * @param isRootedDynamic is true to indicate that the property is already rooted in a dynamic
   *     property and therefore all child properties should be dynamic properties as well
   * @return Property instance for property
   */
  public static Property parse(String propertyName, boolean isRootedDynamic) {
    Tree tree = parse(propertyName);

    if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
      ASTUtil.dumpAST(tree);
    }

    if (tree.getChildCount() == 1) {
      return makeProperty(tree.getChild(0), isRootedDynamic);
    }

    List<Property> properties = new LinkedList<Property>();
    boolean isRootedInDynamic = isRootedDynamic;
    for (int i = 0; i < tree.getChildCount(); i++) {
      Tree child = tree.getChild(i);

      Property property = makeProperty(child, isRootedInDynamic);
      if (property instanceof DynamicSimpleProperty) {
        isRootedInDynamic = true;
      }
      properties.add(property);
    }

    return new NestedProperty(properties);
  }
示例#3
0
  /**
   * Builds a row limit specification.
   *
   * @param node to interrogate
   * @return row limit spec
   */
  public static RowLimitSpec buildRowLimitSpec(Tree node) {
    Object numRows;
    Object offset;

    if (node.getChildCount() == 1) {
      numRows = parseNumOrVariableIdent(node.getChild(0));
      offset = null;
    } else {
      if (node.getChild(node.getChildCount() - 1).getType() == EsperEPL2GrammarParser.COMMA) {
        offset = parseNumOrVariableIdent(node.getChild(0));
        numRows = parseNumOrVariableIdent(node.getChild(1));
      } else {
        numRows = parseNumOrVariableIdent(node.getChild(0));
        offset = parseNumOrVariableIdent(node.getChild(1));
      }
    }

    Integer numRowsInt = null;
    String numRowsVariable = null;
    if (numRows instanceof String) {
      numRowsVariable = (String) numRows;
    } else {
      numRowsInt = (Integer) numRows;
    }

    Integer offsetInt = null;
    String offsetVariable = null;
    if (offset instanceof String) {
      offsetVariable = (String) offset;
    } else {
      offsetInt = (Integer) offset;
    }

    return new RowLimitSpec(numRowsInt, offsetInt, numRowsVariable, offsetVariable);
  }
 private static void assertSingleWord(final String name, final String value, final Tree r) {
   assertEquals(QueryParser.FIELD_NAME, r.getType());
   assertEquals(name, r.getText());
   assertEquals(1, r.getChildCount());
   final Tree c = r.getChild(0);
   assertEquals(QueryParser.SINGLE_WORD, c.getType());
   assertEquals(value, c.getText());
   assertEquals(0, c.getChildCount());
 }
  private void WalkThroughMethod(Tree tree) {
    for (int i = 0; i < tree.getChildCount(); i++) {
      Tree child = tree.getChild(i);
      int treeType = child.getType();
      if (treeType == JavaParser.ABSTRACT) {
        isAbstract = true;
      }
      if (treeType == JavaParser.FINAL) {
        hasClassScope = true;
      }
      if (treeType == JavaParser.PUBLIC) {
        accessControlQualifier = "public";
      }
      if (treeType == JavaParser.PRIVATE) {
        accessControlQualifier = "private";
      }
      if (treeType == JavaParser.PROTECTED) {
        accessControlQualifier = "protected";
      }
      if (treeType == JavaParser.TYPE) {
        declaredReturnType = getReturnType(child);
        deleteTreeChild(child);
      }
      if (treeType == JavaParser.IDENT) {
        name = child.getText();
      }
      if (treeType == JavaParser.THROW || treeType == JavaParser.THROWS) {
        delegateException(child);
        deleteTreeChild(child);
      }
      if (treeType == JavaParser.FORMAL_PARAM_LIST) {
        if (child.getChildCount() > 0) {
          JavaParameterGenerator javaParameterGenerator = new JavaParameterGenerator();
          signature =
              "("
                  + javaParameterGenerator.generateParameterObjects(child, name, belongsToClass)
                  + ")";
          // = this.name + "(" +  + ")";
          deleteTreeChild(child);
        }
      }

      if (treeType == JavaParser.BLOCK_SCOPE) {
        setSignature();
        loopThroughBlockMethod(child);
        deleteTreeChild(child);
      }

      WalkThroughMethod(child);
    }
  }
  @Override
  public Object visit(DelphiPMDNode node, Object data) {
    String lookFor = getStringProperty(LOOK_FOR);
    if (StringUtils.isEmpty(lookFor)) {
      return data;
    }

    if (node.getText().equalsIgnoreCase(lookFor)) {
      Tree beginNode = null;
      for (int i = node.getChildIndex() + 1;
          i < node.getChildIndex() + MAX_LOOK_AHEAD && i < node.getParent().getChildCount();
          ++i) {
        if (node.getParent().getChild(i).getType() == DelphiLexer.BEGIN) {
          beginNode = node.getParent().getChild(i);
          break;
        }
      }
      if (beginNode != null) {
        boolean wasInherited = false;
        for (int c = 0; c < beginNode.getChildCount(); c++) {
          if (beginNode.getChild(c).getType() == DelphiLexer.INHERITED) {
            wasInherited = true;
            break;
          }
        }

        if (!wasInherited) {
          addViolation(data, node);
        }
      }
    }

    return data;
  }
 public GrammarAST getLastSibling() {
   Tree parent = getParent();
   if (parent == null) {
     return null;
   }
   return (GrammarAST) parent.getChild(parent.getChildCount() - 1);
 }
  /**
   * Builder to parse out the ANTLR tree
   *
   * @param t - the ANTLR tree
   * @param builder - for the helper functions
   * @throws TreeParsingException
   */
  public void build(Tree t, TreeBuilder builder) throws TreeParsingException {

    if (t.getType() == JdbcGrammarParser.CONDITION) {
      this.tokenName = JdbcGrammarParser.tokenNames[t.getType()];
      this.tokenType = t.getType();
      this.logger.debug("BUILDING " + this.tokenName);
      for (int i = 0; i < t.getChildCount(); i++) {
        Tree child = t.getChild(i);
        switch (child.getType()) {
          case JdbcGrammarParser.CONDITIONLEFT:
            this.left = new ColumnReference(child.getChild(0), builder, this.selectStatement);
            break;
          case JdbcGrammarParser.CONDITIONRIGHT:
            this.right = new ColumnReference(child.getChild(0), builder, this.selectStatement);
            break;
          case JdbcGrammarParser.COMPARISONOPERATOR:
            this.operator = child.getChild(0).getText();
            break;
          default:
            break;
        }
      }
    } else {
      throw new TreeParsingException("This Tree is not a CONDITION");
    }
  }
 private void loopThroughBlockMethod(Tree tree) {
   for (int i = 0; i < tree.getChildCount(); i++) {
     Tree child = tree.getChild(i);
     int treeType = child.getType();
     if (treeType == JavaParser.VAR_DECLARATION) {
       javaLocalVariableGenerator.generateLocalVariableModel(
           child, belongsToClass, this.belongsToClass + "." + this.name + this.signature);
       deleteTreeChild(child);
     }
     if (treeType == JavaParser.CLASS_CONSTRUCTOR_CALL) {
       delegateInvocation(child, "invocConstructor");
       // ik ben er nog niet uit of deze wel gedelete mag worden
     }
     if (treeType == JavaParser.METHOD_CALL) {
       if (child.getChild(0).getType() == 15) { // getType omdat 15 een punt is
         delegateInvocation(child, "invocMethod");
         deleteTreeChild(child);
       }
     }
     if (treeType == JavaParser.THROW
         || treeType == JavaParser.CATCH
         || treeType == JavaParser.THROWS) {
       delegateException(child);
       deleteTreeChild(child);
     }
     if (treeType == JavaParser.ASSIGN) { // =
       if (child.getChild(0).getType() == 15) { // getType omdat 15 een punt is
         delegateInvocation(child, "accessPropertyOrField");
         deleteTreeChild(child);
       }
     }
     loopThroughBlockMethod(child);
   }
 }
示例#10
0
  private void handleNull(Tree child) {

    if (child.getChildCount() != 0) {
      TreeFormat.println("NOT_NULL");
    } else {
      TreeFormat.println("NULL");
    }
  }
示例#11
0
 private void evalSearchExprTree(Tree root) {
   int count = root.getChildCount();
   for (int i = 0; i < count; i++) {
     Tree child = root.getChild(i);
     evaluateSearchExprNode(child);
     evalSearchExprTree(child); // recursive descent
   }
 }
示例#12
0
 // Ensure that we receive only valid tokens and nodes in the where clause:
 private void evaluateSearchExprNode(Tree node) {
   LOG.info("evaluating text search expression node: " + node.toString());
   switch (node.getType()) {
     case TextSearchLexer.TEXT_AND:
     case TextSearchLexer.TEXT_OR:
       assertTrue(node.getChildCount() >= 2);
       break;
     case TextSearchLexer.TEXT_MINUS:
       assertEquals(1, node.getChildCount());
       break;
     case TextSearchLexer.TEXT_SEARCH_PHRASE_STRING_LIT:
     case TextSearchLexer.TEXT_SEARCH_WORD_LIT:
       evalStringLiteral(node);
       break;
     default:
       fail("[Unexpected node in text search expression: " + node.toString() + "]");
   }
 }
  /**
   * Create argument map, and set their used count to 0
   *
   * @param argsNode Function argument node
   * @return Argument map
   */
  private Map<String, Integer> processFunctionArgs(Tree argsNode) {
    Map<String, Integer> args = new HashMap<String, Integer>();
    for (int i = 0; i < argsNode.getChildCount(); i += 2) {
      Tree idents = argsNode.getChild(i); // TkVariableIdents node

      if (idents.getType() != DelphiLexer.TkVariableIdents) { // check
        // type
        idents = argsNode.getChild(++i);
        if (idents == null || idents.getType() != DelphiLexer.TkVariableIdents) {
          break;
        }
      }

      for (int c = 0; c < idents.getChildCount(); ++c) {
        args.put(idents.getChild(c).getText().toLowerCase(), Integer.valueOf(0));
      }
    }
    return args;
  }
示例#14
0
 public static Tree simplify(Tree tree) {
   for (int i = 0; i < tree.getChildCount(); ++i) {
     Tree child = tree.getChild(i);
     Tree optimized = simplify(child);
     if (child != optimized) {
       tree.setChild(i, optimized);
     }
   }
   switch (tree.getType()) {
     case QueryLexer.CONJUNCTION:
     case QueryLexer.DISJUNCTION:
     case QueryLexer.SEQUENCE:
       if (tree.getChildCount() == 1) {
         return tree.getChild(0);
       }
       break;
   }
   return tree;
 }
示例#15
0
  /**
   * @param tree the tree
   * @since 2.0.0
   */
  public Qualified(Tree tree) {
    super();

    int i = 0;

    for (; i < tree.getChildCount(); i++) {
      final Tree child = tree.getChild(i);
      this.getSegments().add(child.getText());
    }
  }
示例#16
0
 private void printSearchTree(Tree node) {
   LOG.info(indentString() + printSearchNode(node));
   ++indent;
   int count = node.getChildCount();
   for (int i = 0; i < count; i++) {
     Tree child = node.getChild(i);
     printSearchTree(child);
   }
   --indent;
 }
示例#17
0
 private void test(CommonTree initialNode, CommonTree replacedNode) {
   if (initialNode.getType() == FTSParser.TERM
       && initialNode.getChildCount() == 1
       && initialNode.getChild(0).getType() == FTSParser.STAR) {
     // input is the lone star
     Tree node = replacedNode;
     while (true) {
       if (node.getChildCount() == 1) {
         node = node.getChild(0);
         if (node.getType() == FTSParser.TERM) {
           assertEquals(
               "Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR,
               node.getChildCount(),
               2);
           Tree child1 = node.getChild(0);
           assertEquals(
               "Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR,
               child1.getType(),
               FTSParser.ID);
           assertEquals(
               "Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR,
               child1.getText(),
               "T");
           Tree child2 = node.getChild(1);
           assertEquals(
               "Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR,
               child2.getType(),
               FTSParser.FIELD_REF);
           assertEquals(
               "Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR,
               child2.getChild(0).getText(),
               "ISNODE");
           // checking done
           break;
         }
       } else {
         // wrong structure of the replaced node
         fail("Lone star should be mapped to " + FTSQueryParser.VALUE_REPLACELONESTAR);
       }
     }
   }
 }
示例#18
0
  /**
   * @param aliased aliased tree
   * @since $version
   * @author hceylan
   */
  public Aliased(Tree aliased) {
    super();

    this.qualified = new Qualified(aliased.getChild(0));

    if (aliased.getChildCount() > 1) {
      this.alias = aliased.getChild(1).getText();
    } else {
      this.alias = null;
    }
  }
示例#19
0
 /**
  * Returns true if the property is a dynamic property.
  *
  * @param ast property ast
  * @return dynamic or not
  */
 public static boolean isPropertyDynamic(Tree ast) {
   for (int i = 0; i < ast.getChildCount(); i++) {
     int type = ast.getChild(i).getType();
     if ((type == EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_SIMPLE)
         || (type == EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_INDEXED)
         || (type == EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_MAPPED)) {
       return true;
     }
   }
   return false;
 }
示例#20
0
 private void evalWhereTree(Tree root) {
   int count = root.getChildCount();
   if (root.getType() == CmisQlStrictLexer.CONTAINS) {
     evalSearchExprTree(root);
   } else {
     for (int i = 0; i < count; i++) {
       Tree child = root.getChild(i);
       evaluateWhereNode(child);
       evalWhereTree(child); // recursive descent
     }
   }
 }
示例#21
0
 private Tree findTextSearchNode(Tree node) {
   int count = node.getChildCount();
   if (node.getType() == CmisQlStrictLexer.CONTAINS) {
     return node;
   } else {
     for (int i = 0; i < count; i++) {
       Tree child = node.getChild(i);
       node = findTextSearchNode(child); // recursive descent
       if (null != node) return node;
     }
     return null;
   }
 }
示例#22
0
 private boolean traverseTreeAndFindNodeInColumnMap2(Tree node, Object colRef) {
   int count = node.getChildCount();
   LOG.debug("  checking with: " + node + " identity hash code: " + System.identityHashCode(node));
   if (node == colRef) {
     return true;
   }
   boolean found = false;
   for (int i = 0; i < count && !found; i++) {
     Tree child = node.getChild(i);
     found = traverseTreeAndFindNodeInColumnMap2(child, colRef);
   }
   return found;
 }
示例#23
0
  /** Extract the Type information from a TYPE node. */
  private static String _typeName(IterableTree ast) {
    if (ast == null) {
      return null;
    }
    StringBuilder typeName = new StringBuilder();

    if (ast.getType() == JavaParser.TYPE) {

      IterableTree qualifiedType = ast.firstChildOfType(JavaParser.QUALIFIED_TYPE_IDENT);

      if (qualifiedType != null) {
        StringBuilder qualifiedTypeName = new StringBuilder();
        for (int i = 0; i < qualifiedType.getChildCount(); i++) {
          qualifiedTypeName.append("." + qualifiedType.getChild(i).getText());

          IterableTree generics =
              qualifiedType.getChild(i).firstChildOfType(JavaParser.GENERIC_TYPE_ARG_LIST);
          if (generics != null) {
            qualifiedTypeName.append('<');
            qualifiedTypeName.append(_genericsTypeList(generics));
            qualifiedTypeName.append('>');
          }
        }
        /* to remove the leading dot */
        typeName.append(qualifiedTypeName.substring(1));
      } else {
        typeName.append(ast.getChild(0).getText());
      }

      Tree arrayDecl = ast.firstChildOfType(JavaParser.ARRAY_DECLARATOR_LIST);
      if (arrayDecl != null) {
        for (int i = 0; i < arrayDecl.getChildCount(); i++) {
          typeName.append("[]");
        }
      }

    } else if (ast.getType() == JavaParser.QUESTION) {
      typeName.append("?");

      if (ast.getChildCount() > 0) {
        /* ^(QUESTION ^(EXTENDS|SUPER type)) */
        IterableTree extendsAst = ast.getChild(0);
        typeName.append(" ");
        typeName.append(extendsAst.getText());
        typeName.append(" ");
        typeName.append(_typeName(extendsAst.getChild(0)));
      }
    }

    return typeName.toString();
  }
示例#24
0
  public Return(Tree tree, ErrorsCollector ec, SymbolTable st) {
    assert (tree.getType() == CommonCppWithStreamsLexer.RETURN);
    assert (tree.getChildCount() <= 1);

    expr = null;

    Tree p = tree.getParent();
    while (p != null && p.getType() != CommonCppWithStreamsLexer.FUNCTION) {
      p = p.getParent();
    }
    if (p == null) {
      ec.check(false, tree.getLine(), "return from no function");
      return;
    }
    assert (p.getChild(1).getType() == CommonCppWithStreamsLexer.NAME);
    Function funcDef = st.referenceFunctionAndGetIt(p.getChild(1).getText(), tree.getLine());
    assert (funcDef != null);
    funcName = funcDef.getName();
    if (funcDef.getType() == Type.VOID) {
      ec.check(
          tree.getChildCount() == 0,
          tree.getLine(),
          "can not return value out of 'void' function '" + funcName + "'");
    } else {
      if (tree.getChildCount() != 1) {
        ec.check(
            false,
            tree.getLine(),
            "can not return from '"
                + TypeConverter.typeToString(funcDef)
                + "' function '"
                + funcName
                + "' without returning a value");
        return;
      }
      expr = new Expression(tree.getChild(0), ec, st);
    }
  }
示例#25
0
  // check if the map containing all column references in the where clause has an existing node as
  // key
  private boolean traverseTreeAndFindNodeInColumnMap(Tree node, Map<Object, CmisSelector> colRefs) {
    boolean found = false;
    //        System.out.println("cmp to: " + System.identityHashCode(node) + " is: " +
    // node.toString());
    if (null != colRefs.get(node)) {
      return true;
    }

    int count = node.getChildCount();
    for (int i = 0; i < count && !found; i++) {
      Tree child = node.getChild(i);
      found = traverseTreeAndFindNodeInColumnMap(child, colRefs);
    }
    return found;
  }
  /**
   * Process begin node, to look for used arguments
   *
   * @param beginNode Begin node
   * @param args Argument map
   */
  private void processFunctionBegin(Tree beginNode, Map<String, Integer> args) {
    for (int i = 0; i < beginNode.getChildCount(); ++i) {
      Tree child = beginNode.getChild(i);
      String key = child.getText().toLowerCase();
      if (args.containsKey(key)) { // if we are using a argument, increase
        // the counter
        Integer newValue = args.get(key) + 1;
        args.put(key, newValue);
      }

      if (child.getType() == DelphiLexer.BEGIN) {
        processFunctionBegin(child, args);
      }
    }
  }
示例#27
0
  private void printTree(final Tree tree, final int spaces, final StringBuilder sb) {
    for (int i = 0; i < spaces; i++) {
      sb.append(" ");
    }

    if (tree.getText().trim().isEmpty()) {
      sb.append(tree.toString()).append("\n");
    } else {
      sb.append(tree.getText()).append("\n");
    }

    for (int i = 0; i < tree.getChildCount(); i++) {
      printTree(tree.getChild(i), spaces + 2, sb);
    }
  }
 private void deriveParametersFromTree(Tree allParametersTree) {
   int totalParameters = allParametersTree.getChildCount();
   for (int currentChild = 0; currentChild < totalParameters; currentChild++) {
     CommonTree child = (CommonTree) allParametersTree.getChild(currentChild);
     int treeType = child.getType();
     if (treeType == CSharpParser.FIXED_PARAMETER) {
       getParameterName(child);
       getTypeOfParameter(child);
       if (this.nameFound && this.declareTypeFound) {
         this.addToQueue();
       }
       nameFound = false;
       declareTypeFound = false;
     }
     deriveParametersFromTree(child);
   }
 }
示例#29
0
 /**
  * Flattens the tree by pushing down the field name. For example, if the tree looks like this:
  *
  * <pre>
  *                EQ
  *             /      \
  *        VALUE         EQ
  *        /    \      /    \
  *      TEXT  field GLOBAL (N)
  * </pre>
  *
  * Then we will output tree that looks like this:
  *
  * <pre>
  *                EQ
  *             /      \
  *        VALUE       (N)
  *        /    \
  *      TEXT  field
  * </pre>
  *
  * Here <code>(N)</code> is an arbitrary node. We also drop EQ if it is in front of conjunction or
  * disjunction. We do not drop it for other comparators, as we want parsing to fail for foo &lt;
  * (1 2).
  */
 private static Tree flatten(Tree tree, Tree restriction) throws QueryTreeException {
   if (tree.getType() == QueryLexer.VALUE) {
     return tree;
   }
   if (tree.getType() == QueryLexer.HAS || tree.getType() == QueryLexer.EQ) {
     Tree lhs = tree.getChild(0);
     if (lhs.getType() == QueryLexer.VALUE) {
       String myField = lhs.getChild(1).getText();
       if (restriction == null) {
         restriction = lhs;
       } else {
         String otherField = restriction.getChild(1).getText();
         if (!myField.equals(otherField)) {
           throw new QueryTreeException(
               String.format("Restriction on %s and %s", otherField, myField),
               lhs.getChild(1).getCharPositionInLine());
         }
       }
     }
     Tree rhs = tree.getChild(1);
     Tree flattened = flatten(rhs, restriction);
     if (flattened.getType() == QueryLexer.HAS
         || flattened.getType() == QueryLexer.EQ
         || flattened.getType() == QueryLexer.CONJUNCTION
         || flattened.getType() == QueryLexer.DISJUNCTION
         || flattened.getType() == QueryLexer.SEQUENCE) {
       return flattened;
     }
     if (flattened != rhs) {
       tree.setChild(1, flattened);
     }
     if (restriction != lhs) {
       tree.setChild(0, restriction);
     }
     return tree;
   }
   for (int i = 0; i < tree.getChildCount(); ++i) {
     Tree original = tree.getChild(i);
     Tree flattened = flatten(tree.getChild(i), restriction);
     if (original != flattened) {
       tree.setChild(i, flattened);
     }
   }
   return tree;
 }
示例#30
0
  // process a statement of the form: connect hostname/port
  private void executeConnect(CommonTree ast) {
    int portNumber = Integer.parseInt(ast.getChild(1).getText());
    Tree idList = ast.getChild(0);

    StringBuilder hostName = new StringBuilder();
    int idCount = idList.getChildCount();
    for (int idx = 0; idx < idCount; idx++) {
      hostName.append(idList.getChild(idx).getText());
    }

    // disconnect current connection, if any.
    // This is a no-op, if you aren't currently connected.
    CliMain.disconnect();

    // now, connect to the newly specified host name and port
    css_.hostName = hostName.toString();
    css_.thriftPort = portNumber;
    CliMain.connect(css_.hostName, css_.thriftPort);
  }