Пример #1
0
 /**
  * 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);
 }
Пример #2
0
  @Override
  public String visitGameinit(@NotNull DiceGameParser.GameinitContext ctx) {

    if (ctx.ASSN != null) {
      return ctx.ASSN.accept(this) + "\n";
    }

    if (ctx.FROM != null && ctx.TO != null) {
      return "self.min = " + ctx.FROM.getText() + "\nself.max = " + ctx.TO.getText();
    }

    if (ctx.FROM != null) {
      return "self.min = " + ctx.FROM.getText() + "\nself.max = " + ctx.FROM.getText();
    }

    if (ctx.DICEINIT != null) {

      StringBuilder result = new StringBuilder();
      result.append("self.dices = [");
      for (ParseTree diceInit : ctx.children) {
        if (diceInit.getClass() == DiceGameParser.DiceinitContext.class) {
          result.append(diceInit.accept(this) + ", ");
        }
      }
      result.append("]\n");
      return result.toString();
    }
    if (ctx.COND != null) {
      return "def isRunning(self): return (" + ctx.COND.accept(this) + ")";
    }

    return "visitGameinit";
  }
Пример #3
0
 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;
 }
Пример #4
0
 public static void main(String[] args) throws IOException {
   ANTLRInputStream input = new ANTLRFileStream(args[0]);
   SimpleLexer lexer = new SimpleLexer(input);
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   SimpleParser parser = new SimpleParser(tokens);
   ParseTree t = parser.s();
   System.out.println(t.toStringTree(parser));
 }
 public static ParseTree createParseTree(String program) {
   ANTLRInputStream antlrInput = new ANTLRInputStream(program);
   EKPLexer lexer = new EKPLexer(antlrInput);
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   EKPParser parser = new EKPParser(tokens);
   ParseTree tree = parser.programm();
   System.out.println(tree.getText());
   return tree;
 }
Пример #6
0
  /**
   * Gets whether or not {@code a} is an ancestor of or equal to {@code b}.
   *
   * @param a The first tree.
   * @param b The second tree.
   * @return {@code true} if {@code a} is an ancestor of or is equal to {@code b}, otherwise {@code
   *     false}.
   */
  public static boolean isAncestorOf(@NonNull ParseTree a, @NonNull ParseTree b) {
    for (ParseTree current = b; current != null; current = current.getParent()) {
      if (current.equals(a)) {
        return true;
      }
    }

    return false;
  }
 /**
  * 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;
 }
 private String extractIdentifier(ParseTree parse) {
   if (parse instanceof TerminalNode) {
     int type = ((TerminalNode) parse).getSymbol().getType();
     if (type == PSQLParser.Identifier) {
       return parse.getText();
     } else if (type == PSQLParser.QuotedIdentifier) {
       return parse.getText().substring(1, parse.getText().length() - 1);
     }
   }
   return parse.getText();
 }
  /**
   * 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;
  }
Пример #10
0
 public void process(String text) {
   this.depths = new ParseTreeProperty<Integer>();
   this.termCount = 0;
   this.errorCount = 0;
   Lexer lexer = new SentenceLexer(new ANTLRInputStream(text));
   SentenceParser parser = new SentenceParser(new CommonTokenStream(lexer));
   ParseTree tree = parser.sentence();
   new ParseTreeWalker().walk(this, tree);
   System.out.printf("Results for '%s':%n", text);
   System.out.printf("Parse tree: %s%n", tree.toStringTree(parser));
   System.out.printf("Tree depth: %d%n", getDepth(tree));
   System.out.printf("Terminals:  %d%n", this.termCount);
   System.out.printf("Errors:     %d%n%n", this.errorCount);
 }
  /**
   * 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;
  }
Пример #12
0
  @CheckForNull
  public static RuleNode findAncestor(@NonNull ParseTree tree, int ruleIndex) {
    for (ParseTree current = tree; current != null; current = current.getParent()) {
      if (!(current instanceof RuleNode)) {
        continue;
      }

      RuleNode ruleNode = (RuleNode) current;
      if (ruleNode.getRuleContext().getRuleIndex() == ruleIndex) {
        return ruleNode;
      }
    }

    return null;
  }
Пример #13
0
  /**
   * Return a list of all ancestors of this node. The first node of list is the root and the last is
   * the parent of this node.
   *
   * @param <T>
   * @param t
   * @return
   */
  @NonNull
  public static List<? extends ParseTree> getAncestors(@NonNull ParseTree t) {
    if (t.getParent() == null) {
      return Collections.emptyList();
    }

    List<ParseTree> ancestors = new ArrayList<>();
    t = t.getParent();
    while (t != null) {
      ancestors.add(0, t); // insert at start
      t = t.getParent();
    }

    return ancestors;
  }
  /**
   * 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;
  }
Пример #15
0
  @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;
  }
Пример #16
0
  /**
   * Gets whether or not {@code tree} is an epsilon non-terminal in the parse tree. An epsilon tree
   * is a node which does not contain any {@link TerminalNode} descendants.
   *
   * @param tree A node in a parse tree.
   * @return {@code true} if {@code tree} is an epsilon node in the parse tree, otherwise {@code
   *     false}.
   */
  public static boolean isEpsilon(@NonNull ParseTree tree) {
    if (tree instanceof TerminalNode) {
      return false;
    }

    Interval sourceInterval = tree.getSourceInterval();
    return sourceInterval.b < sourceInterval.a;
  }
Пример #17
0
  @CheckForNull
  public static <ContextClass> ContextClass findAncestor(
      @NonNull ParseTree tree, @NonNull Class<ContextClass> nodeType) {
    for (ParseTree current = tree; current != null; current = current.getParent()) {
      if (!(current instanceof RuleNode)) {
        continue;
      }

      RuleNode ruleNode = (RuleNode) current;
      RuleContext ruleContext = ruleNode.getRuleContext();
      if (nodeType.isInstance(ruleContext)) {
        return nodeType.cast(ruleContext);
      }
    }

    return null;
  }
Пример #18
0
 public void setTree(ParseTree tree) {
   this.tree = tree;
   if (tree.getChildCount() > 3) {
     chooseAll = false;
   } else {
     chooseAll = true;
   }
 }
Пример #19
0
 @Override
 public String visitDiceobjects(@NotNull DiceGameParser.DiceobjectsContext ctx) {
   if (ctx.ALL != null) {
     return "self.dices";
   }
   if (ctx.LAST != null) {
     String ret = "[";
     for (ParseTree val : ctx.children) {
       if (val.getClass() == DiceGameParser.DiceobjectContext.class) {
         ret += val.accept(this) + ", ";
       }
     }
     ret += "]";
     return ret;
   }
   return "visitDiceobjects";
 }
Пример #20
0
  @Override
  public String visitDiceinit(@NotNull DiceGameParser.DiceinitContext ctx) {

    StringBuilder result = new StringBuilder();

    result.append("Dice('" + ctx.NAME.getText() + "',[");

    for (ParseTree val : ctx.children) {
      if (val.getClass() == DiceGameParser.FaceContext.class) {
        result.append(val.getText() + ", ");
      }
    }

    result.append("])");

    return result.toString();
  }
 /**
  * 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);
   }
 }
Пример #22
0
  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;
  }
Пример #23
0
 @Override
 public String visitPlayerobjects(@NotNull DiceGameParser.PlayerobjectsContext ctx) {
   if (ctx.ALL != null) {
     return "self.players";
   }
   if (ctx.ACTIVE != null) {
     return "[player for player in self.players if player.isActive()]";
   }
   if (ctx.LAST != null) {
     String ret = "[";
     for (ParseTree val : ctx.children) {
       if (val.getClass() == DiceGameParser.PlayerobjectContext.class) {
         ret += (val.getText() + ", ");
       }
     }
     ret += ctx.LAST.getText() + "]";
     return ret;
   }
   return "visitPlayerobjects";
 }
  public static void main(String[] args) throws Exception {
    System.out.print("Parsing: ");

    Reader reader = null;
    if (args.length > 0) {
      System.out.println(args[0]);
      reader = new BufferedReader(new FileReader(args[0]));
    } else {
      System.out.println("STDIN");
      reader = new BufferedReader(new InputStreamReader(System.in));
    }

    ANTLRInputStream input = new ANTLRInputStream(reader);
    ConfigLexer lexer = new ConfigLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ConfigParser parser = new ConfigParser(tokens);
    parser.setTrace(true);
    ParseTree tree = parser.config();
    System.out.println(tree.toStringTree(parser));
  }
  /**
   * 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;
  }
Пример #26
0
  /**
   * Gets whether or not {@code a} starts after the start of {@code b}.
   *
   * @param a The first tree.
   * @param b The second tree.
   * @return {@code true} if {@code a} starts after the start of {@code b}, otherwise {@code false}.
   */
  public static boolean startsAfterStartOf(@NonNull ParseTree a, @NonNull ParseTree b) {
    // TerminalNode<? extends Token> startNodeA = getStartNode(a);
    // TerminalNode<? extends Token> startNodeB = getStartNode(b);
    // if (startNodeA == null || startNodeB == null) {
    //    throw new NotImplementedException();
    // }

    Interval sourceIntervalA = a.getSourceInterval();
    Interval sourceIntervalB = b.getSourceInterval();

    // if (sourceIntervalA.a == sourceIntervalB.a) {
    //    if (isAncestorOf(a, b)) {
    //        return true;
    //    }
    //
    //    if (isEpsilon(a) || isEpsilon(b)) {
    //        // b could be a child of a later sibling of some ancestor of a
    //        throw new NotImplementedException();
    //    }
    // }

    return sourceIntervalA.a > sourceIntervalB.a;
  }
Пример #27
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;
  }
  /**
   * 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;
  }
Пример #29
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;
  }
Пример #30
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;
    }
  }