Skip to content

Commit e74f480

Browse files
committed
Disallow unescaped newlines in string literals
1 parent b7a4b72 commit e74f480

File tree

6 files changed

+90
-72
lines changed

6 files changed

+90
-72
lines changed

docs/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ token StringLiteral
464464
= <b>&quot;\\&quot;&quot;</b> <a href="#string-literal">StringPart</a>* <b>&quot;\\&quot;&quot;</b>
465465
;
466466
fragment StringPart
467-
= <b>[^\\&quot;\\\\]</b>
467+
= <b>[^\\&quot;\\\\\\r\\n]</b>
468468
| <a href="#escape-sequence">EscapeSequence</a>
469469
;
470470
</pre>

generator/dparsergen/generator/grammarebnf.ebnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ token StringLiteral
367367
368368
/// ditto
369369
fragment StringPart
370-
= [^\"\\]
370+
= [^\"\\\r\n]
371371
| EscapeSequence
372372
;
373373

generator/dparsergen/generator/grammarebnf_lexer.d

Lines changed: 74 additions & 66 deletions
Large diffs are not rendered by default.

generator/dparsergen/generator/lexergenerator.d

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ void generateStateMachine(ref CodeWriter code, const EBNFGrammar lexerGrammar,
968968
goto lexerend;
969969
$$}
970970
else
971-
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar"), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
971+
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar").escapeChar(false), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
972972
$$}
973973
$$} else {
974974
$$needsElse = true;
@@ -1010,15 +1010,15 @@ void generateStateMachine(ref CodeWriter code, const EBNFGrammar lexerGrammar,
10101010
storedStart = size_t.max;
10111011
$$}
10121012
$$if (e.next3.id == size_t.max) {
1013-
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar"), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
1013+
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar").escapeChar(false), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
10141014
$$} else {
10151015
goto state$(e.next3.id);
10161016
$$}
10171017
}
10181018
else if (compareString.length < storedString.length && currentCharCorrect)
10191019
{
10201020
$$if (e.next2.id == size_t.max) {
1021-
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar"), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
1021+
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar").escapeChar(false), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
10221022
$$} else {
10231023
goto state$(e.next2.id);
10241024
$$}
@@ -1027,7 +1027,7 @@ void generateStateMachine(ref CodeWriter code, const EBNFGrammar lexerGrammar,
10271027
{
10281028
storedStart = size_t.max;
10291029
$$if (e.next.id == size_t.max) {
1030-
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar"), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
1030+
throw lexerException(text("Error unexpected \'", $(ascii?"currentChar":"currentDchar").escapeChar(false), "\'"), "$(codepointSetToStr(allAllowedSet).escapeD)", inputCopy.ptr - input.ptr);
10311031
$$} else {
10321032
goto state$(e.next.id);
10331033
$$}
@@ -1541,6 +1541,7 @@ const(char)[] createLexerCode(EBNFGrammar lexerGrammar, string modulename, strin
15411541
module $(modulename);
15421542
import dparsergen.core.grammarinfo;
15431543
import dparsergen.core.parseexception;
1544+
import dparsergen.core.utils;
15441545
import std.conv;
15451546
import std.string;
15461547
import std.typecons;

tests/fail_grammars/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tokennewline1.ebnf text eol=lf
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
GENERATOR_OUTPUT(optdescent, glr, normal, lexer):
3+
---
4+
tests/fail_grammars/tokennewline1.ebnf:7:4: Error unexpected '\n', expected [^\n\r]
5+
---
6+
*/
7+
S = "x
8+
";

0 commit comments

Comments
 (0)