Skip to content

Commit 2c8ebbe

Browse files
authored
feat(grammar): Finalize expression syntax (#55)
For #17
1 parent 306348b commit 2c8ebbe

File tree

6 files changed

+83618
-78395
lines changed

6 files changed

+83618
-78395
lines changed

grammar.js

Lines changed: 73 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ module.exports = grammar({
214214
),
215215

216216
from_arguments: $ => seq(
217-
'(', list_of_trailing($.named_expression), ')',
217+
'(', list_of_trailing($.named_argument), ')',
218218
),
219219

220220
cardinality_and_filter: $ => seq(
@@ -231,15 +231,15 @@ module.exports = grammar({
231231
$.binary_condition,
232232
$._expression,
233233
$.predicate_condition,
234-
$.ternary_operator,
234+
$.ternary,
235235
),
236236

237-
ternary_operator: $ => seq(
237+
ternary: $ => seq(
238238
$._condition,
239239
'?',
240-
$._expression,
240+
field('then', $._expression),
241241
':',
242-
$._expression,
242+
field('else', $._expression),
243243
),
244244

245245
binary_condition: $ => choice(
@@ -286,15 +286,15 @@ module.exports = grammar({
286286
optional(kw('not')),
287287
choice(
288288
seq(kw('in'), $._expression),
289-
seq(kw('between'), $._expression, kw('and'), $._expression),
289+
seq(kw('between'), field('left', $._expression), kw('and'), field('right', $._expression)),
290290
seq(kw('like'), $._expression, optional(seq(kw('escape'), $._expression))),
291291
),
292292
),
293293

294294
_expression: $ => choice(
295295
$._literal,
296296
$.value_path,
297-
$.special_function,
297+
$._special_function,
298298
$.binary_expression,
299299
$.unary_expression,
300300
$.new_expression,
@@ -337,36 +337,21 @@ module.exports = grammar({
337337

338338
case_expression: $ => seq(
339339
kw('case'),
340-
choice(
341-
seq(
342-
$._expression,
343-
repeat1(
344-
seq(
345-
kw('when'),
346-
$._expression,
347-
kw('then'),
348-
$._expression,
349-
),
350-
),
351-
),
352-
seq(
353-
repeat1(
354-
seq(
355-
kw('when'),
356-
$._condition,
357-
kw('then'),
358-
$._expression,
359-
),
340+
seq(
341+
optional($._expression),
342+
repeat1(
343+
seq(
344+
kw('when'),
345+
$._expression,
346+
kw('then'),
347+
$._expression,
360348
),
361349
),
362350
),
363-
optional(
364-
seq(
365-
kw('else'),
366-
$._expression),
367-
),
351+
optional(seq(kw('else'), $._expression)),
368352
kw('end'),
369353
),
354+
370355
parameter_ref_expression: $ => seq(':', choice($.value_path, $.number)),
371356
parenthesized_expression: $ => seq(
372357
'(',
@@ -414,65 +399,67 @@ module.exports = grammar({
414399

415400
window_frame_clause: $ => seq(
416401
kw('rows'),
417-
$.window_frame_extent_spec,
418-
),
419-
420-
window_frame_extent_spec: $ => choice(
421-
$.window_frame_start_spec,
422-
seq(
423-
kw('between'),
424-
$.widow_frame_bound_spec,
425-
kw('and'),
426-
$.widow_frame_bound_spec,
402+
choice(
403+
$.window_frame_start,
404+
seq(
405+
kw('between'),
406+
field('left', $.window_frame_start),
407+
kw('and'),
408+
field('right', $.window_frame_end),
409+
),
427410
),
428411
),
429412

430-
window_frame_start_spec: $ => choice(
431-
seq(kw('unbounded'), kw('preceding')),
432-
seq($.number, kw('preceding')),
433-
seq(kw('current'), kw('row')),
434-
),
435413

436-
widow_frame_bound_spec: $ => choice(
437-
seq(kw('unbounded'), kw('following')),
438-
seq($.number, kw('following')),
439-
$.window_frame_start_spec,
414+
// *_start should only allow preceding, *_end only following, for "unbounded", but
415+
// for simplicity, we allow both.
416+
window_frame_start: $ => $._window_frame_start_end,
417+
window_frame_end: $ => $._window_frame_start_end,
418+
_window_frame_start_end: $ => choice(
419+
seq(kw('unbounded'), choice(kw('following'), kw('preceding'))),
420+
seq(field('offset', $.number), choice(kw('following'), kw('preceding'))),
421+
seq(kw('current'), kw('row')),
440422
),
441423

442-
special_function: $ => choice(
443-
seq(
444-
kw('trim'),
445-
'(',
446-
choice(
447-
seq(
448-
choice(kw('leading'), kw('trailing'), kw('both')),
449-
optional($._expression),
450-
kw('from'),
451-
$._expression,
452-
),
453-
seq(
454-
$._expression,
455-
optional(seq(kw('from'), $._expression)),
456-
),
424+
function_trim: $ => seq(
425+
kw('trim'),
426+
'(',
427+
choice(
428+
seq(
429+
choice(kw('leading'), kw('trailing'), kw('both')),
430+
optional($._expression),
431+
kw('from'),
432+
$._expression,
433+
),
434+
seq(
435+
$._expression,
436+
optional(seq(kw('from'), $._expression)),
457437
),
458-
')',
459-
),
460-
seq(
461-
kw('extract'),
462-
'(',
463-
choice(kw('year'), kw('month'), kw('day'), kw('hour'), kw('minute'), kw('second')),
464-
kw('from'),
465-
$._expression,
466-
')',
467-
),
468-
seq(
469-
kw('cast'),
470-
'(',
471-
$._expression,
472-
kw('as'),
473-
$.type_reference,
474-
')',
475438
),
439+
')',
440+
),
441+
function_extract: $ => seq(
442+
kw('extract'),
443+
'(',
444+
choice(kw('year'), kw('month'), kw('day'), kw('hour'), kw('minute'), kw('second')),
445+
kw('from'),
446+
$._expression,
447+
')',
448+
),
449+
function_cast: $ => seq(
450+
kw('cast'),
451+
'(',
452+
$._expression,
453+
kw('as'),
454+
$.type_reference,
455+
')',
456+
),
457+
458+
// We have a few special functions that are handled in syntax.
459+
_special_function: $ => choice(
460+
$.function_trim,
461+
$.function_extract,
462+
$.function_cast,
476463
),
477464

478465
value_path: $ => list_of(
@@ -484,31 +471,26 @@ module.exports = grammar({
484471
'.',
485472
),
486473

487-
value_p_helper: $ => seq(
488-
),
489-
490474
path_arguments: $ => seq(
491475
'(',
492476
optional(choice(
493-
list_of_trailing($.named_expression),
477+
list_of_trailing($.named_argument),
494478
list_of_trailing($.arrowed_expression),
495-
list_of_trailing($.func_expression),
479+
list_of_trailing($._expression),
496480
seq(kw('all'), $._expression),
497481
list_of(seq(kw('distinct'), $._expression)),
498482
'*',
499483
)),
500484
')',
501485
),
502486

503-
named_expression: $ => seq(
487+
named_argument: $ => seq(
504488
$.identifier,
505489
':',
506490
$._expression,
507491
),
508492

509493
arrowed_expression: $ => seq($.identifier, '=>', $._expression),
510-
func_expression: $ => $._expression,
511-
512494

513495
element_definitions: $ => seq(
514496
'{',

0 commit comments

Comments
 (0)