From 1e5cfb4a1ebf038a306e8aae131be809f960c639 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 30 Oct 2012 08:46:02 -0500 Subject: [PATCH] Add unit test for behavior of lexer with input-position-sensitive predicates --- .../test/org/antlr/v4/test/TestLexerExec.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tool/test/org/antlr/v4/test/TestLexerExec.java b/tool/test/org/antlr/v4/test/TestLexerExec.java index 1f04c7e99..4e4838d6f 100644 --- a/tool/test/org/antlr/v4/test/TestLexerExec.java +++ b/tool/test/org/antlr/v4/test/TestLexerExec.java @@ -266,6 +266,31 @@ public class TestLexerExec extends BaseTest { assertEquals(expecting, found); } + @Test public void testLexerInputPositionSensitivePredicates() throws Exception { + // The predicates here make sense on the left edge, but are currently + // only working on the right edge... resolving this would remove the + // need for Lexer.getSpeculativeText as well. + String grammar = + "lexer grammar L;\n"+ + "WORD1 : ID1+ {System.out.println(getText());} ;\n"+ + "WORD2 : ID2+ {System.out.println(getText());} ;\n"+ + "fragment ID1 : {getCharPositionInLine()<2}? [a-zA-Z];\n"+ + "fragment ID2 : {getCharPositionInLine()>=2}? [a-zA-Z];\n"+ + "WS : (' '|'\\n') -> skip;\n"; + String found = execLexer("L.g4", grammar, "L", "a cde\nabcde\n"); + String expecting = + "a\n" + + "cde\n" + + "ab\n" + + "cde\n" + + "[@0,0:0='a',<1>,1:0]\n" + + "[@1,2:4='cde',<2>,1:2]\n" + + "[@2,6:7='ab',<1>,2:0]\n" + + "[@3,8:10='cde',<2>,2:2]\n" + + "[@4,12:11='',<-1>,3:0]\n"; + assertEquals(expecting, found); + } + @Test public void testActionExecutedInDFA() throws Exception { String grammar = "lexer grammar L;\n"+