Search in sources :

Example 36 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project antlr4 by antlr.

the class ParseTreePatternMatcher method compile.

/**
	 * For repeated use of a tree pattern, compile it to a
	 * {@link ParseTreePattern} using this method.
	 */
public ParseTreePattern compile(String pattern, int patternRuleIndex) {
    List<? extends Token> tokenList = tokenize(pattern);
    ListTokenSource tokenSrc = new ListTokenSource(tokenList);
    CommonTokenStream tokens = new CommonTokenStream(tokenSrc);
    ParserInterpreter parserInterp = new ParserInterpreter(parser.getGrammarFileName(), 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));
    } catch (ParseCancellationException e) {
        throw (RecognitionException) e.getCause();
    } catch (RecognitionException re) {
        throw re;
    } catch (Exception e) {
        throw new CannotInvokeStartRule(e);
    }
    // Make sure tree pattern compilation checks for a complete parse
    if (tokens.LA(1) != Token.EOF) {
        throw new StartRuleDoesNotConsumeFullPattern();
    }
    return new ParseTreePattern(this, pattern, patternRuleIndex, tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ListTokenSource(org.antlr.v4.runtime.ListTokenSource) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 37 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project antlr4 by antlr.

the class XPath method evaluate.

/**
	 * 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();
    // don't set t's parent.
    dummyRoot.children = Collections.singletonList(t);
    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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 38 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project antlr4 by antlr.

the class IterativeParseTreeWalker method walk.

@Override
public void walk(ParseTreeListener listener, ParseTree t) {
    final Deque<ParseTree> nodeStack = new ArrayDeque<ParseTree>();
    final IntegerStack indexStack = new IntegerStack();
    ParseTree currentNode = t;
    int currentIndex = 0;
    while (currentNode != null) {
        // pre-order visit
        if (currentNode instanceof ErrorNode) {
            listener.visitErrorNode((ErrorNode) currentNode);
        } else if (currentNode instanceof TerminalNode) {
            listener.visitTerminal((TerminalNode) currentNode);
        } else {
            final RuleNode r = (RuleNode) currentNode;
            enterRule(listener, r);
        }
        // Move down to first child, if exists
        if (currentNode.getChildCount() > 0) {
            nodeStack.push(currentNode);
            indexStack.push(currentIndex);
            currentIndex = 0;
            currentNode = currentNode.getChild(0);
            continue;
        }
        // No child nodes, so walk tree
        do {
            // post-order visit
            if (currentNode instanceof RuleNode) {
                exitRule(listener, (RuleNode) currentNode);
            }
            // No parent, so no siblings
            if (nodeStack.isEmpty()) {
                currentNode = null;
                currentIndex = 0;
                break;
            }
            // Move to next sibling if possible
            currentNode = nodeStack.peek().getChild(++currentIndex);
            if (currentNode != null) {
                break;
            }
            // No next, sibling, so move up
            currentNode = nodeStack.pop();
            currentIndex = indexStack.pop();
        } while (currentNode != null);
    }
}
Also used : IntegerStack(org.antlr.v4.runtime.misc.IntegerStack) ArrayDeque(java.util.ArrayDeque)

Example 39 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project antlr4 by antlr.

the class BaseJavaTest method execParser.

public ParseTree execParser(String startRuleName, String input, String parserName, String lexerName) throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    return execStartRule(startRuleName, parser);
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) Parser(org.antlr.v4.runtime.Parser)

Example 40 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project antlr4 by antlr.

the class TestAmbigParseTrees method testAmbiguousTrees.

public void testAmbiguousTrees(LexerGrammar lg, Grammar g, String startRule, String input, int decision, String expectedAmbigAlts, String overallTree, String[] expectedParseTrees) {
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    final GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    parser.setProfile(true);
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    // PARSE
    int ruleIndex = g.rules.get(startRule).index;
    ParserRuleContext parseTree = parser.parse(ruleIndex);
    assertEquals(overallTree, Trees.toStringTree(parseTree, nodeTextProvider));
    System.out.println();
    DecisionInfo[] decisionInfo = parser.getParseInfo().getDecisionInfo();
    List<AmbiguityInfo> ambiguities = decisionInfo[decision].ambiguities;
    assertEquals(1, ambiguities.size());
    AmbiguityInfo ambiguityInfo = ambiguities.get(0);
    List<ParserRuleContext> ambiguousParseTrees = GrammarParserInterpreter.getAllPossibleParseTrees(g, parser, tokens, decision, ambiguityInfo.ambigAlts, ambiguityInfo.startIndex, ambiguityInfo.stopIndex, ruleIndex);
    assertEquals(expectedAmbigAlts, ambiguityInfo.ambigAlts.toString());
    assertEquals(ambiguityInfo.ambigAlts.cardinality(), ambiguousParseTrees.size());
    for (int i = 0; i < ambiguousParseTrees.size(); i++) {
        ParserRuleContext t = ambiguousParseTrees.get(i);
        assertEquals(expectedParseTrees[i], Trees.toStringTree(t, nodeTextProvider));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) AmbiguityInfo(org.antlr.v4.runtime.atn.AmbiguityInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)89 CommonToken (org.antlr.v4.runtime.CommonToken)32 Test (org.junit.Test)32 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)24 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)19 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 HtmlElementContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementContext)12 JavadocTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocTagContext)12 JavadocInlineTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocInlineTagContext)10 ReferenceContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ReferenceContext)10 Grammar (org.antlr.v4.tool.Grammar)10 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9 HtmlTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlTagContext)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)8 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)7 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)7