-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
252 lines (219 loc) · 7.66 KB
/
index.js
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
const antlr4 = require('antlr4');
const Lexer = require('./gen/RSQLLexer');
const Parser = require('./gen/RSQLParser');
const Visitor = require('./gen/RSQLVisitor');
const pull = function (obj, path) {
if (obj && path.length) {
const segment = path[0];
const value = obj[segment];
if (path.length === 1) {
return value;
} else {
return pull(value, path.slice(1));
}
} else {
return null;
}
};
const pullPath = function (obj, path) {
return pull(obj, (path || "").split(".").filter(segment => !!segment));
};
function trim(s, c) {
if (c === "]") c = "\\]";
if (c === "\\") c = "\\\\";
return s.replace(new RegExp("^[" + c + "]+|[" + c + "]+$", "g"), "");
}
const trimQuotes = function (val) {
if (val.startsWith('"') && val.endsWith('"')) {
return trim(val, '"');
} else if (val.startsWith("'") && val.endsWith("'")) {
return trim(val, "'");
} else {
return val;
}
};
const coerceString = function (s) {
return trimQuotes(s);
};
const coerceBoolean = function (s) {
const value = trimQuotes(s);
if (/true/.test(value)) {
return true;
} else if (/false/.test(value)) {
return false;
}
};
const coerceNumber = function (s) {
const value = trimQuotes(s);
if (/\./.test(value)) {
return parseFloat(value);
} else {
return parseInt(value);
}
};
const coerceSet = function (s) {
if (Array.isArray(s)) {
return s;
} else if (s !== null && s !== undefined) {
return [s];
} else {
return [];
}
};
const intersection = function (s1, s2) {
const set1 = coerceSet(s1);
const set2 = coerceSet(s2);
const result = [];
set1.forEach(x => {
if (set2.includes(x)) {
result.push(x);
}
});
return result;
};
const intersect = function (s1, s2) {
return intersection(s1, s2).length > 0;
};
const PredicateParser = function () {
};
PredicateParser.prototype.parse = function (s) {
const characters = new antlr4.InputStream(s);
const lexer = new Lexer.RSQLLexer(characters);
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new Parser.RSQLParser(tokens);
parser.buildParseTrees = true;
const tree = parser.statement();
const Visits = function () {
Visitor.RSQLVisitor.call(this);
return this;
};
Visits.prototype = Object.create(Visitor.RSQLVisitor.prototype);
Visits.prototype.constructor = Visits;
Visits.prototype.visitStatement = function (ctx) {
if (ctx.left && ctx.op && ctx.right) {
switch (ctx.op.type) {
case Lexer.RSQLLexer.AND_OPERATOR:
return (function () {
const parseLeft = this.visit(ctx.left);
const parseRight = this.visit(ctx.right);
return (x) => {
return parseLeft(x) && parseRight(x);
};
}.bind(this))();
case Lexer.RSQLLexer.OR_OPERATOR:
return (function () {
const parseLeft = this.visit(ctx.left);
const parseRight = this.visit(ctx.right);
return (x) => {
return parseLeft(x) || parseRight(x);
};
}.bind(this))();
default:
throw new Error("Unknown operator!");
}
} else if (!!ctx.wrapped) {
return this.visit(ctx.wrapped);
} else if (!!ctx.node) {
return this.visit(ctx.node);
} else {
throw new Error("I don't recognize this node.")
}
};
Visits.prototype.visitComparison = function (ctx) {
const key = this.visitField(ctx.target);
let coerced;
if (ctx.single) {
if (key.hint) {
// if a type hint was provided, dispatch on that instead of the default rules that implement the best guess.
switch (key.hint) {
case ":string":
coerced = this.visitString_value(ctx.single);
break;
case ":boolean":
coerced = this.visitBoolean_value(ctx.single);
break;
case ":number":
coerced = this.visitNumber_value(ctx.single);
break;
default:
throw Error("Unrecognized type hint.");
}
} else {
coerced = this.visitSingle_value(ctx.single);
}
} else if (ctx.multi) {
// as we recurse, pass the target along.
ctx.multi.target = ctx.target;
coerced = this.visitMulti_value(ctx.multi);
} else if (ctx.bool) {
coerced = this.visitBoolean_value(ctx.bool);
} else if (ctx.regex) {
coerced = new RegExp(this.visitString_value(ctx.regex));
}
return x => {
const value = pullPath(x, key.key);
switch (ctx.op.type) {
case Lexer.RSQLLexer.EQ:
if(Array.isArray(value)) {
return intersect(value, coerced);
} else {
return value === coerced;
}
case Lexer.RSQLLexer.NE:
if(Array.isArray(value)) {
return !intersect(value, coerced);
} else {
return value !== coerced;
}
case Lexer.RSQLLexer.GT:
return value > coerced;
case Lexer.RSQLLexer.GTE:
return value >= coerced;
case Lexer.RSQLLexer.LT:
return value < coerced;
case Lexer.RSQLLexer.LTE:
return value <= coerced;
case Lexer.RSQLLexer.IN:
return intersect(coerced, value);
case Lexer.RSQLLexer.NIN:
return !intersect(coerced, value);
case Lexer.RSQLLexer.EX:
if (coerced) {
return value !== null && value !== undefined;
} else {
return value === null || value === undefined;
}
case Lexer.RSQLLexer.RE:
return value !== null && value !== undefined && coerced.test(value.toString());
default:
throw new Error("Unknown operator.");
}
};
};
Visits.prototype.visitSingle_value = function (ctx) {
return this.visitChildren(ctx)[0];
};
Visits.prototype.visitMulti_value = function (ctx) {
return this.visit(ctx.children.filter(child => {
// skip any of the intermediate nodes.
return child.constructor === Parser.RSQLParser.Single_valueContext;
}));
};
Visits.prototype.visitString_value = function (ctx) {
return coerceString(ctx.getText());
};
Visits.prototype.visitNumber_value = function (ctx) {
return coerceNumber(ctx.getText())
};
Visits.prototype.visitBoolean_value = function (ctx) {
return coerceBoolean(ctx.getText());
};
Visits.prototype.visitField = function (ctx) {
const field = {};
field.key = ctx.key.text;
field.hint = ctx.hint ? ctx.hint.text : null;
return field;
};
return new Visits().visitStatement(tree);
};
exports.PredicateParser = PredicateParser;