Search in sources :

Example 6 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testChunking.

@Test
public void testChunking() throws Exception {
    ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
    assertEquals("[ID, ' = ', expr, ' ;']", m.split("<ID> = <expr> ;").toString());
    assertEquals("[' ', ID, ' = ', expr]", m.split(" <ID> = <expr>").toString());
    assertEquals("[ID, ' = ', expr]", m.split("<ID> = <expr>").toString());
    assertEquals("[expr]", m.split("<expr>").toString());
    assertEquals("['<x> foo']", m.split("\\<x\\> foo").toString());
    assertEquals("['foo <x> bar ', tag]", m.split("foo \\<x\\> bar <tag>").toString());
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Test(org.junit.Test)

Example 7 with ParseTreePatternMatcher

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

Example 8 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method getPatternMatcher.

public ParseTreePatternMatcher getPatternMatcher(String grammarName) 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 new ParseTreePatternMatcher(lexer, parser);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Lexer(org.antlr.v4.runtime.Lexer) ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Parser(org.antlr.v4.runtime.Parser)

Example 9 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testCompilingPatternConsumesAllTokens.

@Test
public void testCompilingPatternConsumesAllTokens() 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> = <expr> ; extra", m.getParser().getRuleIndex("s"));
    } catch (ParseTreePatternMatcher.StartRuleDoesNotConsumeFullPattern e) {
        failed = true;
    }
    assertTrue(failed);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Test(org.junit.Test)

Example 10 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testExtraClose.

@Test
public void testExtraClose() throws Exception {
    ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
    String result = null;
    try {
        m.split("<expr> >");
    } catch (IllegalArgumentException iae) {
        result = iae.getMessage();
    }
    String expected = "missing start tag in pattern: <expr> >";
    assertEquals(expected, result);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) 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