From 2f2fd4585e66d3b99a4ba5906bca84350930bd23 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 26 May 2013 13:43:56 -0500 Subject: [PATCH] Improved error message for unterminated string literals (fixes #243) --- tool/src/org/antlr/v4/parse/ANTLRLexer.g | 15 +++++++++++---- tool/src/org/antlr/v4/tool/ErrorType.java | 11 +++++++++++ .../org/antlr/v4/test/TestToolSyntaxErrors.java | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tool/src/org/antlr/v4/parse/ANTLRLexer.g b/tool/src/org/antlr/v4/parse/ANTLRLexer.g index 25690fc70..fc46ca517 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRLexer.g +++ b/tool/src/org/antlr/v4/parse/ANTLRLexer.g @@ -618,10 +618,17 @@ SRC : 'src' WSCHARS+ file=ACTION_STRING_LITERAL WSCHARS+ line=INT // may contain unicode escape sequences of the form \uxxxx, where x // is a valid hexadecimal number (as per Java basically). STRING_LITERAL -@init { - int len = 0; -} - : '\'' ( ( ESC_SEQ | ~('\\'|'\''|'\r'|'\n') ) {len++;} )* '\'' + : '\'' ( ( ESC_SEQ | ~('\\'|'\''|'\r'|'\n') ) )* + ( '\'' + | // Unterminated string literal + { + Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1); + t.setLine(state.tokenStartLine); + t.setText(state.text); + t.setCharPositionInLine(state.tokenStartCharPositionInLine); + grammarError(ErrorType.UNTERMINATED_STRING_LITERAL, t); + } + ) ; // A valid hex digit specification diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 605b83c31..b8b0cee3e 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -207,6 +207,17 @@ public enum ErrorType { * */ UNWANTED_LEXER_COMMAND_ARGUMENT(151, "lexer command '' does not take any arguments", ErrorSeverity.ERROR), + /** + * The parser contains an unterminated string literal. + *

+ * The following rule produces this error. + * + *

+	 * x : 'x'; // ok
+	 * y : 'y;  // error 152
+	 * 
+ */ + UNTERMINATED_STRING_LITERAL(152, "unterminated string literal", ErrorSeverity.ERROR), // Backward incompatibility errors V3_TREE_GRAMMAR(200, "tree grammars are not supported in ANTLR 4", ErrorSeverity.ERROR), diff --git a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java index a4b42d744..13b589b5d 100644 --- a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java +++ b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java @@ -172,6 +172,22 @@ public class TestToolSyntaxErrors extends BaseTest { super.testErrors(pair, true); } + /** + * This is a regression test for antlr/antlr4#243 + * "Generate a good message for unterminated strings" + * https://github.com/antlr/antlr4/issues/243 + */ + @Test public void testUnterminatedStringLiteral() { + String[] pair = new String[] { + "grammar A;\n" + + "a : 'x\n" + + " ;\n", + + "error(" + ErrorType.UNTERMINATED_STRING_LITERAL.code + "): A.g4:2:4: unterminated string literal\n" + }; + super.testErrors(pair, true); + } + /** * This is a regression test for antlr/antlr4#262 * "Parser Rule Name Starting With an Underscore"