@@ -69,52 +69,17 @@ lex_peek(JsonLexContext *lex)
69
69
}
70
70
71
71
/*
72
- * lex_accept
73
- *
74
- * accept the look_ahead token and move the lexer to the next token if the
75
- * look_ahead token matches the token parameter. In that case, and if required,
76
- * also hand back the de-escaped lexeme.
77
- *
78
- * returns true if the token matched, false otherwise.
79
- */
80
- static inline bool
81
- lex_accept (JsonLexContext * lex , JsonTokenType token , char * * lexeme )
82
- {
83
- if (lex -> token_type == token )
84
- {
85
- if (lexeme != NULL )
86
- {
87
- if (lex -> token_type == JSON_TOKEN_STRING )
88
- {
89
- if (lex -> strval != NULL )
90
- * lexeme = pstrdup (lex -> strval -> data );
91
- }
92
- else
93
- {
94
- int len = (lex -> token_terminator - lex -> token_start );
95
- char * tokstr = palloc (len + 1 );
96
-
97
- memcpy (tokstr , lex -> token_start , len );
98
- tokstr [len ] = '\0' ;
99
- * lexeme = tokstr ;
100
- }
101
- }
102
- json_lex (lex );
103
- return true;
104
- }
105
- return false;
106
- }
107
-
108
- /*
109
- * lex_accept
72
+ * lex_expect
110
73
*
111
74
* move the lexer to the next token if the current look_ahead token matches
112
75
* the parameter token. Otherwise, report an error.
113
76
*/
114
77
static inline void
115
78
lex_expect (JsonParseContext ctx , JsonLexContext * lex , JsonTokenType token )
116
79
{
117
- if (!lex_accept (lex , token , NULL ))
80
+ if (lex_peek (lex ) == token )
81
+ json_lex (lex );
82
+ else
118
83
report_parse_error (ctx , lex );
119
84
}
120
85
@@ -260,12 +225,14 @@ json_count_array_elements(JsonLexContext *lex)
260
225
lex_expect (JSON_PARSE_ARRAY_START , & copylex , JSON_TOKEN_ARRAY_START );
261
226
if (lex_peek (& copylex ) != JSON_TOKEN_ARRAY_END )
262
227
{
263
- do
228
+ while ( 1 )
264
229
{
265
230
count ++ ;
266
231
parse_array_element (& copylex , & nullSemAction );
232
+ if (copylex .token_type != JSON_TOKEN_COMMA )
233
+ break ;
234
+ json_lex (& copylex );
267
235
}
268
- while (lex_accept (& copylex , JSON_TOKEN_COMMA , NULL ));
269
236
}
270
237
lex_expect (JSON_PARSE_ARRAY_NEXT , & copylex , JSON_TOKEN_ARRAY_END );
271
238
@@ -286,35 +253,41 @@ parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
286
253
{
287
254
char * val = NULL ;
288
255
json_scalar_action sfunc = sem -> scalar ;
289
- char * * valaddr ;
290
256
JsonTokenType tok = lex_peek (lex );
291
257
292
- valaddr = sfunc == NULL ? NULL : & val ;
293
-
294
258
/* a scalar must be a string, a number, true, false, or null */
295
- switch (tok )
259
+ if (tok != JSON_TOKEN_STRING && tok != JSON_TOKEN_NUMBER &&
260
+ tok != JSON_TOKEN_TRUE && tok != JSON_TOKEN_FALSE &&
261
+ tok != JSON_TOKEN_NULL )
262
+ report_parse_error (JSON_PARSE_VALUE , lex );
263
+
264
+ /* if no semantic function, just consume the token */
265
+ if (sfunc == NULL )
296
266
{
297
- case JSON_TOKEN_TRUE :
298
- lex_accept ( lex , JSON_TOKEN_TRUE , valaddr ) ;
299
- break ;
300
- case JSON_TOKEN_FALSE :
301
- lex_accept ( lex , JSON_TOKEN_FALSE , valaddr );
302
- break ;
303
- case JSON_TOKEN_NULL :
304
- lex_accept (lex , JSON_TOKEN_NULL , valaddr );
305
- break ;
306
- case JSON_TOKEN_NUMBER :
307
- lex_accept ( lex , JSON_TOKEN_NUMBER , valaddr );
308
- break ;
309
- case JSON_TOKEN_STRING :
310
- lex_accept ( lex , JSON_TOKEN_STRING , valaddr );
311
- break ;
312
- default :
313
- report_parse_error ( JSON_PARSE_VALUE , lex ) ;
267
+ json_lex ( lex );
268
+ return ;
269
+ }
270
+
271
+ /* extract the de-escaped string value, or the raw lexeme */
272
+ if ( lex_peek ( lex ) == JSON_TOKEN_STRING )
273
+ {
274
+ if (lex -> strval != NULL )
275
+ val = pstrdup ( lex -> strval -> data ) ;
276
+ }
277
+ else
278
+ {
279
+ int len = ( lex -> token_terminator - lex -> token_start );
280
+
281
+ val = palloc ( len + 1 ) ;
282
+ memcpy ( val , lex -> token_start , len );
283
+ val [ len ] = '\0' ;
314
284
}
315
285
316
- if (sfunc != NULL )
317
- (* sfunc ) (sem -> semstate , val , tok );
286
+ /* consume the token */
287
+ json_lex (lex );
288
+
289
+ /* invoke the callback */
290
+ (* sfunc ) (sem -> semstate , val , tok );
318
291
}
319
292
320
293
static void
@@ -330,14 +303,13 @@ parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
330
303
json_ofield_action ostart = sem -> object_field_start ;
331
304
json_ofield_action oend = sem -> object_field_end ;
332
305
bool isnull ;
333
- char * * fnameaddr = NULL ;
334
306
JsonTokenType tok ;
335
307
336
- if (ostart != NULL || oend != NULL )
337
- fnameaddr = & fname ;
338
-
339
- if (!lex_accept (lex , JSON_TOKEN_STRING , fnameaddr ))
308
+ if (lex_peek (lex ) != JSON_TOKEN_STRING )
340
309
report_parse_error (JSON_PARSE_STRING , lex );
310
+ if ((ostart != NULL || oend != NULL ) && lex -> strval != NULL )
311
+ fname = pstrdup (lex -> strval -> data );
312
+ json_lex (lex );
341
313
342
314
lex_expect (JSON_PARSE_OBJECT_LABEL , lex , JSON_TOKEN_COLON );
343
315
@@ -387,16 +359,19 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem)
387
359
*/
388
360
lex -> lex_level ++ ;
389
361
390
- /* we know this will succeed, just clearing the token */
391
- lex_expect ( JSON_PARSE_OBJECT_START , lex , JSON_TOKEN_OBJECT_START );
362
+ Assert ( lex_peek ( lex ) == JSON_TOKEN_OBJECT_START );
363
+ json_lex ( lex );
392
364
393
365
tok = lex_peek (lex );
394
366
switch (tok )
395
367
{
396
368
case JSON_TOKEN_STRING :
397
369
parse_object_field (lex , sem );
398
- while (lex_accept (lex , JSON_TOKEN_COMMA , NULL ))
370
+ while (lex_peek (lex ) == JSON_TOKEN_COMMA )
371
+ {
372
+ json_lex (lex );
399
373
parse_object_field (lex , sem );
374
+ }
400
375
break ;
401
376
case JSON_TOKEN_OBJECT_END :
402
377
break ;
@@ -473,8 +448,11 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem)
473
448
474
449
parse_array_element (lex , sem );
475
450
476
- while (lex_accept (lex , JSON_TOKEN_COMMA , NULL ))
451
+ while (lex_peek (lex ) == JSON_TOKEN_COMMA )
452
+ {
453
+ json_lex (lex );
477
454
parse_array_element (lex , sem );
455
+ }
478
456
}
479
457
480
458
lex_expect (JSON_PARSE_ARRAY_NEXT , lex , JSON_TOKEN_ARRAY_END );
0 commit comments