[njs] Reporting filename in generator errors.

Dmitry Volyntsev xeioex at nginx.com
Tue Feb 5 16:07:31 UTC 2019


details:   https://hg.nginx.org/njs/rev/334b9d9e4357
branches:  
changeset: 760:334b9d9e4357
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Feb 05 00:53:18 2019 +0800
description:
Reporting filename in generator errors.

diffstat:

 njs/njs_generator.c |  42 +++++++++++++++++++++++++++++++-----------
 njs/njs_parser.c    |  13 ++++++++++---
 njs/njs_parser.h    |   2 ++
 3 files changed, 43 insertions(+), 14 deletions(-)

diffs (137 lines):

diff -r 1568a0d46b0d -r 334b9d9e4357 njs/njs_generator.c
--- a/njs/njs_generator.c	Mon Feb 04 23:00:25 2019 +0800
+++ b/njs/njs_generator.c	Tue Feb 05 00:53:18 2019 +0800
@@ -163,8 +163,8 @@ static nxt_int_t njs_generate_function_d
     njs_function_lambda_t *lambda, uint32_t line);
 
 
-static void njs_generate_syntax_error(njs_vm_t *vm, uint32_t token_line,
-    const char* fmt, ...);
+static void njs_generate_syntax_error(njs_vm_t *vm, njs_parser_node_t *node,
+    const char *fmt, ...);
 
 
 #define njs_generate_code(generator, type, code)                              \
@@ -1407,8 +1407,7 @@ njs_generate_continue_statement(njs_vm_t
 
 syntax_error:
 
-    njs_generate_syntax_error(vm, node->token_line,
-                              "Illegal continue statement");
+    njs_generate_syntax_error(vm, node, "Illegal continue statement");
 
     return NXT_ERROR;
 }
@@ -1456,7 +1455,7 @@ njs_generate_break_statement(njs_vm_t *v
 
 syntax_error:
 
-    njs_generate_syntax_error(vm, node->token_line, "Illegal break statement");
+    njs_generate_syntax_error(vm, node, "Illegal break statement");
 
     return NXT_ERROR;
 }
@@ -3162,15 +3161,36 @@ njs_generate_function_debug(njs_vm_t *vm
 
 
 static void
-njs_generate_syntax_error(njs_vm_t *vm, uint32_t token_line,
-    const char* fmt, ...)
+njs_generate_syntax_error(njs_vm_t *vm, njs_parser_node_t *node,
+    const char *fmt, ...)
 {
-    va_list  args;
-    u_char   buf[256], *end;
+    size_t              width;
+    u_char              msg[NXT_MAX_ERROR_STR];
+    u_char              *p, *end;
+    va_list             args;
+    njs_parser_scope_t  *scope;
+
+    p = msg;
+    end = msg + NXT_MAX_ERROR_STR;
 
     va_start(args, fmt);
-    end = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
+    p = nxt_vsprintf(p, end, fmt, args);
     va_end(args);
 
-    njs_syntax_error(vm, "%*s in %uD", end - buf, buf, token_line);
+    scope = node->scope;
+
+    width = nxt_length(" in ") + scope->file.length + NXT_INT_T_LEN;
+
+    if (p > end - width) {
+        p = end - width;
+    }
+
+    if (scope->file.start != NULL) {
+        p = nxt_sprintf(p, end, " in %V:%uD", &scope->file, node->token_line);
+
+    } else {
+        p = nxt_sprintf(p, end, " in %uD", node->token_line);
+    }
+
+    njs_error_new(vm, NJS_OBJECT_SYNTAX_ERROR, msg, p - msg);
 }
diff -r 1568a0d46b0d -r 334b9d9e4357 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Feb 04 23:00:25 2019 +0800
+++ b/njs/njs_parser.c	Tue Feb 05 00:53:18 2019 +0800
@@ -162,6 +162,7 @@ njs_parser(njs_vm_t *vm, njs_parser_t *p
 static njs_ret_t
 njs_parser_scope_begin(njs_vm_t *vm, njs_parser_t *parser, njs_scope_t type)
 {
+    nxt_int_t           ret;
     nxt_uint_t          nesting;
     nxt_array_t         *values;
     njs_parser_scope_t  *scope, *parent;
@@ -188,13 +189,12 @@ njs_parser_scope_begin(njs_vm_t *vm, njs
         }
     }
 
-    scope = nxt_mp_alloc(vm->mem_pool, sizeof(njs_parser_scope_t));
+    scope = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_parser_scope_t));
     if (nxt_slow_path(scope == NULL)) {
         return NXT_ERROR;
     }
 
     scope->type = type;
-    scope->top = NULL;
 
     if (type == NJS_SCOPE_FUNCTION) {
         scope->next_index[0] = type;
@@ -230,6 +230,13 @@ njs_parser_scope_begin(njs_vm_t *vm, njs
     scope->values[0] = values;
     scope->values[1] = NULL;
 
+    if (parser->lexer->file.start != NULL) {
+        ret = njs_name_copy(vm, &scope->file, &parser->lexer->file);
+        if (nxt_slow_path(ret != NXT_OK)) {
+            return NXT_ERROR;
+        }
+    }
+
     parent = parser->scope;
     scope->parent = parent;
     parser->scope = scope;
@@ -2639,7 +2646,7 @@ njs_parser_trace_handler(nxt_trace_t *tr
 void
 njs_parser_error(njs_vm_t *vm, njs_parser_t *parser, njs_value_type_t type,
     const char *fmt, ...)
- {
+{
     size_t       width;
     u_char       *p, *end;
     u_char       msg[NXT_MAX_ERROR_STR];
diff -r 1568a0d46b0d -r 334b9d9e4357 njs/njs_parser.h
--- a/njs/njs_parser.h	Mon Feb 04 23:00:25 2019 +0800
+++ b/njs/njs_parser.h	Tue Feb 05 00:53:18 2019 +0800
@@ -249,6 +249,8 @@ struct njs_parser_scope_s {
     nxt_array_t                     *values[2];  /* Array of njs_value_t. */
     njs_index_t                     next_index[2];
 
+    nxt_str_t                       file;
+
     njs_scope_t                     type:8;
     uint8_t                         nesting;     /* 4 bits */
     uint8_t                         argument_closures;


More information about the nginx-devel mailing list