[njs] Fixed njs_parser_peek_token().

Dmitry Volyntsev xeioex at nginx.com
Sun Mar 17 14:17:24 UTC 2019


details:   https://hg.nginx.org/njs/rev/82f84bd637a3
branches:  
changeset: 829:82f84bd637a3
user:      hongzhidao <hongzhidao at gmail.com>
date:      Sun Mar 17 17:11:09 2019 +0300
description:
Fixed njs_parser_peek_token().

diffstat:

 njs/njs_lexer.h          |  10 +++++++---
 njs/njs_parser.c         |  16 ++++++++++------
 njs/njs_parser.h         |  18 +++++++++++-------
 njs/test/njs_unit_test.c |  15 +++++++++++++++
 4 files changed, 43 insertions(+), 16 deletions(-)

diffs (132 lines):

diff -r a750d6fdc54a -r 82f84bd637a3 njs/njs_lexer.h
--- a/njs/njs_lexer.h	Wed Mar 13 15:04:58 2019 +0800
+++ b/njs/njs_lexer.h	Sun Mar 17 17:11:09 2019 +0300
@@ -247,12 +247,16 @@ typedef struct {
 nxt_int_t njs_lexer_init(njs_vm_t *vm, njs_lexer_t *lexer, nxt_str_t *file,
     u_char *start, u_char *end);
 njs_token_t njs_lexer_token(njs_vm_t *vm, njs_lexer_t *lexer);
-njs_token_t njs_lexer_peek_token(njs_vm_t *vm, njs_lexer_t *lexer, size_t offset);
+njs_token_t njs_lexer_peek_token(njs_vm_t *vm, njs_lexer_t *lexer,
+    size_t offset);
 nxt_int_t njs_lexer_keywords_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash);
 void njs_lexer_keyword(njs_lexer_t *lexer, njs_lexer_token_t *lt);
 
-#define njs_lexer_rollback(lexer)                                       \
-    (lexer)->start = (lexer)->prev_start
+#define njs_lexer_rollback(lexer)                                              \
+    do {                                                                       \
+        nxt_queue_insert_head(&(lexer)->preread, &(lexer)->lexer_token->link); \
+        (lexer)->lexer_token = NULL;                                           \
+    } while (0)
 
 
 #endif /* _NJS_LEXER_H_INCLUDED_ */
diff -r a750d6fdc54a -r 82f84bd637a3 njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Mar 13 15:04:58 2019 +0800
+++ b/njs/njs_parser.c	Sun Mar 17 17:11:09 2019 +0300
@@ -332,6 +332,8 @@ static njs_token_t
 njs_parser_statement(njs_vm_t *vm, njs_parser_t *parser,
     njs_token_t token)
 {
+    size_t  offset;
+
     parser->node = NULL;
 
     switch (token) {
@@ -374,12 +376,6 @@ njs_parser_statement(njs_vm_t *vm, njs_p
 
     default:
 
-        if (token == NJS_TOKEN_NAME
-            && njs_parser_peek_token(vm, parser, 0) == NJS_TOKEN_COLON)
-        {
-            return njs_parser_labelled_statement(vm, parser);
-        }
-
         switch (token) {
         case NJS_TOKEN_VAR:
             token = njs_parser_var_statement(vm, parser);
@@ -398,6 +394,14 @@ njs_parser_statement(njs_vm_t *vm, njs_p
             token = njs_parser_brk_statement(vm, parser, token);
             break;
 
+        case NJS_TOKEN_NAME:
+            offset = 0;
+            if (njs_parser_peek_token(vm, parser, &offset) == NJS_TOKEN_COLON) {
+                return njs_parser_labelled_statement(vm, parser);
+            }
+
+            /* Fall through. */
+
         default:
             token = njs_parser_expression(vm, parser, token);
             break;
diff -r a750d6fdc54a -r 82f84bd637a3 njs/njs_parser.h
--- a/njs/njs_parser.h	Wed Mar 13 15:04:58 2019 +0800
+++ b/njs/njs_parser.h	Sun Mar 17 17:11:09 2019 +0300
@@ -100,10 +100,6 @@ void njs_parser_node_error(njs_vm_t *vm,
     njs_value_type_t type, const char *fmt, ...);
 
 
-#define njs_parser_peek_token(vm, parser, offset)                             \
-    njs_lexer_peek_token(vm, (parser)->lexer, offset)
-
-
 #define njs_parser_is_lvalue(node)                                            \
     ((node)->token == NJS_TOKEN_NAME || (node)->token == NJS_TOKEN_PROPERTY)
 
@@ -128,11 +124,19 @@ njs_parser_token(njs_vm_t *vm, njs_parse
 
     do {
         token = njs_lexer_token(vm, parser->lexer);
+    } while (nxt_slow_path(token == NJS_TOKEN_LINE_END));
 
-        if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
-            return token;
-        }
+    return token;
+}
+
 
+nxt_inline njs_token_t
+njs_parser_peek_token(njs_vm_t *vm, njs_parser_t *parser, size_t *offset)
+{
+    njs_token_t  token;
+
+    do {
+        token = njs_lexer_peek_token(vm, parser->lexer, (*offset)++);
     } while (nxt_slow_path(token == NJS_TOKEN_LINE_END));
 
     return token;
diff -r a750d6fdc54a -r 82f84bd637a3 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Mar 13 15:04:58 2019 +0800
+++ b/njs/test/njs_unit_test.c	Sun Mar 17 17:11:09 2019 +0300
@@ -2058,6 +2058,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a; a = 0; ++ \n a"),
       nxt_string("1") },
 
+    { nxt_string("var a; a = 0\n ++a"),
+      nxt_string("1") },
+
     { nxt_string("a = 0; a \n ++"),
       nxt_string("SyntaxError: Unexpected end of input in 2") },
 
@@ -2508,6 +2511,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("a: throw 'a'"),
       nxt_string("a") },
 
+    { nxt_string("a\n:\n1"),
+      nxt_string("1") },
+
+    { nxt_string("a\n\n:1"),
+      nxt_string("1") },
+
+    { nxt_string("a:\n\n1"),
+      nxt_string("1") },
+
+    { nxt_string("a:\n\n"),
+      nxt_string("SyntaxError: Unexpected end of input in 3") },
+
     { nxt_string("a : var n = 0; b :++n"),
       nxt_string("1") },
 


More information about the nginx-devel mailing list