Example #1
0
  public static Map<String, String> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) {
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
    TreeWizard wiz = new TreeWizard(adaptor, ANTLRParser.tokenNames);
    Map<String, String> lexerRuleToStringLiteral = new HashMap<String, String>();

    List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE);
    if (ruleNodes == null || ruleNodes.size() == 0) return null;

    for (GrammarAST r : ruleNodes) {
      // tool.log("grammar", r.toStringTree());
      Tree name = r.getChild(0);
      if (name.getType() == ANTLRParser.TOKEN_REF) {
        Map nodes = new HashMap();
        boolean isLitRule =
            wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))", nodes);
        if (isLitRule) {
          GrammarAST litNode = (GrammarAST) nodes.get("lit");
          GrammarAST nameNode = (GrammarAST) nodes.get("name");
          lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
          continue;
        }
        // TODO: allow doc comment in there
        nodes = new HashMap();
        // try with action in there
        isLitRule =
            wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", nodes);
        if (isLitRule) {
          GrammarAST litNode = (GrammarAST) nodes.get("lit");
          GrammarAST nameNode = (GrammarAST) nodes.get("name");
          lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText());
        }
      }
    }
    return lexerRuleToStringLiteral;
  }
  /**
   * 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");
    }
  }
Example #3
0
  /** Handle function nodes ^(CONSTRUCTOR_DECL|FUNCTION_METHOD_DECL|VOID_METHOD_DECL ...) */
  private void __function(IterableTree ast) {
    IterableTree modList = ast.firstChildOfType(JavaParser.MODIFIER_LIST);

    Set<String> modifiers = _javaModifiers(modList);
    Visibility visibility = _javaVisibility(modList);
    Tree funcNameAst = ast.firstChildOfType(JavaParser.IDENT);
    String name;
    if (funcNameAst != null) {
      name = funcNameAst.getText();
    } else {
      /* constructor nodes do not have a IDENT child, we take the name of the parent class */
      name = scopeStack.peek().name;
    }
    String returnType = _typeName(ast.firstChildOfType(JavaParser.TYPE));

    Function func =
        new Function(
            name,
            language,
            _source(ast),
            visibility,
            scopeStack.peek(),
            modifiers,
            returnType,
            null,
            false,
            null);
    ((ComplexType) scopeStack.peek()).symbols.add(func);

    scopeStack.push(func);
    walkChildren(ast, JavaParser.FORMAL_PARAM_LIST);
    walkChildren(ast, JavaParser.THROWS_CLAUSE);
    scopeStack.pop();
  }
  /**
   * 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);
  }
  public void execute() {
    int count = tree.getChildCount();

    TreeFormat.println("CreateDefinition");
    TreeFormat.up();
    col_name.execute();
    data_type.execute();
    TreeFormat.down();

    // TreeFormat.up();
    // column_definition_suffix*
    for (int i = 2; i < count; ++i) {
      Tree child = tree.getChild(i);
      int type = child.getType();

      switch (type) {
        case FatwormParser.NULL:
          TreeFormat.up();
          handleNull(child);
          TreeFormat.down();
          break;
        case FatwormParser.DEFAULT:
          TreeFormat.up();
          handleDefault(child);
          TreeFormat.down();
          break;
        case FatwormParser.AUTO_INCREMENT:
          TreeFormat.up();
          handleAutoIncrement(child);
          TreeFormat.down();
          break;
      }
    }
  }
  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);
    }
  }
Example #7
0
 private static Object parseNumOrVariableIdent(Tree child) {
   if (child.getType() == EsperEPL2GrammarParser.IDENT) {
     return child.getText();
   } else {
     return IntValue.parseString(child.getText());
   }
 }
  @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);
 }
 @Test
 public void whereTestContains() {
   String statement = "SELECT p1 FROM MyType WHERE CONTAINS('Beethoven')";
   checkTreeWhere(statement);
   Tree tree = findSearchExpression(statement);
   printSearchTree(tree, statement);
   assertEquals("Beethoven", tree.getChild(0).getText());
 }
 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
   }
 }
 private void getParameterName(CommonTree tree) {
   Tree parameterNameTree = tree.getFirstChildWithType(CSharpParser.IDENTIFIER);
   if (parameterNameTree != null) {
     this.declareName = parameterNameTree.getText();
     if ((this.declareName != null) && (!this.declareName.trim().equals(""))) {
       this.nameFound = true;
     }
   }
 }
Example #13
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());
    }
  }
 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;
 }
 private String getReturnType(Tree tree) {
   // op dit moment worden arraylisten en hashmaps dusdanig gezet als 'hashmap' en 'arraylist' en
   // niet als
   // ArrayList<User> bijv. Dit is wellicht een TODO
   if (tree.getChild(0).getType() == JavaParser.QUALIFIED_TYPE_IDENT) {
     return tree.getChild(0).getChild(0).getText();
   } else {
     return tree.getChild(0).getText();
   }
 }
 /**
  * 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;
 }
Example #17
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;
    }
  }
 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
     }
   }
 }
 @Test
 public void whereTestContainsNoFulltextParse() throws Exception {
   String statement = "SELECT p1 FROM MyType WHERE CONTAINS('Beethoven')";
   walker = getWalker(statement);
   walker.setDoFullTextParse(false);
   walker.query(queryObj, predicateWalker);
   Tree whereTree = walker.getWherePredicateTree();
   Tree tree = findTextSearchNode(whereTree);
   printSearchTree(tree, statement);
   // unparsed, still with quotes
   assertEquals("'Beethoven'", tree.getChild(0).getText());
 }
 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;
   }
 }
 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;
 }
Example #22
0
 /** {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}. */
 public static boolean blockHasWildcardAlt(@NotNull GrammarAST block) {
   for (Object alt : block.getChildren()) {
     if (!(alt instanceof AltAST)) continue;
     AltAST altAST = (AltAST) alt;
     if (altAST.getChildCount() == 1) {
       Tree e = altAST.getChild(0);
       if (e.getType() == ANTLRParser.WILDCARD) {
         return true;
       }
     }
   }
   return false;
 }
 private static String printSearchNode(Tree node) {
   switch (node.getType()) {
     case TextSearchLexer.TEXT_AND:
     case TextSearchLexer.TEXT_OR:
     case TextSearchLexer.TEXT_SEARCH_PHRASE_STRING_LIT:
     case TextSearchLexer.TEXT_SEARCH_WORD_LIT:
       return node.toString();
     case TextSearchLexer.TEXT_MINUS:
       return "MINUS";
     default:
       return "Unknown token: " + node.toString();
   }
 }
Example #24
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();
  }
 @Override
 public int getCharPositionInLine() {
   int col = 0;
   if (token != null) {
     col = token.getCharPositionInLine();
   }
   if (col == 0) {
     Tree child = getChild(0);
     if (child != null) {
       col = child.getCharPositionInLine();
     }
   }
   return col;
 }
 public GrammarAST findFirstType(int ttype) {
   // check this node (the root) first
   if (this.getType() == ttype) {
     return this;
   }
   // else check children
   List<Tree> descendants = descendants(this);
   for (Tree child : descendants) {
     if (child.getType() == ttype) {
       return (GrammarAST) child;
     }
   }
   return null;
 }
Example #27
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);
  }
 @Override
 public int getLine() {
   int line = 0;
   if (token != null) {
     line = token.getLine();
   }
   if (line == 0) {
     Tree child = getChild(0);
     if (child != null) {
       line = child.getLine();
     }
   }
   return line;
 }
  // 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;
  }
Example #30
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);
    }
  }