Skip to content

Instantly share code, notes, and snippets.

@xeioex
Created February 27, 2019 17:26
Show Gist options
  • Save xeioex/5faab0e5238a32771d5773c4ab3960fc to your computer and use it in GitHub Desktop.
Save xeioex/5faab0e5238a32771d5773c4ab3960fc to your computer and use it in GitHub Desktop.
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1551147113 -28800
# Tue Feb 26 10:11:53 2019 +0800
# Node ID d62d0d255b15fc42203c7f6447d009f22ebe528f
# Parent fc69d402ec6deb2efdd7152d78875c93f7c8a606
Improved parser syntax error.
diff --git a/njs/njs_parser.c b/njs/njs_parser.c
--- a/njs/njs_parser.c
+++ b/njs/njs_parser.c
@@ -278,30 +278,28 @@ njs_parser_statement_chain(njs_vm_t *vm,
token = njs_parser_statement(vm, parser, token);
- if (nxt_fast_path(token > NJS_TOKEN_ILLEGAL)) {
-
- if (parser->node != NULL) {
- /* The statement is not empty block or just semicolon. */
-
- node = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
- if (nxt_slow_path(node == NULL)) {
- return NJS_TOKEN_ERROR;
- }
-
- node->left = last;
- node->right = parser->node;
- *dest = node;
-
- while (token == NJS_TOKEN_SEMICOLON) {
- token = njs_parser_token(parser);
- if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
- break;
- }
+ if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+ return njs_parser_unexpected_token(vm, parser, token);
+ }
+
+ if (parser->node != NULL) {
+ /* The statement is not empty block or just semicolon. */
+
+ node = njs_parser_node_new(vm, parser, NJS_TOKEN_STATEMENT);
+ if (nxt_slow_path(node == NULL)) {
+ return NJS_TOKEN_ERROR;
+ }
+
+ node->left = last;
+ node->right = parser->node;
+ *dest = node;
+
+ while (token == NJS_TOKEN_SEMICOLON) {
+ token = njs_parser_token(parser);
+ if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+ break;
}
}
-
- } else if (!njs_is_error(&vm->retval)) {
- (void) njs_parser_unexpected_token(vm, parser, token);
}
return token;
@@ -2830,6 +2828,10 @@ njs_parser_lexer_error(njs_vm_t *vm, njs
{
va_list args;
+ if (njs_is_error(&vm->retval)) {
+ return;
+ }
+
va_start(args, fmt);
njs_parser_scope_error(vm, parser->scope, type, parser->lexer->line, fmt,
args);
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1551147554 -28800
# Tue Feb 26 10:19:14 2019 +0800
# Node ID d3766e90689f891689cc368cc92d12f48aabd0f1
# Parent d62d0d255b15fc42203c7f6447d009f22ebe528f
Refactored njs_value_index().
diff --git a/njs/njs_string.c b/njs/njs_string.c
--- a/njs/njs_string.c
+++ b/njs/njs_string.c
@@ -4313,22 +4313,23 @@ uri_error:
static nxt_int_t
njs_values_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
{
+ u_char *start;
njs_value_t *value;
value = data;
- if (lhq->key.length == sizeof(njs_value_t)
- && memcmp(lhq->key.start, value, sizeof(njs_value_t)) == 0)
- {
- return NXT_OK;
+ if (lhq->key.length == sizeof(njs_value_t)) {
+ start = (u_char *) value;
+
+ } else {
+ /*
+ * Only primitive values are added into values_hash.
+ * If size != sizeof(njs_value_t) it is a long string.
+ */
+ start = value->long_string.data->start;
}
- if (njs_is_string(value)
- && value->long_string.size == lhq->key.length
- && memcmp(value->long_string.data->start, lhq->key.start,
- lhq->key.length)
- == 0)
- {
+ if (memcmp(lhq->key.start, start, lhq->key.length) == 0) {
return NXT_OK;
}
@@ -4358,18 +4359,22 @@ njs_value_index(njs_vm_t *vm, const njs_
u_char *start;
uint32_t value_size, size, length;
nxt_int_t ret;
+ nxt_bool_t long_string;
njs_value_t *value;
njs_string_t *string;
nxt_lvlhsh_t *values_hash;
nxt_lvlhsh_query_t lhq;
- if (src->type != NJS_STRING || src->short_string.size != NJS_STRING_LONG) {
+ long_string = src->type == NJS_STRING
+ && src->short_string.size == NJS_STRING_LONG;
+
+ if (long_string) {
+ size = src->long_string.size;
+ start = src->long_string.data->start;
+
+ } else {
size = sizeof(njs_value_t);
start = (u_char *) src;
-
- } else {
- size = src->long_string.size;
- start = src->long_string.data->start;
}
lhq.key_hash = nxt_djb_hash(start, size);
@@ -4386,7 +4391,7 @@ njs_value_index(njs_vm_t *vm, const njs_
} else {
value_size = 0;
- if (start != (u_char *) src) {
+ if (long_string) {
/* Long string value is allocated together with string. */
value_size = sizeof(njs_value_t) + sizeof(njs_string_t);
@@ -4406,7 +4411,7 @@ njs_value_index(njs_vm_t *vm, const njs_
*value = *src;
- if (start != (u_char *) src) {
+ if (long_string) {
string = (njs_string_t *) ((u_char *) value + sizeof(njs_value_t));
value->long_string.data = string;
@@ -4430,7 +4435,7 @@ njs_value_index(njs_vm_t *vm, const njs_
}
}
- if (start != (u_char *) src) {
+ if (long_string) {
/*
* The source node value must be updated with the shared value
* allocated from the permanent memory pool because the node
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1551288153 -10800
# Wed Feb 27 20:22:33 2019 +0300
# Node ID 182f3627060075d22e9b8c834c28ed5558f229b5
# Parent d3766e90689f891689cc368cc92d12f48aabd0f1
Shell: passing original filename to parser.
diff --git a/njs/njs_shell.c b/njs/njs_shell.c
--- a/njs/njs_shell.c
+++ b/njs/njs_shell.c
@@ -218,7 +218,6 @@ main(int argc, char **argv)
if (opts.file != NULL) {
vm_options.file.start = (u_char *) opts.file;
vm_options.file.length = strlen(opts.file);
- nxt_file_basename(&vm_options.file, &vm_options.file);
} else {
vm_options.file = nxt_string_value("shell");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment