FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
csv-sql/src/parser.js at master · DavidTimms/csv-sql · GitHub
DavidTimms
/
csv-sql
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
csv-sql
/
src
/
parser.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
339 lines (297 loc) · 10.1 KB
Breadcrumbs
csv-sql
/
src
/
parser.js
Copy path
File metadata and controls
339 lines (297 loc) · 10.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
merge
}
from
'./utils'
;
import
{
tokenize
}
from
'./tokenizer'
;
import
{
LEFT
,
RIGHT
}
from
'./operators'
;
import
*
as
ast
from
'./ast'
;
export
function
parseQuery
(
query
)
{
const
{
node
,
rest
}
=
parseSubQuery
(
query
)
.
ifNextToken
(
isType
(
'semicolon'
)
,
curr
=>
curr
.
then
(
semicolon
)
)
;
if
(
rest
.
length
>
0
)
{
const
restString
=
rest
.
map
(
token
=>
token
.
string
)
.
join
(
' '
)
;
throw
SyntaxError
(
`Parser completed with input remaining: "
${
restString
}
"`
)
;
}
return
node
;
}
function
parseSubQuery
(
query
)
{
const
tokens
=
tokenize
(
query
)
;
return
parser
(
tokens
)
.
then
(
keyword
(
'SELECT'
)
)
.
bind
(
'select'
,
outputColumns
)
.
bind
(
'from'
,
fromClause
)
.
bind
(
'where'
,
createConditionClause
(
'WHERE'
)
)
.
map
(
parseGroupByHaving
)
.
bind
(
'orderBy'
,
orderByClause
)
.
bind
(
'limit'
,
limitClause
)
.
bind
(
'offset'
,
offsetClause
)
.
mapNode
(
ast
.
query
)
;
}
function
fromClause
(
rest
)
{
return
parser
(
rest
)
.
ifNextToken
(
isKeyword
(
'FROM'
)
,
curr
=>
curr
.
then
(
keyword
(
'FROM'
)
)
.
just
(
tableName
)
)
;
}
function
parseGroupByHaving
(
parser
)
{
return
parser
.
ifNextToken
(
isKeyword
(
'GROUP'
)
,
curr
=>
curr
.
then
(
keyword
(
'GROUP'
)
)
.
then
(
keyword
(
'BY'
)
)
.
bind
(
'groupBy'
,
many
(
expression
,
{
separator
:
comma
}
)
)
.
bind
(
'having'
,
createConditionClause
(
'HAVING'
)
)
)
.
mapNode
(
node
=>
merge
(
{
groupBy
:
null
,
having
:
null
}
,
node
)
)
;
}
function
createConditionClause
(
conditionType
)
{
return
tokens
=>
parser
(
tokens
)
.
ifNextToken
(
isKeyword
(
conditionType
)
,
curr
=>
curr
.
then
(
keyword
(
conditionType
)
)
.
just
(
expression
)
)
;
}
function
orderByClause
(
tokens
)
{
return
parser
(
tokens
)
.
ifNextToken
(
isKeyword
(
'ORDER'
)
,
curr
=>
curr
.
then
(
keyword
(
'ORDER'
)
)
.
then
(
keyword
(
'BY'
)
)
.
just
(
many
(
orderingTerm
,
{
separator
:
comma
}
)
)
)
;
}
function
orderingTerm
(
tokens
)
{
return
parser
(
tokens
)
.
bind
(
'expression'
,
expression
)
.
bind
(
'direction'
,
tokens
=>
{
const
[
first
,
...
rest
]
=
tokens
;
if
(
isKeyword
(
'ASC'
,
first
)
||
isKeyword
(
'DESC'
,
first
)
)
{
return
parser
(
rest
,
first
.
string
)
;
}
return
parser
(
tokens
,
'ASC'
)
;
}
)
.
mapNode
(
node
=>
ast
.
orderingTerm
(
node
.
expression
,
node
.
direction
)
)
;
}
function
limitClause
(
tokens
)
{
return
parser
(
tokens
)
.
ifNextToken
(
isKeyword
(
'LIMIT'
)
,
curr
=>
curr
.
then
(
keyword
(
'LIMIT'
)
)
.
just
(
number
)
)
;
}
function
offsetClause
(
tokens
)
{
return
parser
(
tokens
)
.
ifNextToken
(
isKeyword
(
'OFFSET'
)
,
curr
=>
curr
.
then
(
keyword
(
'OFFSET'
)
)
.
just
(
number
)
)
;
}
function
atom
(
[
first
,
...
rest
]
)
{
// grouped expression
if
(
isType
(
'parOpen'
,
first
)
)
{
return
expression
(
rest
)
.
then
(
parClose
)
;
}
// literal TRUE, FALSE, or NULL
else
if
(
isType
(
'keyword'
,
first
)
)
{
const
s
=
first
.
string
;
if
(
s
===
'TRUE'
||
s
===
'FALSE'
||
s
===
'NULL'
)
{
return
parser
(
rest
,
ast
.
literal
(
JSON
.
parse
(
s
.
toLowerCase
(
)
)
)
)
;
}
else
if
(
first
.
string
===
'CASE'
)
{
return
caseExpression
(
rest
)
;
}
}
// function call
else
if
(
isType
(
'identifier'
,
first
)
&&
isType
(
'parOpen'
,
rest
[
0
]
)
)
{
const
functionName
=
first
.
value
;
if
(
functionName
.
toUpperCase
(
)
===
'COUNT'
&&
isStar
(
rest
[
1
]
)
)
{
return
parser
(
rest
.
slice
(
2
)
)
.
then
(
parClose
)
.
mapNode
(
node
=>
ast
.
call
(
functionName
,
[
ast
.
star
(
)
]
)
)
;
}
return
parser
(
rest
.
slice
(
1
)
)
.
bind
(
'arguments'
,
many
(
expression
,
{
separator
:
comma
,
min
:
0
}
)
)
.
then
(
parClose
)
.
mapNode
(
node
=>
ast
.
call
(
functionName
,
node
.
arguments
)
)
}
// identifier, number, or string
else
if
(
isType
(
[
'identifier'
,
'number'
,
'string'
]
,
first
)
)
{
return
parser
(
rest
,
first
)
;
}
throw
SyntaxError
(
`Expected an expression, found "
${
first
.
string
||
'(end of input)'
}
"`
)
;
}
function
caseExpression
(
tokens
)
{
return
parser
(
tokens
)
.
ifNextToken
(
not
(
isKeyword
(
'WHEN'
)
)
,
curr
=>
curr
.
bind
(
'switchExpression'
,
expression
)
)
.
bind
(
'cases'
,
many
(
whenThen
,
{
min
:
1
}
)
)
.
ifNextToken
(
isKeyword
(
'ELSE'
)
,
curr
=>
curr
.
then
(
keyword
(
'ELSE'
)
)
.
bind
(
'elseExpression'
,
expression
)
)
.
then
(
keyword
(
'END'
)
)
.
mapNode
(
(
{
switchExpression
,
cases
,
elseExpression
}
)
=>
switchExpression
?
ast
.
caseSwitch
(
switchExpression
,
cases
,
elseExpression
)
:
ast
.
caseIf
(
cases
,
elseExpression
)
)
;
}
function
whenThen
(
tokens
)
{
return
parser
(
tokens
)
.
then
(
keyword
(
'WHEN'
)
)
.
bind
(
'when'
,
expression
)
.
then
(
keyword
(
'THEN'
)
)
.
bind
(
'then'
,
expression
)
.
mapNode
(
node
=>
ast
.
whenThen
(
node
.
when
,
node
.
then
)
)
;
}
function
expression
(
tokens
,
controlOperator
=
null
)
{
let
parser
=
atom
(
tokens
)
;
while
(
true
)
{
const
token
=
parser
.
rest
[
0
]
;
const
tokenIsLesserOperator
=
(
token
&&
token
.
type
===
'operator'
&&
(
controlOperator
===
null
||
token
.
precedence
>
controlOperator
.
precedence
||
(
token
.
precedence
===
controlOperator
.
precedence
&&
controlOperator
.
associativity
===
RIGHT
)
)
)
;
if
(
!
tokenIsLesserOperator
)
return
parser
;
parser
=
parser
.
mapNode
(
left
=>
(
{
left
}
)
)
.
bind
(
'operator'
,
operator
)
.
bind
(
'right'
,
tokens
=>
expression
(
tokens
,
token
)
)
.
mapNode
(
node
=>
ast
.
binaryExpression
(
node
.
operator
,
node
.
left
,
node
.
right
)
)
;
}
}
function
namedExpression
(
tokens
)
{
return
expression
(
tokens
)
.
mapNode
(
expression
=>
(
{
expression
}
)
)
.
ifNextToken
(
isKeyword
(
'AS'
)
,
curr
=>
curr
.
then
(
keyword
(
'AS'
)
)
.
bind
(
'name'
,
atom
)
)
.
mapNode
(
(
{
expression
,
name
}
)
=>
{
return
ast
.
namedExpression
(
expression
,
name
?
name
.
value
:
null
)
}
)
;
}
function
outputColumns
(
tokens
)
{
if
(
isStar
(
tokens
[
0
]
)
)
{
return
parser
(
tokens
.
slice
(
1
)
,
'*'
)
;
}
return
many
(
namedExpression
,
{
separator
:
comma
}
)
(
tokens
)
;
}
function
keyword
(
word
)
{
return
tokens
=>
{
const
first
=
tokens
[
0
]
;
if
(
isKeyword
(
word
,
first
)
)
{
return
parser
(
tokens
.
slice
(
1
)
,
word
)
;
}
else
{
throw
SyntaxError
(
`Expected "
${
word
}
", found "
${
first
.
string
||
'(end of input)'
}
"`
)
;
}
}
;
}
const
tableName
=
parseTokenType
(
'string'
,
{
expected
:
'a table name'
}
)
;
const
identifier
=
parseTokenType
(
'identifier'
,
'an identifier'
)
;
const
operator
=
parseTokenType
(
'operator'
,
{
expected
:
'an operator'
}
)
;
const
comma
=
parseTokenType
(
'comma'
)
;
const
number
=
parseTokenType
(
'number'
)
;
const
semicolon
=
parseTokenType
(
'semicolon'
)
;
const
parOpen
=
parseTokenType
(
'parOpen'
,
{
expected
:
'an opening parenthesis'
}
)
;
const
parClose
=
parseTokenType
(
'parClose'
,
{
expected
:
'a closing parenthesis'
}
)
;
function
parseTokenType
(
typeName
,
{
expected
}
=
{
expected
:
'a '
+
typeName
}
)
{
return
(
[
first
,
...
rest
]
)
=>
{
if
(
isType
(
typeName
,
first
)
)
{
return
parser
(
rest
,
first
.
value
)
;
}
const
found
=
first
&&
first
.
string
||
'(end of input)'
;
throw
SyntaxError
(
`Expected
${
expected
}
, found "
${
found
}
"`
)
;
}
;
}
function
isKeyword
(
keyword
,
token
)
{
if
(
arguments
.
length
<
2
)
{
return
token
=>
isKeyword
(
keyword
,
token
)
;
}
return
token
&&
token
.
type
===
'keyword'
&&
token
.
string
===
keyword
;
}
function
isType
(
types
,
token
)
{
if
(
arguments
.
length
<
2
)
{
return
token
=>
isType
(
types
,
token
)
;
}
if
(
!
Array
.
isArray
(
types
)
)
types
=
[
types
]
;
return
token
&&
types
.
some
(
type
=>
token
.
type
===
type
)
;
}
function
isStar
(
token
)
{
return
token
&&
token
.
string
===
'*'
;
}
function
many
(
parseFunc
,
{
separator
,
min
}
=
{
}
)
{
if
(
min
===
undefined
)
min
=
1
;
return
tokens
=>
{
let
node
,
rest
=
tokens
;
const
parts
=
[
]
;
// TODO refactor this function to provide better error messages for
// sequences with a minimum - i.e. don't swallow the errors until
// the minimum is reached
try
{
for
(
let
i
=
0
;
rest
.
length
>
0
;
i
++
)
{
if
(
separator
&&
i
>
0
)
{
(
{
rest
}
=
separator
(
rest
)
)
;
}
(
{
node
,
rest
}
=
parseFunc
(
rest
)
)
;
parts
.
push
(
node
)
;
}
}
catch
(
e
)
{
if
(
!
(
e
instanceof
SyntaxError
)
)
{
throw
e
;
}
}
if
(
parts
.
length
<
min
)
{
throwUnexpected
(
rest
[
0
]
)
;
}
return
parser
(
rest
,
parts
)
;
}
;
}
function
not
(
predicate
)
{
return
(
...
args
)
=>
!
predicate
(
...
args
)
;
}
function
and
(
predicate1
,
predicate2
)
{
return
(
...
args
)
=>
predicate1
(
...
args
)
&&
predicate2
(
...
args
)
;
}
function
throwUnexpected
(
token
)
{
throw
SyntaxError
(
`Unexpected token: "
${
token
.
string
}
"`
)
;
}
function
printRest
(
parser
)
{
console
.
log
(
parser
.
rest
.
map
(
token
=>
(
{
[
token
.
type
]
:
token
.
string
}
)
)
)
;
return
parser
;
}
function
parser
(
rest
,
node
=
null
)
{
return
{
rest
,
node
,
bind
(
key
,
parseFunc
)
{
const
{
rest
,
node
}
=
parseFunc
(
this
.
rest
)
;
return
parser
(
rest
,
merge
(
this
.
node
,
{
[
key
]
:
node
}
)
)
;
}
,
just
(
parseFunc
)
{
return
parseFunc
(
this
.
rest
)
;
}
,
then
(
parseFunc
)
{
const
{
rest
}
=
parseFunc
(
this
.
rest
)
;
return
parser
(
rest
,
this
.
node
)
;
}
,
ifNextToken
(
predicate
,
ifFunc
,
elseFunc
)
{
if
(
predicate
(
this
.
rest
[
0
]
)
)
{
return
ifFunc
(
this
)
;
}
else
if
(
elseFunc
)
{
return
elseFunc
(
this
)
;
}
else
return
this
;
}
,
mapNode
(
func
)
{
const
mapped
=
func
(
this
.
node
)
;
return
parser
(
this
.
rest
,
mapped
===
undefined
?
this
.
node
:
mapped
)
;
}
,
map
(
func
)
{
return
func
(
this
)
;
}
,
}
;
}
Back
|
FazBrowse Home
|
New Git URL