4
4
* jsonpath_gram.y
5
5
* Grammar definitions for jsonpath datatype
6
6
*
7
+ * Transforms tokenized jsonpath into tree of JsonPathParseItem structs.
8
+ *
7
9
* Copyright (c) 2019, PostgreSQL Global Development Group
8
10
*
9
11
* IDENTIFICATION
@@ -37,15 +39,17 @@ int jsonpath_yylex(union YYSTYPE *yylval_param);
37
39
int jsonpath_yyparse (JsonPathParseResult **result);
38
40
void jsonpath_yyerror (JsonPathParseResult **result, const char *message);
39
41
40
- static JsonPathParseItem *makeItemType (int type);
42
+ static JsonPathParseItem *makeItemType (JsonPathItemType type);
41
43
static JsonPathParseItem *makeItemString (JsonPathString *s);
42
44
static JsonPathParseItem *makeItemVariable (JsonPathString *s);
43
45
static JsonPathParseItem *makeItemKey (JsonPathString *s);
44
46
static JsonPathParseItem *makeItemNumeric (JsonPathString *s);
45
47
static JsonPathParseItem *makeItemBool (bool val);
46
- static JsonPathParseItem *makeItemBinary (int type, JsonPathParseItem *la,
48
+ static JsonPathParseItem *makeItemBinary (JsonPathItemType type,
49
+ JsonPathParseItem *la,
47
50
JsonPathParseItem *ra);
48
- static JsonPathParseItem *makeItemUnary (int type, JsonPathParseItem *a);
51
+ static JsonPathParseItem *makeItemUnary (JsonPathItemType type,
52
+ JsonPathParseItem *a);
49
53
static JsonPathParseItem *makeItemList (List *list);
50
54
static JsonPathParseItem *makeIndexArray (List *list);
51
55
static JsonPathParseItem *makeAny (int first, int last);
@@ -75,9 +79,9 @@ static JsonPathParseItem *makeItemLikeRegex(JsonPathParseItem *expr,
75
79
76
80
%union {
77
81
JsonPathString str;
78
- List *elems; /* list of JsonPathParseItem */
79
- List *indexs; /* list of integers */
80
- JsonPathParseItem *value;
82
+ List *elems; /* list of JsonPathParseItem */
83
+ List *indexs; /* list of integers */
84
+ JsonPathParseItem *value;
81
85
JsonPathParseResult *result;
82
86
JsonPathItemType optype;
83
87
bool boolean;
@@ -160,7 +164,7 @@ comp_op:
160
164
;
161
165
162
166
delimited_predicate :
163
- ' (' predicate ' )' { $$ = $2 ; }
167
+ ' (' predicate ' )' { $$ = $2 ; }
164
168
| EXISTS_P ' (' expr ' )' { $$ = makeItemUnary(jpiExists, $3 ); }
165
169
;
166
170
@@ -170,9 +174,10 @@ predicate:
170
174
| predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1 , $3 ); }
171
175
| predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1 , $3 ); }
172
176
| NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2 ); }
173
- | ' (' predicate ' )' IS_P UNKNOWN_P { $$ = makeItemUnary(jpiIsUnknown, $2 ); }
177
+ | ' (' predicate ' )' IS_P UNKNOWN_P
178
+ { $$ = makeItemUnary(jpiIsUnknown, $2 ); }
174
179
| expr STARTS_P WITH_P starts_with_initial
175
- { $$ = makeItemBinary(jpiStartsWith, $1 , $4 ); }
180
+ { $$ = makeItemBinary(jpiStartsWith, $1 , $4 ); }
176
181
| expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1 , &$3 , NULL ); }
177
182
| expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
178
183
{ $$ = makeItemLikeRegex($1 , &$3 , &$5 ); }
@@ -232,7 +237,8 @@ any_level:
232
237
any_path :
233
238
ANY_P { $$ = makeAny(0 , -1 ); }
234
239
| ANY_P ' {' any_level ' }' { $$ = makeAny($3 , $3 ); }
235
- | ANY_P ' {' any_level TO_P any_level ' }' { $$ = makeAny($3 , $5 ); }
240
+ | ANY_P ' {' any_level TO_P any_level ' }'
241
+ { $$ = makeAny($3 , $5 ); }
236
242
;
237
243
238
244
accessor_op :
@@ -285,10 +291,15 @@ method:
285
291
;
286
292
%%
287
293
288
- static JsonPathParseItem*
289
- makeItemType (int type)
294
+ /*
295
+ * The helper functions below allocate and fill JsonPathParseItem's of various
296
+ * types.
297
+ */
298
+
299
+ static JsonPathParseItem *
300
+ makeItemType (JsonPathItemType type)
290
301
{
291
- JsonPathParseItem* v = palloc (sizeof (*v));
302
+ JsonPathParseItem * v = palloc (sizeof (*v));
292
303
293
304
CHECK_FOR_INTERRUPTS ();
294
305
@@ -298,10 +309,10 @@ makeItemType(int type)
298
309
return v;
299
310
}
300
311
301
- static JsonPathParseItem*
312
+ static JsonPathParseItem *
302
313
makeItemString (JsonPathString *s)
303
314
{
304
- JsonPathParseItem *v;
315
+ JsonPathParseItem *v;
305
316
306
317
if (s == NULL )
307
318
{
@@ -320,7 +331,7 @@ makeItemString(JsonPathString *s)
320
331
static JsonPathParseItem *
321
332
makeItemVariable (JsonPathString *s)
322
333
{
323
- JsonPathParseItem *v;
334
+ JsonPathParseItem *v;
324
335
325
336
v = makeItemType (jpiVariable);
326
337
v->value .string .val = s->val ;
@@ -332,7 +343,7 @@ makeItemVariable(JsonPathString *s)
332
343
static JsonPathParseItem *
333
344
makeItemKey (JsonPathString *s)
334
345
{
335
- JsonPathParseItem *v;
346
+ JsonPathParseItem *v;
336
347
337
348
v = makeItemString (s);
338
349
v->type = jpiKey;
@@ -343,7 +354,7 @@ makeItemKey(JsonPathString *s)
343
354
static JsonPathParseItem *
344
355
makeItemNumeric (JsonPathString *s)
345
356
{
346
- JsonPathParseItem *v;
357
+ JsonPathParseItem *v;
347
358
348
359
v = makeItemType (jpiNumeric);
349
360
v->value .numeric =
@@ -356,15 +367,15 @@ makeItemNumeric(JsonPathString *s)
356
367
static JsonPathParseItem *
357
368
makeItemBool (bool val)
358
369
{
359
- JsonPathParseItem *v = makeItemType (jpiBool);
370
+ JsonPathParseItem *v = makeItemType (jpiBool);
360
371
361
372
v->value .boolean = val;
362
373
363
374
return v;
364
375
}
365
376
366
377
static JsonPathParseItem *
367
- makeItemBinary (int type, JsonPathParseItem* la, JsonPathParseItem *ra)
378
+ makeItemBinary (JsonPathItemType type, JsonPathParseItem * la, JsonPathParseItem *ra)
368
379
{
369
380
JsonPathParseItem *v = makeItemType (type);
370
381
@@ -375,7 +386,7 @@ makeItemBinary(int type, JsonPathParseItem* la, JsonPathParseItem *ra)
375
386
}
376
387
377
388
static JsonPathParseItem *
378
- makeItemUnary (int type, JsonPathParseItem* a)
389
+ makeItemUnary (JsonPathItemType type, JsonPathParseItem * a)
379
390
{
380
391
JsonPathParseItem *v;
381
392
@@ -401,8 +412,9 @@ makeItemUnary(int type, JsonPathParseItem* a)
401
412
static JsonPathParseItem *
402
413
makeItemList (List *list)
403
414
{
404
- JsonPathParseItem *head, *end;
405
- ListCell *cell = list_head (list);
415
+ JsonPathParseItem *head,
416
+ *end;
417
+ ListCell *cell = list_head (list);
406
418
407
419
head = end = (JsonPathParseItem *) lfirst (cell);
408
420
@@ -427,8 +439,8 @@ makeItemList(List *list)
427
439
static JsonPathParseItem *
428
440
makeIndexArray (List *list)
429
441
{
430
- JsonPathParseItem *v = makeItemType (jpiIndexArray);
431
- ListCell *cell;
442
+ JsonPathParseItem *v = makeItemType (jpiIndexArray);
443
+ ListCell *cell;
432
444
int i = 0 ;
433
445
434
446
Assert (list_length (list) > 0 );
@@ -439,7 +451,7 @@ makeIndexArray(List *list)
439
451
440
452
foreach (cell, list)
441
453
{
442
- JsonPathParseItem *jpi = lfirst (cell);
454
+ JsonPathParseItem *jpi = lfirst (cell);
443
455
444
456
Assert (jpi->type == jpiSubscript);
445
457
@@ -453,7 +465,7 @@ makeIndexArray(List *list)
453
465
static JsonPathParseItem *
454
466
makeAny (int first, int last)
455
467
{
456
- JsonPathParseItem *v = makeItemType (jpiAny);
468
+ JsonPathParseItem *v = makeItemType (jpiAny);
457
469
458
470
v->value .anybounds .first = (first >= 0 ) ? first : PG_UINT32_MAX;
459
471
v->value .anybounds .last = (last >= 0 ) ? last : PG_UINT32_MAX;
@@ -465,9 +477,9 @@ static JsonPathParseItem *
465
477
makeItemLikeRegex (JsonPathParseItem *expr, JsonPathString *pattern,
466
478
JsonPathString *flags)
467
479
{
468
- JsonPathParseItem *v = makeItemType (jpiLikeRegex);
469
- int i;
470
- int cflags = REG_ADVANCED;
480
+ JsonPathParseItem *v = makeItemType (jpiLikeRegex);
481
+ int i;
482
+ int cflags = REG_ADVANCED;
471
483
472
484
v->value .like_regex .expr = expr;
473
485
v->value .like_regex .pattern = pattern->val ;
@@ -510,4 +522,12 @@ makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
510
522
return v;
511
523
}
512
524
525
+ /*
526
+ * jsonpath_scan.l is compiled as part of jsonpath_gram.y. Currently, this is
527
+ * unavoidable because jsonpath_gram does not create a .h file to export its
528
+ * token symbols. If these files ever grow large enough to be worth compiling
529
+ * separately, that could be fixed; but for now it seems like useless
530
+ * complication.
531
+ */
532
+
513
533
#include " jsonpath_scan.c"
0 commit comments