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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/**
* Minimal ggsql grammar without external scanner
*
* Uses a simple regex to capture SQL portion as opaque text
*/
// Helper to create case-insensitive keyword patterns
function caseInsensitive(keyword) {
return new RegExp(
keyword
.split('')
.map(letter => `[${letter.toLowerCase()}${letter.toUpperCase()}]`)
.join('')
);
}
module.exports = grammar({
name: 'ggsql',
conflicts: $ => [
[$.sql_portion],
],
rules: {
// Main entry point - SQL followed by VISUALISE statements
query: $ => seq(
optional($.sql_portion),
repeat($.visualise_statement)
),
// SQL portion - multiple statements separated by semicolons
sql_portion: $ => choice(
// Multiple statements with semicolons
prec.right(seq(
$.sql_statement,
repeat1(seq(';', optional($.sql_statement))),
optional(';')
)),
// Single statement (no semicolon before VISUALISE)
$.sql_statement
),
// A single SQL statement - order matters! More specific first
sql_statement: $ => choice(
$.with_statement, // Check WITH first (can contain SELECT)
$.select_statement,
$.create_statement,
$.insert_statement,
$.update_statement,
$.delete_statement,
$.other_sql_statement // Fallback for other SQL
),
// SELECT statement
select_statement: $ => prec(2, seq(
caseInsensitive('SELECT'),
$.select_body
)),
select_body: $ => prec.left(repeat1(choice(
$.from_clause,
$.window_function, // Window functions like ROW_NUMBER() OVER (...)
$.cast_expression, // CAST(expr AS type), TRY_CAST(expr AS type)
$.function_call, // Regular function calls like COUNT(), SUM()
$.sql_keyword,
$.string,
$.number,
',', '*', '.', '=', '<', '>', '!', '+', '-', '/', '%', '|', '&', '^', '~', '::',
$.subquery,
$.identifier
))),
// WITH statement (CTEs) - WITH must be followed by SELECT
with_statement: $ => prec.right(2, seq(
caseInsensitive('WITH'),
optional(caseInsensitive('RECURSIVE')),
$.cte_definition,
repeat(seq(',', $.cte_definition)),
optional($.select_statement) // WITH can optionally be followed by SELECT
)),
cte_definition: $ => seq(
$.identifier,
optional(seq( // Optional column list: df(x, y, id)
'(',
$.identifier,
repeat(seq(',', $.identifier)),
')'
)),
caseInsensitive('AS'),
'(',
choice(
$.with_statement, // Allow nested CTEs
$.select_statement,
$.subquery_body // VALUES (...) and other non-SELECT bodies
),
')'
),
// CREATE statement
create_statement: $ => prec.right(seq(
caseInsensitive('CREATE'),
repeat1(choice(
$.sql_keyword,
$.identifier,
$.string,
$.number,
$.subquery,
',', '(', ')', '*', '.', '=',
/[^\s;(),'"]+/
)),
optional($.select_statement)
)),
// INSERT statement
insert_statement: $ => prec.right(seq(
caseInsensitive('INSERT'),
repeat1(choice(
$.sql_keyword,
$.identifier,
$.string,
$.number,
$.subquery,
',', '(', ')', '*', '.', '=',
/[^\s;(),'"]+/
))
)),
// UPDATE statement
update_statement: $ => prec.right(seq(
caseInsensitive('UPDATE'),
repeat1(choice(
$.sql_keyword,
$.identifier,
$.string,
$.number,
$.subquery,
',', '(', ')', '*', '.', '=',
/[^\s;(),'"]+/
))
)),
// DELETE statement
delete_statement: $ => prec.right(seq(
caseInsensitive('DELETE'),
repeat1(choice(
$.sql_keyword,
$.identifier,
$.string,
$.number,
$.subquery,
',', '(', ')', '*', '.', '=',
/[^\s;(),'"]+/
))
)),
// Other SQL statements - DO NOT match if starts with keywords we handle
// explicitly (WITH, SELECT, CREATE, INSERT, UPDATE, DELETE, VISUALISE)
other_sql_statement: $ => {
const exclude_pattern = /[^\s;(),'"WwSsCcIiUuDdVv]+/;
return prec(-1, repeat1(choice(
$.sql_keyword,
token(exclude_pattern), // Tokens not starting with excluded letters
$.string,
$.number,
$.subquery,
',', '(', ')', '*', '.', '='
)));
},
// Subquery in parentheses - fully recursive, can contain any SQL
// Prioritizes WITH/SELECT statements, falls back to token-by-token parsing
subquery: $ => prec(1, seq(
'(',
choice(
$.with_statement,
$.select_statement,
$.subquery_body
),
')'
)),
// Scalar subquery for use inside expressions (e.g. function arguments)
// Matches (SELECT ...) or (WITH ... SELECT ...),
scalar_subquery: $ => prec(2, seq(
'(',
choice(
$.with_statement,
$.select_statement,
),
')'
)),
// Token-by-token fallback for any other subquery content
subquery_body: $ => repeat1(choice(
$.window_function,
$.cast_expression,
$.function_call,
$.sql_keyword,
$.string,
$.number,
$.identifier,
$.subquery,
',', '*', '.', '=', '<', '>', '!', '::',
token(/[^\s;(),'\"]+/)
)),
// CAST/TRY_CAST expression: CAST(expr AS type) or TRY_CAST(expr AS type)
// Higher precedence than function_call to win over treating CAST as a regular function
cast_expression: $ => prec(3, seq(
choice(caseInsensitive('CAST'), caseInsensitive('TRY_CAST')),
'(',
$.positional_arg,
caseInsensitive('AS'),
$.type_name,
')'
)),
// Type name for CAST expressions: DATE, VARCHAR, DECIMAL(10,2), etc.
type_name: $ => seq(
$.identifier,
optional(seq('(', $.number, optional(seq(',', $.number)), ')'))
),
// Function call with parentheses (can be empty like ROW_NUMBER())
// Used in window functions and general SQL
function_call: $ => prec(2, seq(
$.identifier,
'(',
optional($.function_args),
')'
)),
// Common SQL keywords (to help parser recognize structure)
sql_keyword: $ => choice(
caseInsensitive('FROM'),
caseInsensitive('WHERE'),
caseInsensitive('JOIN'),
caseInsensitive('LEFT'),
caseInsensitive('RIGHT'),
caseInsensitive('INNER'),
caseInsensitive('OUTER'),
caseInsensitive('LATERAL'),
caseInsensitive('CROSS'),
caseInsensitive('NATURAL'),
caseInsensitive('FULL'),
caseInsensitive('ON'),
caseInsensitive('AND'),
caseInsensitive('OR'),
caseInsensitive('NOT'),
caseInsensitive('IN'),
caseInsensitive('EXISTS'),
caseInsensitive('BETWEEN'),
caseInsensitive('LIKE'),
caseInsensitive('ORDER'),
caseInsensitive('GROUP'),
caseInsensitive('BY'),
caseInsensitive('HAVING'),
caseInsensitive('LIMIT'),
caseInsensitive('OFFSET'),
caseInsensitive('DISTINCT'),
caseInsensitive('ALL'),
caseInsensitive('ASC'),
caseInsensitive('DESC'),
caseInsensitive('INTO'),
caseInsensitive('VALUES'),
caseInsensitive('SET'),
caseInsensitive('TABLE'),
caseInsensitive('TEMP'),
caseInsensitive('TEMPORARY'),
caseInsensitive('VIEW'),
caseInsensitive('INDEX'),
caseInsensitive('DATABASE'),
caseInsensitive('SCHEMA'),
caseInsensitive('OVER'),
caseInsensitive('ROWS'),
caseInsensitive('RANGE'),
caseInsensitive('UNBOUNDED'),
caseInsensitive('PRECEDING'),
caseInsensitive('FOLLOWING'),
caseInsensitive('CURRENT'),
caseInsensitive('ROW'),
caseInsensitive('NULLS'),
caseInsensitive('FIRST'),
caseInsensitive('LAST')
),
// Window function: func() OVER (PARTITION BY ... ORDER BY ... frame)
// Higher precedence to match before generic function_call
window_function: $ => prec(4, seq(
field('function', $.identifier),
'(',
optional($.function_args),
')',
caseInsensitive('OVER'),
$.window_specification
)),
function_args: $ => seq(
$.function_arg,
repeat(seq(',', $.function_arg))
),
// Function argument: positional or named
function_arg: $ => choice(
$.named_arg,
$.positional_arg
),
named_arg: $ => seq(
field('name', $.identifier),
choice(':=', '=>'),
field('value', $.positional_arg)
),
// Positional argument: supports complex expressions including:
// - Simple values: identifier, number, string, *
// - Qualified names: table.column
// - Nested function calls: ROUND(AVG(x), 2)
// - Arithmetic expressions: quantity * price
// - Type casts: value::type
positional_arg: $ => prec.left(choice(
// Simple values
$.qualified_name, // Handles both simple identifiers and table.column
$.number,
$.string,
'*',
// CAST/TRY_CAST expression
$.cast_expression,
// Nested function call
$.function_call,
// Scalar subquery: (SELECT ...) or (WITH ... SELECT ...)
$.scalar_subquery,
// Arithmetic/comparison expression (binary operators)
seq($.positional_arg, choice('+', '-', '*', '/', '%', '||', '::', '<', '>', '<=', '>=', '=', '!=', '<>'), $.positional_arg),
// Parenthesized expression
seq('(', $.positional_arg, ')')
)),
// Namespaced identifier: matches "namespace:name" pattern
// Examples: ggsql:penguins, ggsql:airquality
namespaced_identifier: $ => {
const pattern = /[a-zA-Z_][a-zA-Z0-9_]*:[a-zA-Z_][a-zA-Z0-9_]*/;
return token(choice(
pattern,
seq('`', pattern, '`'),
seq('"', pattern, '"')
));
},
window_specification: $ => seq(
'(',
optional($.window_partition_clause),
optional($.window_order_clause),
optional($.frame_clause),
')'
),
window_partition_clause: $ => seq(
caseInsensitive('PARTITION'),
caseInsensitive('BY'),
$.identifier,
repeat(seq(',', $.identifier))
),
window_order_clause: $ => seq(
caseInsensitive('ORDER'),
caseInsensitive('BY'),
$.order_item,
repeat(seq(',', $.order_item))
),
order_item: $ => seq(
$.identifier,
optional(choice(caseInsensitive('ASC'), caseInsensitive('DESC'))),
optional(seq(caseInsensitive('NULLS'), choice(caseInsensitive('FIRST'), caseInsensitive('LAST'))))
),
frame_clause: $ => seq(
choice(caseInsensitive('ROWS'), caseInsensitive('RANGE')),
choice(
seq(caseInsensitive('BETWEEN'), $.frame_bound, caseInsensitive('AND'), $.frame_bound),
$.frame_bound
)
),
frame_bound: $ => choice(
seq(caseInsensitive('UNBOUNDED'), choice(caseInsensitive('PRECEDING'), caseInsensitive('FOLLOWING'))),
seq(caseInsensitive('CURRENT'), caseInsensitive('ROW')),
seq($.number, choice(caseInsensitive('PRECEDING'), caseInsensitive('FOLLOWING')))
),
// Dotted identifier (for catalog.schema.table)
qualified_name: $ => prec.right(seq(
$.identifier,
repeat(seq('.', $.identifier))
)),
table_ref: $ => prec.right(seq(
choice(
field('table', choice($.qualified_name, $.string, $.namespaced_identifier)),
$.subquery,
),
optional(seq(
optional(caseInsensitive('AS')),
field('alias', $.identifier)
))
)),
from_clause: $ => prec.right(1, seq(
caseInsensitive('FROM'),
$.table_ref,
repeat(seq(',', $.table_ref))
)),
// VISUALISE/VISUALIZE [global_mapping] [FROM source] with clauses
// Global mapping sets default aesthetics for all layers
// FROM source can be an identifier (table/CTE) or string (file path)
visualise_statement: $ => prec.dynamic(1, seq(
$.visualise_keyword,
optional($.global_mapping),
optional($.from_clause),
repeat($.viz_clause)
)),
// VISUALISE keyword as explicit high-precedence token
visualise_keyword: $ => token(prec(10, choice(
caseInsensitive("VISUALISE"),
caseInsensitive("VISUALIZE")
))),
// Shared mapping list: comma-separated mapping elements
// Used by both global (VISUALISE) and layer (MAPPING) mappings
mapping_list: $ => seq(
$.mapping_element,
repeat(seq(',', $.mapping_element))
),
// Mapping element: wildcard, explicit, or implicit
mapping_element: $ => choice(
$.wildcard_mapping, // *
$.explicit_mapping, // date AS x
$.implicit_mapping // x (becomes x AS x)
),
// Wildcard mapping: maps all columns to aesthetics with matching names
wildcard_mapping: $ => '*',
// Explicit mapping: value AS aesthetic (name)
explicit_mapping: $ => seq(
field('value', $.mapping_value),
caseInsensitive('AS'),
field('name', $.aesthetic_name)
),
// Implicit mapping: just an identifier (column name = aesthetic name)
implicit_mapping: $ => $.identifier,
// Global mapping after VISUALISE - uses shared mapping_list
global_mapping: $ => $.mapping_list,
// All the visualization clauses (same as current grammar)
viz_clause: $ => choice(
$.draw_clause,
$.scale_clause,
$.facet_clause,
$.project_clause,
$.label_clause,
$.theme_clause,
),
// DRAW clause - syntax: DRAW geom [MAPPING ...] [REMAPPING ...] [SETTING ...] [FILTER ...] [PARTITION BY ...] [ORDER BY ...]
draw_clause: $ => seq(
caseInsensitive('DRAW'),
$.geom_type,
optional($.mapping_clause),
optional($.remapping_clause),
optional($.setting_clause),
optional($.filter_clause),
optional($.partition_clause),
optional($.order_clause)
),
// REMAPPING clause: maps stat-computed columns to aesthetics
// Syntax: REMAPPING count AS y, sum AS size
// Reuses mapping_list for parsing - stat names are treated as column references
remapping_clause: $ => seq(
caseInsensitive('REMAPPING'),
$.mapping_list
),
geom_type: $ => choice(
'point', 'line', 'path', 'bar', 'area', 'tile', 'polygon', 'ribbon',
'histogram', 'density', 'smooth', 'boxplot', 'violin',
'text', 'label', 'segment', 'arrow', 'rule', 'linear', 'errorbar'
),
// MAPPING clause for aesthetic mappings: MAPPING col AS x, "blue" AS color [FROM source]
// Supports: MAPPING x AS x, y AS y FROM cte
// MAPPING FROM cte (inherits global mappings)
// MAPPING * (wildcard)
// MAPPING *, x AS color (wildcard with explicit)
// MAPPING x, y (implicit mappings)
// Requires at least one of: aesthetic mappings or FROM clause
mapping_clause: $ => seq(
caseInsensitive('MAPPING'),
choice(
// Option 1: Just FROM (inherit global mappings)
seq(
caseInsensitive('FROM'),
field('layer_source', choice($.qualified_name, $.string, $.namespaced_identifier))
),
// Option 2: Mapping list (uses shared structure), optionally followed by FROM
seq(
$.mapping_list,
optional(seq(
caseInsensitive('FROM'),
field('layer_source', choice($.qualified_name, $.string, $.namespaced_identifier))
))
)
)
),
mapping_value: $ => choice(
$.column_reference,
$.literal_value
),
// SETTING clause for parameters: SETTING opacity => 0.5, size => 3
setting_clause: $ => seq(
caseInsensitive('SETTING'),
$.parameter_assignment,
repeat(seq(',', $.parameter_assignment))
),
parameter_assignment: $ => seq(
field('name', $.parameter_name),
'=>',
field('value', $.parameter_value)
),
parameter_name: $ => $.identifier,
parameter_value: $ => choice(
$.string,
$.number,
$.boolean,
$.null_literal,
$.array
),
// PARTITION BY clause for grouping: PARTITION BY category, region
partition_clause: $ => seq(
caseInsensitive('PARTITION'),
caseInsensitive('BY'),
$.partition_columns
),
partition_columns: $ => seq(
$.identifier,
repeat(seq(',', $.identifier))
),
// FILTER clause for layer filtering: FILTER <raw SQL WHERE expression>
// The filter_expression captures any valid SQL WHERE clause verbatim
// and passes it to the database backend
filter_clause: $ => seq(
caseInsensitive('FILTER'),
$.filter_expression
),
// Raw SQL expression - captures everything that's valid in a WHERE clause
// Uses prec.right to greedily consume tokens until a clause keyword is hit
filter_expression: $ => prec.right(repeat1($.filter_token)),
// Individual tokens that can appear in a filter expression
// NOTE: This must NOT match PARTITION or ORDER as identifiers, since those
// keywords start subsequent clauses in draw_clause
filter_token: $ => choice(
// SQL keywords commonly used in WHERE clauses
caseInsensitive('AND'),
caseInsensitive('OR'),
caseInsensitive('NOT'),
caseInsensitive('IN'),
caseInsensitive('IS'),
caseInsensitive('NULL'),
caseInsensitive('LIKE'),
caseInsensitive('ILIKE'),
caseInsensitive('BETWEEN'),
caseInsensitive('EXISTS'),
caseInsensitive('ANY'),
caseInsensitive('ALL'),
caseInsensitive('CASE'),
caseInsensitive('WHEN'),
caseInsensitive('THEN'),
caseInsensitive('ELSE'),
caseInsensitive('END'),
caseInsensitive('CAST'),
caseInsensitive('AS'),
caseInsensitive('TRUE'),
caseInsensitive('FALSE'),
// Values and identifiers (lower precedence to allow keywords to take priority)
$.string,
$.number,
$.filter_identifier,
// Comparison operators (as explicit tokens)
token('='),
token('!='),
token('<>'),
token('<='),
token('>='),
token('<'),
token('>'),
// Regex operators (DuckDB/PostgreSQL)
token('~*'), // case-insensitive regex match
token('!~*'), // case-insensitive regex not match
token('!~'), // regex not match
token('~'), // regex match
// Arithmetic operators
token('+'),
token('-'),
token('*'),
token('/'),
token('%'),
token('||'),
// Type cast operator (PostgreSQL style)
token('::'),
// Parentheses for grouping
token('('),
token(')'),
token(','),
token('.')
),
// ORDER BY clause for layer sorting: ORDER BY date ASC, value DESC
order_clause: $ => seq(
caseInsensitive('ORDER'),
caseInsensitive('BY'),
$.order_expression
),
// Raw SQL ORDER BY expression - captures column names and sort directions
order_expression: $ => prec.right(repeat1($.order_token)),
// Individual tokens that can appear in an order expression
order_token: $ => choice(
$.identifier,
$.number,
caseInsensitive('ASC'),
caseInsensitive('DESC'),
caseInsensitive('NULLS'),
caseInsensitive('FIRST'),
caseInsensitive('LAST'),
',',
'.',
'(',
')'
),
// Aesthetic name: either a known aesthetic or any identifier (for custom PROJECT aesthetics)
// Known aesthetics are listed first for syntax highlighting priority
aesthetic_name: $ => choice(
// Position aesthetics (cartesian)
'x', 'y', 'xmin', 'xmax', 'ymin', 'ymax', 'xend', 'yend',
// Position aesthetics (polar)
'theta', 'radius', 'thetamin', 'thetamax', 'radiusmin', 'radiusmax',
'thetaend', 'radiusend',
// Aggregation aesthetic (for bar charts)
'weight',
// Color aesthetics
'color', 'colour', 'fill', 'stroke', 'opacity',
// Size and shape
'size', 'shape', 'linetype', 'linewidth', 'width', 'height',
// Text aesthetics
'label', 'family', 'fontface', 'hjust', 'vjust',
// Specialty aesthetics,
'coef', 'intercept',
// Facet aesthetics
'panel', 'row', 'column',
// Computed variables
'offset',
// Allow any identifier for custom PROJECT aesthetics (e.g., PROJECT a, b TO polar)
$.identifier
),
column_reference: $ => $.identifier,
literal_value: $ => choice(
$.string,
$.number,
$.boolean,
$.null_literal
),
// SCALE clause - SCALE [TYPE] aesthetic [FROM ...] [TO ...] [VIA ...] [SETTING ...] [RENAMING ...]
// Examples:
// SCALE DATE x
// SCALE CONTINUOUS y FROM [0, 100]
// SCALE DISCRETE color FROM ['A', 'B'] TO ['red', 'blue']
// SCALE color TO viridis
// SCALE x FROM [0, 100] SETTING breaks => '1 month'
// SCALE DISCRETE x RENAMING 'A' => 'Alpha', 'B' => 'Beta'
scale_clause: $ => seq(
caseInsensitive('SCALE'),
optional($.scale_type_identifier), // optional type before aesthetic
$.aesthetic_name,
optional($.scale_from_clause),
optional($.scale_to_clause),
optional($.scale_via_clause),
optional($.setting_clause), // reuse existing setting_clause from DRAW
optional($.scale_renaming_clause) // custom label mappings
),
// RENAMING clause for custom axis/legend labels
// Syntax: RENAMING 'A' => 'Alpha', 'B' => 'Beta', 'C' => NULL
scale_renaming_clause: $ => seq(
caseInsensitive('RENAMING'),
$.renaming_assignment,
repeat(seq(',', $.renaming_assignment))
),
renaming_assignment: $ => seq(
field('name', choice(
'*', // Wildcard for template
$.string,
$.number,
$.null_literal // NULL for renaming null values
)),
'=>',
field('value', choice($.string, $.null_literal)) // String label or NULL to suppress
),
// Scale types - describe the nature of the data
scale_type_identifier: $ => choice(
caseInsensitive('CONTINUOUS'), // continuous numeric data
caseInsensitive('DISCRETE'), // categorical/discrete data
caseInsensitive('BINNED'), // binned/bucketed data
caseInsensitive('ORDINAL'), // ordered categorical data with interpolated output
caseInsensitive('IDENTITY') // pass-through scale (data already in output format)
),
// FROM clause - input range specification
scale_from_clause: $ => seq(
caseInsensitive('FROM'),
$.array
),
// TO clause - output range (explicit array or named palette)
scale_to_clause: $ => seq(
caseInsensitive('TO'),
choice(
$.array, // ['red', 'blue'] - explicit values
$.identifier // viridis - named palette
)
),
// VIA clause - transformation method
scale_via_clause: $ => seq(
caseInsensitive('VIA'),
$.identifier
),
// FACET clause - FACET vars [BY vars] [SETTING ...]
// Single variable = wrap layout, BY clause = grid layout
facet_clause: $ => seq(
caseInsensitive('FACET'),
$.facet_vars,
optional(seq(
alias(caseInsensitive('BY'), $.facet_by),
$.facet_vars
)),
optional($.setting_clause) // Reuse from DRAW/SCALE
),
facet_by: $ => 'BY',
facet_vars: $ => seq(
$.identifier,
repeat(seq(',', $.identifier))
),
// PROJECT clause - PROJECT [aesthetics] TO coord_type [SETTING prop => value, ...]
// Examples:
// PROJECT TO cartesian (defaults to x, y)
// PROJECT x, y TO cartesian (explicit aesthetics)
// PROJECT a, b TO cartesian (custom aesthetic names)
// PROJECT TO polar (defaults to theta, radius)
// PROJECT theta, radius TO polar (explicit aesthetics)
// PROJECT TO cartesian SETTING clip => true
project_clause: $ => seq(
caseInsensitive('PROJECT'),
optional($.project_aesthetics),
caseInsensitive('TO'),
$.project_type,
optional(seq(caseInsensitive('SETTING'), $.project_properties))
),
// Optional list of positional aesthetic names for PROJECT clause
project_aesthetics: $ => seq(
$.identifier,
repeat(seq(',', $.identifier))
),
project_type: $ => $.identifier,
project_properties: $ => seq(
$.project_property,
repeat(seq(',', $.project_property))
),
project_property: $ => seq(
field('name', $.project_property_name),
'=>',
field('value', choice($.string, $.number, $.boolean, $.array, $.identifier))
),
project_property_name: $ => $.identifier,
// LABEL clause (repeatable)
label_clause: $ => seq(
caseInsensitive('LABEL'),
optional(seq(
$.label_assignment,
repeat(seq(',', $.label_assignment))
))
),
label_assignment: $ => seq(
field('name', $.label_type),
'=>',
field('value', $.string)
),
label_type: $ => $.identifier,
// THEME clause - THEME [name] [SETTING prop => value, ...]
theme_clause: $ => choice(
// Just theme name
seq(caseInsensitive('THEME'), $.theme_name),
// Theme name with properties
seq(
caseInsensitive('THEME'), $.theme_name, caseInsensitive('SETTING'),
$.theme_property,
repeat(seq(',', $.theme_property))
),
// Just properties (custom theme)
seq(
caseInsensitive('THEME'), caseInsensitive('SETTING'),
$.theme_property,
repeat(seq(',', $.theme_property))
)
),
theme_name: $ => $.identifier,
theme_property: $ => seq(
field('name', $.theme_property_name),
'=>',
field('value', choice($.string, $.number, $.boolean))
),
theme_property_name: $ => $.identifier,
// Basic tokens
bare_identifier: $ => token(/[a-zA-Z_][a-zA-Z0-9_]*/),
quoted_identifier: $ => token(choice(
seq('`', /[^`]+/, '`'),
seq('"', /[^"]+/, '"')
)),
identifier: $ => choice(
$.bare_identifier,
$.quoted_identifier
),
// Identifier for use in filter expressions - uses lower precedence so that
// keywords like PARTITION and ORDER can take priority and end the filter
filter_identifier: $ => token(prec(-1, /[a-zA-Z_][a-zA-Z0-9_]*/)),
number: $ => token(seq(
optional('-'),
choice(
/\d+/,
/\d+\.\d*/,
/\.\d+/
)
)),
string: $ => seq("'", repeat(choice(/[^'\\]/, /\\./)), "'"),
boolean: $ => choice('true', 'false'),
array: $ => seq(
'[',
optional(seq(
$.array_element,
repeat(seq(',', $.array_element))
)),
']'
),
array_element: $ => choice(
$.string,
$.number,
$.boolean,
$.null_literal
),
null_literal: $ => caseInsensitive('NULL'),
// Comments
comment: $ => choice(
seq('//', /.*/),
seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
seq('--', /.*/),
),
},
extras: $ => [
/\s+/, // Whitespace
$.comment, // Comments
],
word: $ => $.bare_identifier,
});