1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*-------------------------------------------------------------------------
*
* jsquery_gram.y
* Grammar definitions for jsquery datatype
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2017-2021, Postgres Professional
* Author: Teodor Sigaev <teodor@sigaev.ru>
*
* IDENTIFICATION
* contrib/jsquery/jsquery_gram.y
*
*-------------------------------------------------------------------------
*/
%{
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "jsquery.h"
/*
* Bison doesn't allocate anything that needs to live across parser calls,
* so we can easily have it use palloc instead of malloc. This prevents
* memory leaks if we error out during parsing. Note this only works with
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
* if possible, so there's not really much problem anyhow, at least if
* you're building with gcc.
*/
#define YYMALLOC palloc
#define YYFREE pfree
/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
#undef fprintf
#define fprintf(file, fmt, msg) fprintf_to_ereport(fmt, msg)
static void
fprintf_to_ereport(const char *fmt, const char *msg)
{
ereport(ERROR, (errmsg_internal("%s", msg)));
}
/* struct string is shared between scan and gram */
typedef struct string {
char *val;
int len;
int total;
} string;
#include "jsquery_gram.h"
/* flex 2.5.4 doesn't bother with a decl for this */
int jsquery_yylex(YYSTYPE * yylval_param);
void jsquery_yyerror(JsQueryParseItem **result, const char *message);
static JsQueryParseItem*
makeItemType(int type)
{
JsQueryParseItem* v = palloc(sizeof(*v));
v->type = type;
v->hint = jsqIndexDefault;
v->next = NULL;
return v;
}
static JsQueryParseItem*
makeIndexArray(string *s)
{
JsQueryParseItem* v = makeItemType(jqiIndexArray);
#if PG_VERSION_NUM >= 120000
v->arrayIndex = pg_strtoint32(s->val);
#else
v->arrayIndex = pg_atoi(s->val, 4, 0);
#endif
return v;
}
static JsQueryParseItem*
makeItemString(string *s)
{
JsQueryParseItem *v;
if (s == NULL)
{
v = makeItemType(jqiNull);
}
else
{
v = makeItemType(jqiString);
v->string.val = s->val;
v->string.len = s->len;
}
return v;
}
static JsQueryParseItem*
makeItemKey(string *s)
{
JsQueryParseItem *v;
v = makeItemString(s);
v->type = jqiKey;
return v;
}
static JsQueryParseItem*
makeItemNumeric(string *s)
{
JsQueryParseItem *v;
v = makeItemType(jqiNumeric);
v->numeric = DatumGetNumeric(DirectFunctionCall3(numeric_in, CStringGetDatum(s->val), 0, -1));
return v;
}
static JsQueryParseItem*
makeItemBool(bool val) {
JsQueryParseItem *v = makeItemType(jqiBool);
v->boolean = val;
return v;
}
static JsQueryParseItem*
makeItemArray(List *list)
{
JsQueryParseItem *v = makeItemType(jqiArray);
v->array.nelems = list_length(list);
if (v->array.nelems > 0)
{
ListCell *cell;
int i = 0;
v->array.elems = palloc(sizeof(JsQueryParseItem) * v->array.nelems);
foreach(cell, list)
v->array.elems[i++] = (JsQueryParseItem*)lfirst(cell);
}
else
{
v->array.elems = NULL;
}
return v;
}
static JsQueryParseItem*
makeItemBinary(int type, JsQueryParseItem* la, JsQueryParseItem *ra)
{
JsQueryParseItem *v = makeItemType(type);
v->args.left = la;
v->args.right = ra;
return v;
}
static JsQueryParseItem*
makeItemUnary(int type, JsQueryParseItem* a)
{
JsQueryParseItem *v = makeItemType(type);
v->arg = a;
return v;
}
static JsQueryParseItem*
makeItemIs(int isType)
{
JsQueryParseItem *v = makeItemType(jqiIs);
v->isType = isType;
return v;
}
static JsQueryParseItem*
makeItemList(List *list) {
JsQueryParseItem *head, *end;
ListCell *cell;
head = end = (JsQueryParseItem*)linitial(list);
while(end->next)
end = end->next;
foreach(cell, list)
{
JsQueryParseItem *c = (JsQueryParseItem*)lfirst(cell);
if (c == head)
continue;
end->next = c;
end = c;
while(end->next)
end = end->next;
}
return head;
}
%}
/* BISON Declarations */
%pure-parser
%expect 0
%name-prefix="jsquery_yy"
%error-verbose
%parse-param {JsQueryParseItem **result}
%union {
string str;
List *elems; /* list of JsQueryParseItem */
JsQueryParseItem *value;
JsQueryHint hint;
}
%token <str> IN_P IS_P OR_P AND_P NOT_P NULL_P TRUE_P
ARRAY_T FALSE_P NUMERIC_T OBJECT_T
STRING_T BOOLEAN_T
%token <str> STRING_P NUMERIC_P INT_P
%type <value> result scalar_value
%type <elems> path value_list
%type <value> key key_any right_expr expr array numeric
%token <hint> HINT_P
%left OR_P
%left AND_P
%right NOT_P
%nonassoc IN_P IS_P
%nonassoc '(' ')'
/* Grammar follows */
%%
result:
expr {
*result = $1;
(void) yynerrs; /* suppress compiler warning */
}
| /* EMPTY */ { *result = NULL; }
;
array:
'[' value_list ']' { $$ = makeItemArray($2); }
;
scalar_value:
STRING_P { $$ = makeItemString(&$1); }
| IN_P { $$ = makeItemString(&$1); }
| IS_P { $$ = makeItemString(&$1); }
| OR_P { $$ = makeItemString(&$1); }
| AND_P { $$ = makeItemString(&$1); }
| NOT_P { $$ = makeItemString(&$1); }
| NULL_P { $$ = makeItemString(NULL); }
| TRUE_P { $$ = makeItemBool(true); }
| ARRAY_T { $$ = makeItemString(&$1); }
| FALSE_P { $$ = makeItemBool(false); }
| NUMERIC_T { $$ = makeItemString(&$1); }
| OBJECT_T { $$ = makeItemString(&$1); }
| STRING_T { $$ = makeItemString(&$1); }
| BOOLEAN_T { $$ = makeItemString(&$1); }
| NUMERIC_P { $$ = makeItemNumeric(&$1); }
| INT_P { $$ = makeItemNumeric(&$1); }
;
value_list:
scalar_value { $$ = lappend(NIL, $1); }
| value_list ',' scalar_value { $$ = lappend($1, $3); }
;
numeric:
NUMERIC_P { $$ = makeItemNumeric(&$1); }
| INT_P { $$ = makeItemNumeric(&$1); }
;
right_expr:
'=' scalar_value { $$ = makeItemUnary(jqiEqual, $2); }
| IN_P '(' value_list ')' { $$ = makeItemUnary(jqiIn, makeItemArray($3)); }
| '=' array { $$ = makeItemUnary(jqiEqual, $2); }
| '=' '*' { $$ = makeItemUnary(jqiEqual, makeItemType(jqiAny)); }
| '<' numeric { $$ = makeItemUnary(jqiLess, $2); }
| '>' numeric { $$ = makeItemUnary(jqiGreater, $2); }
| '<' '=' numeric { $$ = makeItemUnary(jqiLessOrEqual, $3); }
| '>' '=' numeric { $$ = makeItemUnary(jqiGreaterOrEqual, $3); }
| '@' '>' array { $$ = makeItemUnary(jqiContains, $3); }
| '<' '@' array { $$ = makeItemUnary(jqiContained, $3); }
| '&' '&' array { $$ = makeItemUnary(jqiOverlap, $3); }
| IS_P ARRAY_T { $$ = makeItemIs(jbvArray); }
| IS_P NUMERIC_T { $$ = makeItemIs(jbvNumeric); }
| IS_P OBJECT_T { $$ = makeItemIs(jbvObject); }
| IS_P STRING_T { $$ = makeItemIs(jbvString); }
| IS_P BOOLEAN_T { $$ = makeItemIs(jbvBool); }
;
expr:
path { $$ = makeItemList($1); }
| path right_expr { $$ = makeItemList(lappend($1, $2)); }
| path HINT_P right_expr { $3->hint = $2; $$ = makeItemList(lappend($1, $3)); }
| NOT_P expr { $$ = makeItemUnary(jqiNot, $2); }
/*
* In next two lines NOT_P is a path actually, not a an
* logical expression.
*/
| NOT_P HINT_P right_expr { $3->hint = $2; $$ = makeItemList(lappend(lappend(NIL, makeItemKey(&$1)), $3)); }
| NOT_P right_expr { $$ = makeItemList(lappend(lappend(NIL, makeItemKey(&$1)), $2)); }
| path '(' expr ')' { $$ = makeItemList(lappend($1, $3)); }
| '(' expr ')' { $$ = $2; }
| expr AND_P expr { $$ = makeItemBinary(jqiAnd, $1, $3); }
| expr OR_P expr { $$ = makeItemBinary(jqiOr, $1, $3); }
;
/*
* key is always a string, not a bool or numeric
*/
key:
'*' { $$ = makeItemType(jqiAny); }
| '#' { $$ = makeItemType(jqiAnyArray); }
| '%' { $$ = makeItemType(jqiAnyKey); }
| '*' ':' { $$ = makeItemType(jqiAll); }
| '#' ':' { $$ = makeItemType(jqiAllArray); }
| '%' ':' { $$ = makeItemType(jqiAllKey); }
| '$' { $$ = makeItemType(jqiCurrent); }
| '@' '#' { $$ = makeItemType(jqiLength); }
| '#' INT_P { $$ = makeIndexArray(&$2); }
| STRING_P { $$ = makeItemKey(&$1); }
| IN_P { $$ = makeItemKey(&$1); }
| IS_P { $$ = makeItemKey(&$1); }
| OR_P { $$ = makeItemKey(&$1); }
| AND_P { $$ = makeItemKey(&$1); }
| NULL_P { $$ = makeItemKey(&$1); }
| TRUE_P { $$ = makeItemKey(&$1); }
| ARRAY_T { $$ = makeItemKey(&$1); }
| FALSE_P { $$ = makeItemKey(&$1); }
| NUMERIC_T { $$ = makeItemKey(&$1); }
| OBJECT_T { $$ = makeItemKey(&$1); }
| STRING_T { $$ = makeItemKey(&$1); }
| BOOLEAN_T { $$ = makeItemKey(&$1); }
| NUMERIC_P { $$ = makeItemKey(&$1); }
| INT_P { $$ = makeItemKey(&$1); }
;
/*
* NOT keyword needs separate processing
*/
key_any:
key { $$ = $$; }
| '?' '(' expr ')' { $$ = makeItemUnary(jqiFilter, $3); }
| NOT_P { $$ = makeItemKey(&$1); }
;
path:
key { $$ = lappend(NIL, $1); }
| '?' '(' expr ')' { $$ = lappend(NIL, makeItemUnary(jqiFilter, $3)); }
| path '.' key_any { $$ = lappend($1, $3); }
| NOT_P '.' key_any { $$ = lappend(lappend(NIL, makeItemKey(&$1)), $3); }
;
%%
#include "jsquery_scan.c"
|