Fix invalid escape sequences generated for literals containing backslashes

This commit is contained in:
Sam Harwell 2013-01-15 09:17:56 -06:00
parent d3a76ebcb1
commit 0a6d1bff7e
2 changed files with 28 additions and 3 deletions

View File

@ -283,10 +283,17 @@ public class Target {
case 'b':
case 'f':
case '\\':
case 'u': // Assume unnnn
sb.append('\\'); // Pass the escape through as double \\
sb.append('\\'); // so that Java leaves as \u0000 string not char
// Pass the escape through
sb.append('\\');
break;
case 'u': // Assume unnnn
// Pass the escape through as double \\
// so that Java leaves as \u0000 string not char
sb.append('\\');
sb.append('\\');
break;
default:
// Remove the escape by virtue of not adding it here
// Thus \' becomes ' and so on

View File

@ -66,6 +66,24 @@ public class TestLexerExec extends BaseTest {
assertEquals(expecting, found);
}
@Test public void testSlashes() throws Exception {
String grammar =
"lexer grammar L;\n"+
"Backslash : '\\\\';\n" +
"Slash : '/';\n" +
"Vee : '\\\\/';\n" +
"Wedge : '/\\\\';\n"+
"WS : [ \\t] -> skip;";
String found = execLexer("L.g4", grammar, "L", "\\ / \\/ /\\");
String expecting =
"[@0,0:0='\\',<1>,1:0]\n" +
"[@1,2:2='/',<2>,1:2]\n" +
"[@2,4:5='\\/',<3>,1:4]\n" +
"[@3,7:8='/\\',<4>,1:7]\n" +
"[@4,9:8='<EOF>',<-1>,1:9]\n";
assertEquals(expecting, found);
}
@Test
public void testNonGreedyTermination() throws Exception {
String grammar =