summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-05-11 17:23:24 +0200
committerJo-Philipp Wich <jo@mein.io>2021-05-11 17:34:33 +0200
commitf20b56ff64b3144d618d228fbe1a3d07aad96450 (patch)
treea58b582fe73176ac39f1a3a6c2616f800bed0ff4
parent5c4e1ea8dca744482879dfc15ed21fc24beb2775 (diff)
compiler: properly parse slashes in parenthesized division expressions
Due to the special code path parsing the leading label portion of a parenthesized expression, slashes following a label were improperly treated as regular expression literal delimitters, emitting a syntax error when an otherwise valid expression such as `a / 1` was being parsed as first sub expression of a parenthesized expression. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--compiler.c10
-rw-r--r--tests/custom/03_bugs/22_compiler_parenthesized_division19
2 files changed, 27 insertions, 2 deletions
diff --git a/compiler.c b/compiler.c
index f8d63b6..b691b5e 100644
--- a/compiler.c
+++ b/compiler.c
@@ -1153,11 +1153,15 @@ uc_compiler_compile_paren(uc_compiler *compiler, bool assignable)
/* First try to parse a complete parameter expression and remember the
* consumed label tokens as we go. */
while (true) {
- if (uc_compiler_parse_match(compiler, TK_LABEL)) {
+ if (uc_compiler_parse_check(compiler, TK_LABEL)) {
if (!varnames)
varnames = ucv_array_new(NULL);
- ucv_array_push(varnames, ucv_get(compiler->parser->prev.uv));
+ ucv_array_push(varnames, ucv_get(compiler->parser->curr.uv));
+
+ /* A subsequent slash cannot be a regular expression literal */
+ compiler->parser->lex.no_regexp = true;
+ uc_compiler_parse_advance(compiler);
}
else if (uc_compiler_parse_match(compiler, TK_ELLIP)) {
uc_compiler_parse_consume(compiler, TK_LABEL);
@@ -1167,6 +1171,8 @@ uc_compiler_compile_paren(uc_compiler *compiler, bool assignable)
ucv_array_push(varnames, ucv_get(compiler->parser->prev.uv));
+ /* A subsequent slash cannot be a regular expression literal */
+ compiler->parser->lex.no_regexp = true;
uc_compiler_parse_consume(compiler, TK_RPAREN);
maybe_arrowfn = true;
diff --git a/tests/custom/03_bugs/22_compiler_parenthesized_division b/tests/custom/03_bugs/22_compiler_parenthesized_division
new file mode 100644
index 0000000..a70703f
--- /dev/null
+++ b/tests/custom/03_bugs/22_compiler_parenthesized_division
@@ -0,0 +1,19 @@
+When compiling a parenthesized division or division-assignment expression,
+the compiler didn't instruct the lexer to treat a potential subsequent
+slash as operand, leading to an incorrect syntax error exception.
+
+-- Expect stdout --
+0
+0
+0
+0
+-- End --
+
+-- Testcase --
+{%
+ print(a / 1, "\n"); // this was okay
+ print(a /= 1, "\n"); // okay too
+ print((a / 1), "\n"); // this failed
+ print((a /= 1), "\n"); // failed as well
+%}
+-- End --