2 /*-------------------------------------------------------------------------
5 * Grammar definitions for jsonpath datatype
7 * Transforms tokenized jsonpath into tree of JsonPathParseItem structs.
9 * Copyright (c) 2019, PostgreSQL Global Development Group
12 * src/backend/utils/adt/jsonpath_gram.y
14 *-------------------------------------------------------------------------
19 #include "catalog/pg_collation.h"
21 #include "miscadmin.h"
22 #include "nodes/pg_list.h"
23 #include "regex/regex.h"
24 #include "utils/builtins.h"
25 #include "utils/jsonpath.h"
27 /* struct JsonPathString is shared between scan and gram */
28 typedef struct JsonPathString
37 /* flex 2.5.4 doesn't bother with a decl for this */
38 int jsonpath_yylex(union YYSTYPE *yylval_param);
39 int jsonpath_yyparse(JsonPathParseResult **result);
40 void jsonpath_yyerror(JsonPathParseResult **result, const char *message);
42 static JsonPathParseItem *makeItemType(JsonPathItemType type);
43 static JsonPathParseItem *makeItemString(JsonPathString *s);
44 static JsonPathParseItem *makeItemVariable(JsonPathString *s);
45 static JsonPathParseItem *makeItemKey(JsonPathString *s);
46 static JsonPathParseItem *makeItemNumeric(JsonPathString *s);
47 static JsonPathParseItem *makeItemBool(bool val);
48 static JsonPathParseItem *makeItemBinary(JsonPathItemType type,
49 JsonPathParseItem *la,
50 JsonPathParseItem *ra);
51 static JsonPathParseItem *makeItemUnary(JsonPathItemType type,
52 JsonPathParseItem *a);
53 static JsonPathParseItem *makeItemList(List *list);
54 static JsonPathParseItem *makeIndexArray(List *list);
55 static JsonPathParseItem *makeAny(int first, int last);
56 static JsonPathParseItem *makeItemLikeRegex(JsonPathParseItem *expr,
57 JsonPathString *pattern,
58 JsonPathString *flags);
61 * Bison doesn't allocate anything that needs to live across parser calls,
62 * so we can easily have it use palloc instead of malloc. This prevents
63 * memory leaks if we error out during parsing. Note this only works with
64 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
65 * if possible, so there's not really much problem anyhow, at least if
66 * you're building with gcc.
68 #define YYMALLOC palloc
73 /* BISON Declarations */
76 %name-prefix="jsonpath_yy"
78 %parse-param {JsonPathParseResult **result}
82 List *elems; /* list of JsonPathParseItem */
83 List *indexs; /* list of integers */
84 JsonPathParseItem *value;
85 JsonPathParseResult *result;
86 JsonPathItemType optype;
91 %token <str> TO_P NULL_P TRUE_P FALSE_P IS_P UNKNOWN_P EXISTS_P
92 %token <str> IDENT_P STRING_P NUMERIC_P INT_P VARIABLE_P
93 %token <str> OR_P AND_P NOT_P
94 %token <str> LESS_P LESSEQUAL_P EQUAL_P NOTEQUAL_P GREATEREQUAL_P GREATER_P
95 %token <str> ANY_P STRICT_P LAX_P LAST_P STARTS_P WITH_P LIKE_REGEX_P FLAG_P
96 %token <str> ABS_P SIZE_P TYPE_P FLOOR_P DOUBLE_P CEILING_P KEYVALUE_P
100 %type <value> scalar_value path_primary expr array_accessor
101 any_path accessor_op key predicate delimited_predicate
102 index_elem starts_with_initial expr_or_predicate
104 %type <elems> accessor_expr
106 %type <indexs> index_list
108 %type <optype> comp_op method
114 %type <integer> any_level
124 /* Grammar follows */
128 mode expr_or_predicate {
129 *result = palloc(sizeof(JsonPathParseResult));
130 (*result)->expr = $2;
133 | /* EMPTY */ { *result = NULL; }
138 | predicate { $$ = $1; }
142 STRICT_P { $$ = false; }
143 | LAX_P { $$ = true; }
144 | /* EMPTY */ { $$ = true; }
148 STRING_P { $$ = makeItemString(&$1); }
149 | NULL_P { $$ = makeItemString(NULL); }
150 | TRUE_P { $$ = makeItemBool(true); }
151 | FALSE_P { $$ = makeItemBool(false); }
152 | NUMERIC_P { $$ = makeItemNumeric(&$1); }
153 | INT_P { $$ = makeItemNumeric(&$1); }
154 | VARIABLE_P { $$ = makeItemVariable(&$1); }
158 EQUAL_P { $$ = jpiEqual; }
159 | NOTEQUAL_P { $$ = jpiNotEqual; }
160 | LESS_P { $$ = jpiLess; }
161 | GREATER_P { $$ = jpiGreater; }
162 | LESSEQUAL_P { $$ = jpiLessOrEqual; }
163 | GREATEREQUAL_P { $$ = jpiGreaterOrEqual; }
167 '(' predicate ')' { $$ = $2; }
168 | EXISTS_P '(' expr ')' { $$ = makeItemUnary(jpiExists, $3); }
172 delimited_predicate { $$ = $1; }
173 | expr comp_op expr { $$ = makeItemBinary($2, $1, $3); }
174 | predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); }
175 | predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); }
176 | NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); }
177 | '(' predicate ')' IS_P UNKNOWN_P
178 { $$ = makeItemUnary(jpiIsUnknown, $2); }
179 | expr STARTS_P WITH_P starts_with_initial
180 { $$ = makeItemBinary(jpiStartsWith, $1, $4); }
181 | expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); }
182 | expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
183 { $$ = makeItemLikeRegex($1, &$3, &$5); }
187 STRING_P { $$ = makeItemString(&$1); }
188 | VARIABLE_P { $$ = makeItemVariable(&$1); }
192 scalar_value { $$ = $1; }
193 | '$' { $$ = makeItemType(jpiRoot); }
194 | '@' { $$ = makeItemType(jpiCurrent); }
195 | LAST_P { $$ = makeItemType(jpiLast); }
199 path_primary { $$ = list_make1($1); }
200 | '(' expr ')' accessor_op { $$ = list_make2($2, $4); }
201 | '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
202 | accessor_expr accessor_op { $$ = lappend($1, $2); }
206 accessor_expr { $$ = makeItemList($1); }
207 | '(' expr ')' { $$ = $2; }
208 | '+' expr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
209 | '-' expr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
210 | expr '+' expr { $$ = makeItemBinary(jpiAdd, $1, $3); }
211 | expr '-' expr { $$ = makeItemBinary(jpiSub, $1, $3); }
212 | expr '*' expr { $$ = makeItemBinary(jpiMul, $1, $3); }
213 | expr '/' expr { $$ = makeItemBinary(jpiDiv, $1, $3); }
214 | expr '%' expr { $$ = makeItemBinary(jpiMod, $1, $3); }
218 expr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
219 | expr TO_P expr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
223 index_elem { $$ = list_make1($1); }
224 | index_list ',' index_elem { $$ = lappend($1, $3); }
228 '[' '*' ']' { $$ = makeItemType(jpiAnyArray); }
229 | '[' index_list ']' { $$ = makeIndexArray($2); }
233 INT_P { $$ = pg_atoi($1.val, 4, 0); }
234 | LAST_P { $$ = -1; }
238 ANY_P { $$ = makeAny(0, -1); }
239 | ANY_P '{' any_level '}' { $$ = makeAny($3, $3); }
240 | ANY_P '{' any_level TO_P any_level '}'
241 { $$ = makeAny($3, $5); }
246 | '.' '*' { $$ = makeItemType(jpiAnyKey); }
247 | array_accessor { $$ = $1; }
248 | '.' any_path { $$ = $2; }
249 | '.' method '(' ')' { $$ = makeItemType($2); }
250 | '?' '(' predicate ')' { $$ = makeItemUnary(jpiFilter, $3); }
254 key_name { $$ = makeItemKey(&$1); }
284 ABS_P { $$ = jpiAbs; }
285 | SIZE_P { $$ = jpiSize; }
286 | TYPE_P { $$ = jpiType; }
287 | FLOOR_P { $$ = jpiFloor; }
288 | DOUBLE_P { $$ = jpiDouble; }
289 | CEILING_P { $$ = jpiCeiling; }
290 | KEYVALUE_P { $$ = jpiKeyValue; }
295 * The helper functions below allocate and fill JsonPathParseItem's of various
299 static JsonPathParseItem *
300 makeItemType(JsonPathItemType type)
302 JsonPathParseItem *v = palloc(sizeof(*v));
304 CHECK_FOR_INTERRUPTS();
312 static JsonPathParseItem *
313 makeItemString(JsonPathString *s)
315 JsonPathParseItem *v;
319 v = makeItemType(jpiNull);
323 v = makeItemType(jpiString);
324 v->value.string.val = s->val;
325 v->value.string.len = s->len;
331 static JsonPathParseItem *
332 makeItemVariable(JsonPathString *s)
334 JsonPathParseItem *v;
336 v = makeItemType(jpiVariable);
337 v->value.string.val = s->val;
338 v->value.string.len = s->len;
343 static JsonPathParseItem *
344 makeItemKey(JsonPathString *s)
346 JsonPathParseItem *v;
348 v = makeItemString(s);
354 static JsonPathParseItem *
355 makeItemNumeric(JsonPathString *s)
357 JsonPathParseItem *v;
359 v = makeItemType(jpiNumeric);
361 DatumGetNumeric(DirectFunctionCall3(numeric_in,
362 CStringGetDatum(s->val), 0, -1));
367 static JsonPathParseItem *
368 makeItemBool(bool val)
370 JsonPathParseItem *v = makeItemType(jpiBool);
372 v->value.boolean = val;
377 static JsonPathParseItem *
378 makeItemBinary(JsonPathItemType type, JsonPathParseItem *la, JsonPathParseItem *ra)
380 JsonPathParseItem *v = makeItemType(type);
382 v->value.args.left = la;
383 v->value.args.right = ra;
388 static JsonPathParseItem *
389 makeItemUnary(JsonPathItemType type, JsonPathParseItem *a)
391 JsonPathParseItem *v;
393 if (type == jpiPlus && a->type == jpiNumeric && !a->next)
396 if (type == jpiMinus && a->type == jpiNumeric && !a->next)
398 v = makeItemType(jpiNumeric);
400 DatumGetNumeric(DirectFunctionCall1(numeric_uminus,
401 NumericGetDatum(a->value.numeric)));
405 v = makeItemType(type);
412 static JsonPathParseItem *
413 makeItemList(List *list)
415 JsonPathParseItem *head,
417 ListCell *cell = list_head(list);
419 head = end = (JsonPathParseItem *) lfirst(cell);
424 /* append items to the end of already existing list */
428 for_each_cell(cell, lnext(cell))
430 JsonPathParseItem *c = (JsonPathParseItem *) lfirst(cell);
439 static JsonPathParseItem *
440 makeIndexArray(List *list)
442 JsonPathParseItem *v = makeItemType(jpiIndexArray);
446 Assert(list_length(list) > 0);
447 v->value.array.nelems = list_length(list);
449 v->value.array.elems = palloc(sizeof(v->value.array.elems[0]) *
450 v->value.array.nelems);
454 JsonPathParseItem *jpi = lfirst(cell);
456 Assert(jpi->type == jpiSubscript);
458 v->value.array.elems[i].from = jpi->value.args.left;
459 v->value.array.elems[i++].to = jpi->value.args.right;
465 static JsonPathParseItem *
466 makeAny(int first, int last)
468 JsonPathParseItem *v = makeItemType(jpiAny);
470 v->value.anybounds.first = (first >= 0) ? first : PG_UINT32_MAX;
471 v->value.anybounds.last = (last >= 0) ? last : PG_UINT32_MAX;
476 static JsonPathParseItem *
477 makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
478 JsonPathString *flags)
480 JsonPathParseItem *v = makeItemType(jpiLikeRegex);
482 int cflags = REG_ADVANCED;
484 v->value.like_regex.expr = expr;
485 v->value.like_regex.pattern = pattern->val;
486 v->value.like_regex.patternlen = pattern->len;
487 v->value.like_regex.flags = 0;
489 for (i = 0; flags && i < flags->len; i++)
491 switch (flags->val[i])
494 v->value.like_regex.flags |= JSP_REGEX_ICASE;
498 v->value.like_regex.flags &= ~JSP_REGEX_MLINE;
499 v->value.like_regex.flags |= JSP_REGEX_SLINE;
500 cflags |= REG_NEWLINE;
503 v->value.like_regex.flags &= ~JSP_REGEX_SLINE;
504 v->value.like_regex.flags |= JSP_REGEX_MLINE;
505 cflags &= ~REG_NEWLINE;
508 v->value.like_regex.flags |= JSP_REGEX_WSPACE;
509 cflags |= REG_EXPANDED;
512 yyerror(NULL, "unrecognized flag of LIKE_REGEX predicate");
517 /* check regex validity */
518 (void) RE_compile_and_cache(cstring_to_text_with_len(pattern->val,
520 cflags, DEFAULT_COLLATION_OID);
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
533 #include "jsonpath_scan.c"