44 * jsonpath_gram.y
55 * Grammar definitions for jsonpath datatype
66 *
7+ * Transforms tokenized jsonpath into tree of JsonPathParseItem structs.
8+ *
79 * Copyright (c) 2019, PostgreSQL Global Development Group
810 *
911 * IDENTIFICATION
@@ -37,15 +39,17 @@ int jsonpath_yylex(union YYSTYPE *yylval_param);
3739int jsonpath_yyparse (JsonPathParseResult **result);
3840void jsonpath_yyerror (JsonPathParseResult **result, const char *message);
3941
40- static JsonPathParseItem *makeItemType (int type);
42+ static JsonPathParseItem *makeItemType (JsonPathItemType type);
4143static JsonPathParseItem *makeItemString (JsonPathString *s);
4244static JsonPathParseItem *makeItemVariable (JsonPathString *s);
4345static JsonPathParseItem *makeItemKey (JsonPathString *s);
4446static JsonPathParseItem *makeItemNumeric (JsonPathString *s);
4547static JsonPathParseItem *makeItemBool (bool val);
46- static JsonPathParseItem *makeItemBinary (int type, JsonPathParseItem *la,
48+ static JsonPathParseItem *makeItemBinary (JsonPathItemType type,
49+ JsonPathParseItem *la,
4750 JsonPathParseItem *ra);
48- static JsonPathParseItem *makeItemUnary (int type, JsonPathParseItem *a);
51+ static JsonPathParseItem *makeItemUnary (JsonPathItemType type,
52+ JsonPathParseItem *a);
4953static JsonPathParseItem *makeItemList (List *list);
5054static JsonPathParseItem *makeIndexArray (List *list);
5155static JsonPathParseItem *makeAny (int first, int last);
@@ -75,9 +79,9 @@ static JsonPathParseItem *makeItemLikeRegex(JsonPathParseItem *expr,
7579
7680%union {
7781 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;
8185 JsonPathParseResult *result;
8286 JsonPathItemType optype;
8387 bool boolean;
@@ -160,7 +164,7 @@ comp_op:
160164 ;
161165
162166delimited_predicate :
163- ' (' predicate ' )' { $$ = $2 ; }
167+ ' (' predicate ' )' { $$ = $2 ; }
164168 | EXISTS_P ' (' expr ' )' { $$ = makeItemUnary(jpiExists, $3 ); }
165169 ;
166170
@@ -170,9 +174,10 @@ predicate:
170174 | predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1 , $3 ); }
171175 | predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1 , $3 ); }
172176 | 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 ); }
174179 | expr STARTS_P WITH_P starts_with_initial
175- { $$ = makeItemBinary(jpiStartsWith, $1 , $4 ); }
180+ { $$ = makeItemBinary(jpiStartsWith, $1 , $4 ); }
176181 | expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1 , &$3 , NULL ); }
177182 | expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
178183 { $$ = makeItemLikeRegex($1 , &$3 , &$5 ); }
@@ -232,7 +237,8 @@ any_level:
232237any_path :
233238 ANY_P { $$ = makeAny(0 , -1 ); }
234239 | 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 ); }
236242 ;
237243
238244accessor_op :
@@ -285,10 +291,15 @@ method:
285291 ;
286292%%
287293
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)
290301{
291- JsonPathParseItem* v = palloc (sizeof (*v));
302+ JsonPathParseItem * v = palloc (sizeof (*v));
292303
293304 CHECK_FOR_INTERRUPTS ();
294305
@@ -298,10 +309,10 @@ makeItemType(int type)
298309 return v;
299310}
300311
301- static JsonPathParseItem*
312+ static JsonPathParseItem *
302313makeItemString (JsonPathString *s)
303314{
304- JsonPathParseItem *v;
315+ JsonPathParseItem *v;
305316
306317 if (s == NULL )
307318 {
@@ -320,7 +331,7 @@ makeItemString(JsonPathString *s)
320331static JsonPathParseItem *
321332makeItemVariable (JsonPathString *s)
322333{
323- JsonPathParseItem *v;
334+ JsonPathParseItem *v;
324335
325336 v = makeItemType (jpiVariable);
326337 v->value .string .val = s->val ;
@@ -332,7 +343,7 @@ makeItemVariable(JsonPathString *s)
332343static JsonPathParseItem *
333344makeItemKey (JsonPathString *s)
334345{
335- JsonPathParseItem *v;
346+ JsonPathParseItem *v;
336347
337348 v = makeItemString (s);
338349 v->type = jpiKey;
@@ -343,7 +354,7 @@ makeItemKey(JsonPathString *s)
343354static JsonPathParseItem *
344355makeItemNumeric (JsonPathString *s)
345356{
346- JsonPathParseItem *v;
357+ JsonPathParseItem *v;
347358
348359 v = makeItemType (jpiNumeric);
349360 v->value .numeric =
@@ -356,15 +367,15 @@ makeItemNumeric(JsonPathString *s)
356367static JsonPathParseItem *
357368makeItemBool (bool val)
358369{
359- JsonPathParseItem *v = makeItemType (jpiBool);
370+ JsonPathParseItem *v = makeItemType (jpiBool);
360371
361372 v->value .boolean = val;
362373
363374 return v;
364375}
365376
366377static JsonPathParseItem *
367- makeItemBinary (int type, JsonPathParseItem* la, JsonPathParseItem *ra)
378+ makeItemBinary (JsonPathItemType type, JsonPathParseItem * la, JsonPathParseItem *ra)
368379{
369380 JsonPathParseItem *v = makeItemType (type);
370381
@@ -375,7 +386,7 @@ makeItemBinary(int type, JsonPathParseItem* la, JsonPathParseItem *ra)
375386}
376387
377388static JsonPathParseItem *
378- makeItemUnary (int type, JsonPathParseItem* a)
389+ makeItemUnary (JsonPathItemType type, JsonPathParseItem * a)
379390{
380391 JsonPathParseItem *v;
381392
@@ -401,8 +412,9 @@ makeItemUnary(int type, JsonPathParseItem* a)
401412static JsonPathParseItem *
402413makeItemList (List *list)
403414{
404- JsonPathParseItem *head, *end;
405- ListCell *cell = list_head (list);
415+ JsonPathParseItem *head,
416+ *end;
417+ ListCell *cell = list_head (list);
406418
407419 head = end = (JsonPathParseItem *) lfirst (cell);
408420
@@ -427,8 +439,8 @@ makeItemList(List *list)
427439static JsonPathParseItem *
428440makeIndexArray (List *list)
429441{
430- JsonPathParseItem *v = makeItemType (jpiIndexArray);
431- ListCell *cell;
442+ JsonPathParseItem *v = makeItemType (jpiIndexArray);
443+ ListCell *cell;
432444 int i = 0 ;
433445
434446 Assert (list_length (list) > 0 );
@@ -439,7 +451,7 @@ makeIndexArray(List *list)
439451
440452 foreach (cell, list)
441453 {
442- JsonPathParseItem *jpi = lfirst (cell);
454+ JsonPathParseItem *jpi = lfirst (cell);
443455
444456 Assert (jpi->type == jpiSubscript);
445457
@@ -453,7 +465,7 @@ makeIndexArray(List *list)
453465static JsonPathParseItem *
454466makeAny (int first, int last)
455467{
456- JsonPathParseItem *v = makeItemType (jpiAny);
468+ JsonPathParseItem *v = makeItemType (jpiAny);
457469
458470 v->value .anybounds .first = (first >= 0 ) ? first : PG_UINT32_MAX;
459471 v->value .anybounds .last = (last >= 0 ) ? last : PG_UINT32_MAX;
@@ -465,9 +477,9 @@ static JsonPathParseItem *
465477makeItemLikeRegex (JsonPathParseItem *expr, JsonPathString *pattern,
466478 JsonPathString *flags)
467479{
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;
471483
472484 v->value .like_regex .expr = expr;
473485 v->value .like_regex .pattern = pattern->val ;
@@ -510,4 +522,12 @@ makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
510522 return v;
511523}
512524
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+
513533#include " jsonpath_scan.c"
0 commit comments