-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.ts
241 lines (201 loc) · 8.13 KB
/
search.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
241
import { Opcode } from "./opcode"
let strRegex = /^"([^"]*)"/;
let keywordRegex = /^(and|or)\b/;
let operatorRegex = /^(<=|>=|=|<|>)/;
let propRegex = /^\.((?:number|num|name|opcode|op|length|len|group)\b|#)/;
let numberRegex = /^(0x[\da-f]{1,2}|0o[0-8]{1,3}|(?:0b[01]{1,8})|(?:[1-9]\d{0,2}|\d(?!\d)))\b/i;
function parseIntFromPrefixedString(num: string) {
let radix = 10;
if (num.length > 2) {
switch (num[1].toUpperCase()) {
case "X": radix = 16; break;
case "O": radix = 8; break;
case "B": radix = 2; break;
}
}
return parseInt(radix !== 10 ? num.substr(2) : num, radix);
}
class ParsedSearch {
ty: string;
val: any;
prop_ty: any;
supportedOperators: string[];
constructor(ty: string, val: string | any[] | any) {
this.ty = ty;
this.val = val;
if (this.ty === "prop") {
this.prop_ty = null;
if (["number", "num", "opcode", "op", "len", "length"].includes(this.val)) {
this.prop_ty = "number";
} else if (["name", "group"].includes(this.val)) {
this.prop_ty = "str";
}
switch (this.prop_ty) {
case "str": this.supportedOperators = ["="]; break;
case "number": this.supportedOperators = ["<", "<=", "=", ">", ">="]; break;
default: throw new Error(`Unexpected prop type '${this.prop_ty}'`);
}
}
}
push(val: any) {
switch (this.ty) {
case "keyword":
this.val.right.push(val);
break;
case "block":
this.val.push(val);
break;
case "operator":
if (this.val.right !== null) throw new Error("operator::right !== null");
this.val.right = val;
break;
default: throw new TypeError();
}
}
pop() {
switch (this.ty) {
case "keyword": return this.val.right.pop();
case "block": return this.val.pop();
default: throw new TypeError();
}
}
get length() {
switch (this.ty) {
case "keyword": return this.val.right.length;
case "block": return this.val.length;
default: throw new TypeError();
}
}
execOp(opcode: any, opcodeNumber: any, defaultRet: boolean) {
this.ty === "prop" ? defaultRet : this.exec(opcode, opcodeNumber)
}
eval(opcode, opcodeNumber: any) {
switch (this.ty) {
case "number": return parseIntFromPrefixedString(this.val);
case "str": return this.val;
case "prop": {
switch (this.val) {
case "number":
case "num":
case "opcode":
case "op":
case "#":
return opcodeNumber;
case "name": return opcode.Name !== "UNUSED" ? opcode.Name : "";
case "group": return opcode.Name !== "UNUSED" ? opcode.Group : "";
case "length":
case "len": return opcode.Name !== "UNUSED" ? opcode.Length : null;
default: throw new Error(`unregistered prop: ${this.val}`);
}
}
default: throw new Error(`unexpected primitive type in search: ${this.ty}`)
}
}
exec(opcode: Opcode, opcodeNumber: number, defaultTrue: boolean = false) {
switch (this.ty) {
case "block":
for (let node of this.val) if (!node.exec(opcode, opcodeNumber, true)) return false;
return true;
case "keyword": {
if (this.val.val === "and") {
return this.val.left.exec(opcode, opcodeNumber, true) && this.val.right.exec(opcode, opcodeNumber, true);
}
if (this.val.val === "or") {
return this.val.left.exec(opcode, opcodeNumber, true) || this.val.right.exec(opcode, opcodeNumber, true);
}
throw new Error("error made it past validation!!!");
}
case "operator": {
const lhs = this.val.left.eval(opcode, opcodeNumber);
const rhs = this.val.right.eval(opcode, opcodeNumber);
switch (this.val.val) {
case "<": return lhs < rhs;
case "<=": return lhs <= rhs;
case "=": return this.val.right.ty === "str" ? lhs.toUpperCase().includes(rhs.toUpperCase()) : lhs === rhs;
case ">=": return lhs >= rhs;
case ">": return lhs > rhs;
}
}
default:
if (!defaultTrue) throw new Error(`unexpected ty: "${this.ty}"`);
else return true;
}
}
}
function lexOpcodeSearch(offset: number, str: string, result: any) {
let strMatches = strRegex.exec(str.substr(offset));
if (strMatches) {
result.push(new ParsedSearch("str", strMatches[1]));
return strMatches[0].length;
}
let keywordMatches = keywordRegex.exec(str.substr(offset));
if (keywordMatches) {
result.r = new ParsedSearch("keyword", { val: keywordMatches[1], left: result.r, right: new ParsedSearch("block", []) })
return keywordMatches[0].length;
}
let operatorMatches = operatorRegex.exec(str.substr(offset));
if (operatorMatches) {
if (result.r.length == 0 || result.next_op !== null) throw new Error();
result.next_op = new ParsedSearch("operator", { val: operatorMatches[1], left: result.r.pop(), right: null });
return operatorMatches[0].length;
}
let propMatches = propRegex.exec(str.substr(offset));
if (propMatches) {
result.push(new ParsedSearch("prop", propMatches[1]));
return propMatches[0].length;
}
let numberMatches = numberRegex.exec(str.substr(offset));
if (numberMatches) {
result.push(new ParsedSearch("number", numberMatches[1]));
return numberMatches[0].length;
}
if (str[offset] === " ") {
return 1;
}
}
function parseOpcodeSearch(str: string) {
let result = {
r: new ParsedSearch("block", []), next_op: null, push: function(val: any) {
(this.next_op || this.r).push(val);
this.next_op && this.r.push(this.next_op);
this.next_op = null;
}
};
let i = 0;
while (i < str.length) {
i += lexOpcodeSearch(i, str, result);
}
return result.r;
}
export function runOpcodeSearch(str: string, opcode: any, opcodeNumber: number) {
return runValidatedOpcodeSearch(fetchOpcodeSearch(str), opcode, opcodeNumber);
}
export function fetchOpcodeSearch(str: string) {
let parsed = parseOpcodeSearch(str);
if (!validateOpcodeSearch(parsed)) {
return null;
}
return parsed;
}
export function runValidatedOpcodeSearch(search: ParsedSearch | null, opcode: any, opcodeNumber: number) {
return search !== null ? search.exec(opcode, opcodeNumber) : true;
}
function validateOpcodeSearch(parsed: ParsedSearch) {
switch (parsed.ty) {
case "keyword": return (validateOpcodeSearch(parsed.val.left) && validateOpcodeSearch(parsed.val.right))
case "operator": {
const left = parsed.val.left;
const right = parsed.val.right;
const op = parsed.val.val;
return left.ty === "prop" && left.supportedOperators.includes(op) && left.prop_ty === right.ty;
}
case "block": {
for (let node of parsed.val) {
if (!validateOpcodeSearch(node)) return false;
}
return true;
}
case "prop": return true; // happens in situations where a prop is still being typed like `.len=2 .name or .name="LD"`
default: throw new Error(`unexpected ty in validation: ${parsed.ty}`);
}
}