/**
   * 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");
    }
  }
Ejemplo n.º 2
0
 private static Property makeProperty(Tree child, boolean isRootedInDynamic) {
   switch (child.getType()) {
     case EsperEPL2GrammarParser.EVENT_PROP_SIMPLE:
       if (!isRootedInDynamic) {
         return new SimpleProperty(child.getChild(0).getText());
       } else {
         return new DynamicSimpleProperty(child.getChild(0).getText());
       }
     case EsperEPL2GrammarParser.EVENT_PROP_MAPPED:
       String key = StringValue.parseString(child.getChild(1).getText());
       if (!isRootedInDynamic) {
         return new MappedProperty(child.getChild(0).getText(), key);
       } else {
         return new DynamicMappedProperty(child.getChild(0).getText(), key);
       }
     case EsperEPL2GrammarParser.EVENT_PROP_INDEXED:
       int index = IntValue.parseString(child.getChild(1).getText());
       if (!isRootedInDynamic) {
         return new IndexedProperty(child.getChild(0).getText(), index);
       } else {
         return new DynamicIndexedProperty(child.getChild(0).getText(), index);
       }
     case EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_SIMPLE:
       return new DynamicSimpleProperty(child.getChild(0).getText());
     case EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_INDEXED:
       index = IntValue.parseString(child.getChild(1).getText());
       return new DynamicIndexedProperty(child.getChild(0).getText(), index);
     case EsperEPL2GrammarParser.EVENT_PROP_DYNAMIC_MAPPED:
       key = StringValue.parseString(child.getChild(1).getText());
       return new DynamicMappedProperty(child.getChild(0).getText(), key);
     default:
       throw new IllegalStateException(
           "Event property AST node not recognized, type=" + child.getType());
   }
 }
Ejemplo n.º 3
0
 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());
 }
Ejemplo n.º 4
0
  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);
    }
  }
Ejemplo n.º 5
0
 @Override
 public void goingUp(Tree t) {
   TokenProcessor tokenProcessor = tokenProcessorTable.get(t.getType());
   if (tokenProcessor != null) {
     tokenProcessor.goingUp(t);
   }
 }
Ejemplo n.º 6
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;
  }
Ejemplo n.º 7
0
 private static Object parseNumOrVariableIdent(Tree child) {
   if (child.getType() == EsperEPL2GrammarParser.IDENT) {
     return child.getText();
   } else {
     return IntValue.parseString(child.getText());
   }
 }
Ejemplo n.º 8
0
  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 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);
   }
 }
  /**
   * 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;
  }
Ejemplo n.º 11
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);
       }
     }
   }
 }
Ejemplo n.º 12
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
     }
   }
 }
Ejemplo n.º 13
0
 /** See if tree has exact token types and structure; no text */
 public boolean hasSameTreeStructure(Tree other) {
   // check roots first.
   if (this.getType() != other.getType()) return false;
   // if roots match, do full list match test on children.
   Iterator<Tree> thisDescendants = descendants(this, true).iterator();
   Iterator<Tree> otherDescendants = descendants(other, true).iterator();
   while (thisDescendants.hasNext()) {
     if (!otherDescendants.hasNext()) return false;
     if (thisDescendants.next().getType() != otherDescendants.next().getType()) return false;
   }
   return !otherDescendants.hasNext();
 }
  @Override
  public Object visit(DelphiPMDNode node, Object data) {
    if (node.getType() == DelphiLexer.PROCEDURE || node.getType() == DelphiLexer.FUNCTION) {
      Tree nameNode = node.getFirstChildWithType(DelphiLexer.TkFunctionName);
      if (nameNode != null) { // checking function name
        methodName = new StringBuilder(); // building function name
        for (int i = 0; i < nameNode.getChildCount(); ++i) {
          methodName.append(nameNode.getChild(i).getText());
        }
      }

      Tree argsNode = node.getFirstChildWithType(DelphiLexer.TkFunctionArgs);
      if (argsNode == null) {
        return data;
      }

      int lookIndex = 0;
      Tree beginNode = null; // looking for begin statement for function
      do {
        beginNode = node.getParent().getChild(node.getChildIndex() + (++lookIndex));
        if (lookIndex > MAX_LOOK_AHEAD || beginNode == null) {
          break;
        }
      } while (beginNode.getType() != DelphiLexer.BEGIN);

      if (beginNode == null || beginNode.getType() != DelphiLexer.BEGIN) {
        return data; // no begin..end for function
      }

      Map<String, Integer> args = processFunctionArgs(argsNode);
      if (args.isEmpty()) {
        return data; // no arguments
      }

      processFunctionBegin(beginNode, args);
      checkForUnusedArguments(args, data, node);
    }

    return data;
  }
Ejemplo n.º 15
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;
 }
  /**
   * builder to use with the ANTLR tree
   *
   * @param t
   * @param builder
   * @throws Exception
   */
  public void build(Tree t, TreeBuilder builder) throws TreeParsingException {
    if (t.getType() == JdbcGrammarParser.WHEREEXPRESSION) {
      this.tokenType = t.getType();
      this.tokenName = JdbcGrammarParser.tokenNames[this.tokenType];
      this.logger.debug("BUILDING " + this.tokenName);

      for (int i = 0; i < t.getChildCount(); i++) {
        Tree child = t.getChild(i);
        switch (child.getType()) {
          case JdbcGrammarParser.DISJUNCTION:
            this.expression = (new Disjunction(child, builder, this, this.selectStatement));
            break;
          case JdbcGrammarParser.CONJUNCTION:
            logger.debug("BUILDING CONJUNCTION OR DISJUNCTION FROM CONJUNCTION");
            Node built = Conjunction.buildFromConjunction(child, builder, this, selectStatement);
            if (built.getTokenType() == JdbcGrammarParser.CONJUNCTION) {
              this.expression = (Conjunction.class.cast(built));
              logger.debug("CONJUNCTION BUILT AND ADDED TO WHEREEXPRESSION");
            } else {
              this.expression = (Disjunction.class.cast(built));
              logger.debug("DISJUNCTION BUILT AND ADDED TO WHEREEXPRESSION");
            }
            break;
          case JdbcGrammarParser.NEGATION:
            this.expression = (new Negation(child, builder, this, this.selectStatement));
            break;
          case JdbcGrammarParser.BOOLEANEXPRESSIONITEM:
            this.expression =
                (new BooleanExpressionItem(child, builder, this, this.selectStatement));
            break;
          default:
            break;
        }
      }

    } else {
      throw new TreeParsingException("This Tree is not a WHEREEXPRESSION");
    }
  }
Ejemplo n.º 17
0
 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();
   }
 }
Ejemplo n.º 18
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;
   }
 }
  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);
    }
  }
Ejemplo n.º 20
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);
    }
  }
Ejemplo n.º 21
0
 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;
 }
 public boolean isTopLevelSimpleSelectStarQuery() {
   if (alias != null || destToSelExpr.size() != 1 || !isSimpleSelectQuery()) {
     return false;
   }
   for (ASTNode selExprs : destToSelExpr.values()) {
     if (selExprs.getChildCount() != 1) {
       return false;
     }
     Tree sel = selExprs.getChild(0).getChild(0);
     if (sel == null || sel.getType() != HiveParser.TOK_ALLCOLREF) {
       return false;
     }
   }
   return true;
 }
Ejemplo n.º 23
0
  public void execute() {
    int type = tree.getType();
    if (type == FatwormParser.ID) {
      TreeFormat.println(tree.getText());
      return;
    }

    TreeFormat.println("AS");
    Tree child = tree.getChild(0);
    type = child.getType();

    TreeFormat.up();
    switch (type) {
      case FatwormParser.ID:
        TreeFormat.println(child.getText());
        break;
      default:
        Subquery subquery = new Subquery(child);
        subquery.execute();
    }
    Alias alias = new Alias(tree.getChild(1));
    alias.execute();
    TreeFormat.down();
  }
  /**
   * 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);
      }
    }
  }
Ejemplo n.º 25
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() + "]");
   }
 }
Ejemplo n.º 26
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;
 }
Ejemplo n.º 27
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;
 }
Ejemplo n.º 28
0
  /** Return list of (TOKEN_NAME node, 'literal' node) pairs */
  public static List<Pair<GrammarAST, GrammarAST>> getStringLiteralAliasesFromLexerRules(
      GrammarRootAST ast) {
    String[] patterns = {
      "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))",
      "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))",
      "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))",
      "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) .)))",
      "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . .)))",
      "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .))))",
      "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . (LEXER_ACTION_CALL . .))))",
      "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .) .)))",
      // TODO: allow doc comment in there
    };
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
    org.antlr.runtime.tree.TreeWizard wiz =
        new org.antlr.runtime.tree.TreeWizard(adaptor, ANTLRParser.tokenNames);
    List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral =
        new ArrayList<Pair<GrammarAST, GrammarAST>>();

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

    for (GrammarAST r : ruleNodes) {
      // tool.log("grammar", r.toStringTree());
      //			System.out.println("chk: "+r.toStringTree());
      org.antlr.runtime.tree.Tree name = r.getChild(0);
      if (name.getType() == ANTLRParser.TOKEN_REF) {
        // check rule against patterns
        boolean isLitRule;
        for (String pattern : patterns) {
          isLitRule = defAlias(r, pattern, wiz, lexerRuleToStringLiteral);
          if (isLitRule) break;
        }
        //				if ( !isLitRule ) System.out.println("no pattern matched");
      }
    }
    return lexerRuleToStringLiteral;
  }
Ejemplo n.º 29
0
 // Ensure that we receive only valid tokens and nodes in the where clause:
 private void evaluateWhereNode(Tree node) {
   LOG.info("evaluating node: " + node.toString());
   switch (node.getType()) {
     case CmisQlStrictLexer.WHERE:
       break; // ignore
     case CmisQlStrictLexer.COL:
       evalColumn(node);
       break;
     case CmisQlStrictLexer.IN_LIST:
       evalInList(node);
       break;
     case CmisQlStrictLexer.IN_ANY:
       evalInAny(node);
       break;
     case CmisQlStrictLexer.EQ_ANY:
       evalEqAny(node);
       break;
     case CmisQlStrictLexer.NOT_LIKE:
       evalNotLike(node);
       break;
     case CmisQlStrictLexer.NOT_IN:
       evalNotIn(node);
       break;
     case CmisQlStrictLexer.IS_NULL:
       evalIsNull(node);
       break;
     case CmisQlStrictLexer.IS_NOT_NULL:
       evalIsNotNull(node);
       break;
     case CmisQlStrictLexer.LT:
       evalLessThan(node);
       break;
     case CmisQlStrictLexer.BOOL_LIT:
       evalBooleanLiteral(node);
       break;
     case CmisQlStrictLexer.TIME_LIT:
       evalTimeLiteral(node);
       break;
     case CmisQlStrictLexer.STRING_LIT:
       evalStringLiteral(node);
       break;
     case CmisQlStrictLexer.CONTAINS:
       evalContains(node);
       break;
     case CmisQlStrictLexer.ExactNumLit:
       evalExactNumLiteral(node);
       break;
     case CmisQlStrictLexer.LTEQ:
       evalLessOrEqual(node);
       break;
     case CmisQlStrictLexer.NOT:
       evalNot(node);
       break;
     case CmisQlStrictLexer.ID:
       evalId(node);
       break;
     case CmisQlStrictLexer.AND:
       evalAnd(node);
       break;
     case CmisQlStrictLexer.IN:
       evalIn(node);
       break;
     case CmisQlStrictLexer.EQ:
       evalEquals(node);
       break;
     case CmisQlStrictLexer.NUM_LIT:
       evalNumLiteral(node);
       break;
     case CmisQlStrictLexer.LIKE:
       evalLike(node);
       break;
     case CmisQlStrictLexer.NULL:
       evalNull(node);
       break;
     case CmisQlStrictLexer.GTEQ:
       evalGreaterThan(node);
       break;
     case CmisQlStrictLexer.ApproxNumLit:
       evalApproxNumLiteral(node);
       break;
     case CmisQlStrictLexer.IN_FOLDER:
       evalInFolder(node);
       break;
     case CmisQlStrictLexer.NEQ:
       evalNotEquals(node);
       break;
     case CmisQlStrictLexer.SCORE:
       evalScore(node);
       break;
     case CmisQlStrictLexer.IN_TREE:
       evalInTree(node);
       break;
     case CmisQlStrictLexer.OR:
       evalOr(node);
       break;
     case CmisQlStrictLexer.GT:
       evalGreaterThan(node);
       break;
     case CmisQlStrictLexer.TIMESTAMP:
       evalTimeLiteral(node);
       break;
     default:
       fail("[Unexpected node in WHERE clause: " + node.toString() + "]");
   }
 }
Ejemplo n.º 30
0
  private static String printNode(Tree node) {
    switch (node.getType()) {
      case CmisQlStrictLexer.TABLE:
        return "#TABLE";
      case CmisQlStrictLexer.COL:
        return "#COL";
      case CmisQlStrictLexer.IN_LIST:
        return "#IN_LIST";
      case CmisQlStrictLexer.SEL_LIST:
        return "#SEL_LIST";
      case CmisQlStrictLexer.EQ_ANY:
        return "#EQ_ANY";
      case CmisQlStrictLexer.NOT_LIKE:
        return "#NOT_LIKE";
      case CmisQlStrictLexer.NOT_IN:
        return "#NOT_IN";
      case CmisQlStrictLexer.IN_ANY:
        return "#IN_ANY";
      case CmisQlStrictLexer.NOT_IN_ANY:
        return "#NOT_IN_ANY";
      case CmisQlStrictLexer.IS_NULL:
        return "#IS_NULL";
      case CmisQlStrictLexer.IS_NOT_NULL:
        return "#IS_NOT_NULL";
      case CmisQlStrictLexer.ORDER_BY:
        return "#ORDER_BY";

      case CmisQlStrictLexer.WHERE:
      case CmisQlStrictLexer.LT:
      case CmisQlStrictLexer.STAR:
      case CmisQlStrictLexer.BOOL_LIT:
      case CmisQlStrictLexer.INNER:
      case CmisQlStrictLexer.TIME_LIT:
      case CmisQlStrictLexer.ORDER:
      case CmisQlStrictLexer.STRING_LIT:
      case CmisQlStrictLexer.CONTAINS:
      case CmisQlStrictLexer.ExactNumLit:
      case CmisQlStrictLexer.LTEQ:
      case CmisQlStrictLexer.NOT:
      case CmisQlStrictLexer.ID:
      case CmisQlStrictLexer.AND:
      case CmisQlStrictLexer.EOF:
      case CmisQlStrictLexer.AS:
      case CmisQlStrictLexer.IN:
      case CmisQlStrictLexer.LPAR:
      case CmisQlStrictLexer.Digits:
      case CmisQlStrictLexer.COMMA:
      case CmisQlStrictLexer.IS:
      case CmisQlStrictLexer.LEFT:
      case CmisQlStrictLexer.Sign:
      case CmisQlStrictLexer.EQ:
      case CmisQlStrictLexer.DOT:
      case CmisQlStrictLexer.NUM_LIT:
      case CmisQlStrictLexer.SELECT:
      case CmisQlStrictLexer.LIKE:
      case CmisQlStrictLexer.OUTER:
      case CmisQlStrictLexer.BY:
      case CmisQlStrictLexer.ASC:
      case CmisQlStrictLexer.NULL:
      case CmisQlStrictLexer.ON:
      case CmisQlStrictLexer.RIGHT:
      case CmisQlStrictLexer.GTEQ:
      case CmisQlStrictLexer.ApproxNumLit:
      case CmisQlStrictLexer.JOIN:
      case CmisQlStrictLexer.IN_FOLDER:
      case CmisQlStrictLexer.WS:
      case CmisQlStrictLexer.NEQ:
      case CmisQlStrictLexer.ANY:
      case CmisQlStrictLexer.SCORE:
      case CmisQlStrictLexer.IN_TREE:
      case CmisQlStrictLexer.OR:
      case CmisQlStrictLexer.GT:
      case CmisQlStrictLexer.RPAR:
      case CmisQlStrictLexer.DESC:
      case CmisQlStrictLexer.FROM:
      case CmisQlStrictLexer.TIMESTAMP:
        return node.toString();
      default:
        return "[Unknown token: " + node.toString() + "]";
    }
  }