Repository navigation
Expand file tree
/
Copy pathgrammar.coffee
More file actions
227 lines (185 loc) · 7.45 KB
/
Copy pathgrammar.coffee
File metadata and controls
227 lines (185 loc) · 7.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
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
{Parser} = require 'jison'
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/
o = (patternString, action, options) ->
patternString = patternString.replace /\s{2,}/g, ' '
return [patternString, '$$ = $1;', options] unless action
action = if match = unwrap.exec action then match[1] else "(#{action}())"
action = action.replace /\bnew /g, '$&yy.'
[patternString, "$$ = #{action};", options]
grammar =
Root: [
o 'Query EOF'
]
Query: [
o "SelectQuery"
o "SelectQuery Unions", -> $1.unions = $2; $1
]
SelectQuery: [
o "SelectWithLimitQuery"
o "BasicSelectQuery"
]
BasicSelectQuery: [
o 'Select'
o 'Select OrderClause', -> $1.order = $2; $1
o 'Select GroupClause', -> $1.group = $2; $1
o 'Select GroupClause OrderClause', -> $1.group = $2; $1.order = $3; $1
]
SelectWithLimitQuery: [
o 'SelectQuery LimitClause', -> $1.limit = $2; $1
]
Select: [
o 'SelectClause'
o 'SelectClause WhereClause', -> $1.where = $2; $1
]
SelectClause: [
o 'SELECT Fields FROM Table', -> new Select($2, $4, false)
o 'SELECT DISTINCT Fields FROM Table', -> new Select($3, $5, true)
o 'SELECT Fields FROM Table Joins', -> new Select($2, $4, false, $5)
o 'SELECT DISTINCT Fields FROM Table Joins', -> new Select($3, $5, true, $6)
]
Table: [
o 'Literal', -> new Table($1)
o 'Pattern', -> $1
o 'LEFT_PAREN List RIGHT_PAREN', -> $2
o 'LEFT_PAREN Query RIGHT_PAREN', -> new SubSelect($2)
o 'LEFT_PAREN Query RIGHT_PAREN Literal', -> new SubSelect($2, $4)
o 'Literal WINDOW WINDOW_FUNCTION LEFT_PAREN Number RIGHT_PAREN',
-> new Table($1, $2, $3, $5)
o 'Pattern WINDOW WINDOW_FUNCTION LEFT_PAREN Number RIGHT_PAREN',
-> $1.setWindow($2, $3, $5)
]
Pattern: [
o 'PATTERN LEFT_SQUARE_BRACKET PatternClauses RIGHT_SQUARE_BRACKET',
-> new Pattern($3)
]
PatternClauses: [
o 'PatternClauses RIGHT_ROCKET PatternClause', -> $1.concat($3)
o 'PatternClause', -> [$1]
]
PatternClause: [
o 'LITERAL EQUAL LITERAL LEFT_PAREN Expression RIGHT_PAREN',
-> new PatternConstraint($1, $3, $5)
o 'LITERAL EQUAL LITERAL', -> new PatternConstraint($1, $3)
]
Unions: [
o 'Union', -> [$1]
o 'Unions Union', -> $1.concat($3)
]
Union: [
o 'UNION SelectQuery', -> new Union($2)
o 'UNION ALL SelectQuery', -> new Union($3, true)
]
Joins: [
o 'Join', -> [$1]
o 'Joins Join', -> $1.concat($2)
]
Join: [
o 'JOIN Table ON Expression', -> new Join($2, $4)
o 'LEFT JOIN Table ON Expression', -> new Join($3, $5, 'LEFT')
o 'RIGHT JOIN Table ON Expression', -> new Join($3, $5, 'RIGHT')
o 'LEFT INNER JOIN Table ON Expression', -> new Join($4, $6, 'LEFT', 'INNER')
o 'RIGHT INNER JOIN Table ON Expression', -> new Join($4, $6, 'RIGHT', 'INNER')
o 'LEFT OUTER JOIN Table ON Expression', -> new Join($4, $6, 'LEFT', 'OUTER')
o 'RIGHT OUTER JOIN Table ON Expression', -> new Join($4, $6, 'RIGHT', 'OUTER')
]
WhereClause: [
o 'WHERE Expression', -> new Where($2)
]
LimitClause: [
o 'LIMIT Number', -> new Limit($2)
]
OrderClause: [
o 'ORDER BY OrderArgs', -> new Order($3)
]
OrderArgs: [
o 'OrderArg', -> [$1]
o 'OrderArgs SEPARATOR OrderArg', -> $1.concat($3)
]
OrderArg: [
o 'Value', -> new OrderArgument($1, 'ASC')
o 'Value DIRECTION', -> new OrderArgument($1, $2)
]
GroupClause: [
o 'GroupBasicClause'
o 'GroupBasicClause HavingClause', -> $1.having = $2; $1
]
GroupBasicClause: [
o 'GROUP BY ArgumentList', -> new Group($3)
]
HavingClause: [
o 'HAVING Expression', -> new Having($2)
]
Expression: [
o 'LEFT_PAREN Expression RIGHT_PAREN', -> $2
o 'Expression MATH Expression', -> new Op($2, $1, $3)
o 'Expression MATH_MULTI Expression', -> new Op($2, $1, $3)
o 'Expression EQUAL Expression', -> new Op($2, $1, $3)
o 'Expression OPERATOR Expression', -> new Op($2, $1, $3)
o 'Expression CONDITIONAL Expression', -> new Op($2, $1, $3)
o 'Value IN Table', -> new Op($2, $1, $3)
o 'Value'
]
Value: [
o 'Literal'
o 'Number'
o 'String'
o 'Function'
o 'UserFunction'
o 'Boolean'
]
List: [
o 'ArgumentList', -> new ListValue($1)
]
Number: [
o 'NUMBER', -> new NumberValue($1)
]
Boolean: [
o 'BOOLEAN', -> new BooleanValue($1)
]
String: [
o 'STRING', -> new StringValue($1, "'")
o 'DBLSTRING', -> new StringValue($1, '"')
]
Literal: [
o 'LITERAL', -> new LiteralValue($1)
o 'Literal DOT LITERAL', -> new LiteralValue($1, $3)
]
Function: [
o "FUNCTION LEFT_PAREN ArgumentList RIGHT_PAREN", -> new FunctionValue($1, $3)
]
UserFunction: [
o "LITERAL LEFT_PAREN ArgumentList RIGHT_PAREN", -> new FunctionValue($1, $3, true)
]
ArgumentList: [
o 'Expression', -> [$1]
o 'ArgumentList SEPARATOR Value', -> $1.concat($3)
]
Fields: [
o 'Field', -> [$1]
o 'Fields SEPARATOR Field', -> $1.concat($3)
]
Field: [
o 'STAR', -> new Star()
o 'Expression', -> new Field($1)
o 'Expression AS Literal', -> new Field($1, $3)
]
tokens = []
operators = [
['left', 'Op']
['left', 'MATH_MULTI']
['left', 'MATH']
['left', 'EQUAL']
['left', 'OPERATOR']
['left', 'CONDITIONAL']
]
for name, alternatives of grammar
grammar[name] = for alt in alternatives
for token in alt[0].split ' '
tokens.push token unless grammar[token]
alt[1] = "return #{alt[1]}" if name is 'Root'
alt
exports.parser = new Parser
tokens : tokens.join ' '
bnf : grammar
operators : operators.reverse()
startSymbol : 'Root'