Commit 8b17298f authored by Alexander Korotkov's avatar Alexander Korotkov

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
parent d303122e
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* jsonpath_gram.y * jsonpath_gram.y
* Grammar definitions for jsonpath datatype * Grammar definitions for jsonpath datatype
* *
* Transforms tokenized jsonpath into tree of JsonPathParseItem structs.
*
* Copyright (c) 2019, PostgreSQL Global Development Group * Copyright (c) 2019, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
...@@ -37,15 +39,17 @@ int jsonpath_yylex(union YYSTYPE *yylval_param); ...@@ -37,15 +39,17 @@ int jsonpath_yylex(union YYSTYPE *yylval_param);
int jsonpath_yyparse(JsonPathParseResult **result); int jsonpath_yyparse(JsonPathParseResult **result);
void jsonpath_yyerror(JsonPathParseResult **result, const char *message); void jsonpath_yyerror(JsonPathParseResult **result, const char *message);
static JsonPathParseItem *makeItemType(int type); static JsonPathParseItem *makeItemType(JsonPathItemType type);
static JsonPathParseItem *makeItemString(JsonPathString *s); static JsonPathParseItem *makeItemString(JsonPathString *s);
static JsonPathParseItem *makeItemVariable(JsonPathString *s); static JsonPathParseItem *makeItemVariable(JsonPathString *s);
static JsonPathParseItem *makeItemKey(JsonPathString *s); static JsonPathParseItem *makeItemKey(JsonPathString *s);
static JsonPathParseItem *makeItemNumeric(JsonPathString *s); static JsonPathParseItem *makeItemNumeric(JsonPathString *s);
static JsonPathParseItem *makeItemBool(bool val); static JsonPathParseItem *makeItemBool(bool val);
static JsonPathParseItem *makeItemBinary(int type, JsonPathParseItem *la, static JsonPathParseItem *makeItemBinary(JsonPathItemType type,
JsonPathParseItem *la,
JsonPathParseItem *ra); JsonPathParseItem *ra);
static JsonPathParseItem *makeItemUnary(int type, JsonPathParseItem *a); static JsonPathParseItem *makeItemUnary(JsonPathItemType type,
JsonPathParseItem *a);
static JsonPathParseItem *makeItemList(List *list); static JsonPathParseItem *makeItemList(List *list);
static JsonPathParseItem *makeIndexArray(List *list); static JsonPathParseItem *makeIndexArray(List *list);
static JsonPathParseItem *makeAny(int first, int last); static JsonPathParseItem *makeAny(int first, int last);
...@@ -170,7 +174,8 @@ predicate: ...@@ -170,7 +174,8 @@ predicate:
| predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); } | predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); }
| predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); } | predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); }
| NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); } | NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); }
| '(' predicate ')' IS_P UNKNOWN_P { $$ = makeItemUnary(jpiIsUnknown, $2); } | '(' predicate ')' IS_P UNKNOWN_P
{ $$ = makeItemUnary(jpiIsUnknown, $2); }
| expr STARTS_P WITH_P starts_with_initial | expr STARTS_P WITH_P starts_with_initial
{ $$ = makeItemBinary(jpiStartsWith, $1, $4); } { $$ = makeItemBinary(jpiStartsWith, $1, $4); }
| expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); } | expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); }
...@@ -232,7 +237,8 @@ any_level: ...@@ -232,7 +237,8 @@ any_level:
any_path: any_path:
ANY_P { $$ = makeAny(0, -1); } ANY_P { $$ = makeAny(0, -1); }
| ANY_P '{' any_level '}' { $$ = makeAny($3, $3); } | ANY_P '{' any_level '}' { $$ = makeAny($3, $3); }
| ANY_P '{' any_level TO_P any_level '}' { $$ = makeAny($3, $5); } | ANY_P '{' any_level TO_P any_level '}'
{ $$ = makeAny($3, $5); }
; ;
accessor_op: accessor_op:
...@@ -285,10 +291,15 @@ method: ...@@ -285,10 +291,15 @@ method:
; ;
%% %%
static JsonPathParseItem* /*
makeItemType(int type) * The helper functions below allocate and fill JsonPathParseItem's of various
* types.
*/
static JsonPathParseItem *
makeItemType(JsonPathItemType type)
{ {
JsonPathParseItem* v = palloc(sizeof(*v)); JsonPathParseItem *v = palloc(sizeof(*v));
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
...@@ -298,7 +309,7 @@ makeItemType(int type) ...@@ -298,7 +309,7 @@ makeItemType(int type)
return v; return v;
} }
static JsonPathParseItem* static JsonPathParseItem *
makeItemString(JsonPathString *s) makeItemString(JsonPathString *s)
{ {
JsonPathParseItem *v; JsonPathParseItem *v;
...@@ -364,7 +375,7 @@ makeItemBool(bool val) ...@@ -364,7 +375,7 @@ makeItemBool(bool val)
} }
static JsonPathParseItem * static JsonPathParseItem *
makeItemBinary(int type, JsonPathParseItem* la, JsonPathParseItem *ra) makeItemBinary(JsonPathItemType type, JsonPathParseItem *la, JsonPathParseItem *ra)
{ {
JsonPathParseItem *v = makeItemType(type); JsonPathParseItem *v = makeItemType(type);
...@@ -375,7 +386,7 @@ makeItemBinary(int type, JsonPathParseItem* la, JsonPathParseItem *ra) ...@@ -375,7 +386,7 @@ makeItemBinary(int type, JsonPathParseItem* la, JsonPathParseItem *ra)
} }
static JsonPathParseItem * static JsonPathParseItem *
makeItemUnary(int type, JsonPathParseItem* a) makeItemUnary(JsonPathItemType type, JsonPathParseItem *a)
{ {
JsonPathParseItem *v; JsonPathParseItem *v;
...@@ -401,7 +412,8 @@ makeItemUnary(int type, JsonPathParseItem* a) ...@@ -401,7 +412,8 @@ makeItemUnary(int type, JsonPathParseItem* a)
static JsonPathParseItem * static JsonPathParseItem *
makeItemList(List *list) makeItemList(List *list)
{ {
JsonPathParseItem *head, *end; JsonPathParseItem *head,
*end;
ListCell *cell = list_head(list); ListCell *cell = list_head(list);
head = end = (JsonPathParseItem *) lfirst(cell); head = end = (JsonPathParseItem *) lfirst(cell);
...@@ -510,4 +522,12 @@ makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern, ...@@ -510,4 +522,12 @@ makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
return v; return v;
} }
/*
* jsonpath_scan.l is compiled as part of jsonpath_gram.y. Currently, this is
* unavoidable because jsonpath_gram does not create a .h file to export its
* token symbols. If these files ever grow large enough to be worth compiling
* separately, that could be fixed; but for now it seems like useless
* complication.
*/
#include "jsonpath_scan.c" #include "jsonpath_scan.c"
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment