[njs] Parser: fixed regexp-literals parsing with '=' characters.

Dmitry Volyntsev xeioex at nginx.com
Tue Jul 14 17:22:18 UTC 2020


details:   https://hg.nginx.org/njs/rev/42d20205e054
branches:  
changeset: 1467:42d20205e054
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jul 14 17:20:29 2020 +0000
description:
Parser: fixed regexp-literals parsing with '=' characters.

Previously, njs lexer decoded '/=' as an assignment token.

This closes #329 issue on Github.

diffstat:

 src/njs_parser.c         |  5 +++++
 src/test/njs_unit_test.c |  6 ++++++
 2 files changed, 11 insertions(+), 0 deletions(-)

diffs (45 lines):

diff -r fcb5e172abaf -r 42d20205e054 src/njs_parser.c
--- a/src/njs_parser.c	Tue Jul 14 13:16:06 2020 +0000
+++ b/src/njs_parser.c	Tue Jul 14 17:20:29 2020 +0000
@@ -1115,6 +1115,7 @@ njs_parser_primary_expression_test(njs_p
 
     /* 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 *
     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) {
diff -r fcb5e172abaf -r 42d20205e054 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jul 14 13:16:06 2020 +0000
+++ b/src/test/njs_unit_test.c	Tue Jul 14 17:20:29 2020 +0000
@@ -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") },
 


More information about the nginx-devel mailing list