@willbooster/tree-sitter-sql 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/common/common.mjs +52 -0
- package/package.json +71 -4
- package/sqlite/grammar.js +1266 -0
- package/sqlite/package.json +4 -0
- package/sqlite/src/node-types.json +7639 -0
- package/tree-sitter-sqlite.wasm +0 -0
- package/tree-sitter.json +38 -0
|
@@ -0,0 +1,1266 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* oxlint-disable no-control-regex -- Tokens exclude NUL characters, which end the input of SQLite. */
|
|
3
|
+
|
|
4
|
+
/** @import {GrammarSymbols, RuleOrLiteral} from '@willbooster/web-tree-sitter/dsl' */
|
|
5
|
+
|
|
6
|
+
// The SQL dialect that SQLite's parser accepts in its default build. The rules follow SQLite's own grammar, src/parse.y,
|
|
7
|
+
// and its tokenizer, src/tokenize.c, and also reject what SQLite rejects while parsing, beyond those two files (e.g. an
|
|
8
|
+
// unknown join type, or ORDER BY on a SELECT before UNION).
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
blockComment,
|
|
12
|
+
commaSep,
|
|
13
|
+
commaSep1,
|
|
14
|
+
keyword,
|
|
15
|
+
keywordPattern,
|
|
16
|
+
keywords,
|
|
17
|
+
lineComment,
|
|
18
|
+
quoted,
|
|
19
|
+
} from '../common/common.mjs';
|
|
20
|
+
|
|
21
|
+
// The CLI injects the DSL before loading this grammar.
|
|
22
|
+
const { grammar, alias, choice, field, optional, prec, repeat, repeat1, reserved, seq, token } =
|
|
23
|
+
/** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
|
|
24
|
+
|
|
25
|
+
const PREC = {
|
|
26
|
+
OR: 1,
|
|
27
|
+
AND: 2,
|
|
28
|
+
NOT: 3,
|
|
29
|
+
EQUALITY: 4,
|
|
30
|
+
RELATIONAL: 5,
|
|
31
|
+
ESCAPE: 6,
|
|
32
|
+
BITWISE: 7,
|
|
33
|
+
ADDITIVE: 8,
|
|
34
|
+
MULTIPLICATIVE: 9,
|
|
35
|
+
CONCAT: 10,
|
|
36
|
+
COLLATE: 11,
|
|
37
|
+
UNARY: 12,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// The keywords that are never identifiers: those that parse.y does not let fall back to an identifier.
|
|
41
|
+
const RESERVED = [
|
|
42
|
+
'ADD',
|
|
43
|
+
'ALL',
|
|
44
|
+
'ALTER',
|
|
45
|
+
'AND',
|
|
46
|
+
'AS',
|
|
47
|
+
'AUTOINCREMENT',
|
|
48
|
+
'BETWEEN',
|
|
49
|
+
'CASE',
|
|
50
|
+
'CHECK',
|
|
51
|
+
'COLLATE',
|
|
52
|
+
'COMMIT',
|
|
53
|
+
'CONSTRAINT',
|
|
54
|
+
'CREATE',
|
|
55
|
+
'DEFAULT',
|
|
56
|
+
'DEFERRABLE',
|
|
57
|
+
'DELETE',
|
|
58
|
+
'DISTINCT',
|
|
59
|
+
'DROP',
|
|
60
|
+
'ELSE',
|
|
61
|
+
'ESCAPE',
|
|
62
|
+
'EXCEPT',
|
|
63
|
+
'EXISTS',
|
|
64
|
+
'FOREIGN',
|
|
65
|
+
'FROM',
|
|
66
|
+
'GROUP',
|
|
67
|
+
'HAVING',
|
|
68
|
+
'IN',
|
|
69
|
+
'INDEX',
|
|
70
|
+
'INSERT',
|
|
71
|
+
'INTERSECT',
|
|
72
|
+
'INTO',
|
|
73
|
+
'IS',
|
|
74
|
+
'ISNULL',
|
|
75
|
+
'JOIN',
|
|
76
|
+
'LIMIT',
|
|
77
|
+
'NOT',
|
|
78
|
+
'NOTHING',
|
|
79
|
+
'NOTNULL',
|
|
80
|
+
'NULL',
|
|
81
|
+
'ON',
|
|
82
|
+
'OR',
|
|
83
|
+
'ORDER',
|
|
84
|
+
'PRIMARY',
|
|
85
|
+
'REFERENCES',
|
|
86
|
+
'RETURNING',
|
|
87
|
+
'SELECT',
|
|
88
|
+
'SET',
|
|
89
|
+
'TABLE',
|
|
90
|
+
'THEN',
|
|
91
|
+
'TO',
|
|
92
|
+
'TRANSACTION',
|
|
93
|
+
'UNION',
|
|
94
|
+
'UNIQUE',
|
|
95
|
+
'UPDATE',
|
|
96
|
+
'USING',
|
|
97
|
+
'VALUES',
|
|
98
|
+
'WHEN',
|
|
99
|
+
'WHERE',
|
|
100
|
+
];
|
|
101
|
+
// Names (`nm` in parse.y) may also be these, but an identifier in a narrower position (`id` and `ids`) may not.
|
|
102
|
+
const JOIN_KEYWORDS = ['CROSS', 'FULL', 'INNER', 'LEFT', 'NATURAL', 'OUTER', 'RIGHT'];
|
|
103
|
+
|
|
104
|
+
// SQLite accepts any sequence of one to three join keywords whose combination is not contradictory
|
|
105
|
+
// (`sqlite3JoinType` in src/select.c): `INNER` or `CROSS` with an outer join, or `OUTER` without a side, is an error.
|
|
106
|
+
/** @type {string[][]} */
|
|
107
|
+
const JOIN_TYPES = [];
|
|
108
|
+
/** @param {string[]} words */
|
|
109
|
+
const isJoinType = (words) => {
|
|
110
|
+
const inner = words.some((word) => word === 'INNER' || word === 'CROSS');
|
|
111
|
+
const side = words.some((word) => word === 'LEFT' || word === 'RIGHT' || word === 'FULL');
|
|
112
|
+
return !(inner && (side || words.includes('OUTER'))) && !(words.includes('OUTER') && !side);
|
|
113
|
+
};
|
|
114
|
+
/** @param {string[]} words */
|
|
115
|
+
const addJoinTypes = (words) => {
|
|
116
|
+
if (words.length > 0 && isJoinType(words)) JOIN_TYPES.push(words);
|
|
117
|
+
if (words.length < 3) for (const word of JOIN_KEYWORDS) addJoinTypes([...words, word]);
|
|
118
|
+
};
|
|
119
|
+
addJoinTypes([]);
|
|
120
|
+
|
|
121
|
+
// SQLite's identifier characters: ASCII letters, digits, `_`, `$`, and every non-ASCII character. A byte order mark
|
|
122
|
+
// before an identifier is white space instead.
|
|
123
|
+
const IDENTIFIER_START = String.raw`A-Za-z_\u0080-\uFEFE\uFF00-\u{10FFFF}`;
|
|
124
|
+
const IDENTIFIER_PART = String.raw`A-Za-z0-9_$\u0080-\u{10FFFF}`;
|
|
125
|
+
|
|
126
|
+
// Digit separators (`_`) may appear only between two digits.
|
|
127
|
+
const DIGITS = '[0-9](_?[0-9])*';
|
|
128
|
+
const HEX_DIGITS = '[0-9a-fA-F](_?[0-9a-fA-F])*';
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {GrammarSymbols<string>} $
|
|
132
|
+
* @param {RuleOrLiteral} target
|
|
133
|
+
*/
|
|
134
|
+
const insertHead = ($, target) =>
|
|
135
|
+
seq(
|
|
136
|
+
choice(seq(keyword('INSERT'), optional($._or_conflict)), keyword('REPLACE')),
|
|
137
|
+
keyword('INTO'),
|
|
138
|
+
field('table', target),
|
|
139
|
+
optional($.column_list)
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {GrammarSymbols<string>} $
|
|
144
|
+
* @param {RuleOrLiteral} target
|
|
145
|
+
*/
|
|
146
|
+
const triggerUpdate = ($, target) =>
|
|
147
|
+
seq(
|
|
148
|
+
keyword('UPDATE'),
|
|
149
|
+
optional($._or_conflict),
|
|
150
|
+
field('table', target),
|
|
151
|
+
$.set_clause,
|
|
152
|
+
optional($.from_clause),
|
|
153
|
+
optional($.where_clause)
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {GrammarSymbols<string>} $
|
|
158
|
+
* @param {RuleOrLiteral} head
|
|
159
|
+
*/
|
|
160
|
+
const triggerInsert = ($, head) =>
|
|
161
|
+
seq(head, $.select_statement, repeat($.upsert_clause), optional(alias($._final_upsert_clause, $.upsert_clause)));
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {GrammarSymbols<string>} $
|
|
165
|
+
* @param {RuleOrLiteral} target
|
|
166
|
+
*/
|
|
167
|
+
const triggerDelete = ($, target) => seq(keywords('DELETE FROM'), field('table', target), optional($.where_clause));
|
|
168
|
+
|
|
169
|
+
export default grammar({
|
|
170
|
+
name: 'sqlite',
|
|
171
|
+
|
|
172
|
+
// A byte order mark is white space anywhere.
|
|
173
|
+
extras: ($) => [/[\t\n\f\r ][\t\n\v\f\r ]*/, new RegExp(String.raw`\uFEFF`), $.comment],
|
|
174
|
+
|
|
175
|
+
externals: ($) => [
|
|
176
|
+
$._module_argument_token,
|
|
177
|
+
$._illegal_token,
|
|
178
|
+
$._unreachable,
|
|
179
|
+
$.comment,
|
|
180
|
+
$.blob_literal,
|
|
181
|
+
$._over_before_window,
|
|
182
|
+
// WINDOW before a name and AS is the keyword wherever it appears (`analyzeWindowKeyword` in src/tokenize.c): the
|
|
183
|
+
// scanner reads it as this token where a WINDOW clause may start, and as an illegal token where a name may appear.
|
|
184
|
+
$._window_before_name,
|
|
185
|
+
],
|
|
186
|
+
|
|
187
|
+
word: ($) => $.identifier,
|
|
188
|
+
|
|
189
|
+
reserved: {
|
|
190
|
+
global: () => RESERVED.map(keywordPattern),
|
|
191
|
+
// `ids` in parse.y: the alias that follows an expression or a table without `AS`, a type name, or a collation.
|
|
192
|
+
ids: () => [...RESERVED, ...JOIN_KEYWORDS, 'INDEXED'].map(keywordPattern),
|
|
193
|
+
window_name: () => [...RESERVED, 'FILTER', 'INDEXED'].map(keywordPattern),
|
|
194
|
+
// `id` in parse.y: an identifier as a default value.
|
|
195
|
+
id: () => [...RESERVED, ...JOIN_KEYWORDS].map(keywordPattern),
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
supertypes: ($) => [$._statement],
|
|
199
|
+
|
|
200
|
+
conflicts: ($) => [
|
|
201
|
+
// `OVER`, `FILTER`, and `WINDOW` are keywords only when the tokens after them make them one (`analyzeOverKeyword`
|
|
202
|
+
// and its siblings in src/tokenize.c); otherwise they are an alias.
|
|
203
|
+
[$.function_call],
|
|
204
|
+
[$.result_column],
|
|
205
|
+
[$.table],
|
|
206
|
+
[$.table_function],
|
|
207
|
+
[$.subquery_table],
|
|
208
|
+
[$.join_table],
|
|
209
|
+
],
|
|
210
|
+
|
|
211
|
+
rules: {
|
|
212
|
+
source_file: ($) => seq(optional($._statement), repeat(seq(';', optional($._statement)))),
|
|
213
|
+
|
|
214
|
+
_statement: ($) => choice($.explain_statement, $._explainable_statement),
|
|
215
|
+
|
|
216
|
+
_explainable_statement: ($) =>
|
|
217
|
+
choice(
|
|
218
|
+
$.select_statement,
|
|
219
|
+
$.insert_statement,
|
|
220
|
+
$.update_statement,
|
|
221
|
+
$.delete_statement,
|
|
222
|
+
$.create_table_statement,
|
|
223
|
+
$.create_virtual_table_statement,
|
|
224
|
+
$.create_view_statement,
|
|
225
|
+
$.create_index_statement,
|
|
226
|
+
$.create_trigger_statement,
|
|
227
|
+
$.drop_statement,
|
|
228
|
+
$.alter_table_statement,
|
|
229
|
+
$.begin_statement,
|
|
230
|
+
$.commit_statement,
|
|
231
|
+
$.rollback_statement,
|
|
232
|
+
$.savepoint_statement,
|
|
233
|
+
$.release_statement,
|
|
234
|
+
$.pragma_statement,
|
|
235
|
+
$.vacuum_statement,
|
|
236
|
+
$.attach_statement,
|
|
237
|
+
$.detach_statement,
|
|
238
|
+
$.reindex_statement,
|
|
239
|
+
$.analyze_statement
|
|
240
|
+
),
|
|
241
|
+
|
|
242
|
+
explain_statement: ($) => seq(keyword('EXPLAIN'), optional(keywords('QUERY PLAN')), $._explainable_statement),
|
|
243
|
+
|
|
244
|
+
// Transactions
|
|
245
|
+
|
|
246
|
+
begin_statement: ($) =>
|
|
247
|
+
seq(
|
|
248
|
+
keyword('BEGIN'),
|
|
249
|
+
optional(choice(keyword('DEFERRED'), keyword('IMMEDIATE'), keyword('EXCLUSIVE'))),
|
|
250
|
+
optional($._transaction)
|
|
251
|
+
),
|
|
252
|
+
|
|
253
|
+
commit_statement: ($) => seq(choice(keyword('COMMIT'), keyword('END')), optional($._transaction)),
|
|
254
|
+
|
|
255
|
+
rollback_statement: ($) =>
|
|
256
|
+
seq(
|
|
257
|
+
keyword('ROLLBACK'),
|
|
258
|
+
optional($._transaction),
|
|
259
|
+
optional(seq(keyword('TO'), optional(keyword('SAVEPOINT')), field('savepoint', $._name)))
|
|
260
|
+
),
|
|
261
|
+
|
|
262
|
+
_transaction: ($) => seq(keyword('TRANSACTION'), optional(field('name', $._name))),
|
|
263
|
+
|
|
264
|
+
savepoint_statement: ($) => seq(keyword('SAVEPOINT'), field('name', $._name)),
|
|
265
|
+
|
|
266
|
+
release_statement: ($) => seq(keyword('RELEASE'), optional(keyword('SAVEPOINT')), field('name', $._name)),
|
|
267
|
+
|
|
268
|
+
// Schema statements
|
|
269
|
+
|
|
270
|
+
// A temporary table or view may name only the temp schema (`sqlite3StartTable` in src/build.c).
|
|
271
|
+
create_table_statement: ($) =>
|
|
272
|
+
seq(
|
|
273
|
+
keyword('CREATE'),
|
|
274
|
+
choice(
|
|
275
|
+
seq($.temporary, keyword('TABLE'), optional($.if_not_exists), field('name', $._temporary_object_name)),
|
|
276
|
+
seq(keyword('TABLE'), optional($.if_not_exists), field('name', $.qualified_name))
|
|
277
|
+
),
|
|
278
|
+
choice(
|
|
279
|
+
seq(
|
|
280
|
+
'(',
|
|
281
|
+
commaSep1($.column_definition),
|
|
282
|
+
optional(seq(',', $._table_constraint, repeat(seq(optional(','), $._table_constraint)))),
|
|
283
|
+
')',
|
|
284
|
+
commaSep($.table_option)
|
|
285
|
+
),
|
|
286
|
+
seq(keyword('AS'), $.select_statement)
|
|
287
|
+
)
|
|
288
|
+
),
|
|
289
|
+
|
|
290
|
+
temporary: () => choice(keyword('TEMP'), keyword('TEMPORARY')),
|
|
291
|
+
|
|
292
|
+
if_not_exists: () => keywords('IF NOT EXISTS'),
|
|
293
|
+
|
|
294
|
+
if_exists: () => keywords('IF EXISTS'),
|
|
295
|
+
|
|
296
|
+
table_option: () => choice(keywords('WITHOUT ROWID'), keyword('STRICT')),
|
|
297
|
+
|
|
298
|
+
// GENERATED after a column name or a type name without a size is a word of the type name, since SQLite's parser
|
|
299
|
+
// lets the keyword fall back to an identifier where it has no action of its own (`%fallback ID` in src/parse.y);
|
|
300
|
+
// a GENERATED ALWAYS constraint may follow a sized type or another constraint only.
|
|
301
|
+
column_definition: ($) =>
|
|
302
|
+
seq(
|
|
303
|
+
field('name', $._name),
|
|
304
|
+
choice(
|
|
305
|
+
seq(
|
|
306
|
+
optional(alias($._type_words, $.type_name)),
|
|
307
|
+
optional(seq($._leading_column_constraint, repeat($._column_constraint)))
|
|
308
|
+
),
|
|
309
|
+
seq(alias($._sized_type_name, $.type_name), repeat($._column_constraint))
|
|
310
|
+
)
|
|
311
|
+
),
|
|
312
|
+
|
|
313
|
+
type_name: ($) => choice($._type_words, $._sized_type_name),
|
|
314
|
+
|
|
315
|
+
_type_words: ($) => repeat1(choice($._narrow_name, alias(keyword('GENERATED'), $.identifier))),
|
|
316
|
+
|
|
317
|
+
_sized_type_name: ($) => seq($._type_words, '(', $._signed_number, optional(seq(',', $._signed_number)), ')'),
|
|
318
|
+
|
|
319
|
+
_leading_column_constraint: ($) =>
|
|
320
|
+
choice(
|
|
321
|
+
$.constraint_name,
|
|
322
|
+
$.default_constraint,
|
|
323
|
+
$.null_constraint,
|
|
324
|
+
$.not_null_constraint,
|
|
325
|
+
$.primary_key_constraint,
|
|
326
|
+
$.unique_constraint,
|
|
327
|
+
$.check_constraint,
|
|
328
|
+
$.foreign_key_clause,
|
|
329
|
+
$.deferrable_clause,
|
|
330
|
+
$.collate_clause,
|
|
331
|
+
alias($._generated_column_expression, $.generated_column_constraint)
|
|
332
|
+
),
|
|
333
|
+
|
|
334
|
+
_column_constraint: ($) =>
|
|
335
|
+
choice(
|
|
336
|
+
$.constraint_name,
|
|
337
|
+
$.default_constraint,
|
|
338
|
+
$.null_constraint,
|
|
339
|
+
$.not_null_constraint,
|
|
340
|
+
$.primary_key_constraint,
|
|
341
|
+
$.unique_constraint,
|
|
342
|
+
$.check_constraint,
|
|
343
|
+
$.foreign_key_clause,
|
|
344
|
+
$.deferrable_clause,
|
|
345
|
+
$.collate_clause,
|
|
346
|
+
$.generated_column_constraint
|
|
347
|
+
),
|
|
348
|
+
|
|
349
|
+
constraint_name: ($) => seq(keyword('CONSTRAINT'), field('name', $._name)),
|
|
350
|
+
|
|
351
|
+
default_constraint: ($) =>
|
|
352
|
+
seq(
|
|
353
|
+
keyword('DEFAULT'),
|
|
354
|
+
field(
|
|
355
|
+
'value',
|
|
356
|
+
choice(
|
|
357
|
+
$._term,
|
|
358
|
+
seq('(', $._expression, ')'),
|
|
359
|
+
alias($._signed_term, $.unary_expression),
|
|
360
|
+
reserved('id', $.identifier),
|
|
361
|
+
$.quoted_identifier
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
),
|
|
365
|
+
|
|
366
|
+
_signed_term: ($) => seq(field('operator', choice('+', '-')), field('operand', $._term)),
|
|
367
|
+
|
|
368
|
+
null_constraint: ($) => seq(keyword('NULL'), optional($.conflict_clause)),
|
|
369
|
+
|
|
370
|
+
not_null_constraint: ($) => seq(keywords('NOT NULL'), optional($.conflict_clause)),
|
|
371
|
+
|
|
372
|
+
// A descending primary key is no alias of the rowid, so it may not autoincrement (`sqlite3AddPrimaryKey` in
|
|
373
|
+
// src/build.c).
|
|
374
|
+
primary_key_constraint: ($) =>
|
|
375
|
+
seq(
|
|
376
|
+
keywords('PRIMARY KEY'),
|
|
377
|
+
choice(
|
|
378
|
+
seq(optional(keyword('ASC')), optional($.conflict_clause), optional(keyword('AUTOINCREMENT'))),
|
|
379
|
+
seq(keyword('DESC'), optional($.conflict_clause))
|
|
380
|
+
)
|
|
381
|
+
),
|
|
382
|
+
|
|
383
|
+
unique_constraint: ($) => seq(keyword('UNIQUE'), optional($.conflict_clause)),
|
|
384
|
+
|
|
385
|
+
check_constraint: ($) => seq(keyword('CHECK'), '(', $._expression, ')'),
|
|
386
|
+
|
|
387
|
+
collate_clause: ($) => seq(keyword('COLLATE'), field('collation', $._narrow_name)),
|
|
388
|
+
|
|
389
|
+
generated_column_constraint: ($) =>
|
|
390
|
+
choice(seq(keywords('GENERATED ALWAYS'), $._generated_column_expression), $._generated_column_expression),
|
|
391
|
+
|
|
392
|
+
// An identifier after the expression names how the column is stored, and GENERATED falls back to one there, so it
|
|
393
|
+
// starts no further constraint.
|
|
394
|
+
_generated_column_expression: ($) =>
|
|
395
|
+
prec.right(
|
|
396
|
+
seq(
|
|
397
|
+
keyword('AS'),
|
|
398
|
+
'(',
|
|
399
|
+
$._expression,
|
|
400
|
+
')',
|
|
401
|
+
optional(choice(keyword('STORED'), keyword('VIRTUAL'), $._generated_as_storage))
|
|
402
|
+
)
|
|
403
|
+
),
|
|
404
|
+
|
|
405
|
+
_generated_as_storage: ($) => seq(keyword('GENERATED'), $._unreachable),
|
|
406
|
+
|
|
407
|
+
conflict_clause: ($) => seq(keywords('ON CONFLICT'), $._conflict_resolution),
|
|
408
|
+
|
|
409
|
+
_conflict_resolution: () =>
|
|
410
|
+
choice(keyword('ROLLBACK'), keyword('ABORT'), keyword('FAIL'), keyword('IGNORE'), keyword('REPLACE')),
|
|
411
|
+
|
|
412
|
+
foreign_key_clause: ($) =>
|
|
413
|
+
seq(
|
|
414
|
+
keyword('REFERENCES'),
|
|
415
|
+
field('table', $._name),
|
|
416
|
+
optional($.column_list),
|
|
417
|
+
repeat(choice($.foreign_key_action, $.foreign_key_match))
|
|
418
|
+
),
|
|
419
|
+
|
|
420
|
+
foreign_key_action: () =>
|
|
421
|
+
seq(
|
|
422
|
+
keyword('ON'),
|
|
423
|
+
choice(keyword('DELETE'), keyword('UPDATE'), keyword('INSERT')),
|
|
424
|
+
choice(
|
|
425
|
+
keywords('SET NULL'),
|
|
426
|
+
keywords('SET DEFAULT'),
|
|
427
|
+
keyword('CASCADE'),
|
|
428
|
+
keyword('RESTRICT'),
|
|
429
|
+
keywords('NO ACTION')
|
|
430
|
+
)
|
|
431
|
+
),
|
|
432
|
+
|
|
433
|
+
foreign_key_match: ($) => seq(keyword('MATCH'), field('name', $._name)),
|
|
434
|
+
|
|
435
|
+
deferrable_clause: () =>
|
|
436
|
+
seq(
|
|
437
|
+
optional(keyword('NOT')),
|
|
438
|
+
keyword('DEFERRABLE'),
|
|
439
|
+
optional(seq(keyword('INITIALLY'), choice(keyword('DEFERRED'), keyword('IMMEDIATE'))))
|
|
440
|
+
),
|
|
441
|
+
|
|
442
|
+
_table_constraint: ($) =>
|
|
443
|
+
choice(
|
|
444
|
+
$.constraint_name,
|
|
445
|
+
alias($._table_primary_key_constraint, $.primary_key_constraint),
|
|
446
|
+
alias($._table_unique_constraint, $.unique_constraint),
|
|
447
|
+
alias($._table_check_constraint, $.check_constraint),
|
|
448
|
+
$.foreign_key_constraint
|
|
449
|
+
),
|
|
450
|
+
|
|
451
|
+
_table_primary_key_constraint: ($) =>
|
|
452
|
+
seq(
|
|
453
|
+
keywords('PRIMARY KEY'),
|
|
454
|
+
'(',
|
|
455
|
+
// Only a single column may autoincrement (`sqlite3AddPrimaryKey` in src/build.c).
|
|
456
|
+
choice(seq($.indexed_column, keyword('AUTOINCREMENT')), commaSep1($.indexed_column)),
|
|
457
|
+
')',
|
|
458
|
+
optional($.conflict_clause)
|
|
459
|
+
),
|
|
460
|
+
|
|
461
|
+
_table_unique_constraint: ($) =>
|
|
462
|
+
seq(keyword('UNIQUE'), '(', commaSep1($.indexed_column), ')', optional($.conflict_clause)),
|
|
463
|
+
|
|
464
|
+
_table_check_constraint: ($) => seq(keyword('CHECK'), '(', $._expression, ')', optional($.conflict_clause)),
|
|
465
|
+
|
|
466
|
+
foreign_key_constraint: ($) =>
|
|
467
|
+
seq(keywords('FOREIGN KEY'), $.column_list, $.foreign_key_clause, optional($.deferrable_clause)),
|
|
468
|
+
|
|
469
|
+
column_list: ($) => seq('(', commaSep1($._name), ')'),
|
|
470
|
+
|
|
471
|
+
create_virtual_table_statement: ($) =>
|
|
472
|
+
seq(
|
|
473
|
+
keywords('CREATE VIRTUAL TABLE'),
|
|
474
|
+
optional($.if_not_exists),
|
|
475
|
+
field('name', $.qualified_name),
|
|
476
|
+
keyword('USING'),
|
|
477
|
+
field('module', $._name),
|
|
478
|
+
optional(seq('(', optional($.module_argument), repeat(seq(',', optional($.module_argument))), ')'))
|
|
479
|
+
),
|
|
480
|
+
|
|
481
|
+
// A module receives the text of each argument and parses it itself.
|
|
482
|
+
module_argument: ($) => repeat1(choice($._module_argument_token, $._module_argument_group)),
|
|
483
|
+
|
|
484
|
+
_module_argument_group: ($) =>
|
|
485
|
+
seq('(', repeat(choice($._module_argument_token, ',', $._module_argument_group)), ')'),
|
|
486
|
+
|
|
487
|
+
create_view_statement: ($) =>
|
|
488
|
+
seq(
|
|
489
|
+
keyword('CREATE'),
|
|
490
|
+
choice(
|
|
491
|
+
seq($.temporary, keyword('VIEW'), optional($.if_not_exists), field('name', $._temporary_object_name)),
|
|
492
|
+
seq(keyword('VIEW'), optional($.if_not_exists), field('name', $.qualified_name))
|
|
493
|
+
),
|
|
494
|
+
optional($.column_list),
|
|
495
|
+
keyword('AS'),
|
|
496
|
+
$.select_statement
|
|
497
|
+
),
|
|
498
|
+
|
|
499
|
+
create_index_statement: ($) =>
|
|
500
|
+
seq(
|
|
501
|
+
keyword('CREATE'),
|
|
502
|
+
optional(keyword('UNIQUE')),
|
|
503
|
+
keyword('INDEX'),
|
|
504
|
+
optional($.if_not_exists),
|
|
505
|
+
field('name', $.qualified_name),
|
|
506
|
+
keyword('ON'),
|
|
507
|
+
field('table', $._name),
|
|
508
|
+
'(',
|
|
509
|
+
commaSep1($.indexed_column),
|
|
510
|
+
')',
|
|
511
|
+
optional($.where_clause)
|
|
512
|
+
),
|
|
513
|
+
|
|
514
|
+
// A statement in a trigger may name its target table with a schema only if the trigger belongs to the temp schema
|
|
515
|
+
// (`sqlite3TriggerStepSrc` in src/trigger.c): a temporary trigger, which may not have a qualified name itself
|
|
516
|
+
// (`sqlite3BeginTrigger`), or one whose name or table is in the temp schema. A table without a schema may be in it.
|
|
517
|
+
create_trigger_statement: ($) =>
|
|
518
|
+
seq(
|
|
519
|
+
keyword('CREATE'),
|
|
520
|
+
choice(
|
|
521
|
+
seq(
|
|
522
|
+
$.temporary,
|
|
523
|
+
keyword('TRIGGER'),
|
|
524
|
+
optional($.if_not_exists),
|
|
525
|
+
field('name', $._unqualified_temp_name),
|
|
526
|
+
$._trigger_event,
|
|
527
|
+
field('table', $.qualified_name),
|
|
528
|
+
$._trigger_body
|
|
529
|
+
),
|
|
530
|
+
seq(
|
|
531
|
+
keyword('TRIGGER'),
|
|
532
|
+
optional($.if_not_exists),
|
|
533
|
+
choice(
|
|
534
|
+
seq(
|
|
535
|
+
field('name', $._temp_qualified_name),
|
|
536
|
+
$._trigger_event,
|
|
537
|
+
field('table', $.qualified_name),
|
|
538
|
+
$._trigger_body
|
|
539
|
+
),
|
|
540
|
+
seq(
|
|
541
|
+
field('name', $._other_qualified_name),
|
|
542
|
+
$._trigger_event,
|
|
543
|
+
field('table', $.qualified_name),
|
|
544
|
+
$._restricted_trigger_body
|
|
545
|
+
),
|
|
546
|
+
seq(
|
|
547
|
+
field('name', $._unqualified_temp_name),
|
|
548
|
+
$._trigger_event,
|
|
549
|
+
choice(
|
|
550
|
+
seq(field('table', choice($._unqualified_temp_name, $._temp_qualified_name)), $._trigger_body),
|
|
551
|
+
seq(field('table', $._other_qualified_name), $._restricted_trigger_body)
|
|
552
|
+
)
|
|
553
|
+
)
|
|
554
|
+
)
|
|
555
|
+
)
|
|
556
|
+
)
|
|
557
|
+
),
|
|
558
|
+
|
|
559
|
+
_unqualified_name: ($) => alias($._name_only, $.qualified_name),
|
|
560
|
+
|
|
561
|
+
_temporary_object_name: ($) => choice($._unqualified_temp_name, $._temp_qualified_name),
|
|
562
|
+
|
|
563
|
+
// Where a name in the temp schema may start, `temp` is a keyword or a token of its own, which may still be a name.
|
|
564
|
+
_unqualified_temp_name: ($) => choice($._unqualified_name, alias($._temp_name_only, $.qualified_name)),
|
|
565
|
+
|
|
566
|
+
_name_only: ($) => field('name', $._name),
|
|
567
|
+
|
|
568
|
+
_temp_name_only: ($) => field('name', $._temp_schema),
|
|
569
|
+
|
|
570
|
+
// SQLite looks up a schema by its name without quotes, ignoring case (`sqlite3FindDb` in src/build.c).
|
|
571
|
+
_temp_schema: ($) =>
|
|
572
|
+
choice(
|
|
573
|
+
alias(keyword('TEMP'), $.identifier),
|
|
574
|
+
alias($._quoted_temp, $.quoted_identifier),
|
|
575
|
+
alias($._string_temp, $.string_literal)
|
|
576
|
+
),
|
|
577
|
+
|
|
578
|
+
_quoted_temp: () => token(prec(1, choice(/"[tT][eE][mM][pP]"/, /`[tT][eE][mM][pP]`/, /\[[tT][eE][mM][pP]\]/))),
|
|
579
|
+
|
|
580
|
+
_string_temp: () => token(prec(1, /'[tT][eE][mM][pP]'/)),
|
|
581
|
+
|
|
582
|
+
_temp_qualified_name: ($) => alias($._temp_qualified_name_parts, $.qualified_name),
|
|
583
|
+
|
|
584
|
+
_temp_qualified_name_parts: ($) => seq(field('schema', $._temp_schema), '.', field('name', $._name)),
|
|
585
|
+
|
|
586
|
+
_other_qualified_name: ($) => alias($._other_qualified_name_parts, $.qualified_name),
|
|
587
|
+
|
|
588
|
+
_other_qualified_name_parts: ($) =>
|
|
589
|
+
seq(field('schema', choice($.identifier, $.quoted_identifier, $.string_literal)), '.', field('name', $._name)),
|
|
590
|
+
|
|
591
|
+
_trigger_event: ($) =>
|
|
592
|
+
seq(
|
|
593
|
+
optional(choice(keyword('BEFORE'), keyword('AFTER'), keywords('INSTEAD OF'))),
|
|
594
|
+
choice(
|
|
595
|
+
keyword('DELETE'),
|
|
596
|
+
keyword('INSERT'),
|
|
597
|
+
seq(keyword('UPDATE'), optional(seq(keyword('OF'), commaSep1($._name))))
|
|
598
|
+
),
|
|
599
|
+
keyword('ON')
|
|
600
|
+
),
|
|
601
|
+
|
|
602
|
+
_trigger_body: ($) => seq($._trigger_condition, repeat1(seq($._trigger_statement, ';')), keyword('END')),
|
|
603
|
+
|
|
604
|
+
_restricted_trigger_body: ($) =>
|
|
605
|
+
seq($._trigger_condition, repeat1(seq($._restricted_trigger_statement, ';')), keyword('END')),
|
|
606
|
+
|
|
607
|
+
_trigger_condition: ($) =>
|
|
608
|
+
seq(
|
|
609
|
+
optional(keywords('FOR EACH ROW')),
|
|
610
|
+
optional(seq(keyword('WHEN'), field('condition', $._expression))),
|
|
611
|
+
keyword('BEGIN')
|
|
612
|
+
),
|
|
613
|
+
|
|
614
|
+
// INSERT, UPDATE, and DELETE statements in a trigger take no WITH clause, INDEXED BY clause, or RETURNING clause.
|
|
615
|
+
_trigger_statement: ($) =>
|
|
616
|
+
choice(
|
|
617
|
+
alias($._trigger_update_statement, $.update_statement),
|
|
618
|
+
alias($._trigger_insert_statement, $.insert_statement),
|
|
619
|
+
alias($._trigger_delete_statement, $.delete_statement),
|
|
620
|
+
$.select_statement
|
|
621
|
+
),
|
|
622
|
+
|
|
623
|
+
_restricted_trigger_statement: ($) =>
|
|
624
|
+
choice(
|
|
625
|
+
alias($._restricted_trigger_update_statement, $.update_statement),
|
|
626
|
+
alias($._restricted_trigger_insert_statement, $.insert_statement),
|
|
627
|
+
alias($._restricted_trigger_delete_statement, $.delete_statement),
|
|
628
|
+
$.select_statement
|
|
629
|
+
),
|
|
630
|
+
|
|
631
|
+
_trigger_update_statement: ($) => triggerUpdate($, $.table_target),
|
|
632
|
+
|
|
633
|
+
_restricted_trigger_update_statement: ($) => triggerUpdate($, $._unqualified_table_target),
|
|
634
|
+
|
|
635
|
+
_trigger_insert_statement: ($) => triggerInsert($, $._insert_head),
|
|
636
|
+
|
|
637
|
+
_restricted_trigger_insert_statement: ($) => triggerInsert($, $._restricted_insert_head),
|
|
638
|
+
|
|
639
|
+
_trigger_delete_statement: ($) => triggerDelete($, $.table_target),
|
|
640
|
+
|
|
641
|
+
_restricted_trigger_delete_statement: ($) => triggerDelete($, $._unqualified_table_target),
|
|
642
|
+
|
|
643
|
+
_unqualified_table_target: ($) => alias($._unqualified_table_target_parts, $.table_target),
|
|
644
|
+
|
|
645
|
+
_unqualified_table_target_parts: ($) =>
|
|
646
|
+
seq(field('name', $._unqualified_name), optional(seq(keyword('AS'), field('alias', $._name)))),
|
|
647
|
+
|
|
648
|
+
_restricted_insert_head: ($) => insertHead($, $._unqualified_table_target),
|
|
649
|
+
|
|
650
|
+
drop_statement: ($) =>
|
|
651
|
+
seq(
|
|
652
|
+
keyword('DROP'),
|
|
653
|
+
choice(keyword('TABLE'), keyword('VIEW'), keyword('INDEX'), keyword('TRIGGER')),
|
|
654
|
+
optional($.if_exists),
|
|
655
|
+
field('name', $.qualified_name)
|
|
656
|
+
),
|
|
657
|
+
|
|
658
|
+
alter_table_statement: ($) =>
|
|
659
|
+
seq(
|
|
660
|
+
keywords('ALTER TABLE'),
|
|
661
|
+
field('table', $.qualified_name),
|
|
662
|
+
choice(
|
|
663
|
+
seq(keywords('RENAME TO'), field('new_name', $._name)),
|
|
664
|
+
seq(
|
|
665
|
+
keyword('RENAME'),
|
|
666
|
+
optional(keyword('COLUMN')),
|
|
667
|
+
field('column', $._name),
|
|
668
|
+
keyword('TO'),
|
|
669
|
+
field('new_name', $._name)
|
|
670
|
+
),
|
|
671
|
+
seq(keyword('ADD'), optional(keyword('COLUMN')), $.column_definition),
|
|
672
|
+
seq(keyword('DROP'), optional(keyword('COLUMN')), field('column', $._name)),
|
|
673
|
+
seq(keywords('DROP CONSTRAINT'), field('constraint', $._name)),
|
|
674
|
+
seq(
|
|
675
|
+
keyword('ALTER'),
|
|
676
|
+
optional(keyword('COLUMN')),
|
|
677
|
+
field('column', $._name),
|
|
678
|
+
choice(keywords('DROP NOT NULL'), seq(keywords('SET NOT NULL'), optional($.conflict_clause)))
|
|
679
|
+
),
|
|
680
|
+
seq(keyword('ADD'), optional($.constraint_name), alias($._table_check_constraint, $.check_constraint))
|
|
681
|
+
)
|
|
682
|
+
),
|
|
683
|
+
|
|
684
|
+
// Other statements
|
|
685
|
+
|
|
686
|
+
pragma_statement: ($) =>
|
|
687
|
+
seq(
|
|
688
|
+
keyword('PRAGMA'),
|
|
689
|
+
field('name', $.qualified_name),
|
|
690
|
+
optional(choice(seq('=', field('value', $._pragma_value)), seq('(', field('value', $._pragma_value), ')')))
|
|
691
|
+
),
|
|
692
|
+
|
|
693
|
+
// A name, which takes the illegal tokens, stands for the unsigned number's.
|
|
694
|
+
_pragma_value: ($) =>
|
|
695
|
+
choice(
|
|
696
|
+
$._unseparated_number,
|
|
697
|
+
alias($._signed_unseparated_number, $.unary_expression),
|
|
698
|
+
$._name,
|
|
699
|
+
keyword('ON'),
|
|
700
|
+
keyword('DELETE'),
|
|
701
|
+
keyword('DEFAULT')
|
|
702
|
+
),
|
|
703
|
+
|
|
704
|
+
vacuum_statement: ($) =>
|
|
705
|
+
seq(
|
|
706
|
+
keyword('VACUUM'),
|
|
707
|
+
optional(field('schema', $._name)),
|
|
708
|
+
optional(seq(keyword('INTO'), field('file', $._expression)))
|
|
709
|
+
),
|
|
710
|
+
|
|
711
|
+
attach_statement: ($) =>
|
|
712
|
+
seq(
|
|
713
|
+
keyword('ATTACH'),
|
|
714
|
+
optional(keyword('DATABASE')),
|
|
715
|
+
field('file', $._expression),
|
|
716
|
+
keyword('AS'),
|
|
717
|
+
field('schema', $._expression),
|
|
718
|
+
optional(seq(keyword('KEY'), field('key', $._expression)))
|
|
719
|
+
),
|
|
720
|
+
|
|
721
|
+
detach_statement: ($) => seq(keyword('DETACH'), optional(keyword('DATABASE')), field('schema', $._expression)),
|
|
722
|
+
|
|
723
|
+
reindex_statement: ($) => seq(keyword('REINDEX'), optional(field('name', $.qualified_name))),
|
|
724
|
+
|
|
725
|
+
analyze_statement: ($) => seq(keyword('ANALYZE'), optional(field('name', $.qualified_name))),
|
|
726
|
+
|
|
727
|
+
// Data manipulation
|
|
728
|
+
|
|
729
|
+
insert_statement: ($) =>
|
|
730
|
+
seq(
|
|
731
|
+
optional($.with_clause),
|
|
732
|
+
$._insert_head,
|
|
733
|
+
choice(
|
|
734
|
+
seq(
|
|
735
|
+
$.select_statement,
|
|
736
|
+
repeat($.upsert_clause),
|
|
737
|
+
optional(alias($._final_upsert_clause, $.upsert_clause)),
|
|
738
|
+
optional($.returning_clause)
|
|
739
|
+
),
|
|
740
|
+
seq(keywords('DEFAULT VALUES'), optional($.returning_clause))
|
|
741
|
+
)
|
|
742
|
+
),
|
|
743
|
+
|
|
744
|
+
_insert_head: ($) => insertHead($, $.table_target),
|
|
745
|
+
|
|
746
|
+
_or_conflict: ($) => seq(keyword('OR'), $._conflict_resolution),
|
|
747
|
+
|
|
748
|
+
table_target: ($) => seq(field('name', $.qualified_name), optional(seq(keyword('AS'), field('alias', $._name)))),
|
|
749
|
+
|
|
750
|
+
// Only the last upsert clause may leave out the conflict target.
|
|
751
|
+
upsert_clause: ($) => seq(keywords('ON CONFLICT'), $.conflict_target, keyword('DO'), $._upsert_action),
|
|
752
|
+
|
|
753
|
+
_final_upsert_clause: ($) => seq(keywords('ON CONFLICT'), keyword('DO'), $._upsert_action),
|
|
754
|
+
|
|
755
|
+
conflict_target: ($) => seq('(', commaSep1($.indexed_column), ')', optional($.where_clause)),
|
|
756
|
+
|
|
757
|
+
_upsert_action: ($) => choice(keyword('NOTHING'), seq(keyword('UPDATE'), $.set_clause, optional($.where_clause))),
|
|
758
|
+
|
|
759
|
+
update_statement: ($) =>
|
|
760
|
+
seq(
|
|
761
|
+
optional($.with_clause),
|
|
762
|
+
keyword('UPDATE'),
|
|
763
|
+
optional($._or_conflict),
|
|
764
|
+
field('table', $.table_target),
|
|
765
|
+
optional($.indexed_clause),
|
|
766
|
+
$.set_clause,
|
|
767
|
+
optional($.from_clause),
|
|
768
|
+
optional($.where_clause),
|
|
769
|
+
optional($.returning_clause)
|
|
770
|
+
),
|
|
771
|
+
|
|
772
|
+
set_clause: ($) => seq(keyword('SET'), commaSep1($.assignment)),
|
|
773
|
+
|
|
774
|
+
assignment: ($) => seq(field('left', choice($._name, $.column_list)), '=', field('right', $._expression)),
|
|
775
|
+
|
|
776
|
+
delete_statement: ($) =>
|
|
777
|
+
seq(
|
|
778
|
+
optional($.with_clause),
|
|
779
|
+
keywords('DELETE FROM'),
|
|
780
|
+
field('table', $.table_target),
|
|
781
|
+
optional($.indexed_clause),
|
|
782
|
+
optional($.where_clause),
|
|
783
|
+
optional($.returning_clause)
|
|
784
|
+
),
|
|
785
|
+
|
|
786
|
+
// `TABLE.*` is an error in a RETURNING clause (`isAsteriskTerm` in src/trigger.c).
|
|
787
|
+
returning_clause: ($) => seq(keyword('RETURNING'), commaSep1(choice($.result_column, alias('*', $.all_columns)))),
|
|
788
|
+
|
|
789
|
+
// Queries
|
|
790
|
+
|
|
791
|
+
select_statement: ($) =>
|
|
792
|
+
seq(
|
|
793
|
+
optional($.with_clause),
|
|
794
|
+
repeat(seq(choice($.select_core, $.values_clause), $.compound_operator)),
|
|
795
|
+
// An ORDER BY or LIMIT clause applies to the whole compound query, and follows a SELECT, not VALUES.
|
|
796
|
+
choice($.values_clause, seq($.select_core, optional($.order_by_clause), optional($.limit_clause)))
|
|
797
|
+
),
|
|
798
|
+
|
|
799
|
+
with_clause: ($) => seq(keyword('WITH'), optional(keyword('RECURSIVE')), commaSep1($.common_table_expression)),
|
|
800
|
+
|
|
801
|
+
common_table_expression: ($) =>
|
|
802
|
+
seq(
|
|
803
|
+
field('name', $._name),
|
|
804
|
+
optional($.column_list),
|
|
805
|
+
keyword('AS'),
|
|
806
|
+
optional(choice(keyword('MATERIALIZED'), keywords('NOT MATERIALIZED'))),
|
|
807
|
+
'(',
|
|
808
|
+
$.select_statement,
|
|
809
|
+
')'
|
|
810
|
+
),
|
|
811
|
+
|
|
812
|
+
compound_operator: () => choice(keyword('UNION'), keywords('UNION ALL'), keyword('INTERSECT'), keyword('EXCEPT')),
|
|
813
|
+
|
|
814
|
+
values_clause: ($) => seq(keyword('VALUES'), commaSep1($.row)),
|
|
815
|
+
|
|
816
|
+
row: ($) => seq('(', commaSep1($._expression), ')'),
|
|
817
|
+
|
|
818
|
+
select_core: ($) =>
|
|
819
|
+
seq(
|
|
820
|
+
keyword('SELECT'),
|
|
821
|
+
optional(choice(keyword('DISTINCT'), keyword('ALL'))),
|
|
822
|
+
commaSep1(choice($.result_column, alias('*', $.all_columns), $.table_columns)),
|
|
823
|
+
optional($.from_clause),
|
|
824
|
+
optional($.where_clause),
|
|
825
|
+
optional($.group_by_clause),
|
|
826
|
+
optional($.having_clause),
|
|
827
|
+
optional($.window_clause)
|
|
828
|
+
),
|
|
829
|
+
|
|
830
|
+
result_column: ($) => seq(field('value', $._expression), optional($._alias)),
|
|
831
|
+
|
|
832
|
+
table_columns: ($) => seq(field('table', $._name), '.', '*'),
|
|
833
|
+
|
|
834
|
+
_alias: ($) =>
|
|
835
|
+
choice(
|
|
836
|
+
seq(keyword('AS'), field('alias', $._name)),
|
|
837
|
+
field(
|
|
838
|
+
'alias',
|
|
839
|
+
choice(
|
|
840
|
+
$._narrow_name,
|
|
841
|
+
alias(keyword('OVER'), $.identifier),
|
|
842
|
+
alias(keyword('FILTER'), $.identifier),
|
|
843
|
+
alias(keyword('WINDOW'), $.identifier)
|
|
844
|
+
)
|
|
845
|
+
)
|
|
846
|
+
),
|
|
847
|
+
|
|
848
|
+
from_clause: ($) => seq(keyword('FROM'), $._join_list),
|
|
849
|
+
|
|
850
|
+
// SQLite reads ON after a table as a join constraint even where that is an error, so ON never starts an upsert
|
|
851
|
+
// clause after a FROM clause.
|
|
852
|
+
_join_list: ($) => prec.right(seq($._table_or_subquery, optional($._misplaced_join_constraint), repeat($._join))),
|
|
853
|
+
|
|
854
|
+
_join: ($) =>
|
|
855
|
+
prec.right(
|
|
856
|
+
choice(
|
|
857
|
+
seq($.join_operator, $._table_or_subquery, optional($.join_constraint)),
|
|
858
|
+
seq(
|
|
859
|
+
alias($._natural_join_operator, $.join_operator),
|
|
860
|
+
$._table_or_subquery,
|
|
861
|
+
optional($._misplaced_join_constraint)
|
|
862
|
+
)
|
|
863
|
+
)
|
|
864
|
+
),
|
|
865
|
+
|
|
866
|
+
// ON is an error after the first table or after a natural join (`sqlite3SrcListAppendFromTerm` in src/build.c).
|
|
867
|
+
_misplaced_join_constraint: ($) => seq(keyword('ON'), $._unreachable),
|
|
868
|
+
|
|
869
|
+
_table_or_subquery: ($) => choice($.table, $.table_function, $.subquery_table, $.join_table),
|
|
870
|
+
|
|
871
|
+
table: ($) => seq(field('name', $.qualified_name), optional($._alias), optional($.indexed_clause)),
|
|
872
|
+
|
|
873
|
+
table_function: ($) => seq(field('name', $.qualified_name), '(', commaSep($._expression), ')', optional($._alias)),
|
|
874
|
+
|
|
875
|
+
subquery_table: ($) => seq('(', $.select_statement, ')', optional($._alias)),
|
|
876
|
+
|
|
877
|
+
join_table: ($) => seq('(', $._join_list, ')', optional($._alias)),
|
|
878
|
+
|
|
879
|
+
indexed_clause: ($) => choice(seq(keywords('INDEXED BY'), field('index', $._name)), keywords('NOT INDEXED')),
|
|
880
|
+
|
|
881
|
+
join_operator: () =>
|
|
882
|
+
choice(
|
|
883
|
+
',',
|
|
884
|
+
...JOIN_TYPES.filter((words) => !words.includes('NATURAL')).map((words) =>
|
|
885
|
+
seq(...words.map(keyword), keyword('JOIN'))
|
|
886
|
+
),
|
|
887
|
+
keyword('JOIN')
|
|
888
|
+
),
|
|
889
|
+
|
|
890
|
+
// A natural join may not have an ON or USING clause.
|
|
891
|
+
_natural_join_operator: () =>
|
|
892
|
+
choice(
|
|
893
|
+
...JOIN_TYPES.filter((words) => words.includes('NATURAL')).map((words) =>
|
|
894
|
+
seq(...words.map(keyword), keyword('JOIN'))
|
|
895
|
+
)
|
|
896
|
+
),
|
|
897
|
+
|
|
898
|
+
join_constraint: ($) =>
|
|
899
|
+
choice(seq(keyword('ON'), field('condition', $._expression)), seq(keyword('USING'), $.column_list)),
|
|
900
|
+
|
|
901
|
+
where_clause: ($) => seq(keyword('WHERE'), field('condition', $._expression)),
|
|
902
|
+
|
|
903
|
+
group_by_clause: ($) => seq(keywords('GROUP BY'), commaSep1($._expression)),
|
|
904
|
+
|
|
905
|
+
having_clause: ($) => seq(keyword('HAVING'), field('condition', $._expression)),
|
|
906
|
+
|
|
907
|
+
window_clause: ($) =>
|
|
908
|
+
seq(
|
|
909
|
+
choice(keyword('WINDOW'), alias($._window_before_name, 'WINDOW')),
|
|
910
|
+
alias($._first_named_window, $.named_window),
|
|
911
|
+
repeat(seq(',', $.named_window))
|
|
912
|
+
),
|
|
913
|
+
|
|
914
|
+
// SQLite's tokenizer reads WINDOW and OVER as keywords only before a token that its `getToken` (src/tokenize.c)
|
|
915
|
+
// counts as a name, which neither FILTER nor INDEXED is, though both are names elsewhere.
|
|
916
|
+
_first_named_window: ($) =>
|
|
917
|
+
seq(field('name', $._window_name), keyword('AS'), '(', optional($.window_definition), ')'),
|
|
918
|
+
|
|
919
|
+
_window_name: ($) =>
|
|
920
|
+
choice(reserved('window_name', $.identifier), $.quoted_identifier, $.string_literal, $._illegal),
|
|
921
|
+
|
|
922
|
+
named_window: ($) => seq(field('name', $._name), keyword('AS'), '(', optional($.window_definition), ')'),
|
|
923
|
+
|
|
924
|
+
window_definition: ($) =>
|
|
925
|
+
choice(seq(field('base', $._name), optional($._window_specification)), $._window_specification),
|
|
926
|
+
|
|
927
|
+
_window_specification: ($) =>
|
|
928
|
+
choice(
|
|
929
|
+
seq($.partition_by_clause, optional($.order_by_clause), optional($.frame_clause)),
|
|
930
|
+
seq($.order_by_clause, optional($.frame_clause)),
|
|
931
|
+
$.frame_clause
|
|
932
|
+
),
|
|
933
|
+
|
|
934
|
+
partition_by_clause: ($) => seq(keywords('PARTITION BY'), commaSep1($._expression)),
|
|
935
|
+
|
|
936
|
+
frame_clause: ($) =>
|
|
937
|
+
seq(
|
|
938
|
+
choice(keyword('RANGE'), keyword('ROWS'), keyword('GROUPS')),
|
|
939
|
+
// A frame may not start after it ends (`sqlite3WindowAlloc` in src/window.c); a frame with a single bound ends at
|
|
940
|
+
// the current row.
|
|
941
|
+
choice(
|
|
942
|
+
$._frame_start_before_current_row,
|
|
943
|
+
alias($._current_row, $.frame_bound),
|
|
944
|
+
seq(
|
|
945
|
+
keyword('BETWEEN'),
|
|
946
|
+
choice(
|
|
947
|
+
seq($._frame_start_before_current_row, keyword('AND'), $._frame_end),
|
|
948
|
+
seq(
|
|
949
|
+
alias($._current_row, $.frame_bound),
|
|
950
|
+
keyword('AND'),
|
|
951
|
+
choice(alias($._current_row, $.frame_bound), $._frame_end_after_current_row)
|
|
952
|
+
),
|
|
953
|
+
seq(alias($._following, $.frame_bound), keyword('AND'), $._frame_end_after_current_row)
|
|
954
|
+
)
|
|
955
|
+
)
|
|
956
|
+
),
|
|
957
|
+
optional($.frame_exclusion)
|
|
958
|
+
),
|
|
959
|
+
|
|
960
|
+
_frame_start_before_current_row: ($) =>
|
|
961
|
+
choice(alias($._unbounded_preceding, $.frame_bound), alias($._preceding, $.frame_bound)),
|
|
962
|
+
|
|
963
|
+
_frame_end: ($) =>
|
|
964
|
+
choice(alias($._preceding, $.frame_bound), alias($._current_row, $.frame_bound), $._frame_end_after_current_row),
|
|
965
|
+
|
|
966
|
+
_frame_end_after_current_row: ($) =>
|
|
967
|
+
choice(alias($._following, $.frame_bound), alias($._unbounded_following, $.frame_bound)),
|
|
968
|
+
|
|
969
|
+
_unbounded_preceding: () => keywords('UNBOUNDED PRECEDING'),
|
|
970
|
+
|
|
971
|
+
_unbounded_following: () => keywords('UNBOUNDED FOLLOWING'),
|
|
972
|
+
|
|
973
|
+
_preceding: ($) => seq($._expression, keyword('PRECEDING')),
|
|
974
|
+
|
|
975
|
+
_following: ($) => seq($._expression, keyword('FOLLOWING')),
|
|
976
|
+
|
|
977
|
+
_current_row: () => keywords('CURRENT ROW'),
|
|
978
|
+
|
|
979
|
+
frame_exclusion: () =>
|
|
980
|
+
seq(
|
|
981
|
+
keyword('EXCLUDE'),
|
|
982
|
+
choice(keywords('NO OTHERS'), keywords('CURRENT ROW'), keyword('GROUP'), keyword('TIES'))
|
|
983
|
+
),
|
|
984
|
+
|
|
985
|
+
order_by_clause: ($) => seq(keywords('ORDER BY'), commaSep1($.ordering_term)),
|
|
986
|
+
|
|
987
|
+
// NULLS FIRST and NULLS LAST order query results only (`sqlite3HasExplicitNulls` in src/build.c).
|
|
988
|
+
indexed_column: ($) => seq(field('value', $._expression), optional(choice(keyword('ASC'), keyword('DESC')))),
|
|
989
|
+
|
|
990
|
+
ordering_term: ($) =>
|
|
991
|
+
seq(
|
|
992
|
+
field('value', $._expression),
|
|
993
|
+
optional(choice(keyword('ASC'), keyword('DESC'))),
|
|
994
|
+
optional(choice(keywords('NULLS FIRST'), keywords('NULLS LAST')))
|
|
995
|
+
),
|
|
996
|
+
|
|
997
|
+
limit_clause: ($) =>
|
|
998
|
+
seq(
|
|
999
|
+
keyword('LIMIT'),
|
|
1000
|
+
choice(
|
|
1001
|
+
seq(field('limit', $._expression), optional(seq(keyword('OFFSET'), field('offset', $._expression)))),
|
|
1002
|
+
// The offset comes first after a comma.
|
|
1003
|
+
seq(field('offset', $._expression), ',', field('limit', $._expression))
|
|
1004
|
+
)
|
|
1005
|
+
),
|
|
1006
|
+
|
|
1007
|
+
// Expressions
|
|
1008
|
+
|
|
1009
|
+
_expression: ($) =>
|
|
1010
|
+
choice(
|
|
1011
|
+
$._term,
|
|
1012
|
+
$.column_reference,
|
|
1013
|
+
$.bind_parameter,
|
|
1014
|
+
$.parenthesized_expression,
|
|
1015
|
+
$.row_value,
|
|
1016
|
+
$.subquery,
|
|
1017
|
+
$.exists_expression,
|
|
1018
|
+
$.function_call,
|
|
1019
|
+
$.cast_expression,
|
|
1020
|
+
$.case_expression,
|
|
1021
|
+
$.raise_expression,
|
|
1022
|
+
$.unary_expression,
|
|
1023
|
+
$.binary_expression,
|
|
1024
|
+
$.collate_expression,
|
|
1025
|
+
$.like_expression,
|
|
1026
|
+
$.between_expression,
|
|
1027
|
+
$.in_expression,
|
|
1028
|
+
$.null_test_expression
|
|
1029
|
+
),
|
|
1030
|
+
|
|
1031
|
+
_term: ($) => choice($.null, $.numeric_literal, $.string_literal, $.blob_literal, $.current_time, $._illegal),
|
|
1032
|
+
|
|
1033
|
+
// A token that SQLite's tokenizer rejects, which the scanner reports where a literal or a name may appear. It never
|
|
1034
|
+
// completes a rule, so it always leaves an error.
|
|
1035
|
+
_illegal: ($) => seq($._illegal_token, $._unreachable),
|
|
1036
|
+
|
|
1037
|
+
null: () => keyword('NULL'),
|
|
1038
|
+
|
|
1039
|
+
current_time: () => choice(keyword('CURRENT_DATE'), keyword('CURRENT_TIME'), keyword('CURRENT_TIMESTAMP')),
|
|
1040
|
+
|
|
1041
|
+
column_reference: ($) =>
|
|
1042
|
+
choice(
|
|
1043
|
+
field('name', $._identifier),
|
|
1044
|
+
seq(field('table', $._name), '.', field('name', $._name)),
|
|
1045
|
+
seq(field('schema', $._name), '.', field('table', $._name), '.', field('name', $._name))
|
|
1046
|
+
),
|
|
1047
|
+
|
|
1048
|
+
parenthesized_expression: ($) => seq('(', $._expression, ')'),
|
|
1049
|
+
|
|
1050
|
+
row_value: ($) => seq('(', $._expression, ',', commaSep1($._expression), ')'),
|
|
1051
|
+
|
|
1052
|
+
subquery: ($) => seq('(', $.select_statement, ')'),
|
|
1053
|
+
|
|
1054
|
+
exists_expression: ($) => seq(keyword('EXISTS'), '(', $.select_statement, ')'),
|
|
1055
|
+
|
|
1056
|
+
function_call: ($) =>
|
|
1057
|
+
seq(
|
|
1058
|
+
field('name', $._identifier),
|
|
1059
|
+
'(',
|
|
1060
|
+
optional(
|
|
1061
|
+
choice(
|
|
1062
|
+
alias('*', $.all_columns),
|
|
1063
|
+
seq(
|
|
1064
|
+
optional(choice(keyword('DISTINCT'), keyword('ALL'))),
|
|
1065
|
+
commaSep($._expression),
|
|
1066
|
+
optional($.order_by_clause)
|
|
1067
|
+
)
|
|
1068
|
+
)
|
|
1069
|
+
),
|
|
1070
|
+
')',
|
|
1071
|
+
optional($.filter_clause),
|
|
1072
|
+
optional($.over_clause)
|
|
1073
|
+
),
|
|
1074
|
+
|
|
1075
|
+
filter_clause: ($) => seq(keyword('FILTER'), '(', $.where_clause, ')'),
|
|
1076
|
+
|
|
1077
|
+
// OVER before WINDOW is the keyword, never an alias (`analyzeOverKeyword` in src/tokenize.c); the scanner reads it
|
|
1078
|
+
// so that the parser does not also try the alias.
|
|
1079
|
+
over_clause: ($) =>
|
|
1080
|
+
seq(
|
|
1081
|
+
choice(keyword('OVER'), alias($._over_before_window, 'OVER')),
|
|
1082
|
+
choice(seq('(', optional($.window_definition), ')'), field('window', $._window_name))
|
|
1083
|
+
),
|
|
1084
|
+
|
|
1085
|
+
cast_expression: ($) =>
|
|
1086
|
+
seq(keyword('CAST'), '(', field('value', $._expression), keyword('AS'), optional($.type_name), ')'),
|
|
1087
|
+
|
|
1088
|
+
case_expression: ($) =>
|
|
1089
|
+
seq(
|
|
1090
|
+
keyword('CASE'),
|
|
1091
|
+
optional(field('operand', $._expression)),
|
|
1092
|
+
repeat1($.when_clause),
|
|
1093
|
+
optional(seq(keyword('ELSE'), field('else', $._expression))),
|
|
1094
|
+
keyword('END')
|
|
1095
|
+
),
|
|
1096
|
+
|
|
1097
|
+
when_clause: ($) =>
|
|
1098
|
+
seq(keyword('WHEN'), field('condition', $._expression), keyword('THEN'), field('result', $._expression)),
|
|
1099
|
+
|
|
1100
|
+
raise_expression: ($) =>
|
|
1101
|
+
seq(
|
|
1102
|
+
keyword('RAISE'),
|
|
1103
|
+
'(',
|
|
1104
|
+
choice(
|
|
1105
|
+
keyword('IGNORE'),
|
|
1106
|
+
seq(choice(keyword('ROLLBACK'), keyword('ABORT'), keyword('FAIL')), ',', field('message', $._expression))
|
|
1107
|
+
),
|
|
1108
|
+
')'
|
|
1109
|
+
),
|
|
1110
|
+
|
|
1111
|
+
unary_expression: ($) =>
|
|
1112
|
+
choice(
|
|
1113
|
+
prec.right(PREC.NOT, seq(field('operator', keyword('NOT')), field('operand', $._expression))),
|
|
1114
|
+
prec.right(PREC.UNARY, seq(field('operator', choice('~', '+', '-')), field('operand', $._expression)))
|
|
1115
|
+
),
|
|
1116
|
+
|
|
1117
|
+
binary_expression: ($) => {
|
|
1118
|
+
/** @type {[number, RuleOrLiteral][]} */
|
|
1119
|
+
const operators = [
|
|
1120
|
+
[PREC.OR, keyword('OR')],
|
|
1121
|
+
[PREC.AND, keyword('AND')],
|
|
1122
|
+
[
|
|
1123
|
+
PREC.EQUALITY,
|
|
1124
|
+
choice(
|
|
1125
|
+
'=',
|
|
1126
|
+
'==',
|
|
1127
|
+
'!=',
|
|
1128
|
+
'<>',
|
|
1129
|
+
keyword('IS'),
|
|
1130
|
+
keywords('IS NOT'),
|
|
1131
|
+
keywords('IS DISTINCT FROM'),
|
|
1132
|
+
keywords('IS NOT DISTINCT FROM')
|
|
1133
|
+
),
|
|
1134
|
+
],
|
|
1135
|
+
[PREC.RELATIONAL, choice('<', '<=', '>', '>=')],
|
|
1136
|
+
[PREC.BITWISE, choice('&', '|', '<<', '>>')],
|
|
1137
|
+
[PREC.ADDITIVE, choice('+', '-')],
|
|
1138
|
+
[PREC.MULTIPLICATIVE, choice('*', '/', '%')],
|
|
1139
|
+
[PREC.CONCAT, choice('||', '->', '->>')],
|
|
1140
|
+
];
|
|
1141
|
+
return choice(
|
|
1142
|
+
...operators.map(([precedence, operator]) =>
|
|
1143
|
+
prec.left(
|
|
1144
|
+
precedence,
|
|
1145
|
+
seq(field('left', $._expression), field('operator', operator), field('right', $._expression))
|
|
1146
|
+
)
|
|
1147
|
+
)
|
|
1148
|
+
);
|
|
1149
|
+
},
|
|
1150
|
+
|
|
1151
|
+
collate_expression: ($) =>
|
|
1152
|
+
prec.left(
|
|
1153
|
+
PREC.COLLATE,
|
|
1154
|
+
seq(field('value', $._expression), keyword('COLLATE'), field('collation', $._narrow_name))
|
|
1155
|
+
),
|
|
1156
|
+
|
|
1157
|
+
// An ESCAPE belongs to the innermost LIKE whose pattern it follows, since it binds more tightly than LIKE (src/parse.y),
|
|
1158
|
+
// while the whole expression with its escape operand binds as LIKE. The precedence of a region governs the positions
|
|
1159
|
+
// between its steps, where ESCAPE is shifted, and that of the last step governs the reduction.
|
|
1160
|
+
like_expression: ($) =>
|
|
1161
|
+
choice(
|
|
1162
|
+
prec.left(PREC.EQUALITY, seq($._like_head, field('right', $._expression))),
|
|
1163
|
+
seq(
|
|
1164
|
+
$._like_head,
|
|
1165
|
+
prec(PREC.ESCAPE, seq(field('right', $._expression), keyword('ESCAPE'))),
|
|
1166
|
+
prec.left(PREC.EQUALITY, field('escape', $._expression))
|
|
1167
|
+
)
|
|
1168
|
+
),
|
|
1169
|
+
|
|
1170
|
+
_like_head: ($) =>
|
|
1171
|
+
prec.left(
|
|
1172
|
+
PREC.EQUALITY,
|
|
1173
|
+
seq(
|
|
1174
|
+
field('left', $._expression),
|
|
1175
|
+
optional(keyword('NOT')),
|
|
1176
|
+
field('operator', choice(keyword('LIKE'), keyword('GLOB'), keyword('REGEXP'), keyword('MATCH')))
|
|
1177
|
+
)
|
|
1178
|
+
),
|
|
1179
|
+
|
|
1180
|
+
between_expression: ($) =>
|
|
1181
|
+
prec.left(
|
|
1182
|
+
PREC.EQUALITY,
|
|
1183
|
+
seq(
|
|
1184
|
+
field('value', $._expression),
|
|
1185
|
+
optional(keyword('NOT')),
|
|
1186
|
+
keyword('BETWEEN'),
|
|
1187
|
+
field('low', $._expression),
|
|
1188
|
+
keyword('AND'),
|
|
1189
|
+
field('high', $._expression)
|
|
1190
|
+
)
|
|
1191
|
+
),
|
|
1192
|
+
|
|
1193
|
+
in_expression: ($) =>
|
|
1194
|
+
prec.left(
|
|
1195
|
+
PREC.EQUALITY,
|
|
1196
|
+
seq(
|
|
1197
|
+
field('value', $._expression),
|
|
1198
|
+
optional(keyword('NOT')),
|
|
1199
|
+
keyword('IN'),
|
|
1200
|
+
choice(
|
|
1201
|
+
seq('(', choice($.select_statement, commaSep($._expression)), ')'),
|
|
1202
|
+
seq(field('table', $.qualified_name), optional(seq('(', commaSep($._expression), ')')))
|
|
1203
|
+
)
|
|
1204
|
+
)
|
|
1205
|
+
),
|
|
1206
|
+
|
|
1207
|
+
null_test_expression: ($) =>
|
|
1208
|
+
choice(
|
|
1209
|
+
prec.left(PREC.EQUALITY, seq(field('value', $._expression), choice(keyword('ISNULL'), keyword('NOTNULL')))),
|
|
1210
|
+
prec.left(PREC.NOT, seq(field('value', $._expression), keywords('NOT NULL')))
|
|
1211
|
+
),
|
|
1212
|
+
|
|
1213
|
+
// Names
|
|
1214
|
+
|
|
1215
|
+
qualified_name: ($) => seq(optional(seq(field('schema', $._name), '.')), field('name', $._name)),
|
|
1216
|
+
|
|
1217
|
+
// `nm` in parse.y: a name, which may also be a string literal.
|
|
1218
|
+
_name: ($) => choice($._identifier, $.string_literal, $._illegal),
|
|
1219
|
+
|
|
1220
|
+
// `ids` in parse.y.
|
|
1221
|
+
_narrow_name: ($) => choice(reserved('ids', $.identifier), $.quoted_identifier, $.string_literal, $._illegal),
|
|
1222
|
+
|
|
1223
|
+
_identifier: ($) => choice($.identifier, $.quoted_identifier),
|
|
1224
|
+
|
|
1225
|
+
identifier: () => new RegExp(`[${IDENTIFIER_START}][${IDENTIFIER_PART}]*`, 'u'),
|
|
1226
|
+
|
|
1227
|
+
quoted_identifier: () => token(choice(quoted('"'), quoted('`'), /\[[^\]\u0000]*\]/)),
|
|
1228
|
+
|
|
1229
|
+
// Literals
|
|
1230
|
+
|
|
1231
|
+
string_literal: () => quoted("'"),
|
|
1232
|
+
|
|
1233
|
+
numeric_literal: () =>
|
|
1234
|
+
token(
|
|
1235
|
+
choice(
|
|
1236
|
+
new RegExp(`0[xX]${HEX_DIGITS}`),
|
|
1237
|
+
new RegExp(`(${DIGITS}(\\.(${DIGITS})?)?|\\.${DIGITS})([eE][+-]?${DIGITS})?`)
|
|
1238
|
+
)
|
|
1239
|
+
),
|
|
1240
|
+
|
|
1241
|
+
// A number without digit separators (`number` in parse.y).
|
|
1242
|
+
// Each position of a number also takes the illegal tokens the scanner reports, such as a number followed by an
|
|
1243
|
+
// identifier character, which the lexer would otherwise split.
|
|
1244
|
+
_signed_number: ($) => choice($._unsigned_number, alias($._signed_unseparated_number, $.unary_expression)),
|
|
1245
|
+
|
|
1246
|
+
_unsigned_number: ($) => choice($._unseparated_number, $._illegal),
|
|
1247
|
+
|
|
1248
|
+
_signed_unseparated_number: ($) => seq(field('operator', choice('+', '-')), field('operand', $._unsigned_number)),
|
|
1249
|
+
|
|
1250
|
+
_unseparated_number: ($) =>
|
|
1251
|
+
alias(token(choice(/0[xX][0-9a-fA-F]+/, /([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?/)), $.numeric_literal),
|
|
1252
|
+
|
|
1253
|
+
blob_literal: () => /[xX]'([0-9a-fA-F][0-9a-fA-F])*'/,
|
|
1254
|
+
|
|
1255
|
+
// A `#` followed by a digit names a register, which only SQLite's own nested statements may use, and a numbered
|
|
1256
|
+
// parameter counts from 1 (`sqlite3ExprAssignVarNumber` in src/expr.c).
|
|
1257
|
+
bind_parameter: () =>
|
|
1258
|
+
new RegExp(
|
|
1259
|
+
`\\?([0-9]*[1-9][0-9]*)?|([:@$](::)*[${IDENTIFIER_PART}]|#((::)+[${IDENTIFIER_PART}]|[${IDENTIFIER_START}$]))([${IDENTIFIER_PART}]|::)*(\\([^\\t\\n\\v\\f\\r )\\x00]*\\))?`,
|
|
1260
|
+
'u'
|
|
1261
|
+
),
|
|
1262
|
+
|
|
1263
|
+
// SQLite reads an unterminated block comment as a comment to the end of the input, except for `/*` alone at the end.
|
|
1264
|
+
comment: () => token(choice(lineComment, blockComment, /\/\*(([^*\u0000]|\*+[^*/\u0000])+\**|\*+)/)),
|
|
1265
|
+
},
|
|
1266
|
+
});
|