/
pgsql.sbnf
330 lines (278 loc) · 11.1 KB
/
pgsql.sbnf
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
SCOPE = 'source.pgsql'
# High-level, should be terse.
prototype : (~comment)* ;
main : (~any)* ;
comment : comment-line | comment-block ;
any : punctuation | number | string | operator | identifier ;
number : number-float | number-int ;
punctuation : dot | comma | semi | parens | brackets | braces ;
operator : operator-cast | operator-star | operator-tilde | operator-any ;
identifier : keyword | param | ident-other ;
keyword : bool | kw-common | kw-reserved ;
param : param-ordinal | param-named ;
atom : number | string | ident-other ;
# Reference:
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
#
# Postgres currently does not allow the empty quoted identifier "", but for our
# purposes it's better to scope it like an identifier.
IDENT = '\b[[:alpha:]_][[:alnum:]_]*\b'
IDENT_QUOTED = '"[^"]*"'
OP_CHAR = '\*\/\<\>\=\~\!\@\#\%\^\&\|\`\?\+\-'
OP_BEGIN = '(?!<[#[OP_CHAR]])'
OP_END = '(?=[^#[OP_CHAR]]|$)'
OP_ANY = '[#[OP_CHAR]]+'
# Syntactic hack. Should never match real text.
NONE = `dede961a7c944bdca31f56b82e486e05`
comment-line{comment.line} : '--'{punctuation.definition.comment}~'\n' ;
comment-block{comment.block} : comment-block-begin (~comment-block)* ~comment-block-end ;
comment-block-begin : `/*`{punctuation.definition.comment.begin} ;
comment-block-end : `*/`{punctuation.definition.comment.end} ;
parens : paren-begin (~any)* paren-end ;
brackets : bracket-begin (~any)* bracket-end ;
braces : brace-begin (~any)* brace-end ;
dot : `.`{punctuation.accessor.dot} ;
comma : `,`{punctuation.separator.sequence} ;
semi : `;`{punctuation.terminator} ;
paren-begin : `(`{punctuation.section.parens.begin} ;
paren-end : `)`{punctuation.section.parens.end} ;
bracket-begin : `[`{punctuation.section.brackets.begin} ;
bracket-end : `]`{punctuation.section.brackets.end} ;
brace-begin : `{`{punctuation.section.braces.begin} ;
brace-end : `}`{punctuation.section.braces.end} ;
# TODO support the `Ee` notation.
number-float : '\d+(\.)\d+'{constant.numeric, 1: punctuation.separator.decimal} ;
number-int : '\d+'{constant.numeric} ;
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html
#
# TODO: support dollar-quoted strings.
string : string-single ;
# TODO: support escapes.
# TODO: support bitstrings.
string-single{string.single, include-prototype: false} :
'(\b[Ee])?(\')'{1: storage.modifier, 2: punctuation.definition.string.begin}
~`'`{punctuation.definition.string.end}
;
op[pattern] : '#[OP_BEGIN]#[pattern]#[OP_END]'{keyword.operator} ;
operator-cast : op[`::`] ident-storage-type? ;
operator-star : ident-ns? op[`*`] ;
operator-tilde : op['~*|~|!~*|!~'] ; # TODO: followed by string scoped as regexp.
operator-any : OP_ANY{keyword.operator} ;
param-ordinal : '(\$)\d+'{variable.parameter.ordinal, 1: punctuation.definition.variable} ;
param-named : '(?!<:)(:)#[IDENT]'{variable.parameter.named, 1: punctuation.definition.variable} ;
bool{constant.language.bool} : word['true|false'] ;
# https://www.postgresql.org/docs/current/sql-keywords-appendix.html
# https://github.com/postgres/postgres/blob/master/src/include/parser/kwlist.h
kw-reserved{keyword.reserved} : word['all|analyse|analyze|and|any|array|as|asc|asymmetric|both|case|cast|check|collate|column|constraint|create|current_catalog|current_date|current_role|current_time|current_timestamp|current_user|default|deferrable|desc|distinct|do|else|end|except|false|fetch|for|foreign|from|grant|group|having|in|initially|intersect|into|lateral|leading|limit|localtime|localtimestamp|not|null|offset|on|only|or|order|placing|primary|references|returning|select|session_user|some|symmetric|table|then|to|trailing|true|union|unique|user|using|variadic|when|where|window|with'] ;
# Non-reserved keywords which are common enough to be scoped non-contextually.
kw-common :
NONE
| create
| alter
| drop
| is
| order
| group
| join
| kw['set|commit|rollback|between|full']
;
# https://www.postgresql.org/docs/current/sql-commands.html
create : create-or-replace (
NONE
| create-table
| create-trigger
| create-function
| create-index
| create-view
| create-schema
| create-extension
| create-type
| create-domain
| create-aggregate
| ident-kw
) ;
alter : kw['alter'] (alter-table | alter-domain | alter-type | ident-kw) ;
create-schema : kw['schema'] if-not-exists? ident-decl-schema ;
create-extension : kw['extension'] if-not-exists? ;
create-type : kw['type'] ident-decl-type (kw['as'] kw['enum']?)? ;
create-domain : kw['domain'] ident-decl-type kw['as'] ident-storage-type constraint? ;
create-aggregate : kw['aggregate'] ident-decl-function fn-params? ;
# https://www.postgresql.org/docs/current/sql-createtable.html
create-table :
kw['global|local|temporary|temp|unlogged']? kw['table']
if-not-exists?
ident-decl-table
paren-begin
# Needs to be simplified.
((constraint | declare-column) (comma? constraint | comma declare-column | any)*)?
paren-end
;
# Note: we're not scoping the procedure call as `variable.function` because the
# latter isn't generally supported by this syntax yet. Supporting it only here
# would be surprising.
create-trigger :
kw['trigger'] if-not-exists? ident-decl-trigger
(
kw['after|before|or|insert|update|delete']*
kw['on'] ident-other
kw['for|each|row|statement|execute']*
kw['procedure']
)?
;
# https://www.postgresql.org/docs/current/sql-createfunction.html
#
# TODO support PLPGSQL in function body (auto-detect from initial `begin`).
create-function :
kw['function']
if-not-exists?
ident-decl-function
fn-params
fn-return?
fn-meta*
(kw['as|return'] | semi)
;
create-index : kw['unique']? kw['index'] if-not-exists? ident-decl-index ;
# TODO: parameter list for `recursive view`.
create-view : kw['materialized|recursive']? kw['view'] ident-decl-view ;
alter-table{meta.alter.table} :
kw['table'] if-exists? kw['only']? ident-other alter-table-action
;
# Placeholder.
alter-domain : kw['domain'] ;
# Placeholder.
alter-type : kw['type'] ;
alter-table-action :
NONE
| (kw['add'] alter-table-action-add)
| (kw['drop'] alter-table-action-drop)
| (kw['alter'] alter-table-action-alter)
| (kw['rename'] alter-table-action-rename)
| kw['inherit']
;
alter-table-action-add :
NONE
| (kw['constraint'] if-not-exists? ident-decl-constraint (kw['not'] kw['valid'])?)
| (kw['column']? if-not-exists? declare-column)
;
alter-table-action-drop :
ident-kw if-exists? ident-other kw['restrict|cascade']?
;
alter-table-action-alter :
NONE
| (kw['constraint'] ident-other (kw['not']? kw['deferrable'])? (kw['initially'] kw['deferred|immediate'])?)
| (kw['column']? ident-other alter-table-action-alter-column)
;
alter-table-action-rename :
NONE
| (kw['to'] ident-decl-table)
| (kw['constraint'] ident-other kw['to'] ident-decl-constraint)
| (kw['column']? ident-other kw['to'] ident-decl-column)
;
alter-table-action-alter-column :
NONE
| (kw['add'] alter-table-action-alter-column-add)
| (kw['set'] alter-table-action-alter-column-set)
| (kw['drop'] alter-table-action-alter-column-drop)
| (type-using)
;
alter-table-action-alter-column-add :
kw['generated'] (kw['always'] | kw['by'] kw['default']) kw['as'] kw['identity']
;
alter-table-action-alter-column-set :
NONE
| (kw['data'] type-using)
| (kw['default'])
| (kw['not'] kw['null'])
;
alter-table-action-alter-column-drop :
NONE
| (kw['default'])
| (kw['not'] kw['null'])
| (kw['expression'] if-exists?)
;
type-using : kw['type'] ident-storage-type kw['using']? ;
# TODO support various modifiers, defaults, etc.
fn-params : paren-begin (kw-reserved | param-typed | any)* paren-end ;
param-typed : ident-decl-param? ident-storage-type ;
# TODO support `table`, `setof` and possibly more.
fn-return : kw['returns'] (
NONE
| kw['trigger']
| kw['setof'] ident-storage-type
| kw['table'] fn-params
| ident-storage-type
) ;
# https://www.postgresql.org/docs/current/sql-createfunction.html
fn-meta{meta.function.meta} :
NONE
| (kw['window|immutable|stable|parallel|unsafe|restricted|safe|volatile|strict|external'])
| (kw['language'] ident-kw)
| (kw['transform'] kw['for'] kw['type'] ident-storage-type ident-storage-type*)
| (kw['not']? kw['leakproof'])
| (kw['security'] kw['invoker|definer'])
| (kw['cost'] number)
| (kw['rows'] number)
| (kw['support'] ident-other)
| (kw['set'] ident-other (kw['to'] atom | operator-any atom | kw['from'] kw['current']))
;
# Placeholder.
drop : kw['drop'] if-exists? ;
is :
kw['is'] kw['not']? (bool | kw['null'] | kw['unknown'] | (kw['distinct'] kw['from']))
;
order : kw['order'] kw['by'] ;
group : kw['group'] kw['by'] ;
# https://www.postgresql.org/docs/current/sql-select.html
join :
NONE
| kw['cross'] kw['join']
| kw['natural']? join-type
;
# https://www.postgresql.org/docs/current/sql-select.html
join-type :
NONE
| kw['inner']? kw['join']
| kw['left|right|full'] kw['outer']? kw['join']
;
constraint : kw['constraint'] ident-decl-constraint? ;
# WTB better naming scheme for entity name rules.
# TODO: support various contextual keywords after the type, such as `primary key`.
declare-column : ident-decl-column ident-storage-type ;
create-or-replace : (kw['create'] kw['or'] kw['replace']) | kw['create'] ;
if-not-exists : kw['if'] kw['not'] kw['exists'] ;
if-exists : kw['if'] kw['exists'] ;
word[pattern] : '(?i)\b(?:#[pattern])\b' ;
kw[pattern]{keyword} : word[pattern] ;
id[scope]{#[scope]} : ident ;
ident : IDENT | IDENT_QUOTED ;
ident-kw : IDENT{keyword} ;
ident-other : ident-ns? id['variable.other'] ;
ident-ns : ident-ns['variable.other'] ;
ident-ns[scope] : '(#[IDENT]|#[IDENT_QUOTED]|(\()\s*(?:#[IDENT]|#[IDENT_QUOTED])\s*(\)))\s*(\.)'{
1: #[scope],
2: punctuation.section.parens.begin,
3: punctuation.section.parens.end,
4: punctuation.accessor.dot
} ;
# TODO better name.
# TODO support types consisting of multiple words, parametrized types, etc.
ident-storage-type : ident-ns? type (bracket-begin bracket-end)? ;
type{storage.type} : type-char | ident ;
# Note: technically, the whitespace could include newlines and tabs, and our
# regexps don't support that. TODO fix.
type-char :
word['character\s+varying|varchar|character|char']
(paren-begin number-int paren-end)?
;
# Rules starting with "ident-decl-" must use "declaration" scopes, as opposed
# to "usage" scopes. Most of these scopes should be added to symbol index.
ident-decl-param : ident-ns? id['variable.parameter'] ;
ident-decl-column : ident-ns? id['variable.other.member.declaration'] ;
ident-decl-constraint : ident-ns? id['entity.name.constraint'] ;
ident-decl-table : ident-ns? id['entity.name.type.table'] ;
ident-decl-schema : ident-ns? id['entity.name.schema'] ;
ident-decl-trigger : ident-ns? id['entity.name.trigger'] ;
ident-decl-function : ident-ns? id['entity.name.function'] ;
ident-decl-index : ident-ns? id['entity.name.index'] ;
ident-decl-view : ident-ns? id['entity.name.view'] ;
ident-decl-type : ident-ns? id['entity.name.type'] ;