Search in sources :

Example 1 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testPatternMatchesStartRule2.

@Test
public void testPatternMatchesStartRule2() throws Exception {
    String grammar = "grammar X2;\n" + "s : ID '=' expr ';' | 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> <ID> ;", m.getParser().getRuleIndex("s"));
    } catch (NoViableAltException e) {
        failed = true;
    }
    assertTrue(failed);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Test(org.junit.Test)

Example 2 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testTokenizingPattern.

@Test
public void testTokenizingPattern() throws Exception {
    String grammar = "grammar X1;\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("X1.g4", grammar, "X1Parser", "X1Lexer", false);
    assertTrue(ok);
    ParseTreePatternMatcher m = getPatternMatcher("X1");
    List<? extends Token> tokens = m.tokenize("<ID> = <expr> ;");
    String results = tokens.toString();
    String expected = "[ID:3, [@-1,1:1='=',<1>,1:1], expr:7, [@-1,1:1=';',<2>,1:1]]";
    assertEquals(expected, results);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Test(org.junit.Test)

Example 3 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testInvertedTags.

@Test
public void testInvertedTags() throws Exception {
    ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
    String result = null;
    try {
        m.split(">expr<");
    } catch (IllegalArgumentException iae) {
        result = iae.getMessage();
    }
    String expected = "tag delimiters out of order in pattern: >expr<";
    assertEquals(expected, result);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Test(org.junit.Test)

Example 4 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testDelimiters.

@Test
public void testDelimiters() throws Exception {
    ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
    m.setDelimiters("<<", ">>", "$");
    String result = m.split("<<ID>> = <<expr>> ;$<< ick $>>").toString();
    assertEquals("[ID, ' = ', expr, ' ;<< ick >>']", result);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) Test(org.junit.Test)

Example 5 with ParseTreePatternMatcher

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

the class TestParseTreeMatcher method testUnclosedTag.

@Test
public void testUnclosedTag() throws Exception {
    ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
    String result = null;
    try {
        m.split("<expr hi mom");
    } catch (IllegalArgumentException iae) {
        result = iae.getMessage();
    }
    String expected = "unterminated tag in pattern: <expr hi mom";
    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