/**
  * 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);
 }
 public void setTree(ParseTree tree) {
   this.tree = tree;
   if (tree.getChildCount() > 3) {
     chooseAll = false;
   } else {
     chooseAll = true;
   }
 }
 /**
  * Creates JavadocNodeImpl node on base of ParseTree node.
  *
  * @param parseTree ParseTree node
  * @param parent DetailNode that will be parent of new node
  * @param index child index that has new node
  * @return JavadocNodeImpl node on base of ParseTree node.
  */
 private JavadocNodeImpl createJavadocNode(ParseTree parseTree, DetailNode parent, int index) {
   final JavadocNodeImpl node = new JavadocNodeImpl();
   node.setText(parseTree.getText());
   node.setColumnNumber(getColumn(parseTree));
   node.setLineNumber(getLine(parseTree) + blockCommentLineNumber);
   node.setIndex(index);
   node.setType(getTokenType(parseTree));
   node.setParent(parent);
   node.setChildren((DetailNode[]) new JavadocNodeImpl[parseTree.getChildCount()]);
   return node;
 }
  /**
   * 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;
  }
  /**
   * Gets token type of ParseTree node from JavadocTokenTypes class.
   *
   * @param node ParseTree node.
   * @return token type from JavadocTokenTypes
   */
  private static int getTokenType(ParseTree node) {
    final int tokenType;

    if (node.getChildCount() == 0) {
      tokenType = ((TerminalNode) node).getSymbol().getType();
    } else {
      final String className = getNodeClassNameWithoutContext(node);
      final String typeName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, className);
      tokenType = JavadocUtils.getTokenId(typeName);
    }

    return tokenType;
  }
  /**
   * 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;
  }
  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;
  }
  /**
   * 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;
  }
Exemple #10
0
  /**
   * Return a list of all nodes starting at {@code t} as root that satisfy the path. The root {@code
   * /} is relative to the node passed to {@link #evaluate}.
   */
  public Collection<ParseTree> evaluate(final ParseTree t) {
    ParserRuleContext dummyRoot = new ParserRuleContext();
    dummyRoot.children = Collections.singletonList(t); // don't set t's parent.

    Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot);

    int i = 0;
    while (i < elements.length) {
      Collection<ParseTree> next = new LinkedHashSet<ParseTree>();
      for (ParseTree node : work) {
        if (node.getChildCount() > 0) {
          // only try to match next element if it has children
          // e.g., //func/*/stat might have a token node for which
          // we can't go looking for stat nodes.
          Collection<? extends ParseTree> matching = elements[i].evaluate(node);
          next.addAll(matching);
        }
      }
      i++;
      work = next;
    }

    return work;
  }
Exemple #11
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 #12
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;
    }
  }