-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetasqlqueryparser.peg
233 lines (206 loc) · 6.83 KB
/
metasqlqueryparser.peg
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
/* Gil Moskowitz, 2014
Parse MetaSQL with pegjs - http://pegjs.majda.cz
TODO:
break
continue
*/
{
var params = {byCust: true
//, byProdcat: true
//, byItem: true
, period_id_list: [1, 2, 3, 4, 5]
//, salesDollars: true
//, baseCurrAbbr: 'USD'
//, capacityUnits: 'EA'
//, altCapacityUnits: 'CS'
//, inventoryUnits: 'DZ'
//, custgrp: true
//, prodcategory: true
//, item_id: 5
//, cust_id: 4
//, custtype_id: 3
//, custtype_pattern: 'A[lL][lL]'
//, prodcat_id: 2
//, prodcat_pattern: '[sS].*[aA]'
//, custgrp_pattern: 'b'
//, warehous_id: 1
}
, queryDef = {group: ""
, name: ""
, notes: ""
}
, mqloptions = {valuesub: true
, literalwrap: true
}
, loopstack = []
, stackIdx = function(id) {
var idx = 1;
if (loopstack.length > 0) {
for (idx = 0; idx < loopstack.length; idx++)
if (loopstack[stackidx].id === id)
break;
}
return idx;
}
;
}
start
= text:querytext ';'*
{
var result = '';
if (queryDef.group && queryDef.name)
result = '/* group: ' + queryDef.group
+ '\n name: ' + queryDef.name
+ '\n notes: ' + queryDef.notes + '\n */\n';
result += text.cooked + ';'
}
querytext
= t1:sqltext ws? t2:(querytext*)
{
return { raw: t1 + ' ' + t2.raw
, cooked: t1 + ' ' + t2.join(' ') };
}
/ t1:mqlclause ws? t2:(querytext*)
{
return { raw: t1.raw + ' ' + t2.raw
, cooked: t1.cooked + ' ' + t2.cooked.join(' '); }
}
/ linecomment+ ws? t2:(querytext*) { return t2.cooked.join(' '); }
/ BLOCKCOMMENT+ ws? t2:(querytext*) { return t2.cooked.join(' '); }
sqltext
= ws? text:([a-zA-Z0-9,/._*=\'\":()~ \t\n]+) { return text.join(''); }
ws
= [ \t\n]+
mqlclause
= ws? value:mqlvalue { return { raw: value, cooked: value }; }
/ ws? literal:mqlliteral { return { raw: literal, cooked: literal}; }
/ ws? ifcond:mqlif iftext:querytext
elifcond:(cond:mqlelseif text:querytext)*
mqlelse elsetext:querytext?
mqlendif
{ if (ifcond) { return iftext }
if (elifcond !== null) {
for (var i = 0; i < elifcond.length; i++) {
if (elifcond[i].cond) { return elifcond[i].text }
}
}
return elsetext
}
/ ws? ifcond:mqlif iftext:querytext
elifcond:(cond:mqlelseif text:querytext)*
mqlendif
{ if (ifcond) { return iftext }
if (elifcond !== null) {
for (var i = 0; i < elifcond.length; i++) {
if (elifcond[i].cond) { return elifcond[i].text }
}
}
}
/ ws? start:mqlforeach content:querytext? finish:mqlendforeach
{
var result = start + "[\n";
for (var i = 0; i < param[id].length; i++) {
result += parse(content.raw) + "\n";
}
result += "]" + finish;
}
mqlvalue
= MQLOPEN ws? VALUE ws? '(' ws? id:quotedidentifier ws? ')' ws? MQLCLOSE
{
var stackidx, listidx;
if (params[id] is an array) {
stackidx = stackIdx(id);
for () {
if (loopstack[stackidx].id === id)
break;
}
if (stackidx < loopstack.length) {
listidx = loopstack[stackidx].count++;
if (listidx >= params[id].length)
listidx = params[id].length - 1;
return mqloptions.valuesub ? ("'" + params[id][listidx] + "'")
: (':' + id + '__' + listidx);
}
else
return mqloptions.valuesub ? ("'" + params[id][0] + "'")
: (':' + id + '__' + 0);
}
return mqloptions.valuesub ? ("'" + params[id] + "'") : (':' + id);
}
mqlliteral
= MQLOPEN ws? LITERAL ws? '(' ws? id:quotedidentifier ws? ')' ws? MQLCLOSE
{ var wrap = mqloptions.literalwrap ? '"' : '';
return (wrap + params[id] + wrap);
}
linecomment
= groupcomment
/ namecomment
/ notescomment
/ simplelinecomment
mqlif
= MQLOPEN ws? IF ws? cond:mqlcondition ws? MQLCLOSE { return cond }
mqlelseif
= MQLOPEN ws? ELSEIF ws? cond:mqlcondition ws? MQLCLOSE { return cond }
mqlelse
= MQLOPEN ws? ELSE ws? MQLCLOSE
mqlendif
= MQLOPEN ws? ENDIF ws? MQLCLOSE
mqlforeach
= MQLOPEN ws? FOREACH ws? '(' ws? id:quotedidentifier ws? ')' ws? MQLCLOSE
{ loopstack.unshift({ id: id, count: 0 }); return id; }
mqlendforeach
= MQLOPEN ws? ENDFOREACH ws? MQLCLOSE { return loopstack.shift().id; }
mqlcondition
= NOT ws+ EXISTS ws? '(' ws? id:quotedidentifier ws? ')'
{ return ! (id in params) }
/ EXISTS ws? '(' ws? id:quotedidentifier ws? ')'
{ return (id in params) }
/ REEXISTS ws? '(' ws? re:regexp ws? ')'
{ for (var prop in params) {
if (prop.match(re)) {
return true
}
}
return false
}
/ ISFIRST ws? '(' ws? id:quotedidentifier ws? ')'
{ return loopstack[stackIdx(id)].count === 0; }
/ ISLAST ws? '(' ws? id:quotedidentifier ws? ')'
{ return loopstack[stackIdx(id)].count >= params[id].length; }
groupcomment
= ws? '--' [ \t]* [Gg][Rr][Oo][Uu][Pp] [ \t]* ':' [ \t] id:identifier [ \t]* '\n' { queryDef.group = id }
namecomment
= ws? '--' [ \t]* [Nn][Aa][Mm][Ee] [ \t]* ':' [ \t] id:identifier [ \t]* '\n' { queryDef.name = id }
notescomment
= ws? '--' [ \t]* [Nn][Oo][Tt][Ee][Ss] [ \t]* ':'
first:linecommenttext '\n'
rest:(simplelinecomment*)
{ queryDef.notes = first + ' ' + rest.join(' ') }
simplelinecomment
= ws? '--' ws? content:([^\n]*) '\n' { return content.join("") }
linecommenttext
= ws? str:([^\n]*) { return str.join("") }
quotedidentifier
= '"' id:identifier '"' { return id }
/ "'" id:identifier "'" { return id }
regexp
= "'" str:([^']*) "'" { return str.join("") }
/ '"' str:([^"]*) '"' { return str.join("") }
identifier
= first:[a-zA-Z_] rest:[a-zA-Z0-9_]* { return first + rest.join("") }
VALUE = [Vv] [Aa] [Ll] [Uu] [Ee]
LITERAL = [Ll] [Ii] [Tt] [Ee] [Rr] [Aa] [Ll]
IF = [Ii] [Ff]
ELSEIF = [Ee] [Ll] [Ss] [Ee] [Ii] [Ff]
ELSE = [Ee] [Ll] [Ss] [Ee]
ENDIF = [Ee] [Nn] [Dd] [Ii] [Ff]
NOT = [Nn] [Oo] [Tt]
EXISTS = [Ee] [Xx] [Ii] [Ss] [Tt] [Ss]
REEXISTS = [Rr] [Ee] [Ee] [Xx] [Ii] [Ss] [Tt] [Ss]
ISFIRST = [Ii] [Ss] [Ff] [Ii] [Rr] [Ss] [Tt]
ISLAST = [Ii] [Ss] [Ll] [Aa] [Ss] [Tt]
FOREACH = [Ff] [Oo] [Rr] [Ee] [Aa] [Cc] [Hh]
ENDFOREACH = [Ee] [Nn] [Dd] [Ff] [Oo] [Rr] [Ee] [Aa] [Cc] [Hh]
BLOCKCOMMENT = '/*' [.*] '*/'
MQLOPEN = '<?'
MQLCLOSE = '?>'