Skip to content

Commit 22bfcfc

Browse files
authored
Treat a malformed escape sequence as an error. Fixes openscad#331. (openscad#6099)
1 parent 6afea6f commit 22bfcfc

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/core/lexer.l

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,27 @@ use[ \t\r\n]*"<" { BEGIN(cond_use); LOCATION_COUNT_LINES(parserlloc, yyte
179179

180180
\" { BEGIN(cond_string); stringcontents.clear(); }
181181
<cond_string>{
182+
%{/* Termination */%}
183+
\" { BEGIN(INITIAL); parserlval.text = strdup(stringcontents.c_str()); return TOK_STRING; }
184+
<<EOF>> { parsererror("Unterminated string"); return TOK_ERROR; }
185+
186+
%{/* Escape sequences */%}
182187
\\n { stringcontents += '\n'; }
183188
\\t { stringcontents += '\t'; }
184189
\\r { stringcontents += '\r'; }
185190
\\\\ { stringcontents += '\\'; }
186191
\\\" { stringcontents += '"'; }
187-
{UNICODE} { /* parser_error_pos -= strlen(lexertext) - 1; */ stringcontents += lexertext; }
188192
\\x[0-7]{H} { unsigned long i = strtoul(lexertext + 2, NULL, 16); stringcontents += (i == 0 ? ' ' : (unsigned char)(i & 0xff)); }
189193
\\u{H}{4}|\\U{H}{6} { const auto c = strtoul(lexertext + 2, NULL, 16); stringcontents += str_utf8_wrapper(c).toString(); }
190-
[^\\\n\"] { stringcontents += lexertext; }
191-
[\n\r] { LOCATION_ADD_LINES(parserlloc, yyleng); }
192-
\" { BEGIN(INITIAL); parserlval.text = strdup(stringcontents.c_str()); return TOK_STRING; }
193-
<<EOF>> { parsererror("Unterminated string"); return TOK_ERROR; }
194+
\\ { LOG(message_group::Warning, LOCATION(parserlloc), "", "Undefined escape sequence"); }
195+
196+
%{/* Special characters */%}
197+
\n { LOCATION_ADD_LINES(parserlloc, yyleng); }
198+
199+
%{/* Everything else */%}
200+
{UNICODE} { /* parser_error_pos -= strlen(lexertext) - 1; */ stringcontents += lexertext; }
201+
. { stringcontents += lexertext; }
202+
194203
}
195204
196205
[\t ] { LOCATION_NEXT(parserlloc); }

tests/regression/echo/unicode-tests-expected.echo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 4
2+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 4
3+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 6
4+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 6
5+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 8
6+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 8
7+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 10
8+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 10
9+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 12
10+
WARNING: Undefined escape sequence in file ../../tests/data/scad/misc/unicode-tests.scad, line 14
111
ECHO: "A"
212
ECHO: "01::x80xFF"
313
ECHO: "u0 U0"

0 commit comments

Comments
 (0)