public int execute() throws Exception {
   if (QueryInfo.get__dbInfo() == null) {
     throw new Exception("No choosed database");
   }
   String tableName = tree.getChild(2).getText();
   tableInfo = DictCenterManager.findTableWithName(QueryInfo.get__dbInfo(), tableName);
   int ret = 0;
   if (!chooseAll) {
     table = DataTableManager.loadTable(tableInfo);
     int len = table.getRecords().size();
     for (cur = 0; cur < len; cur++) {
       visitor.visitTree((sqlParser.ExprContext) tree.getChild(3).getChild(1));
       if (((ValueTree) tree.getChild(3).getChild(1)).getValue().getValue().equals(true)) {
         ret++;
       } else {
         resTable.getRecords().add(table.getRecords().get(cur));
       }
     }
     table.setRecords(resTable.getRecords());
   } else {
     ret = table.getRecords().size();
     table.getRecords().clear();
   }
   DataTableManager.storeTable(table);
   return ret;
 }
 /**
  * Adds the sub-ASTs corresponding to the children of a given Antlr tree to an AST, and sets the
  * AST property of the tree.
  */
 private void computeDepth(ParseTree tree) {
   int depth = 1;
   for (int i = 0; i < tree.getChildCount(); i++) {
     depth = Math.max(depth, getDepth(tree.getChild(i)) + 1);
   }
   setDepth(tree, depth);
 }
 /**
  * Creates child nodes for each node from 'nodes' array.
  *
  * @param parseTreeParent original ParseTree parent node
  * @param nodes array of JavadocNodeImpl nodes
  */
 private void insertChildrenNodes(final JavadocNodeImpl[] nodes, ParseTree parseTreeParent) {
   for (int i = 0; i < nodes.length; i++) {
     final JavadocNodeImpl currentJavadocNode = nodes[i];
     final ParseTree currentParseTreeNodeChild = parseTreeParent.getChild(i);
     final JavadocNodeImpl[] subChildren =
         createChildrenNodes(currentJavadocNode, currentParseTreeNodeChild);
     currentJavadocNode.setChildren((DetailNode[]) subChildren);
   }
 }
  /**
   * Gets next sibling of ParseTree node.
   *
   * @param node ParseTree node
   * @return next sibling of ParseTree node.
   */
  private static ParseTree getNextSibling(ParseTree node) {
    ParseTree nextSibling = null;

    if (node.getParent() != null) {
      final ParseTree parent = node.getParent();
      final int childCount = parent.getChildCount();

      int index = 0;
      while (true) {
        final ParseTree currentNode = parent.getChild(index);
        if (currentNode.equals(node)) {
          if (index != childCount - 1) {
            nextSibling = parent.getChild(index + 1);
          }
          break;
        }
        index++;
      }
    }
    return nextSibling;
  }
  /**
   * Creates children Javadoc nodes base on ParseTree node's children.
   *
   * @param parentJavadocNode node that will be parent for created children
   * @param parseTreeNode original ParseTree node
   * @return array of Javadoc nodes
   */
  private JavadocNodeImpl[] createChildrenNodes(
      JavadocNodeImpl parentJavadocNode, ParseTree parseTreeNode) {
    final JavadocNodeImpl[] children = new JavadocNodeImpl[parseTreeNode.getChildCount()];

    for (int j = 0; j < children.length; j++) {
      final JavadocNodeImpl child =
          createJavadocNode(parseTreeNode.getChild(j), parentJavadocNode, j);

      children[j] = child;
    }
    return children;
  }
  /**
   * Creates root JavadocNodeImpl node base on ParseTree root node.
   *
   * @param parseTreeNode ParseTree root node
   * @return root Javadoc node
   */
  private JavadocNodeImpl createRootJavadocNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createJavadocNode(parseTreeNode, null, -1);

    final int childCount = parseTreeNode.getChildCount();
    final JavadocNodeImpl[] children = new JavadocNodeImpl[childCount];

    for (int i = 0; i < childCount; i++) {
      final JavadocNodeImpl child =
          createJavadocNode(parseTreeNode.getChild(i), rootJavadocNode, i);
      children[i] = child;
    }
    rootJavadocNode.setChildren((DetailNode[]) children);
    return rootJavadocNode;
  }
  @CheckForNull
  public static TerminalNode findTerminalNode(@NonNull ParseTree node, Token symbol) {
    if (symbol == null) {
      return null;
    }

    if (node instanceof TerminalNode) {
      TerminalNode terminalNode = (TerminalNode) node;
      if (Utils.equals(terminalNode.getSymbol(), symbol)) {
        return terminalNode;
      }

      return null;
    }

    for (int i = 0; i < node.getChildCount(); i++) {
      ParseTree child = node.getChild(i);
      TerminalNode stopNode = ParseTrees.getStopNode(child);
      if (stopNode == null) {
        continue;
      }

      Token stopSymbol = stopNode.getSymbol();
      if (stopSymbol.getStopIndex() < symbol.getStartIndex()) {
        continue;
      }

      TerminalNode startNode = ParseTrees.getStartNode(child);
      assert startNode != null;

      stopSymbol = startNode.getSymbol();
      if (stopSymbol == null || stopSymbol.getStartIndex() > symbol.getStopIndex()) {
        break;
      }

      if (stopSymbol.equals(symbol)) {
        return startNode;
      }

      TerminalNode terminalNode = findTerminalNode(child, symbol);
      if (terminalNode != null) {
        return terminalNode;
      }
    }

    return null;
  }
  /**
   * Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
   *
   * @param parseTreeNode root node of ParseTree
   * @return root of DetailNode tree
   */
  private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
    final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);

    JavadocNodeImpl currentJavadocParent = rootJavadocNode;
    ParseTree parseTreeParent = parseTreeNode;

    while (currentJavadocParent != null) {
      // remove unnecessary children tokens
      if (currentJavadocParent.getType() == JavadocTokenTypes.TEXT) {
        currentJavadocParent.setChildren((DetailNode[]) JavadocNodeImpl.EMPTY_DETAIL_NODE_ARRAY);
      }

      final JavadocNodeImpl[] children = (JavadocNodeImpl[]) currentJavadocParent.getChildren();

      insertChildrenNodes(children, parseTreeParent);

      if (children.length > 0) {
        currentJavadocParent = children[0];
        parseTreeParent = parseTreeParent.getChild(0);
      } else {
        JavadocNodeImpl nextJavadocSibling =
            (JavadocNodeImpl) JavadocUtils.getNextSibling(currentJavadocParent);

        ParseTree nextParseTreeSibling = getNextSibling(parseTreeParent);

        if (nextJavadocSibling == null) {
          JavadocNodeImpl tempJavadocParent = (JavadocNodeImpl) currentJavadocParent.getParent();

          ParseTree tempParseTreeParent = parseTreeParent.getParent();

          while (nextJavadocSibling == null && tempJavadocParent != null) {

            nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(tempJavadocParent);

            nextParseTreeSibling = getNextSibling(tempParseTreeParent);

            tempJavadocParent = (JavadocNodeImpl) tempJavadocParent.getParent();
            tempParseTreeParent = tempParseTreeParent.getParent();
          }
        }
        currentJavadocParent = nextJavadocSibling;
        parseTreeParent = nextParseTreeSibling;
      }
    }

    return rootJavadocNode;
  }
  public static TerminalNode getStopNode(ParseTree context) {
    if (context == null) {
      return null;
    }

    if (context instanceof TerminalNode) {
      return (TerminalNode) context;
    }

    for (int i = context.getChildCount() - 1; i >= 0; i--) {
      TerminalNode stopNode = getStopNode(context.getChild(i));
      if (stopNode != null) {
        return stopNode;
      }
    }

    return null;
  }
Exemple #10
0
  /** Resolve include statements */
  private static boolean resolveIncludes(
      ParseTree tree, boolean debug, Set<String> alreadyIncluded) {
    boolean changed = false;
    if (tree instanceof IncludeFileContext) {
      // Parent file: The one that is including the other file
      File parentFile =
          new File(((IncludeFileContext) tree).getStart().getInputStream().getSourceName());

      // Included file name
      String includedFilename = StatementInclude.includeFileName(tree.getChild(1).getText());

      // Find file (look into all include paths)
      File includedFile = StatementInclude.includeFile(includedFilename, parentFile);
      if (includedFile == null) {
        CompilerMessages.get()
            .add(
                tree,
                parentFile,
                "\n\tIncluded file not found: '"
                    + includedFilename
                    + "'\n\tSearch path: "
                    + Config.get().getIncludePath(),
                MessageType.ERROR);
        return false;
      }

      // Already included? don't bother
      String canonicalFileName = Gpr.getCanonicalFileName(includedFile);
      if (alreadyIncluded.contains(canonicalFileName)) {
        if (debug)
          Gpr.debug(
              "File already included: '"
                  + includedFilename
                  + "'\tCanonical path: '"
                  + canonicalFileName
                  + "'");
        return false;
      }
      if (!includedFile.canRead()) {
        CompilerMessages.get()
            .add(
                tree,
                parentFile,
                "\n\tCannot read included file: '" + includedFilename + "'",
                MessageType.ERROR);
        return false;
      }

      // Parse
      ParseTree treeinc = createAst(includedFile, debug, alreadyIncluded);
      if (treeinc == null) {
        CompilerMessages.get()
            .add(
                tree,
                parentFile,
                "\n\tFatal error including file '" + includedFilename + "'",
                MessageType.ERROR);
        return false;
      }

      // Is a child always a RuleContext?
      for (int i = 0; i < treeinc.getChildCount(); i++) {
        ((IncludeFileContext) tree).addChild((RuleContext) treeinc.getChild(i));
      }
    } else {
      for (int i = 0; i < tree.getChildCount(); i++)
        changed |= resolveIncludes(tree.getChild(i), debug, alreadyIncluded);
    }

    return changed;
  }
Exemple #11
0
  /**
   * Create an AST from a program (using ANTLR lexer & parser) Returns null if error Use
   * 'alreadyIncluded' to keep track of from 'include' statements
   */
  public static ParseTree createAst(File file, boolean debug, Set<String> alreadyIncluded) {
    alreadyIncluded.add(Gpr.getCanonicalFileName(file));
    String fileName = file.toString();
    String filePath = fileName;

    BigDataScriptLexer lexer = null;
    BigDataScriptParser parser = null;

    try {
      filePath = file.getCanonicalPath();

      // Input stream
      if (!Gpr.canRead(filePath)) {
        CompilerMessages.get().addError("Can't read file '" + filePath + "'");
        return null;
      }

      // Create a CharStream that reads from standard input
      ANTLRFileStream input = new ANTLRFileStream(fileName);

      // ---
      // Lexer: Create a lexer that feeds off of input CharStream
      // ---
      lexer =
          new BigDataScriptLexer(input) {
            @Override
            public void recover(LexerNoViableAltException e) {
              throw new RuntimeException(e); // Bail out
            }
          };

      // ---
      // Parser
      // ---
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      parser = new BigDataScriptParser(tokens);

      // Parser error handling
      parser.setErrorHandler(
          new CompileErrorStrategy()); // Bail out with exception if errors in parser
      parser.addErrorListener(new CompilerErrorListener()); // Catch some other error messages that
      // 'CompileErrorStrategy' fails to catch

      // Begin parsing at main rule
      ParseTree tree = parser.programUnit();

      // Error loading file?
      if (tree == null) {
        System.err.println("Can't parse file '" + filePath + "'");
        return null;
      }

      // Show main nodes
      if (debug) {
        Timer.showStdErr("AST:");
        for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
          Tree child = tree.getChild(childNum);
          System.err.println(
              "\t\tChild " + childNum + ":\t" + child + "\tTree:'" + child.toStringTree() + "'");
        }
      }

      // Included files
      boolean resolveIncludePending = true;
      while (resolveIncludePending)
        resolveIncludePending = resolveIncludes(tree, debug, alreadyIncluded);

      return tree;
    } catch (Exception e) {
      String msg = e.getMessage();
      CompilerMessages.get()
          .addError(
              "Could not compile "
                  + filePath //
                  + (msg != null ? " :" + e.getMessage() : "") //
              );
      return null;
    }
  }