Skip to content

Commit

Permalink
readJSON: fix parsing JSONish when the field names are also JS reserv…
Browse files Browse the repository at this point in the history
…ed words

Also tidy up lexer using a #define for reserved words
  • Loading branch information
gfwilliams committed Nov 29, 2023
1 parent d6d1fff commit 9949075
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/jslex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ void jslGetTokenString(char *str, size_t len) {
char *jslGetTokenValueAsString() {
assert(lex->tokenl < JSLEX_MAX_TOKEN_LENGTH);
lex->token[lex->tokenl] = 0; // add final null
if (lex->tokenl==0 && lex->tk >= _LEX_R_LIST_START && lex->tk <= _LEX_R_LIST_END) {
if (lex->tokenl==0 && LEX_IS_RESERVED_WORD(lex->tk)) {
// pretokenised - so we'll work out the name from our token name list
// this isn't fast, but won't be called very often
jslTokenAsString(lex->tk, lex->token, sizeof(lex->token));
Expand All @@ -1094,7 +1094,7 @@ int jslGetTokenLength() {
JsVar *jslGetTokenValueAsVar() {
if (lex->tokenValue) {
return jsvLockAgain(lex->tokenValue);
} else if (lex->tk >= _LEX_R_LIST_START && lex->tk <= _LEX_R_LIST_END) {
} else if (LEX_IS_RESERVED_WORD(lex->tk)) {
// in pretokenised code, we must make this up
return jsvNewFromString(jslReservedWordAsString(lex->tk));
} else {
Expand All @@ -1106,7 +1106,7 @@ JsVar *jslGetTokenValueAsVar() {

bool jslIsIDOrReservedWord() {
return lex->tk == LEX_ID ||
(lex->tk >= _LEX_R_LIST_START && lex->tk <= _LEX_R_LIST_END);
LEX_IS_RESERVED_WORD(lex->tk);
}

/* Match failed - report error message */
Expand Down
3 changes: 3 additions & 0 deletions src/jslex.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ _LEX_OPERATOR2_END = LEX_NULLISH,
_LEX_TOKENS_END = _LEX_OPERATOR2_END, /* always the last entry for symbols */
} LEX_TYPES;

// Is the supplied token an ID that is a JS reserved word
#define LEX_IS_RESERVED_WORD(tk) (tk >= _LEX_R_LIST_START && tk <= _LEX_R_LIST_END)


typedef struct JslCharPos {
JsvStringIterator it;
Expand Down
6 changes: 3 additions & 3 deletions src/jswrap_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ JsVar *jswrap_json_parse_internal(JSONFlags flags) {
case '{': {
JsVar *obj = jsvNewObject(); if (!obj) return 0;
jslGetNextToken(); // {
while ((lex->tk == LEX_STR || lex->tk == LEX_ID) && !jspHasError()) {
if (!(flags&JSON_DROP_QUOTES) && (lex->tk == LEX_ID)) {
jslMatch(LEX_STR);
while ((lex->tk == LEX_STR || jslIsIDOrReservedWord()) && !jspHasError()) {
if (!(flags&JSON_DROP_QUOTES) && jslIsIDOrReservedWord()) {
jslMatch(LEX_STR);
return obj;
}
JsVar *key = jsvAsArrayIndexAndUnLock(jslGetTokenValueAsVar());
Expand Down

0 comments on commit 9949075

Please sign in to comment.