-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.ts
240 lines (237 loc) · 6.81 KB
/
parse.ts
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
function parse(tokens: string[]) {
const operators: any = []
const operands: any = []
const priority: any = {
"!": 3,
"&&": 2,
"||": 1
}
function operate(operator: string, a: any, b: any = 0) {
if (a === undefined || b === undefined) {
return { error: true }
}
let x = {}
switch (operator) {
case "||":
x = { '$or': [a, b] }
break;
case "&&":
x = { '$and': [a, b] }
break;
case "!":
x = { '$nor': [a] }
break;
}
return x
}
function x(operator: string) {
while (operators.length > 0 && operators[operators.length - 1] !== "(" && priority[operators[operators.length - 1]] >= priority[operator]) {
const last_operator = operators.pop()
if (last_operator !== "!") {
const b = operands.pop()
const a = operands.pop()
operands.push(operate(last_operator, a, b))
} else {
const a = operands.pop()
operands.push(operate(last_operator, a))
}
}
operators.push(operator)
}
function execute_until_opening_bracket() {
while (operators.length > 0) {
const last_operator = operators.pop()
if (last_operator === "(") {
break;
}
if (last_operator !== "!") {
const b = operands.pop()
const a = operands.pop()
operands.push(operate(last_operator, a, b))
} else {
const a = operands.pop()
operands.push(operate(last_operator, a))
}
}
}
function execute_remaining() {
while (operators.length > 0) {
const last_operator = operators.pop()
if (last_operator === ")" || last_operator === "(") {
continue;
}
if (last_operator !== "!") {
const b = operands.pop()
const a = operands.pop()
operands.push(operate(last_operator, a, b))
} else {
const a = operands.pop()
operands.push(operate(last_operator, a))
}
}
}
const comparison_operators=["==","<=",">=","<",">"]
const comparison_operators_mongodb:{ [key: string]: string; }={"==":"$eq",">":"$gt","<":"$lt","<=":"$lte",">=":"$gte"}
const expr_tags=["height","width"]
for (const token of tokens) {
switch (token) {
case "||":
x("||")
break;
case ",":
case "&&":
x("&&")
break;
case "-":
case "!":
x("!")
break;
case "(":
operators.push("(")
break;
case ")":
execute_until_opening_bracket()
break;
default:
if(expr_tags.some(tag => token.indexOf(tag)===0)){
let index_of_operator=-1
let comp_operator=""
for(const operator of comparison_operators){
if(token.indexOf(operator)!==-1){
index_of_operator=token.indexOf(operator)
comp_operator=operator
break
}
}
if(index_of_operator!==-1){
const arr=[token.slice(0,index_of_operator).trim(),comp_operator,token.slice(comp_operator.length+index_of_operator).trim()]
if(!isNaN(Number(arr[2]))){
operands.push({ [arr[0]]: {[comparison_operators_mongodb[arr[1]]]: parseInt(arr[2])} })
}
}
}else{
operands.push({ tags: token })
}
break;
}
}
execute_remaining()
if (operands.length !== 1 || operators.length !== 0) {
return { error: true }
} else {
return operands[0]
}
}
function tokenize(query: string) {
function detect_brackets(str: string) {
const tokens = ["&&", "||", "#!", ",", "#-"];
const tokens2 = ["&&", "||", "#!", "(", ")", ",", "#-"];
const stack = [];
const closing_brackets = [];
const opening_brackets = [];
for (let i = 0; i < str.length; i++) {
if (tokens2.includes(str[i])) {
stack.push({ token: str[i], idx: i });
}
if (str[i + 1] && tokens2.includes(str[i] + str[i + 1])) {
stack.push({ token: str[i] + str[i + 1], idx: i });
}
if (stack.length > 0 && str[i] === ")") {
let x = stack.pop();
let m = false;
if (!x) {
return "##errror##"
}
while (x.token !== "(") {
x = stack.pop();
if (!x) {
return "##errror##"
}
if (tokens.includes(x.token)) {
m = true;
}
}
if (!m && (x.idx > 0 && (tokens.includes(str[x.idx - 1])) || (str[x.idx - 2] && tokens.includes(str[x.idx - 2] + str[x.idx - 1]))) ||
(x.idx === 0)) {
closing_brackets.push(i);
opening_brackets.push(x.idx);
}
else if (m) {
closing_brackets.push(i);
opening_brackets.push(x.idx);
}
}
}
const arr = str.split("");
for (let i = 0; i < closing_brackets.length; i++) {
arr[opening_brackets[i]] = "(%";
arr[closing_brackets[i]] = "%)";
}
str = arr.join("");
return str;
}
function not_operator_fix(str: string) {
const str_arr = str.split("");
const tokens = ["&&", "||", "!", ",", "-", "(", ")"];
const not_operators = ["!", "-"];
const stack = [];
for (let i = 0; i < str.length; i++) {
if (tokens.includes(str[i])) {
stack.push({ token: str[i], idx: i });
}
if (str[i + 1] && tokens.includes(str[i] + str[i + 1])) {
stack.push({ token: str[i] + str[i + 1], idx: i });
}
}
if (stack[0] && stack[0].idx === 0 && not_operators.includes(stack[0].token)) {
str_arr[0] = "#" + stack[0].token;
}
for (let i = 1; i < stack.length; i++) {
if (not_operators.includes(stack[i].token)) {
if (stack[i - 1].token !== ")" && stack[i - 1].token.length + stack[i - 1].idx === stack[i].idx) {
str_arr[stack[i].idx] = "#" + stack[i].token;
}
}
}
return str_arr.join("");
}
function split(str: string) {
const operators = ["&&", "||", "#!", "(%", "%)", ",", "#-"];
const temp_char = "#$#";
str = not_operator_fix(str);
str = detect_brackets(str);
if (str === "##errror##") {
return []
}
console.log(str);
for (const operator of operators) {
str = str.replaceAll(operator, temp_char + operator + temp_char);
}
const arr = str.split(temp_char).map((el) => el.trim()).filter((el) => el !== "");
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "(%") {
arr[i] = "(";
}
if (arr[i] === "#-") {
arr[i] = "-";
}
if (arr[i] === "#!") {
arr[i] = "!";
}
if (arr[i] === "%)") {
arr[i] = ")";
}
}
return arr;
}
return split(query)
}
function build_ast(str: string) {
const tokens = tokenize(str)
if (tokens.length === 0) {
return { error: true }
}
const ast = parse(tokens)
return ast
}
console.log(JSON.stringify(build_ast("forest&&height>=1920&&width>=1080")))