[njs] Simplified parser tree building.

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 31 12:46:27 UTC 2019


details:   https://hg.nginx.org/njs/rev/9f2779de4cdd
branches:  
changeset: 748:9f2779de4cdd
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Jan 29 16:30:13 2019 +0800
description:
Simplified parser tree building.

diffstat:

 njs/njs_parser.c            |  146 +++++++++++++------------------------------
 njs/njs_parser.h            |   20 ++++-
 njs/njs_parser_expression.c |   62 ++++-------------
 3 files changed, 77 insertions(+), 151 deletions(-)

diffs (896 lines):

diff -r ac9b1c01a9b0 -r 9f2779de4cdd njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Jan 30 18:50:33 2019 +0300
+++ b/njs/njs_parser.c	Tue Jan 29 16:30:13 2019 +0800
@@ -134,14 +134,13 @@ njs_parser(njs_vm_t *vm, njs_parser_t *p
     if (node == NULL) {
         /* Empty string, just semicolons or variables declarations. */
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, 0);
         if (nxt_slow_path(node == NULL)) {
             return NXT_ERROR;
         }
     }
 
     node->token = NJS_TOKEN_END;
-    node->scope = parser->scope;
 
     parser->scope->node = node;
 
@@ -263,12 +262,11 @@ njs_parser_statement_chain(njs_vm_t *vm,
         if (parser->node != NULL) {
             /* The statement is not empty block or just semicolon. */
 
-            node = njs_parser_node_alloc(vm);
+            node = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
             if (nxt_slow_path(node == NULL)) {
                 return NJS_TOKEN_ERROR;
             }
 
-            node->token = NJS_TOKEN_STATEMENT;
             node->left = last;
             node->right = parser->node;
             parser->node = node;
@@ -418,12 +416,11 @@ njs_parser_block_statement(njs_vm_t *vm,
     if (parser->node != NULL) {
         /* The statement is not empty block or just semicolon. */
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_BLOCK);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_BLOCK;
         node->left = parser->node;
         node->right = NULL;
         parser->node = node;
@@ -495,12 +492,11 @@ njs_parser_function_declaration(njs_vm_t
     njs_function_t     *function;
     njs_parser_node_t  *node;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_FUNCTION);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_FUNCTION;
     node->token_line = parser->lexer->token_line;
 
     token = njs_parser_token(parser);
@@ -560,14 +556,12 @@ njs_parser_function_expression(njs_vm_t 
     njs_parser_node_t      *node;
     njs_function_lambda_t  *lambda;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_FUNCTION_EXPRESSION);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_FUNCTION_EXPRESSION;
     node->token_line = parser->lexer->token_line;
-    node->scope = parser->scope;
     parser->node = node;
 
     token = njs_parser_token(parser);
@@ -745,26 +739,22 @@ njs_parser_function_lambda(njs_vm_t *vm,
          * There is no function body or the last function body
          * body statement is not "return" statement.
          */
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_STATEMENT;
         node->left = parser->node;
-        parser->node = node;
-
-        node->right = njs_parser_node_alloc(vm);
+        node->right = njs_parser_node_new(vm, parser, NJS_TOKEN_RETURN);
         if (nxt_slow_path(node->right == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->right->token = NJS_TOKEN_RETURN;
+        parser->node = node;
     }
 
     parent->right = parser->node;
 
-    parser->node->scope = parser->scope;
     parser->scope->node = parser->node;
 
     parser->node = parent;
@@ -793,12 +783,11 @@ njs_parser_return_statement(njs_vm_t *vm
         }
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_RETURN);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_RETURN;
     parser->node = node;
 
     token = njs_lexer_token(parser->lexer);
@@ -866,13 +855,11 @@ njs_parser_var_statement(njs_vm_t *vm, n
             return NJS_TOKEN_ERROR;
         }
 
-        name = njs_parser_node_alloc(vm);
+        name = njs_parser_node_new(vm, parser, NJS_TOKEN_NAME);
         if (nxt_slow_path(name == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        name->token = NJS_TOKEN_NAME;
-
         ret = njs_parser_variable_reference(vm, parser, name, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
@@ -900,22 +887,20 @@ njs_parser_var_statement(njs_vm_t *vm, n
             expr = parser->node;
         }
 
-        assign = njs_parser_node_alloc(vm);
+        assign = njs_parser_node_new(vm, parser, NJS_TOKEN_VAR);
         if (nxt_slow_path(assign == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        assign->token = NJS_TOKEN_VAR;
         assign->u.operation = njs_vmcode_move;
         assign->left = name;
         assign->right = expr;
 
-        stmt = njs_parser_node_alloc(vm);
+        stmt = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
         if (nxt_slow_path(stmt == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        stmt->token = NJS_TOKEN_STATEMENT;
         stmt->left = left;
         stmt->right = assign;
         parser->node = stmt;
@@ -960,23 +945,21 @@ njs_parser_if_statement(njs_vm_t *vm, nj
             return token;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_BRANCHING);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_BRANCHING;
         node->left = stmt;
         node->right = parser->node;
         parser->node = node;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_IF);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_IF;
     node->left = cond;
     node->right = parser->node;
     parser->node = node;
@@ -1000,13 +983,11 @@ njs_parser_switch_statement(njs_vm_t *vm
         return token;
     }
 
-    swtch = njs_parser_node_alloc(vm);
+    swtch = njs_parser_node_new(vm, parser, NJS_TOKEN_SWITCH);
     if (nxt_slow_path(swtch == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    swtch->token = NJS_TOKEN_SWITCH;
-    swtch->scope = parser->scope;
     swtch->left = parser->node;
     last = &swtch->right;
 
@@ -1020,7 +1001,7 @@ njs_parser_switch_statement(njs_vm_t *vm
         if (token == NJS_TOKEN_CASE || token == NJS_TOKEN_DEFAULT) {
 
             do {
-                node = njs_parser_node_alloc(vm);
+                node = njs_parser_node_new(vm, parser, 0);
                 if (nxt_slow_path(node == NULL)) {
                     return NJS_TOKEN_ERROR;
                 }
@@ -1038,7 +1019,7 @@ njs_parser_switch_statement(njs_vm_t *vm
 
                     node->left = parser->node;
 
-                    branch = njs_parser_node_alloc(vm);
+                    branch = njs_parser_node_new(vm, parser, 0);
                     if (nxt_slow_path(branch == NULL)) {
                         return NJS_TOKEN_ERROR;
                     }
@@ -1117,12 +1098,11 @@ njs_parser_while_statement(njs_vm_t *vm,
         return token;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_WHILE);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_WHILE;
     node->left = parser->node;
     node->right = cond;
     parser->node = node;
@@ -1158,12 +1138,11 @@ njs_parser_do_while_statement(njs_vm_t *
         return token;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_DO);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_DO;
     node->left = stmt;
     node->right = parser->node;
     parser->node = node;
@@ -1265,22 +1244,21 @@ njs_parser_for_statement(njs_vm_t *vm, n
         return token;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_FOR);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    cond = njs_parser_node_alloc(vm);
+    cond = njs_parser_node_new(vm, parser, 0);
     if (nxt_slow_path(cond == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    body = njs_parser_node_alloc(vm);
+    body = njs_parser_node_new(vm, parser, 0);
     if (nxt_slow_path(body == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_FOR;
     node->left = init;
     node->right = cond;
 
@@ -1328,13 +1306,11 @@ njs_parser_for_var_statement(njs_vm_t *v
             return NJS_TOKEN_ERROR;
         }
 
-        name = njs_parser_node_alloc(vm);
+        name = njs_parser_node_new(vm, parser, NJS_TOKEN_NAME);
         if (nxt_slow_path(name == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        name->token = NJS_TOKEN_NAME;
-
         ret = njs_parser_variable_reference(vm, parser, name, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
@@ -1366,22 +1342,20 @@ njs_parser_for_var_statement(njs_vm_t *v
             expr = parser->node;
         }
 
-        assign = njs_parser_node_alloc(vm);
+        assign = njs_parser_node_new(vm, parser, NJS_TOKEN_VAR);
         if (nxt_slow_path(assign == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        assign->token = NJS_TOKEN_VAR;
         assign->u.operation = njs_vmcode_move;
         assign->left = name;
         assign->right = expr;
 
-        stmt = njs_parser_node_alloc(vm);
+        stmt = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
         if (nxt_slow_path(stmt == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        stmt->token = NJS_TOKEN_STATEMENT;
         stmt->left = left;
         stmt->right = assign;
         parser->node = stmt;
@@ -1411,12 +1385,11 @@ njs_parser_for_var_in_statement(njs_vm_t
         return token;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_IN);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_IN;
     node->left = name;
     node->right = parser->node;
 
@@ -1430,12 +1403,11 @@ njs_parser_for_var_in_statement(njs_vm_t
         return token;
     }
 
-    foreach = njs_parser_node_alloc(vm);
+    foreach = njs_parser_node_new(vm, parser, NJS_TOKEN_FOR_IN);
     if (nxt_slow_path(foreach == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    foreach->token = NJS_TOKEN_FOR_IN;
     foreach->left = node;
     foreach->right = parser->node;
 
@@ -1460,12 +1432,11 @@ njs_parser_for_in_statement(njs_vm_t *vm
         return NJS_TOKEN_ILLEGAL;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_FOR_IN);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_FOR_IN;
     node->left = parser->node;
 
     token = njs_parser_match(vm, parser, token, NJS_TOKEN_CLOSE_PARENTHESIS);
@@ -1491,12 +1462,11 @@ njs_parser_continue_statement(njs_vm_t *
     njs_token_t        token;
     njs_parser_node_t  *node;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_CONTINUE);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_CONTINUE;
     node->token_line = parser->lexer->token_line;
     parser->node = node;
 
@@ -1525,12 +1495,11 @@ njs_parser_break_statement(njs_vm_t *vm,
     njs_token_t        token;
     njs_parser_node_t  *node;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_BREAK);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_BREAK;
     node->token_line = parser->lexer->token_line;
     parser->node = node;
 
@@ -1566,13 +1535,11 @@ njs_parser_try_statement(njs_vm_t *vm, n
         return token;
     }
 
-    try = njs_parser_node_alloc(vm);
+    try = njs_parser_node_new(vm, parser, NJS_TOKEN_TRY);
     if (nxt_slow_path(try == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    try->token = NJS_TOKEN_TRY;
-    try->scope = parser->scope;
     try->left = parser->node;
 
     if (token == NJS_TOKEN_CATCH) {
@@ -1590,12 +1557,11 @@ njs_parser_try_statement(njs_vm_t *vm, n
             return NJS_TOKEN_ILLEGAL;
         }
 
-        catch = njs_parser_node_alloc(vm);
+        catch = njs_parser_node_new(vm, parser, NJS_TOKEN_CATCH);
         if (nxt_slow_path(catch == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        catch->token = NJS_TOKEN_CATCH;
         try->right = catch;
 
         /*
@@ -1612,13 +1578,11 @@ njs_parser_try_statement(njs_vm_t *vm, n
             return NJS_TOKEN_ERROR;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_NAME);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_NAME;
-
         ret = njs_parser_variable_reference(vm, parser, node, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
@@ -1652,12 +1616,11 @@ njs_parser_try_statement(njs_vm_t *vm, n
             return token;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_FINALLY);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_FINALLY;
         node->right = parser->node;
 
         if (try->right != NULL) {
@@ -1714,13 +1677,11 @@ njs_parser_throw_statement(njs_vm_t *vm,
     njs_token_t        token;
     njs_parser_node_t  *node;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_THROW);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_THROW;
-
     token = njs_lexer_token(parser->lexer);
     if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
         return token;
@@ -1831,14 +1792,11 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
         return njs_parser_function_expression(vm, parser);
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, token);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = token;
-    node->scope = parser->scope;
-
     switch (token) {
 
     case NJS_TOKEN_NAME:
@@ -1862,7 +1820,6 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
 
     case NJS_TOKEN_OPEN_BRACE:
         node->token = NJS_TOKEN_OBJECT;
-        node->scope = parser->scope;
 
         nxt_thread_log_debug("JS: OBJECT");
 
@@ -1880,7 +1837,6 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
 
     case NJS_TOKEN_OPEN_BRACKET:
         node->token = NJS_TOKEN_ARRAY;
-        node->scope = parser->scope;
 
         nxt_thread_log_debug("JS: ARRAY");
 
@@ -1905,7 +1861,6 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
         nxt_thread_log_debug("REGEX: '%V'", &parser->lexer->text);
 
         node->token = NJS_TOKEN_REGEXP;
-        node->scope = parser->scope;
 
         break;
 
@@ -2008,8 +1963,6 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
             return NJS_TOKEN_ILLEGAL;
         }
 
-        node->token = NJS_TOKEN_ARGUMENTS;
-
         break;
 
     case NJS_TOKEN_OBJECT_CONSTRUCTOR:
@@ -2219,20 +2172,18 @@ njs_parser_object(njs_vm_t *vm, njs_pars
             break;
         }
 
-        object = njs_parser_node_alloc(vm);
+        object = njs_parser_node_new(vm, parser, NJS_TOKEN_OBJECT_VALUE);
         if (nxt_slow_path(object == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        object->token = NJS_TOKEN_OBJECT_VALUE;
         object->u.object = obj;
 
-        propref = njs_parser_node_alloc(vm);
+        propref = njs_parser_node_new(vm, parser, NJS_TOKEN_PROPERTY);
         if (nxt_slow_path(propref == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        propref->token = NJS_TOKEN_PROPERTY;
         propref->left = object;
         propref->right = parser->node;
 
@@ -2250,22 +2201,20 @@ njs_parser_object(njs_vm_t *vm, njs_pars
             return token;
         }
 
-        assign = njs_parser_node_alloc(vm);
+        assign = njs_parser_node_new(vm, parser, NJS_TOKEN_ASSIGNMENT);
         if (nxt_slow_path(assign == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        assign->token = NJS_TOKEN_ASSIGNMENT;
         assign->u.operation = njs_vmcode_move;
         assign->left = propref;
         assign->right = parser->node;
 
-        stmt = njs_parser_node_alloc(vm);
+        stmt = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
         if (nxt_slow_path(stmt == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        stmt->token = NJS_TOKEN_STATEMENT;
         stmt->left = left;
         stmt->right = assign;
 
@@ -2310,31 +2259,28 @@ njs_parser_array(njs_vm_t *vm, njs_parse
             continue;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_NUMBER);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_NUMBER;
         node->u.value.data.u.number = index;
         node->u.value.type = NJS_NUMBER;
         node->u.value.data.truth = (index != 0);
         index++;
 
-        object = njs_parser_node_alloc(vm);
+        object = njs_parser_node_new(vm, parser, NJS_TOKEN_OBJECT_VALUE);
         if (nxt_slow_path(object == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        object->token = NJS_TOKEN_OBJECT_VALUE;
         object->u.object = obj;
 
-        propref = njs_parser_node_alloc(vm);
+        propref = njs_parser_node_new(vm, parser, NJS_TOKEN_PROPERTY);
         if (nxt_slow_path(propref == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        propref->token = NJS_TOKEN_PROPERTY;
         propref->left = object;
         propref->right = node;
 
@@ -2343,22 +2289,20 @@ njs_parser_array(njs_vm_t *vm, njs_parse
             return token;
         }
 
-        assign = njs_parser_node_alloc(vm);
+        assign = njs_parser_node_new(vm, parser, NJS_TOKEN_ASSIGNMENT);
         if (nxt_slow_path(assign == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        assign->token = NJS_TOKEN_ASSIGNMENT;
         assign->u.operation = njs_vmcode_move;
         assign->left = propref;
         assign->right = parser->node;
 
-        stmt = njs_parser_node_alloc(vm);
+        stmt = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
         if (nxt_slow_path(stmt == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        stmt->token = NJS_TOKEN_STATEMENT;
         stmt->left = left;
         stmt->right = assign;
 
diff -r ac9b1c01a9b0 -r 9f2779de4cdd njs/njs_parser.h
--- a/njs/njs_parser.h	Wed Jan 30 18:50:33 2019 +0300
+++ b/njs/njs_parser.h	Tue Jan 29 16:30:13 2019 +0800
@@ -283,10 +283,6 @@ struct njs_parser_node_s {
 };
 
 
-#define njs_parser_node_alloc(vm)                                             \
-    nxt_mp_zalloc((vm)->mem_pool, sizeof(njs_parser_node_t))
-
-
 struct njs_parser_s {
     njs_lexer_t                     *lexer;
     njs_parser_node_t               *node;
@@ -337,6 +333,22 @@ void njs_parser_ref_error(njs_vm_t *vm, 
     ...);
 
 
+nxt_inline njs_parser_node_t *
+njs_parser_node_new(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token)
+{
+    njs_parser_node_t  *node;
+
+    node = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_parser_node_t));
+
+    if (nxt_fast_path(node != NULL)) {
+        node->token = token;
+        node->scope = parser->scope;
+    }
+
+    return node;
+}
+
+
 extern const nxt_lvlhsh_proto_t  njs_keyword_hash_proto;
 
 
diff -r ac9b1c01a9b0 -r 9f2779de4cdd njs/njs_parser_expression.c
--- a/njs/njs_parser_expression.c	Wed Jan 30 18:50:33 2019 +0300
+++ b/njs/njs_parser_expression.c	Tue Jan 29 16:30:13 2019 +0800
@@ -235,14 +235,12 @@ njs_parser_var_expression(njs_vm_t *vm, 
             return NJS_TOKEN_ILLEGAL;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, token);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = token;
         node->u.operation = operation;
-        node->scope = parser->scope;
         node->left = parser->node;
 
         token = njs_parser_token(parser);
@@ -370,14 +368,12 @@ njs_parser_assignment_expression(njs_vm_
             return NJS_TOKEN_ILLEGAL;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, token);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = token;
         node->u.operation = operation;
-        node->scope = parser->scope;
         node->left = parser->node;
 
         token = njs_parser_token(parser);
@@ -419,22 +415,19 @@ njs_parser_conditional_expression(njs_vm
             return token;
         }
 
-        cond = njs_parser_node_alloc(vm);
+        cond = njs_parser_node_new(vm, parser, NJS_TOKEN_CONDITIONAL);
         if (nxt_slow_path(cond == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        cond->token = NJS_TOKEN_CONDITIONAL;
-        cond->scope = parser->scope;
         cond->left = parser->node;
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_BRANCHING);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
         cond->right = node;
-        node->token = NJS_TOKEN_BRANCHING;
 
         token = njs_parser_assignment_expression(vm, parser, token);
         if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
@@ -497,14 +490,12 @@ njs_parser_binary_expression(njs_vm_t *v
 
     found:
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, token);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = token;
         node->u.operation = op->operation;
-        node->scope = parser->scope;
         node->left = parser->node;
         node->left->dest = node;
 
@@ -538,14 +529,12 @@ njs_parser_exponential_expression(njs_vm
 
     if (token == NJS_TOKEN_EXPONENTIATION) {
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, token);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = token;
         node->u.operation = njs_vmcode_exponentiation;
-        node->scope = parser->scope;
         node->left = parser->node;
         node->left->dest = node;
 
@@ -673,14 +662,12 @@ njs_parser_unary_expression(njs_vm_t *vm
         node->u.reference.type = NJS_TYPEOF;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, token);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = token;
     node->u.operation = operation;
-    node->scope = parser->scope;
     node->left = parser->node;
     node->left->dest = node;
     parser->node = node;
@@ -727,14 +714,12 @@ njs_parser_inc_dec_expression(njs_vm_t *
         return NJS_TOKEN_ILLEGAL;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, token);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = token;
     node->u.operation = operation;
-    node->scope = parser->scope;
     node->left = parser->node;
     parser->node = node;
 
@@ -783,14 +768,12 @@ njs_parser_post_inc_dec_expression(njs_v
         return NJS_TOKEN_ILLEGAL;
     }
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, token);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = token;
     node->u.operation = operation;
-    node->scope = parser->scope;
     node->left = parser->node;
     parser->node = node;
 
@@ -837,13 +820,11 @@ njs_parser_call_expression(njs_vm_t *vm,
             break;
 
         case NJS_TOKEN_PROPERTY:
-            func = njs_parser_node_alloc(vm);
+            func = njs_parser_node_new(vm, parser, NJS_TOKEN_METHOD_CALL);
             if (nxt_slow_path(func == NULL)) {
                 return NJS_TOKEN_ERROR;
             }
 
-            func->token = NJS_TOKEN_METHOD_CALL;
-            func->scope = parser->scope;
             func->left = node;
 
             break;
@@ -863,13 +844,11 @@ njs_parser_call_expression(njs_vm_t *vm,
              * NJS_TOKEN_REGEXP_CONSTRUCTOR,
              * NJS_TOKEN_EVAL.
              */
-            func = njs_parser_node_alloc(vm);
+            func = njs_parser_node_new(vm, parser, NJS_TOKEN_FUNCTION_CALL);
             if (nxt_slow_path(func == NULL)) {
                 return NJS_TOKEN_ERROR;
             }
 
-            func->token = NJS_TOKEN_FUNCTION_CALL;
-            func->scope = parser->scope;
             func->left = node;
 
             break;
@@ -930,13 +909,11 @@ njs_parser_new_expression(njs_vm_t *vm, 
         break;
 
     case NJS_TOKEN_PROPERTY:
-        func = njs_parser_node_alloc(vm);
+        func = njs_parser_node_new(vm, parser, NJS_TOKEN_METHOD_CALL);
         if (nxt_slow_path(func == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        func->token = NJS_TOKEN_METHOD_CALL;
-        func->scope = parser->scope;
         func->left = node;
 
         break;
@@ -956,13 +933,11 @@ njs_parser_new_expression(njs_vm_t *vm, 
          * NJS_TOKEN_REGEXP_CONSTRUCTOR,
          * NJS_TOKEN_EVAL.
          */
-        func = njs_parser_node_alloc(vm);
+        func = njs_parser_node_new(vm, parser, NJS_TOKEN_FUNCTION_CALL);
         if (nxt_slow_path(func == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        func->token = NJS_TOKEN_FUNCTION_CALL;
-        func->scope = parser->scope;
         func->left = node;
 
         break;
@@ -999,14 +974,12 @@ njs_parser_property_expression(njs_vm_t 
             return token;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_PROPERTY);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_PROPERTY;
         node->u.operation = njs_vmcode_property_get;
-        node->scope = parser->scope;
         node->left = parser->node;
 
         if (token == NJS_TOKEN_DOT) {
@@ -1047,13 +1020,11 @@ njs_parser_property_name(njs_vm_t *vm, n
     nxt_int_t          ret;
     njs_parser_node_t  *node;
 
-    node = njs_parser_node_alloc(vm);
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_STRING);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    node->token = NJS_TOKEN_STRING;
-
     ret = njs_parser_string_create(vm, &node->u.value);
     if (nxt_slow_path(ret != NXT_OK)) {
         return NJS_TOKEN_ERROR;
@@ -1107,12 +1078,11 @@ njs_parser_arguments(njs_vm_t *vm, njs_p
             return token;
         }
 
-        node = njs_parser_node_alloc(vm);
+        node = njs_parser_node_new(vm, parser, NJS_TOKEN_ARGUMENT);
         if (nxt_slow_path(node == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        node->token = NJS_TOKEN_ARGUMENT;
         node->index = index;
         index += sizeof(njs_value_t);
 


More information about the nginx-devel mailing list