Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b17298

Browse files
committed
Cosmetic changes for jsonpath_gram.y and jsonpath_scan.l
This commit include formatting improvements, renamings and comments. Also, it makes jsonpath_scan.l be more uniform with other our lexers. Firstly, states names are renamed to more short alternatives. Secondly, <INITIAL> prefix removed from the rules. Corresponding rules are moved to the tail, so they would anyway work only in initial state. Author: Alexander Korotkov Reviewed-by: John Naylor
1 parent d303122 commit 8b17298

File tree

2 files changed

+223
-189
lines changed

2 files changed

+223
-189
lines changed

src/backend/utils/adt/jsonpath_gram.y

+50-30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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);
3739
int jsonpath_yyparse(JsonPathParseResult **result);
3840
void jsonpath_yyerror(JsonPathParseResult **result, const char *message);
3941

40-
static JsonPathParseItem *makeItemType(int type);
42+
static JsonPathParseItem *makeItemType(JsonPathItemType type);
4143
static JsonPathParseItem *makeItemString(JsonPathString *s);
4244
static JsonPathParseItem *makeItemVariable(JsonPathString *s);
4345
static JsonPathParseItem *makeItemKey(JsonPathString *s);
4446
static JsonPathParseItem *makeItemNumeric(JsonPathString *s);
4547
static 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);
4953
static JsonPathParseItem *makeItemList(List *list);
5054
static JsonPathParseItem *makeIndexArray(List *list);
5155
static 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

162166
delimited_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:
232237
any_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

238244
accessor_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 *
302313
makeItemString(JsonPathString *s)
303314
{
304-
JsonPathParseItem *v;
315+
JsonPathParseItem *v;
305316

306317
if (s == NULL)
307318
{
@@ -320,7 +331,7 @@ makeItemString(JsonPathString *s)
320331
static JsonPathParseItem *
321332
makeItemVariable(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)
332343
static JsonPathParseItem *
333344
makeItemKey(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)
343354
static JsonPathParseItem *
344355
makeItemNumeric(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)
356367
static JsonPathParseItem *
357368
makeItemBool(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

366377
static 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

377388
static 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)
401412
static JsonPathParseItem *
402413
makeItemList(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)
427439
static JsonPathParseItem *
428440
makeIndexArray(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)
453465
static JsonPathParseItem *
454466
makeAny(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 *
465477
makeItemLikeRegex(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

Comments
 (0)