[njs] Getting rid of lexer property and property_token.

Dmitry Volyntsev xeioex at nginx.com
Thu Jun 20 22:36:16 UTC 2019


details:   https://hg.nginx.org/njs/rev/d6ffea147fd3
branches:  
changeset: 1012:d6ffea147fd3
user:      hongzhidao <hongzhidao at gmail.com>
date:      Thu Jun 20 07:36:57 2019 -0400
description:
Getting rid of lexer property and property_token.

diffstat:

 njs/njs_lexer.c             |   5 --
 njs/njs_lexer.h             |   4 +-
 njs/njs_lexer_keyword.c     |   1 +
 njs/njs_parser.c            |  31 ----------------
 njs/njs_parser.h            |  21 ++++++++++-
 njs/njs_parser_expression.c |  31 +++++++++-------
 njs/njs_parser_terminal.c   |  83 ++++++++++++++++++++++++++++----------------
 7 files changed, 92 insertions(+), 84 deletions(-)

diffs (316 lines):

diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_lexer.c
--- a/njs/njs_lexer.c	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_lexer.c	Thu Jun 20 07:36:57 2019 -0400
@@ -420,11 +420,6 @@ njs_lexer_token_name_resolve(njs_lexer_t
 {
     if (lt->token == NJS_TOKEN_NAME) {
         njs_lexer_keyword(lexer, lt);
-
-        if (lexer->property) {
-            lexer->property_token = lt->token;
-            lt->token = NJS_TOKEN_NAME;
-        }
     }
 
     return lt->token;
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_lexer.h
--- a/njs/njs_lexer.h	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_lexer.h	Thu Jun 20 07:36:57 2019 -0400
@@ -231,13 +231,11 @@ typedef struct {
 typedef struct {
     njs_lexer_token_t               *lexer_token;
     nxt_queue_t                     preread; /*  of njs_lexer_token_t */
+    uint8_t                         keyword;
 
     u_char                          *prev_start;
     njs_token_t                     prev_token:16;
 
-    uint8_t                         property;      /* 1 bit */
-    njs_token_t                     property_token:16;
-
     uint32_t                        line;
     nxt_str_t                       file;
 
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_lexer_keyword.c	Thu Jun 20 07:36:57 2019 -0400
@@ -191,5 +191,6 @@ njs_lexer_keyword(njs_lexer_t *lexer, nj
         keyword = lhq.value;
         lt->token = keyword->token;
         lt->number = keyword->number;
+        lexer->keyword = 1;
     }
 }
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_parser.c	Thu Jun 20 07:36:57 2019 -0400
@@ -2064,37 +2064,6 @@ njs_parser_grouping_expression(njs_vm_t 
 }
 
 
-njs_token_t
-njs_parser_property_token(njs_vm_t *vm, njs_parser_t *parser)
-{
-    nxt_int_t          ret;
-    njs_token_t        token;
-    njs_parser_node_t  *node;
-
-    parser->lexer->property = 1;
-
-    token = njs_parser_token(vm, parser);
-
-    parser->lexer->property = 0;
-
-    if (token == NJS_TOKEN_NAME) {
-        node = njs_parser_node_new(vm, parser, NJS_TOKEN_STRING);
-        if (nxt_slow_path(node == NULL)) {
-            return NJS_TOKEN_ERROR;
-        }
-
-        ret = njs_parser_string_create(vm, &node->u.value);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return NJS_TOKEN_ERROR;
-        }
-
-        parser->node = node;
-    }
-
-    return token;
-}
-
-
 nxt_int_t
 njs_parser_match_arrow_expression(njs_vm_t *vm, njs_parser_t *parser,
     njs_token_t token)
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_parser.h
--- a/njs/njs_parser.h	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_parser.h	Thu Jun 20 07:36:57 2019 -0400
@@ -94,7 +94,6 @@ njs_token_t njs_parser_template_literal(
     njs_parser_node_t *parent);
 njs_parser_node_t *njs_parser_argument(njs_vm_t *vm, njs_parser_t *parser,
     njs_parser_node_t *expr, njs_index_t index);
-njs_token_t njs_parser_property_token(njs_vm_t *vm, njs_parser_t *parser);
 nxt_int_t njs_parser_string_create(njs_vm_t *vm, njs_value_t *value);
 njs_token_t njs_parser_lambda_statements(njs_vm_t *vm, njs_parser_t *parser,
     njs_token_t token);
@@ -187,6 +186,26 @@ njs_parser_node_new(njs_vm_t *vm, njs_pa
 }
 
 
+nxt_inline njs_parser_node_t *
+njs_parser_node_string(njs_vm_t *vm, njs_parser_t *parser)
+{
+    nxt_int_t          ret;
+    njs_parser_node_t  *node;
+
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_STRING);
+    if (nxt_slow_path(node == NULL)) {
+        return NULL;
+    }
+
+    ret = njs_parser_string_create(vm, &node->u.value);
+    if (nxt_slow_path(ret != NXT_OK)) {
+        return NULL;
+    }
+
+    return node;
+}
+
+
 nxt_inline njs_token_t
 njs_parser_match(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token,
     njs_token_t match)
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_parser_expression.c
--- a/njs/njs_parser_expression.c	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_parser_expression.c	Thu Jun 20 07:36:57 2019 -0400
@@ -884,7 +884,7 @@ static njs_token_t
 njs_parser_property_expression(njs_vm_t *vm, njs_parser_t *parser,
     njs_token_t token)
 {
-    njs_parser_node_t  *node;
+    njs_parser_node_t  *node, *prop_node;
 
     for ( ;; ) {
         if (token != NJS_TOKEN_DOT
@@ -902,18 +902,22 @@ njs_parser_property_expression(njs_vm_t 
         node->left = parser->node;
 
         if (token == NJS_TOKEN_DOT) {
+            token = njs_parser_token(vm, parser);
 
-            token = njs_parser_property_token(vm, parser);
+            if (token != NJS_TOKEN_NAME && !parser->lexer->keyword) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            prop_node = njs_parser_node_string(vm, parser);
+            if (nxt_slow_path(prop_node == NULL)) {
+                return NJS_TOKEN_ERROR;
+            }
+
+            token = njs_parser_token(vm, parser);
             if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
                 return token;
             }
 
-            if (token != NJS_TOKEN_NAME) {
-                return NJS_TOKEN_ILLEGAL;
-            }
-
-            token = njs_parser_token(vm, parser);
-
         } else {
             token = njs_parser_token(vm, parser);
             if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
@@ -921,13 +925,14 @@ njs_parser_property_expression(njs_vm_t 
             }
 
             token = njs_parser_property_brackets(vm, parser, token);
+            if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+                return token;
+            }
+
+            prop_node = parser->node;
         }
 
-        if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
-            return token;
-        }
-
-        node->right = parser->node;
+        node->right = prop_node;
         parser->node = node;
     }
 }
diff -r 2fb43ddbce84 -r d6ffea147fd3 njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c	Mon Jun 10 22:23:56 2019 -0400
+++ b/njs/njs_parser_terminal.c	Thu Jun 20 07:36:57 2019 -0400
@@ -488,7 +488,7 @@ njs_parser_object(njs_vm_t *vm, njs_pars
     uint32_t           hash, token_line;
     nxt_int_t          ret;
     nxt_str_t          name;
-    njs_token_t        token;
+    njs_token_t        token, prop_token;
     njs_lexer_t        *lexer;
     njs_parser_node_t  *object, *property, *expression;
 
@@ -506,54 +506,69 @@ njs_parser_object(njs_vm_t *vm, njs_pars
     object->u.object = obj;
 
     for ( ;; ) {
-        token = njs_parser_property_token(vm, parser);
-
-        if (token == NJS_TOKEN_CLOSE_BRACE) {
-            break;
+        prop_token = njs_parser_token(vm, parser);
+        if (nxt_slow_path(prop_token <= NJS_TOKEN_ILLEGAL)) {
+            return prop_token;
         }
 
-        name.start = NULL;
+        nxt_memzero(&name, sizeof(nxt_str_t));
+
+        switch (prop_token) {
+
+        case NJS_TOKEN_CLOSE_BRACE:
+            goto done;
 
-        switch (token) {
+        case NJS_TOKEN_NUMBER:
+        case NJS_TOKEN_STRING:
+        case NJS_TOKEN_ESCAPE_STRING:
+            token = njs_parser_terminal(vm, parser, prop_token);
+            if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+                return token;
+            }
 
-        case NJS_TOKEN_NAME:
+            property = parser->node;
+            break;
+
+        default:
+            if (prop_token != NJS_TOKEN_NAME && !lexer->keyword) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            property = njs_parser_node_string(vm, parser);
+            if (nxt_slow_path(property == NULL)) {
+                return NJS_TOKEN_ERROR;
+            }
+
             name = *njs_parser_text(parser);
-
             hash = njs_parser_key_hash(parser);
             token_line = njs_parser_token_line(parser);
 
             token = njs_parser_token(vm, parser);
             break;
-
-        case NJS_TOKEN_NUMBER:
-        case NJS_TOKEN_STRING:
-        case NJS_TOKEN_ESCAPE_STRING:
-            token = njs_parser_terminal(vm, parser, token);
-            break;
-
-        default:
-            return NJS_TOKEN_ILLEGAL;
         }
 
-        if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
-            return token;
-        }
+        switch (token) {
 
-        property = parser->node;
+        case NJS_TOKEN_COMMA:
+        case NJS_TOKEN_CLOSE_BRACE:
 
-        if (name.start != NULL
-            && (token == NJS_TOKEN_COMMA || token == NJS_TOKEN_CLOSE_BRACE)
-            && lexer->property_token != NJS_TOKEN_THIS
-            && lexer->property_token != NJS_TOKEN_GLOBAL_THIS)
-        {
-            expression = njs_parser_reference(vm, parser, lexer->property_token,
-                                              &name, hash, token_line);
+            if (name.length == 0
+                || prop_token == NJS_TOKEN_THIS
+                || prop_token == NJS_TOKEN_GLOBAL_THIS)
+            {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            expression = njs_parser_reference(vm, parser, prop_token, &name,
+                                              hash, token_line);
             if (nxt_slow_path(expression == NULL)) {
                 return NJS_TOKEN_ERROR;
             }
 
-        } else {
-            token = njs_parser_match(vm, parser, token, NJS_TOKEN_COLON);
+            break;
+
+        case NJS_TOKEN_COLON:
+            token = njs_parser_token(vm, parser);
             if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
                 return token;
             }
@@ -564,6 +579,10 @@ njs_parser_object(njs_vm_t *vm, njs_pars
             }
 
             expression = parser->node;
+            break;
+
+        default:
+            return NJS_TOKEN_ILLEGAL;
         }
 
         ret = njs_parser_object_property(vm, parser, obj, property, expression);
@@ -580,6 +599,8 @@ njs_parser_object(njs_vm_t *vm, njs_pars
         }
     }
 
+done:
+
     parser->node = obj;
 
     return njs_parser_token(vm, parser);


More information about the nginx-devel mailing list