Njs: Parser: unexpected token continue

Created on 11 Jun 2020  路  2Comments  路  Source: nginx/njs

@lexborisov Take a look.

var a = 1;

while (0) {
    switch (a) {
    case 0:
        if (1) {
            continue // end without ';'
        }
        var b = 1;

    case 1:
        var c = 1;
    }
}

SyntaxError: Unexpected token "case"

var a = 1;

while (0) {
    switch (a) {
    case 0:
        if (1) {
            continue; // end with ';'
        }
        var b = 1;

    case 1:
        var c = 1;
    }
}

works well

I can't find the cause. It seems njs_parser_stack_pop is missed in someplace.
Noticed that I didn't put break, it's no problem.

bug

All 2 comments

@hongzhidao

Please, try this patch:

# HG changeset patch
# User Alexander Borisov <[email protected]>
# Date 1592226703 -10800
#      Mon Jun 15 16:11:43 2020 +0300
# Node ID bf61b4ce3109ae73212f72f28d4c433c6aa7dafb
# Parent  b33402f10e82b4043f79e60222feb326c4b0b45b
Parser: fixed semicolon check after continue/break statement.

This closes #318 issue on GitHub.

diff -r b33402f10e82 -r bf61b4ce3109 src/njs_parser.c
--- a/src/njs_parser.c  Sun May 31 08:45:41 2020 +0300
+++ b/src/njs_parser.c  Mon Jun 15 16:11:43 2020 +0300
@@ -5645,13 +5645,11 @@ njs_parser_break_continue(njs_parser_t *
         return njs_parser_failed(parser);

     default:
-        if (!parser->strict_semicolon
-            && parser->lexer->prev_type == NJS_TOKEN_LINE_END)
-        {
-            break;
-        }
-
         if (njs_lexer_token_is_label_identifier(token)) {
+            if (parser->lexer->prev_type == NJS_TOKEN_LINE_END) {
+                return njs_parser_stack_pop(parser);
+            }
+
             if (njs_label_find(parser->vm, parser->scope,
                                token->unique_id) == NULL)
             {
diff -r b33402f10e82 -r bf61b4ce3109 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c  Sun May 31 08:45:41 2020 +0300
+++ b/src/test/njs_unit_test.c  Mon Jun 15 16:11:43 2020 +0300
@@ -2828,6 +2828,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var i; for (i in [1,2,3]) {Object.seal({});}"),
       njs_str("undefined") },

+    { njs_str("while (0) {continue\n}"),
+      njs_str("undefined") },
+
     /* break. */

     { njs_str("break"),
@@ -2896,6 +2899,9 @@ static njs_unit_test_t  njs_test[] =
                  "for (i in a) if (a[i] > 4) break; s += a[i]; s"),
       njs_str("5") },

+    { njs_str("while (0) {break\n}"),
+      njs_str("undefined") },
+
     /* Labels. */

     { njs_str("var n = 0; a:{n++}; a:{n++}; n"),

@lexborisov
Works well, now. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xbb123 picture xbb123  路  4Comments

fishioon picture fishioon  路  3Comments

drsm picture drsm  路  3Comments

porunov picture porunov  路  3Comments

drsm picture drsm  路  4Comments