Search in sources :

Example 1 with ParseTreePattern

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

the class ParseTreePatternMatcher method match.

/**
	 * Compare {@code pattern} matched against {@code tree} and return a
	 * {@link ParseTreeMatch} object that contains the matched elements, or the
	 * node at which the match failed. Pass in a compiled pattern instead of a
	 * string representation of a tree pattern.
	 */
public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern) {
    MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>();
    ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels);
    return new ParseTreeMatch(tree, pattern, labels, mismatchedNode);
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with ParseTreePattern

use of org.antlr.v4.runtime.tree.pattern.ParseTreePattern 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 3 with ParseTreePattern

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

the class TestParseTreeMatcher method checkPatternMatch.

public ParseTreeMatch checkPatternMatch(String grammar, String startRule, String input, String pattern, String grammarName, boolean invertMatch) throws Exception {
    String grammarFileName = grammarName + ".g4";
    String parserName = grammarName + "Parser";
    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);
    else
        assertTrue(matched);
    return match;
}
Also used : ParseTreePattern(org.antlr.v4.runtime.tree.pattern.ParseTreePattern) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeMatch(org.antlr.v4.runtime.tree.pattern.ParseTreeMatch)

Example 4 with ParseTreePattern

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

the class ParseTreePatternMatcher method matches.

/** 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;
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 5 with ParseTreePattern

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

the class TestParseTreeMatcher method testCompilingPattern.

@Test
public void testCompilingPattern() throws Exception {
    String grammar = "grammar X2;\n" + "s : ID '=' expr ';' ;\n" + "expr : ID | INT ;\n" + "ID : [a-z]+ ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
    boolean ok = rawGenerateAndBuildRecognizer("X2.g4", grammar, "X2Parser", "X2Lexer", false);
    assertTrue(ok);
    ParseTreePatternMatcher m = getPatternMatcher("X2");
    ParseTreePattern t = m.compile("<ID> = <expr> ;", m.getParser().getRuleIndex("s"));
    String results = t.getPatternTree().toStringTree(m.getParser());
    String expected = "(s <ID> = (expr <expr>) ;)";
    assertEquals(expected, results);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) ParseTreePattern(org.antlr.v4.runtime.tree.pattern.ParseTreePattern) Test(org.junit.Test)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 ParseTreePattern (org.antlr.v4.runtime.tree.pattern.ParseTreePattern)4 ParseTreePatternMatcher (org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher)3 Test (org.junit.Test)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 MultiMap (org.antlr.v4.runtime.misc.MultiMap)2 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 Lexer (org.antlr.v4.runtime.Lexer)1 ListTokenSource (org.antlr.v4.runtime.ListTokenSource)1 Parser (org.antlr.v4.runtime.Parser)1 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 ParseTreeMatch (org.antlr.v4.runtime.tree.pattern.ParseTreeMatch)1