]> git.kaiwu.me - njs.git/commitdiff
Parser: fixed regexp-literals parsing with '=' characters.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 14 Jul 2020 17:20:29 +0000 (17:20 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 14 Jul 2020 17:20:29 +0000 (17:20 +0000)
Previously, njs lexer decoded '/=' as an assignment token.

This closes #329 issue on Github.

src/njs_parser.c
src/test/njs_unit_test.c

index 5fb5a86393703e817feaa8c3909eaaa9817757ce..4803292ce9eb4badda9dcbf719c19b40bf7f12d4 100644 (file)
@@ -1115,6 +1115,7 @@ njs_parser_primary_expression_test(njs_parser_t *parser,
 
     /* RegularExpressionLiteral */
     case NJS_TOKEN_DIVISION:
+    case NJS_TOKEN_DIVISION_ASSIGNMENT:
         node = njs_parser_node_new(parser, NJS_TOKEN_REGEXP);
         if (node == NULL) {
             return NJS_ERROR;
@@ -1201,6 +1202,10 @@ njs_parser_regexp_literal(njs_parser_t *parser, njs_lexer_token_t *token,
     value = &parser->node->u.value;
     lexer = parser->lexer;
 
+    if (token->type == NJS_TOKEN_DIVISION_ASSIGNMENT) {
+        lexer->start--;
+    }
+
     for (p = lexer->start; p < lexer->end; p++) {
 
         switch (*p) {
index 1a6dee39e2623da64163d181dccf2b7ff6e729b7..5b184808d1665f6666473f43d5d6aaa7393cd88c 100644 (file)
@@ -7582,6 +7582,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'abc'.replace(/f/, 'X')"),
       njs_str("abc") },
 
+    { njs_str("'AB=C==='.replace(/=*$/, '')"),
+      njs_str("AB=C") },
+
     { njs_str("('a'.repeat(33) + 'bb').replace(/bb/, 'CC').slice(31)"),
       njs_str("aaCC") },
 
@@ -7784,6 +7787,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("/]/"),
       njs_str("/\\]/") },
 
+    { njs_str("/=/"),
+      njs_str("/=/") },
+
     { njs_str("/["),
       njs_str("SyntaxError: Unterminated RegExp \"/[\" in 1") },