Package org.antlr.v4.runtime.tree

Examples of org.antlr.v4.runtime.tree.ParseTree


  /** Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a
   *  compiled pattern instead of a string representation of a tree pattern.
   */
  public boolean matches(ParseTree tree, ParseTreePattern pattern) {
    MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>();
    ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels);
    return mismatchedNode == null;
  }
View Full Code Here


   * string representation of a tree pattern.
   */
  @NotNull
  public ParseTreeMatch match(@NotNull ParseTree tree, @NotNull ParseTreePattern pattern) {
    MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>();
    ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels);
    return new ParseTreeMatch(tree, pattern, labels, mismatchedNode);
  }
View Full Code Here

                                 parser.getVocabulary(),
                                 Arrays.asList(parser.getRuleNames()),
                                 parser.getATNWithBypassAlts(),
                                 tokens);

    ParseTree tree = null;
    try {
      parserInterp.setErrorHandler(new BailErrorStrategy());
      tree = parserInterp.parse(patternRuleIndex);
//      System.out.println("pattern tree = "+tree.toStringTree(parserInterp));
    }
View Full Code Here

    // x and <ID>, x and y, or x and x; or could be mismatched types
    if ( tree instanceof TerminalNode && patternTree instanceof TerminalNode ) {
      TerminalNode t1 = (TerminalNode)tree;
      TerminalNode t2 = (TerminalNode)patternTree;
      ParseTree mismatchedNode = null;
      // both are tokens and they have same type
      if ( t1.getSymbol().getType() == t2.getSymbol().getType() ) {
        if ( t2.getSymbol() instanceof TokenTagToken ) { // x and <ID>
          TokenTagToken tokenTagToken = (TokenTagToken)t2.getSymbol();
          // track label->list-of-nodes for both token name and label (if any)
          labels.map(tokenTagToken.getTokenName(), tree);
          if ( tokenTagToken.getLabel()!=null ) {
            labels.map(tokenTagToken.getLabel(), tree);
          }
        }
        else if ( t1.getText().equals(t2.getText()) ) {
          // x and x
        }
        else {
          // x and y
          if (mismatchedNode == null) {
            mismatchedNode = t1;
          }
        }
      }
      else {
        if (mismatchedNode == null) {
          mismatchedNode = t1;
        }
      }

      return mismatchedNode;
    }

    if ( tree instanceof ParserRuleContext && patternTree instanceof ParserRuleContext ) {
      ParserRuleContext r1 = (ParserRuleContext)tree;
      ParserRuleContext r2 = (ParserRuleContext)patternTree;
      ParseTree mismatchedNode = null;
      // (expr ...) and <expr>
      RuleTagToken ruleTagToken = getRuleTagToken(r2);
      if ( ruleTagToken!=null ) {
        ParseTreeMatch m = null;
        if ( r1.getRuleContext().getRuleIndex() == r2.getRuleContext().getRuleIndex() ) {
          // track label->list-of-nodes for both rule name and label (if any)
          labels.map(ruleTagToken.getRuleName(), tree);
          if ( ruleTagToken.getLabel()!=null ) {
            labels.map(ruleTagToken.getLabel(), tree);
          }
        }
        else {
          if (mismatchedNode == null) {
            mismatchedNode = r1;
          }
        }

        return mismatchedNode;
      }

      // (expr ...) and (expr ...)
      if ( r1.getChildCount()!=r2.getChildCount() ) {
        if (mismatchedNode == null) {
          mismatchedNode = r1;
        }

        return mismatchedNode;
      }

      int n = r1.getChildCount();
      for (int i = 0; i<n; i++) {
        ParseTree childMatch = matchImpl(r1.getChild(i), patternTree.getChild(i), labels);
        if ( childMatch != null ) {
          return childMatch;
        }
      }
View Full Code Here

    String lexerName = grammarName+"Lexer";
    boolean ok =
      rawGenerateAndBuildRecognizer(grammarFileName, grammar, parserName, lexerName, false);
    assertTrue(ok);

    ParseTree result = execParser(startRule, input, parserName, lexerName);

    ParseTreePattern p = getPattern(grammarName, pattern, startRule);
    ParseTreeMatch match = p.match(result);
    boolean matched = match.succeeded();
    if ( invertMatch ) assertFalse(matched);
View Full Code Here

    super(adapters);
  }

  protected boolean isTerminalNode(ParseTree tree, int index, int symbolType) {
    if (tree.getChildCount() > index) {
      ParseTree node = tree.getChild(index);
      return node instanceof TerminalNode
          && ((TerminalNode) node).getSymbol().getType() == symbolType;
    } else {
      return false;
    }
View Full Code Here

      }
      return tokens;
    }
    final int childCount = tree.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final ParseTree child = tree.getChild(i);
      tokens.addAll(parseTree(child));
    }
    return tokens;
  }
View Full Code Here

          break;
        case FRACTION:
          outputQueue.push(new FractionNode(nextNode.getText()));
          break;
        case MINUS: {
          ParseTree parent = nextNode.getParent();
          if (parent instanceof NegexpContext) {
            nextNode = tokenList.poll();
            EquationNode node = null;
            if (LPAREN == getType(nextNode)) {
              Queue<TerminalNode> newTokenList = new ArrayDeque<TerminalNode>();
View Full Code Here

    ParserInterpreter parser = g.createParserInterpreter(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(printError);
    Rule start = g.getRule(startRule);
    ParseTree tree = parser.parse(start.index);

    // this loop works around a bug in ANTLR 4.2
    // https://github.com/antlr/antlr4/issues/461
    // https://github.com/antlr/intellij-plugin-v4/issues/23
    while (tree.getParent() != null) {
      tree = tree.getParent();
    }
    String sexpression = toStringTree(tree, Arrays.asList(parser.getRuleNames())).trim();

    return sexpression;
  }
View Full Code Here

            // not in scope of Javadoc comment.
            // Offset is line number of beginning of Javadoc comment.
            mErrorListener.setOffset(aBlockCommentAst.getLineNo() - 1);

            try {
                final ParseTree parseTree = parseJavadoc(javadocComment);

                final DetailNode node = convertParseTree2DetailNode(parseTree);

                processTree(node);
            }
View Full Code Here

TOP

Related Classes of org.antlr.v4.runtime.tree.ParseTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.