[njs] Making njs_lexer_rollback() more generic.

Dmitry Volyntsev xeioex at nginx.com
Mon Apr 29 13:13:07 UTC 2019


details:   https://hg.nginx.org/njs/rev/8f9f3d1e454e
branches:  
changeset: 938:8f9f3d1e454e
user:      hongzhidao <hongzhidao at gmail.com>
date:      Mon Apr 29 13:31:16 2019 +0800
description:
Making njs_lexer_rollback() more generic.

diffstat:

 njs/njs_lexer.c             |  32 ++++++++++++++++++++++++--------
 njs/njs_lexer.h             |   6 +-----
 njs/njs_parser_expression.c |   7 ++++++-
 3 files changed, 31 insertions(+), 14 deletions(-)

diffs (98 lines):

diff -r 9753a6388cb3 -r 8f9f3d1e454e njs/njs_lexer.c
--- a/njs/njs_lexer.c	Sun Apr 28 14:56:33 2019 +0300
+++ b/njs/njs_lexer.c	Mon Apr 29 13:31:16 2019 +0800
@@ -307,6 +307,11 @@ njs_lexer_token(njs_vm_t *vm, njs_lexer_
 
     lexer->prev_start = lexer->start;
 
+    if (lexer->lexer_token != NULL) {
+        lexer->prev_token = lexer->lexer_token->token;
+        nxt_mp_free(vm->mem_pool, lexer->lexer_token);
+    }
+
     if (nxt_queue_is_empty(&lexer->preread)) {
         lt = njs_lexer_token_push(vm, lexer);
         if (nxt_slow_path(lt == NULL)) {
@@ -314,14 +319,7 @@ njs_lexer_token(njs_vm_t *vm, njs_lexer_
         }
     }
 
-    lt = njs_lexer_token_pop(lexer);
-
-    if (lexer->lexer_token != NULL && lexer->lexer_token != lt) {
-        lexer->prev_token = lexer->lexer_token->token;
-        nxt_mp_free(vm->mem_pool, lexer->lexer_token);
-    }
-
-    lexer->lexer_token = lt;
+    lexer->lexer_token = njs_lexer_token_pop(lexer);
 
     return njs_lexer_token_name_resolve(lexer, lexer->lexer_token);
 }
@@ -399,6 +397,24 @@ njs_lexer_token_pop(njs_lexer_t *lexer)
 }
 
 
+nxt_int_t
+njs_lexer_rollback(njs_vm_t *vm, njs_lexer_t *lexer)
+{
+    njs_lexer_token_t  *lt;
+
+    lt = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_lexer_token_t));
+    if (nxt_slow_path(lt == NULL)) {
+        return NXT_ERROR;
+    }
+
+    *lt = *lexer->lexer_token;
+
+    nxt_queue_insert_head(&lexer->preread, &lt->link);
+
+    return NXT_OK;
+}
+
+
 static njs_token_t
 njs_lexer_token_name_resolve(njs_lexer_t *lexer, njs_lexer_token_t *lt)
 {
diff -r 9753a6388cb3 -r 8f9f3d1e454e njs/njs_lexer.h
--- a/njs/njs_lexer.h	Sun Apr 28 14:56:33 2019 +0300
+++ b/njs/njs_lexer.h	Mon Apr 29 13:31:16 2019 +0800
@@ -258,13 +258,9 @@ nxt_int_t njs_lexer_init(njs_vm_t *vm, n
 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);
+nxt_int_t njs_lexer_rollback(njs_vm_t *vm, njs_lexer_t *lexer);
 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)                                              \
-    do {                                                                       \
-        nxt_queue_insert_head(&(lexer)->preread, &(lexer)->lexer_token->link); \
-    } while (0)
-
 
 #endif /* _NJS_LEXER_H_INCLUDED_ */
diff -r 9753a6388cb3 -r 8f9f3d1e454e njs/njs_parser_expression.c
--- a/njs/njs_parser_expression.c	Sun Apr 28 14:56:33 2019 +0300
+++ b/njs/njs_parser_expression.c	Mon Apr 29 13:31:16 2019 +0800
@@ -682,6 +682,7 @@ static njs_token_t
 njs_parser_post_inc_dec_expression(njs_vm_t *vm, njs_parser_t *parser,
     njs_token_t token)
 {
+    nxt_int_t               ret;
     njs_parser_node_t       *node;
     njs_vmcode_operation_t  operation;
 
@@ -709,7 +710,11 @@ njs_parser_post_inc_dec_expression(njs_v
     /* Automatic semicolon insertion. */
 
     if (parser->lexer->prev_token == NJS_TOKEN_LINE_END) {
-        njs_lexer_rollback(parser->lexer);
+        ret = njs_lexer_rollback(vm, parser->lexer);
+        if (nxt_slow_path(ret != NXT_OK)) {
+            return NJS_TOKEN_ERROR;
+        }
+
         return NJS_TOKEN_SEMICOLON;
     }
 


More information about the nginx-devel mailing list