This repository was archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.coffee
More file actions
49 lines (40 loc) · 1.45 KB
/
grammar.coffee
File metadata and controls
49 lines (40 loc) · 1.45 KB
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
PREC =
COMMA: -1
CLAUSE: 1
# rough sketch for an SQL grammar
module.exports = grammar
name: 'SQL'
conflicts: ($) => [
]
# word: ($) => $._identifier
rules:
source_file: ($) => repeat $.statement
statement: ($) => seq repeat1($.clause), ';'
# give clause a higher priority
clause: ($) => seq prec.dynamic(1, $.clause_keyword),
$._expressions
clause_keyword: ($) => prec.dynamic PREC.CLAUSE,
choice "WHERE", "SELECT", "FROM", "UPDATE", "SET"
# column name or table.name
column_identifier: ($) => choice $._identifier,
seq($.table, '.', $._identifier )
# give the choice between a single expression or the beginning of a sequence
_expressions: ($) => choice $._expression, $.sequence_expression
_expression: ($) => choice(
$.column_identifier,
$.asterisk,
$.string,
$.number
)
sequence_expression: ($) => prec PREC.COMMA,
seq $._expression, $.delim,
choice $._expression, $.sequence_expression
delim: ($) => ','
table: ($) => $._identifier
number: ($) => /\d+\.*\d*/
string: ($) => choice /"\w+"/, /'\w+'/
_identifier: ($) =>
alpha = /[a-zA-Z_]+/
alphanum = /[\w_]+/
token seq alpha, repeat(alphanum)
asterisk: ($) => '*'