Search in sources :

Example 46 with LexerGrammar

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

the class TestATNParserPrediction method testContinuePrediction2.

@Test
public void testContinuePrediction2() throws Exception {
    // ID is ambig for first two alts, but ID SEMI lets us move forward with alt 3
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + // one char
    "ID : 'a'..'z' ;\n" + "SEMI : ';' ;\n" + "INT : '0'..'9'+ ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {ID,SEMI,INT}\n" + "a : ID | ID | ID SEMI ;\n");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "a;", 3);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 47 with LexerGrammar

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

the class TestATNSerialization method testLexerAction.

@Test
public void testLexerAction() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' {a} ;\n" + "B : 'b' ;\n" + "C : 'c' {c} ;\n");
    String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:RULE_START 2\n" + "6:RULE_STOP 2\n" + "7:BASIC 0\n" + "8:BASIC 0\n" + "9:BASIC 0\n" + "10:BASIC 1\n" + "11:BASIC 1\n" + "12:BASIC 2\n" + "13:BASIC 2\n" + "14:BASIC 2\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "rule 2:5 3\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "0->5 EPSILON 0,0,0\n" + "1->7 EPSILON 0,0,0\n" + "3->10 EPSILON 0,0,0\n" + "5->12 EPSILON 0,0,0\n" + "7->8 ATOM 97,0,0\n" + "8->9 ACTION 0,0,0\n" + "9->2 EPSILON 0,0,0\n" + "10->11 ATOM 98,0,0\n" + "11->4 EPSILON 0,0,0\n" + "12->13 ATOM 99,0,0\n" + "13->14 ACTION 2,1,0\n" + "14->6 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getRuleNames()), Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 48 with LexerGrammar

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

the class TestATNSerialization method testLexerTwoRules.

@Test
public void testLexerTwoRules() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n");
    String expecting = "max type 2\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:BASIC 0\n" + "6:BASIC 0\n" + "7:BASIC 1\n" + "8:BASIC 1\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "1->5 EPSILON 0,0,0\n" + "3->7 EPSILON 0,0,0\n" + "5->6 ATOM 97,0,0\n" + "6->2 EPSILON 0,0,0\n" + "7->8 ATOM 98,0,0\n" + "8->4 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getRuleNames()), Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 49 with LexerGrammar

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

the class TestATNSerialization method testLexerEOF.

@Test
public void testLexerEOF() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "INT : 'a' EOF ;\n");
    String expecting = "max type 1\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "5:BASIC 0\n" + "rule 0:1 1\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "1->3 EPSILON 0,0,0\n" + "3->4 ATOM 97,0,0\n" + "4->5 ATOM 0,0,1\n" + "5->2 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getRuleNames()), Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 50 with LexerGrammar

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

the class TestATNSerialization method testLexerWildcardWithMode.

@Test
public void testLexerWildcardWithMode() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : 'a'..'z'+ ;\n" + "mode CMT;" + "COMMENT : '*/' {skip(); popMode();} ;\n" + "JUNK : . {more();} ;\n");
    String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:TOKEN_START -1\n" + "2:RULE_START 0\n" + "3:RULE_STOP 0\n" + "4:RULE_START 1\n" + "5:RULE_STOP 1\n" + "6:RULE_START 2\n" + "7:RULE_STOP 2\n" + "8:BASIC 0\n" + "9:PLUS_BLOCK_START 0 10\n" + "10:BLOCK_END 0\n" + "11:PLUS_LOOP_BACK 0\n" + "12:LOOP_END 0 11\n" + "13:BASIC 1\n" + "14:BASIC 1\n" + "15:BASIC 1\n" + "16:BASIC 1\n" + "17:BASIC 1\n" + "18:BASIC 2\n" + "19:BASIC 2\n" + "20:BASIC 2\n" + "rule 0:2 1\n" + "rule 1:4 2\n" + "rule 2:6 3\n" + "mode 0:0\n" + "mode 1:1\n" + "0->2 EPSILON 0,0,0\n" + "1->4 EPSILON 0,0,0\n" + "1->6 EPSILON 0,0,0\n" + "2->9 EPSILON 0,0,0\n" + "4->13 EPSILON 0,0,0\n" + "6->18 EPSILON 0,0,0\n" + "8->10 RANGE 97,122,0\n" + "9->8 EPSILON 0,0,0\n" + "10->11 EPSILON 0,0,0\n" + "11->9 EPSILON 0,0,0\n" + "11->12 EPSILON 0,0,0\n" + "12->3 EPSILON 0,0,0\n" + "13->14 ATOM 42,0,0\n" + "14->15 ATOM 47,0,0\n" + "15->16 EPSILON 0,0,0\n" + "16->17 ACTION 1,0,0\n" + "17->5 EPSILON 0,0,0\n" + "18->19 WILDCARD 0,0,0\n" + "19->20 ACTION 2,1,0\n" + "20->7 EPSILON 0,0,0\n" + "0:0\n" + "1:1\n" + "2:11\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getRuleNames()), Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

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