Welcome! Log In Create A New Profile

Advanced

[njs] Reporting filename in parser errors.

Dmitry Volyntsev
February 04, 2019 03:34AM
details: https://hg.nginx.org/njs/rev/06e1b1790f9a
branches:
changeset: 755:06e1b1790f9a
user: hongzhidao <hongzhidao@gmail.com>
date: Mon Feb 04 05:33:19 2019 +0800
description:
Reporting filename in parser errors.

diffstat:

njs/njs.c | 1 +
njs/njs.h | 1 +
njs/njs_parser.c | 69 ++++++++++++++++++++++++++++++++++---------------------
njs/njs_parser.h | 14 ++++++++---
njs/njs_shell.c | 5 ++++
nxt/nxt_types.h | 2 +
6 files changed, 61 insertions(+), 31 deletions(-)

diffs (173 lines):

diff -r 83581029a022 -r 06e1b1790f9a njs/njs.c
--- a/njs/njs.c Mon Feb 04 05:26:42 2019 +0800
+++ b/njs/njs.c Mon Feb 04 05:33:19 2019 +0800
@@ -244,6 +244,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
lexer->start = *start;
lexer->end = end;
lexer->line = 1;
+ lexer->file = vm->options.file;
lexer->keywords_hash = vm->shared->keywords_hash;

if (vm->backtrace != NULL) {
diff -r 83581029a022 -r 06e1b1790f9a njs/njs.h
--- a/njs/njs.h Mon Feb 04 05:26:42 2019 +0800
+++ b/njs/njs.h Mon Feb 04 05:33:19 2019 +0800
@@ -141,6 +141,7 @@ typedef struct {
njs_external_ptr_t external;
njs_vm_shared_t *shared;
njs_vm_ops_t *ops;
+ nxt_str_t file;

uint8_t trailer; /* 1 bit */
uint8_t init; /* 1 bit */
diff -r 83581029a022 -r 06e1b1790f9a njs/njs_parser.c
--- a/njs/njs_parser.c Mon Feb 04 05:26:42 2019 +0800
+++ b/njs/njs_parser.c Mon Feb 04 05:33:19 2019 +0800
@@ -2604,9 +2604,10 @@ u_char *
njs_parser_trace_handler(nxt_trace_t *trace, nxt_trace_data_t *td,
u_char *start)
{
- u_char *p;
- size_t size;
- njs_vm_t *vm;
+ u_char *p;
+ size_t size;
+ njs_vm_t *vm;
+ njs_lexer_t *lexer;

size = nxt_length("InternalError: ");
memcpy(start, "InternalError: ", size);
@@ -2618,7 +2619,15 @@ njs_parser_trace_handler(nxt_trace_t *tr
p = trace->handler(trace, td, p);

if (vm->parser != NULL) {
- njs_internal_error(vm, "%s in %uD", start, vm->parser->lexer->line);
+ lexer = vm->parser->lexer;
+
+ if (lexer->file.start != NULL) {
+ njs_internal_error(vm, "%s in %V:%uD", start, &lexer->file,
+ lexer->line);
+ } else {
+ njs_internal_error(vm, "%s in %uD", start, lexer->line);
+ }
+
} else {
njs_internal_error(vm, "%s", start);
}
@@ -2628,30 +2637,36 @@ njs_parser_trace_handler(nxt_trace_t *tr


void
-njs_parser_syntax_error(njs_vm_t *vm, njs_parser_t *parser, const char* fmt,
- ...)
-{
- va_list args;
- u_char buf[256], *end;
+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];
+ va_list args;
+ njs_lexer_t *lexer;
+
+ 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, parser->lexer->line);
-}
-
-
-void
-njs_parser_ref_error(njs_vm_t *vm, njs_parser_t *parser, const char* fmt,
- ...)
-{
- va_list args;
- u_char buf[256], *end;
-
- va_start(args, fmt);
- end = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
- va_end(args);
-
- njs_reference_error(vm, "%*s in %uD", end - buf, buf, parser->lexer->line);
-}
+ lexer = parser->lexer;
+
+ width = nxt_length(" in ") + lexer->file.length + NXT_INT_T_LEN;
+
+ if (p > end - width) {
+ p = end - width;
+ }
+
+ if (lexer->file.start != NULL) {
+ p = nxt_sprintf(p, end, " in %V:%uD", &lexer->file, lexer->line);
+
+ } else {
+ p = nxt_sprintf(p, end, " in %uD", lexer->line);
+ }
+
+ njs_error_new(vm, type, msg, p - msg);
+ }
diff -r 83581029a022 -r 06e1b1790f9a njs/njs_parser.h
--- a/njs/njs_parser.h Mon Feb 04 05:26:42 2019 +0800
+++ b/njs/njs_parser.h Mon Feb 04 05:33:19 2019 +0800
@@ -221,6 +221,8 @@ typedef struct {

nxt_lvlhsh_t keywords_hash;

+ nxt_str_t file;
+
u_char *start;
u_char *prev_start;
u_char *end;
@@ -327,10 +329,14 @@ njs_index_t njs_variable_index(njs_vm_t
nxt_bool_t njs_parser_has_side_effect(njs_parser_node_t *node);
u_char *njs_parser_trace_handler(nxt_trace_t *trace, nxt_trace_data_t *td,
u_char *start);
-void njs_parser_syntax_error(njs_vm_t *vm, njs_parser_t *parser,
- const char* fmt, ...);
-void njs_parser_ref_error(njs_vm_t *vm, njs_parser_t *parser, const char* fmt,
- ...);
+void njs_parser_error(njs_vm_t *vm, njs_parser_t *parser,
+ njs_value_type_t type, const char *fmt, ...);
+
+#define njs_parser_syntax_error(vm, parser, fmt, ...) \
+ njs_parser_error(vm, parser, NJS_OBJECT_SYNTAX_ERROR, fmt, ##__VA_ARGS__)
+
+#define njs_parser_ref_error(vm, parser, fmt, ...) \
+ njs_parser_error(vm, parser, NJS_OBJECT_REF_ERROR, fmt, ##__VA_ARGS__)


nxt_inline njs_parser_node_t *
diff -r 83581029a022 -r 06e1b1790f9a njs/njs_shell.c
--- a/njs/njs_shell.c Mon Feb 04 05:26:42 2019 +0800
+++ b/njs/njs_shell.c Mon Feb 04 05:33:19 2019 +0800
@@ -215,6 +215,11 @@ main(int argc, char **argv)

nxt_memzero(&vm_options, sizeof(njs_vm_opt_t));

+ if (opts.file != NULL) {
+ vm_options.file.start = (u_char *) opts.file;
+ vm_options.file.length = strlen(opts.file);
+ }
+
vm_options.init = !opts.interactive;
vm_options.accumulative = opts.interactive;
vm_options.backtrace = 1;
diff -r 83581029a022 -r 06e1b1790f9a nxt/nxt_types.h
--- a/nxt/nxt_types.h Mon Feb 04 05:26:42 2019 +0800
+++ b/nxt/nxt_types.h Mon Feb 04 05:33:19 2019 +0800
@@ -116,5 +116,7 @@ typedef pid_t nxt_pid_t;

#define NXT_DOUBLE_LEN (1 + DBL_MAX_10_EXP)

+#define NXT_MAX_ERROR_STR 2048
+

#endif /* _NXT_TYPES_H_INCLUDED_ */
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Reporting filename in parser errors.

Dmitry Volyntsev 109 February 04, 2019 03:34AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 77
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready