Search in sources :

Example 11 with ParseTreePatternMatcher

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

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

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

the class TestParseTreeMatcher method testPatternMatchesStartRule.

@Test
public void testPatternMatchesStartRule() 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");
    boolean failed = false;
    try {
        m.compile("<ID> ;", m.getParser().getRuleIndex("s"));
    } catch (InputMismatchException e) {
        failed = true;
    }
    assertTrue(failed);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) Test(org.junit.Test)

Aggregations

ParseTreePatternMatcher (org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher)13 Test (org.junit.Test)12 ParseTreePattern (org.antlr.v4.runtime.tree.pattern.ParseTreePattern)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)1 Lexer (org.antlr.v4.runtime.Lexer)1 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)1 Parser (org.antlr.v4.runtime.Parser)1