From 49f9e093e87c4f30e195f176f46db57214eb92f7 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sat, 18 May 2013 11:18:58 -0500 Subject: [PATCH] Clean up character and string literals in the lexer --- tool/test/org/antlr/v4/test/Java-LR.g4 | 50 ++++++++++++++++++++------ tool/test/org/antlr/v4/test/Java.g4 | 50 ++++++++++++++++++++------ 2 files changed, 78 insertions(+), 22 deletions(-) diff --git a/tool/test/org/antlr/v4/test/Java-LR.g4 b/tool/test/org/antlr/v4/test/Java-LR.g4 index 4bab50c3d..44c10d865 100644 --- a/tool/test/org/antlr/v4/test/Java-LR.g4 +++ b/tool/test/org/antlr/v4/test/Java-LR.g4 @@ -1092,26 +1092,54 @@ BooleanLiteral | 'false' ; +// §3.10.4 Character Literals + CharacterLiteral - : '\'' ( EscapeSequence | ~('\''|'\\') ) '\'' - ; + : '\'' SingleCharacter '\'' + | '\'' EscapeSequence '\'' + ; + +fragment +SingleCharacter + : ~['\\] + ; + +// §3.10.5 String Literals StringLiteral - : '"' ( EscapeSequence | ~('\\'|'"') )* '"' - ; + : '"' StringCharacters? '"' + ; + +fragment +StringCharacters + : StringCharacter+ + ; + +fragment +StringCharacter + : ~["\\] + | EscapeSequence + ; + +// §3.10.6 Escape Sequences for Character and String Literals fragment EscapeSequence - : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') - | OctalEscape - ; + : '\\' [btnfr"'\\] + | OctalEscape + ; fragment OctalEscape - : '\\' ('0'..'3') ('0'..'7') ('0'..'7') - | '\\' ('0'..'7') ('0'..'7') - | '\\' ('0'..'7') - ; + : '\\' OctalDigit + | '\\' OctalDigit OctalDigit + | '\\' ZeroToThree OctalDigit OctalDigit + ; + +fragment +ZeroToThree + : [0-3] + ; // §3.10.7 The Null Literal diff --git a/tool/test/org/antlr/v4/test/Java.g4 b/tool/test/org/antlr/v4/test/Java.g4 index 48470d16e..13d1bd4d6 100644 --- a/tool/test/org/antlr/v4/test/Java.g4 +++ b/tool/test/org/antlr/v4/test/Java.g4 @@ -1176,26 +1176,54 @@ BooleanLiteral | 'false' ; +// §3.10.4 Character Literals + CharacterLiteral - : '\'' ( EscapeSequence | ~('\''|'\\') ) '\'' - ; + : '\'' SingleCharacter '\'' + | '\'' EscapeSequence '\'' + ; + +fragment +SingleCharacter + : ~['\\] + ; + +// §3.10.5 String Literals StringLiteral - : '"' ( EscapeSequence | ~('\\'|'"') )* '"' - ; + : '"' StringCharacters? '"' + ; + +fragment +StringCharacters + : StringCharacter+ + ; + +fragment +StringCharacter + : ~["\\] + | EscapeSequence + ; + +// §3.10.6 Escape Sequences for Character and String Literals fragment EscapeSequence - : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') - | OctalEscape - ; + : '\\' [btnfr"'\\] + | OctalEscape + ; fragment OctalEscape - : '\\' ('0'..'3') ('0'..'7') ('0'..'7') - | '\\' ('0'..'7') ('0'..'7') - | '\\' ('0'..'7') - ; + : '\\' OctalDigit + | '\\' OctalDigit OctalDigit + | '\\' ZeroToThree OctalDigit OctalDigit + ; + +fragment +ZeroToThree + : [0-3] + ; // §3.10.7 The Null Literal