Search in sources :

Example 96 with Lexer

use of org.antlr.v4.runtime.Lexer 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 97 with Lexer

use of org.antlr.v4.runtime.Lexer 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 98 with Lexer

use of org.antlr.v4.runtime.Lexer 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 99 with Lexer

use of org.antlr.v4.runtime.Lexer 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 100 with Lexer

use of org.antlr.v4.runtime.Lexer 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

Test (org.junit.Test)427 LexerGrammar (org.antlr.v4.tool.LexerGrammar)407 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)278 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)145 Grammar (org.antlr.v4.tool.Grammar)125 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)108 CharStream (org.antlr.v4.runtime.CharStream)98 ParseTree (org.antlr.v4.runtime.tree.ParseTree)91 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)56 IOException (java.io.IOException)44 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 Token (org.antlr.v4.runtime.Token)41 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)39 ArrayList (java.util.ArrayList)36 RecognitionException (org.antlr.v4.runtime.RecognitionException)26 StringReader (java.io.StringReader)23 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)23 TokenStream (org.antlr.v4.runtime.TokenStream)23 Lexer (org.antlr.v4.runtime.Lexer)22