Search in sources :

Example 6 with ParseTreePattern

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

the class TestParseTreeMatcher method testCompilingMultipleTokens.

@Test
public void testCompilingMultipleTokens() throws Exception {
    String grammar = "grammar X2;\n" + "s : ID '=' ID ';' ;\n" + "ID : [a-z]+ ;\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> = <ID> ;", m.getParser().getRuleIndex("s"));
    String results = t.getPatternTree().toStringTree(m.getParser());
    String expected = "(s <ID> = <ID> ;)";
    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)

Example 7 with ParseTreePattern

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

the class TestParseTreeMatcher method testHiddenTokensNotSeenByTreePatternParser.

@Test
public void testHiddenTokensNotSeenByTreePatternParser() 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]+ -> channel(HIDDEN) ;\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)

Example 8 with ParseTreePattern

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

the class TestParseTreeMatcher method getPattern.

public ParseTreePattern getPattern(String grammarName, String pattern, String ruleName) throws Exception {
    Class<? extends Lexer> lexerClass = loadLexerClassFromTempDir(grammarName + "Lexer");
    Constructor<? extends Lexer> ctor = lexerClass.getConstructor(CharStream.class);
    Lexer lexer = ctor.newInstance((CharStream) null);
    Class<? extends Parser> parserClass = loadParserClassFromTempDir(grammarName + "Parser");
    Constructor<? extends Parser> pctor = parserClass.getConstructor(TokenStream.class);
    Parser parser = pctor.newInstance(new CommonTokenStream(lexer));
    return parser.compileParseTreePattern(pattern, parser.getRuleIndex(ruleName));
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Lexer(org.antlr.v4.runtime.Lexer) Parser(org.antlr.v4.runtime.Parser)

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