Search in sources :

Example 41 with Grammar

use of org.antlr.v4.tool.Grammar 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 42 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestParseTreeMatcher method testIDNodeWithLabelMatches.

@Test
public void testIDNodeWithLabelMatches() throws Exception {
    String grammar = "grammar X8;\n" + "s : ID ';' ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
    String input = "x ;";
    String pattern = "<id:ID>;";
    ParseTreeMatch m = checkPatternMatch(grammar, "s", input, pattern, "X8");
    assertEquals("{ID=[x], id=[x]}", m.getLabels().toString());
    assertNotNull(m.get("id"));
    assertNotNull(m.get("ID"));
    assertEquals("x", m.get("id").getText());
    assertEquals("x", m.get("ID").getText());
    assertEquals("[x]", m.getAll("id").toString());
    assertEquals("[x]", m.getAll("ID").toString());
    assertNull(m.get("undefined"));
    assertEquals("[]", m.getAll("undefined").toString());
}
Also used : ParseTreeMatch(org.antlr.v4.runtime.tree.pattern.ParseTreeMatch) Test(org.junit.Test)

Example 43 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestParseTreeMatcher method testLabelGetsLastIDNode.

@Test
public void testLabelGetsLastIDNode() throws Exception {
    String grammar = "grammar X9;\n" + "s : ID ID ';' ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
    String input = "x y;";
    String pattern = "<id:ID> <id:ID>;";
    ParseTreeMatch m = checkPatternMatch(grammar, "s", input, pattern, "X9");
    assertEquals("{ID=[x, y], id=[x, y]}", m.getLabels().toString());
    assertNotNull(m.get("id"));
    assertNotNull(m.get("ID"));
    assertEquals("y", m.get("id").getText());
    assertEquals("y", m.get("ID").getText());
    assertEquals("[x, y]", m.getAll("id").toString());
    assertEquals("[x, y]", m.getAll("ID").toString());
    assertNull(m.get("undefined"));
    assertEquals("[]", m.getAll("undefined").toString());
}
Also used : ParseTreeMatch(org.antlr.v4.runtime.tree.pattern.ParseTreeMatch) Test(org.junit.Test)

Example 44 with Grammar

use of org.antlr.v4.tool.Grammar 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 45 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testImportFileLocationInSubdir.

@Test
public void testImportFileLocationInSubdir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    String subdir = tmpdir + "/sub";
    BaseRuntimeTest.mkdir(subdir);
    writeFile(subdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", subdir);
    assertEquals(0, equeue.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15