Search in sources :

Example 36 with LexerGrammar

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

the class TestATNLexerInterpreter method testWildOnEndFirstAlt.

@Test
public void testWildOnEndFirstAlt() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + // should pursue '.' since xyz hits stop first, before 2nd alt
    "A : 'xy' .\n" + "  | 'xy'\n" + "  ;\n" + "Z : 'z'\n" + "  ;\n");
    checkLexerMatches(lg, "xy", "A, EOF");
    checkLexerMatches(lg, "xyz", "A, EOF");
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 37 with LexerGrammar

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

the class TestATNLexerInterpreter method testShortLongRule2.

@Test
public void testShortLongRule2() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + // make sure nongreedy mech cut off doesn't kill this alt
    "A : 'xyz'\n" + "  | 'xy'\n" + "  ;\n");
    checkLexerMatches(lg, "xy", "A, EOF");
    checkLexerMatches(lg, "xyz", "A, EOF");
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 38 with LexerGrammar

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

the class TestATNLexerInterpreter method testLexerLoops.

@Test
public void testLexerLoops() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "INT : '0'..'9'+ ;\n" + "ID : 'a'..'z'+ ;\n");
    String expecting = "ID, INT, ID, INT, EOF";
    checkLexerMatches(lg, "a34bde3", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 39 with LexerGrammar

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

the class TestATNLexerInterpreter method testLexerKeywordIDAmbiguity.

@Test
public void testLexerKeywordIDAmbiguity() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "KEND : 'end' ;\n" + "ID : 'a'..'z'+ ;\n" + "WS : (' '|'\\n')+ ;");
    String expecting = "ID, EOF";
    // checkLexerMatches(lg, "e", expecting);
    expecting = "KEND, EOF";
    checkLexerMatches(lg, "end", expecting);
    expecting = "ID, EOF";
    checkLexerMatches(lg, "ending", expecting);
    expecting = "ID, WS, KEND, WS, ID, EOF";
    checkLexerMatches(lg, "a end bcd", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 40 with LexerGrammar

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

the class TestATNLexerInterpreter method testEOFAtEndOfLineComment.

@Test
public void testEOFAtEndOfLineComment() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "CMT : '//' ~('\\n')* ;\n");
    String expecting = "CMT, EOF";
    checkLexerMatches(lg, "//x", expecting);
}
Also used : 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