Skip to content

Commit bf90a72

Browse files
committed
finalize grammar rework
1 parent fb39796 commit bf90a72

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

expected/jsquery.out

+10-8
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,17 @@ select 'in < 1'::jsquery;
410410
(1 row)
411411

412412
select 'not is < 1'::jsquery;
413-
ERROR: bad jsquery representation
414-
LINE 1: select 'not is < 1'::jsquery;
415-
^
416-
DETAIL: syntax error, unexpected '<' at or near "<"
413+
jsquery
414+
----------------
415+
NOT ("is" < 1)
416+
(1 row)
417+
417418
select 'not in < 1'::jsquery;
418-
ERROR: bad jsquery representation
419-
LINE 1: select 'not in < 1'::jsquery;
420-
^
421-
DETAIL: syntax error, unexpected '<', expecting '(' at or near "<"
419+
jsquery
420+
----------------
421+
NOT ("in" < 1)
422+
(1 row)
423+
422424
select 'in in (1,2)'::jsquery;
423425
jsquery
424426
----------------

jsquery_gram.y

+33-36
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,18 @@ makeItemList(List *list) {
216216
%token <str> STRING_P NUMERIC_P
217217

218218
%type <value> result scalar_value
219-
%type <str> key
220219

221220
%type <elems> path value_list
222221

223-
%type <value> path_elem path_elem_any right_expr expr array
222+
%type <value> key key_any right_expr expr array
224223

225224
%token <hint> HINT_P
226225

227-
%type <hint> opt_hint
228-
229226
%left OR_P
230227
%left AND_P
231228
%right NOT_P
232229
%nonassoc IN_P IS_P
233-
%nonassoc XXX
230+
%nonassoc '(' ')'
234231

235232
/* Grammar follows */
236233
%%
@@ -262,11 +259,6 @@ scalar_value:
262259
| NUMERIC_P { $$ = makeItemNumeric(&$1); }
263260
;
264261

265-
opt_hint:
266-
HINT_P { $$ = $1; }
267-
| /* EMPTY */ %prec XXX { $$ = jsqIndexDefault; }
268-
;
269-
270262
value_list:
271263
scalar_value { $$ = lappend(NIL, $1); }
272264
| value_list ',' scalar_value { $$ = lappend($1, $3); }
@@ -292,11 +284,17 @@ right_expr:
292284
;
293285

294286
expr:
295-
path opt_hint right_expr { $3->hint = $2; $$ = makeItemList(lappend($1, $3)); }
287+
path right_expr { $$ = makeItemList(lappend($1, $2)); }
288+
| path HINT_P right_expr { $3->hint = $2; $$ = makeItemList(lappend($1, $3)); }
289+
| NOT_P expr { $$ = makeItemUnary(jqiNot, $2); }
290+
/*
291+
* In next two lines NOT_P is a patch actually, not a an
292+
* logical expression.
293+
*/
294+
| NOT_P HINT_P right_expr { $3->hint = $2; $$ = makeItemList(lappend(lappend(NIL, makeItemKey(&$1)), $3)); }
295+
| NOT_P right_expr { $$ = makeItemList(lappend(lappend(NIL, makeItemKey(&$1)), $2)); }
296296
| path '(' expr ')' { $$ = makeItemList(lappend($1, $3)); }
297297
| '(' expr ')' { $$ = $2; }
298-
| NOT_P expr { $$ = makeItemUnary(jqiNot, $2); }
299-
| NOT_P opt_hint right_expr { $3->hint = $2; $$ = makeItemList(lappend(lappend(NIL, makeItemKey(&$1)), $3)); }
300298
| expr AND_P expr { $$ = makeItemBinary(jqiAnd, $1, $3); }
301299
| expr OR_P expr { $$ = makeItemBinary(jqiOr, $1, $3); }
302300
;
@@ -305,39 +303,38 @@ expr:
305303
* key is always a string, not a bool or numeric
306304
*/
307305
key:
308-
STRING_P { $$ = $1; }
309-
| IN_P { $$ = $1; }
310-
| IS_P { $$ = $1; }
311-
| OR_P { $$ = $1; }
312-
| AND_P { $$ = $1; }
313-
| NULL_P { $$ = $1; }
314-
| TRUE_P { $$ = $1; }
315-
| ARRAY_T { $$ = $1; }
316-
| FALSE_P { $$ = $1; }
317-
| NUMERIC_T { $$ = $1; }
318-
| OBJECT_T { $$ = $1; }
319-
| STRING_T { $$ = $1; }
320-
| BOOLEAN_T { $$ = $1; }
321-
| NUMERIC_P { $$ = $1; }
322-
;
323-
324-
path_elem:
325306
'*' { $$ = makeItemType(jqiAny); }
326307
| '#' { $$ = makeItemType(jqiAnyArray); }
327308
| '%' { $$ = makeItemType(jqiAnyKey); }
328309
| '$' { $$ = makeItemType(jqiCurrent); }
329-
| key { $$ = makeItemKey(&$1); }
310+
| STRING_P { $$ = makeItemKey(&$1); }
311+
| IN_P { $$ = makeItemKey(&$1); }
312+
| IS_P { $$ = makeItemKey(&$1); }
313+
| OR_P { $$ = makeItemKey(&$1); }
314+
| AND_P { $$ = makeItemKey(&$1); }
315+
| NULL_P { $$ = makeItemKey(&$1); }
316+
| TRUE_P { $$ = makeItemKey(&$1); }
317+
| ARRAY_T { $$ = makeItemKey(&$1); }
318+
| FALSE_P { $$ = makeItemKey(&$1); }
319+
| NUMERIC_T { $$ = makeItemKey(&$1); }
320+
| OBJECT_T { $$ = makeItemKey(&$1); }
321+
| STRING_T { $$ = makeItemKey(&$1); }
322+
| BOOLEAN_T { $$ = makeItemKey(&$1); }
323+
| NUMERIC_P { $$ = makeItemKey(&$1); }
330324
;
331325

332-
path_elem_any:
333-
path_elem { $$ = $$; }
326+
/*
327+
* NOT keyword needs separate processing
328+
*/
329+
key_any:
330+
key { $$ = $$; }
334331
| NOT_P { $$ = makeItemKey(&$1); }
335332
;
336333

337334
path:
338-
path_elem { $$ = lappend(NIL, $1); }
339-
| path '.' path_elem_any { $$ = lappend($1, $3); }
340-
| NOT_P '.' path_elem_any { $$ = lappend(lappend(NIL, makeItemKey(&$1)), $3); }
335+
key { $$ = lappend(NIL, $1); }
336+
| path '.' key_any { $$ = lappend($1, $3); }
337+
| NOT_P '.' key_any { $$ = lappend(lappend(NIL, makeItemKey(&$1)), $3); }
341338
;
342339

343340
%%

0 commit comments

Comments
 (0)