[njs] Simplified parsing of var statements.

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


details:   https://hg.nginx.org/njs/rev/601a52c421d1
branches:  
changeset: 939:601a52c421d1
user:      hongzhidao <hongzhidao at gmail.com>
date:      Mon Apr 29 15:57:36 2019 +0300
description:
Simplified parsing of var statements.

diffstat:

 njs/njs_parser.c   |  37 +++++++++++++------------------------
 njs/njs_variable.c |  28 +++++++++++++++++++++-------
 2 files changed, 34 insertions(+), 31 deletions(-)

diffs (144 lines):

diff -r 8f9f3d1e454e -r 601a52c421d1 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Apr 29 13:31:16 2019 +0800
+++ b/njs/njs_parser.c	Mon Apr 29 15:57:36 2019 +0300
@@ -38,7 +38,7 @@ static njs_parser_node_t *njs_parser_ret
 static njs_token_t njs_parser_return_statement(njs_vm_t *vm,
     njs_parser_t *parser);
 static njs_token_t njs_parser_var_statement(njs_vm_t *vm, njs_parser_t *parser,
-    nxt_bool_t var_in);
+    njs_token_t parent, nxt_bool_t var_in);
 static njs_token_t njs_parser_if_statement(njs_vm_t *vm, njs_parser_t *parser);
 static njs_token_t njs_parser_switch_statement(njs_vm_t *vm,
     njs_parser_t *parser);
@@ -377,7 +377,7 @@ njs_parser_statement(njs_vm_t *vm, njs_p
 
         switch (token) {
         case NJS_TOKEN_VAR:
-            token = njs_parser_var_statement(vm, parser, 0);
+            token = njs_parser_var_statement(vm, parser, token, 0);
             break;
 
         case NJS_TOKEN_RETURN:
@@ -490,28 +490,13 @@ njs_parser_block_statement(njs_vm_t *vm,
 static njs_token_t
 njs_parser_block(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token)
 {
-    njs_parser_node_t  *node;
-
     if (token == NJS_TOKEN_FUNCTION) {
         njs_parser_syntax_error(vm, parser,
               "Functions can only be declared at top level or inside a block");
         return NJS_TOKEN_ILLEGAL;
     }
 
-    token = njs_parser_statement(vm, parser, token);
-    if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
-        return token;
-    }
-
-    node = parser->node;
-
-    if (node != NULL && node->token == NJS_TOKEN_BLOCK) {
-        parser->node = node->left;
-
-        nxt_mp_free(vm->mem_pool, node);
-    }
-
-    return token;
+    return njs_parser_statement(vm, parser, token);
 }
 
 
@@ -1051,10 +1036,14 @@ njs_parser_return_statement(njs_vm_t *vm
 
 
 static njs_token_t
-njs_parser_var_statement(njs_vm_t *vm, njs_parser_t *parser, nxt_bool_t var_in)
+njs_parser_var_statement(njs_vm_t *vm, njs_parser_t *parser, njs_token_t parent,
+    nxt_bool_t var_in)
 {
-    njs_token_t        token;
-    njs_parser_node_t  *left, *stmt, *name, *assign, *expr;
+    njs_token_t          token;
+    njs_parser_node_t    *left, *stmt, *name, *assign, *expr;
+    njs_variable_type_t  type;
+
+    type = NJS_VARIABLE_VAR;
 
     parser->node = NULL;
     left = NULL;
@@ -1080,7 +1069,7 @@ njs_parser_var_statement(njs_vm_t *vm, n
         name = njs_parser_variable_node(vm, parser,
                                         njs_parser_text(parser),
                                         njs_parser_key_hash(parser),
-                                        NJS_VARIABLE_VAR);
+                                        type);
         if (nxt_slow_path(name == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1111,7 +1100,7 @@ njs_parser_var_statement(njs_vm_t *vm, n
             expr = parser->node;
         }
 
-        assign = njs_parser_node_new(vm, parser, NJS_TOKEN_VAR);
+        assign = njs_parser_node_new(vm, parser, parent);
         if (nxt_slow_path(assign == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1400,7 +1389,7 @@ njs_parser_for_statement(njs_vm_t *vm, n
 
         if (token == NJS_TOKEN_VAR) {
 
-            token = njs_parser_var_statement(vm, parser, 1);
+            token = njs_parser_var_statement(vm, parser, token, 1);
             if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
                 return token;
             }
diff -r 8f9f3d1e454e -r 601a52c421d1 njs/njs_variable.c
--- a/njs/njs_variable.c	Mon Apr 29 13:31:16 2019 +0800
+++ b/njs/njs_variable.c	Mon Apr 29 15:57:36 2019 +0300
@@ -91,16 +91,23 @@ njs_variable_scope_add(njs_vm_t *vm, njs
     if (nxt_lvlhsh_find(&scope->variables, lhq) == NXT_OK) {
         var = lhq->value;
 
-        if (scope->module || scope->type == NJS_SCOPE_BLOCK
-            || (scope->type == NJS_SCOPE_GLOBAL && vm->options.module))
-        {
+        if (scope->module || scope->type == NJS_SCOPE_BLOCK) {
+
             if (type == NJS_VARIABLE_FUNCTION
                 || var->type == NJS_VARIABLE_FUNCTION)
             {
-                njs_parser_syntax_error(vm, vm->parser,
-                                        "\"%V\" has already been declared",
-                                        &lhq->key);
-                return NULL;
+                goto fail;
+            }
+        }
+
+        if (scope->type == NJS_SCOPE_GLOBAL) {
+
+            if (vm->options.module) {
+                if (type == NJS_VARIABLE_FUNCTION
+                    || var->type == NJS_VARIABLE_FUNCTION)
+                {
+                    goto fail;
+                }
             }
         }
 
@@ -128,6 +135,13 @@ njs_variable_scope_add(njs_vm_t *vm, njs
     njs_type_error(vm, "lvlhsh insert failed");
 
     return NULL;
+
+fail:
+
+    njs_parser_syntax_error(vm, vm->parser,
+                            "\"%V\" has already been declared",
+                            &lhq->key);
+    return NULL;
 }
 
 


More information about the nginx-devel mailing list