Search in sources :

Example 66 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestAmbigParseTrees method testAmbigAltInLeftRecursiveBelowStartRule.

@Test
@Ignore("Cannot currently determine outer alternatives for left-factored non-precedence rules")
public void testAmbigAltInLeftRecursiveBelowStartRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "SELF : 'self' ;\n" + "ID : [a-z]+ ;\n" + "DOT : '.' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : e ;\n" + "e : p | e DOT ID ;\n" + "p : SELF" + "  | SELF DOT ID" + "  ;", lg);
    String startRule = "s";
    String input = "self.x";
    String expectedAmbigAlts = "{1, 2}";
    // decision in p
    int decision = 1;
    String expectedOverallTree = "(s:1 (e:2 (e:1 (p:1 self)) . x))";
    String[] expectedParseTrees = { "(e:2 (e:1 (p:1 self)) . x)", "(p:2 self . x)" };
    testAmbiguousTrees(lg, g, startRule, input, decision, expectedAmbigAlts, expectedOverallTree, expectedParseTrees);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 67 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNConstruction method testLexerIsntSetMultiCharString.

@Test
public void testLexerIsntSetMultiCharString() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar P;\n" + "A : ('0x' | '0X') ;");
    String expecting = "s0->RuleStart_A_1\n" + "RuleStart_A_1->BlockStart_7\n" + "BlockStart_7->s3\n" + "BlockStart_7->s5\n" + "s3-'0'->s4\n" + "s5-'0'->s6\n" + "s4-'x'->BlockEnd_8\n" + "s6-'X'->BlockEnd_8\n" + "BlockEnd_8->RuleStop_A_2\n";
    checkTokensRule(g, null, expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 68 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNConstruction method testCharSetUnicodePropertyEscape.

@Test
public void testCharSetUnicodePropertyEscape() throws Exception {
    // The Gothic script is long dead and unlikely to change (which would
    // cause this test to fail)
    LexerGrammar g = new LexerGrammar("lexer grammar P;\n" + "A : [\\p{Gothic}] ;");
    String expecting = "s0->RuleStart_A_1\n" + "RuleStart_A_1->s3\n" + "s3-{66352..66378}->s4\n" + "s4->RuleStop_A_2\n";
    checkTokensRule(g, null, expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 69 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNConstruction method testRangeOrRange.

@Test
public void testRangeOrRange() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar P;\n" + "A : ('a'..'c' 'h' | 'q' 'j'..'l') ;");
    String expecting = "s0->RuleStart_A_1\n" + "RuleStart_A_1->BlockStart_7\n" + "BlockStart_7->s3\n" + "BlockStart_7->s5\n" + "s3-'a'..'c'->s4\n" + "s5-'q'->s6\n" + "s4-'h'->BlockEnd_8\n" + "s6-'j'..'l'->BlockEnd_8\n" + "BlockEnd_8->RuleStop_A_2\n";
    checkTokensRule(g, null, expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 70 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNConstruction method checkTokensRule.

void checkTokensRule(LexerGrammar g, String modeName, String expecting) {
    // }
    if (modeName == null)
        modeName = "DEFAULT_MODE";
    if (g.modes.get(modeName) == null) {
        System.err.println("no such mode " + modeName);
        return;
    }
    ParserATNFactory f = new LexerATNFactory(g);
    ATN nfa = f.createATN();
    ATNState startState = nfa.modeNameToStartState.get(modeName);
    ATNPrinter serializer = new ATNPrinter(g, startState);
    String result = serializer.asString();
    // System.out.print(result);
    assertEquals(expecting, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNPrinter(org.antlr.v4.automata.ATNPrinter) ATN(org.antlr.v4.runtime.atn.ATN) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)444 Test (org.junit.Test)410 Grammar (org.antlr.v4.tool.Grammar)140 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)120 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)100 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)72 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)58 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)26 ParseTree (org.antlr.v4.runtime.tree.ParseTree)24 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 CharStream (org.antlr.v4.runtime.CharStream)20 TokenStream (org.antlr.v4.runtime.TokenStream)17 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)14 STGroupString (org.stringtemplate.v4.STGroupString)14 ArrayList (java.util.ArrayList)13 ATNFactory (org.antlr.v4.automata.ATNFactory)12 DFA (org.antlr.v4.runtime.dfa.DFA)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12