This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
jsgrep.js
594 lines (527 loc) · 15.3 KB
/
jsgrep.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
var _ = require('underscore');
var Narcissus = require('narcissus/main');
function tokenString(tt) {
var t = Narcissus.definitions.tokens[tt];
return /^\W/.test(t) ? Narcissus.definitions.opTypeNames[t] : t.toUpperCase();
}
/**
* Match a set of jsgrep patterns against an AST. Options are:
*
* - source: AST or String. Source to match against.
* - filename: String. Used for errors, and to read from is source isn't given.
* - patterns: Array of AST or String. List of patterns to match with.
* - pattern: AST or String. Single pattern to match with.
* - strictMatches: Boolean. Controls strict matching.
* - callback: Function. Called with (node, metavariables) when any pattern
* matches an AST node.
*/
var jsgrep = exports.jsgrep = function(options) {
var ast = options.source;
if (!ast) {
ast = require('fs').readFileSync(options.filename).toString();
}
if (ast instanceof String) {
ast = Narcissus.parser.parse(ast, options.filename, 1);
}
var patterns = options.patterns || [ options.pattern ];
for (var i = 0; i < patterns.length; i++) {
if (patterns[i] instanceof String) {
patterns[i] = Narcissus.parser.parse(patterns[i], 'pattern', 0);
}
}
forEachNode(ast, function(node) {
_.each(patterns, function(pattern, patternNum) {
options.variables = {};
if (astIsEqual(node, pattern, options)) {
options.callback.call(null, node, options.variables);
}
});
});
}
/**
* XXX(rpatterson): this needs testing!
*/
function astMatchEllipsis(nodes, patterns, config, matcher) {
const tokens = Narcissus.definitions.tokenIds;
if (!matcher) {
matcher = astIsEqual;
}
function go(i, j) {
if (i == nodes.length && j == patterns.length) {
return true;
}
if (j == patterns.length) {
return false;
}
if (patterns[j].type == tokens.ELLIPSIS && j == patterns.length - 1) {
return true;
}
if(i == nodes.length) {
return false;
}
if (patterns[j].type == tokens.ELLIPSIS) {
// ..., A, ... is ambiguous, and if A was matched later in the pattern, we
// would have to backtrack to the ellipsis to try a different match.
// That's annoying.
// XXX(rpatterson): This incorrectly bails for (..., A)
config.failOnMetavar = true;
var ret = go(i, j + 1) || go(i + 1, j);
config.failOnMetavar = false;
return ret;
}
return matcher(nodes[i], patterns[j], config) &&
go(i + 1, j + 1);
}
var result = go(0, 0);
return result;
}
var astIsEqual = exports.astIsEqual = function(node, pattern, config) {
const tokens = Narcissus.definitions.tokenIds;
if (pattern.type == tokens.IDENTIFIER &&
/^[A-Z](_.*)?$/.test(pattern.value)) {
if (pattern.value in config.variables) {
// Variable already matched, compare this node to that value
return astIsEqual(node, config.variables[pattern.value]);
} else {
if (config.failOnMetavar) {
throw {
message: "Matching metavariables inside partially-matched lists is " +
"unsupported. Sorry!"
};
}
// Bind variable to this value
config.variables[pattern.value] = node;
return true;
}
}
if (node.type != pattern.type) {
return false;
}
if (node.type == tokens.OBJECT_INIT && !config.strictMatches) {
// Strict matching will be handled normally (below).
if (pattern.children.length > node.children.length) {
return false;
}
var keys = _.clone(pattern.children);
for (var i = 0; i < node.children.length; i++) {
for (var j = 0; j < keys.length;) {
config.failOnMetavar = true;
var match = astIsEqual(node.children[i], keys[j], config);
config.failOnMetavar = false;
if (match) {
keys.splice(j, 1);
break;
} else {
j++;
}
}
if (keys.length == 0) {
break;
}
}
// No keys left over -> match.
return keys.length == 0;
}
switch(node.type) {
// Core values
case tokens.FALSE:
case tokens.IDENTIFIER:
case tokens.NULL:
case tokens.NUMBER:
case tokens.REGEXP:
case tokens.STRING:
case tokens.THIS:
case tokens.TRUE:
return node.value == pattern.value;
break;
// 0-child statements
case tokens.BREAK:
case tokens.CONTINUE:
return true;
break;
// Unary expressions
case tokens.BITWISE_NOT:
case tokens.DECREMENT:
case tokens.INCREMENT:
case tokens.NEW:
case tokens.NEW_WITH_ARGS:
case tokens.NOT:
case tokens.TYPEOF:
case tokens.UNARY_MINUS:
case tokens.UNARY_PLUS:
case tokens.VOID:
// Binary expressions
case tokens.AND:
case tokens.BITWISE_AND:
case tokens.BITWISE_OR:
case tokens.BITWISE_XOR:
case tokens.DIV:
case tokens.EQ:
case tokens.GE:
case tokens.GT:
case tokens.IN:
case tokens.INSTANCEOF:
case tokens.LE:
case tokens.LSH:
case tokens.LT:
case tokens.MINUS:
case tokens.MOD:
case tokens.MUL:
case tokens.NE:
case tokens.OR:
case tokens.PLUS:
case tokens.RSH:
case tokens.STRICT_EQ:
case tokens.STRICT_NE:
case tokens.ULSH:
case tokens.URSH:
// Other
case tokens.ASSIGN:
//case tokens.BLOCK:
case tokens.CALL:
case tokens.COMMA:
case tokens.DELETE:
case tokens.DOT:
case tokens.HOOK:
case tokens.INDEX:
// Special
case tokens.OBJECT_INIT:
//case tokens.SCRIPT:
if (node.children.length == pattern.children.length) {
for (var i = 0; i < node.children.length; i++) {
if (!astIsEqual(node.children[i], pattern.children[i], config)) {
return false;
}
}
return true;
}
break;
case tokens.PROPERTY_INIT:
if (node.children.length != 2 || pattern.children.length != 2) {
throw {
message: "astIsEqual: Unsupported PROPERTY_INIT"
};
}
// In strict mode, the ordering will prevent an ambiguous match.
config.failOnMetavar = !config.strictMatches;
var match = astIsEqual(node.children[0], pattern.children[0], config);
config.failOnMetavar = false;
if (!match) {
return false;
}
return astIsEqual(node.children[1], pattern.children[1], config);
break;
case tokens.ARRAY_INIT:
case tokens.LIST:
return astMatchEllipsis(node.children, pattern.children, config);
break;
case tokens.LET:
case tokens.VAR:
// All of var's children are IDENTIFIERs with name/initializer values
// TODO: this does not support destructuring assignments
// XXX: Note that "A = B" as a pattern will *not* match "var A = B"
if (config.strictMatches) {
// TODO: this is using astMatchEllipsis even though the parser won't
// accept an ellipsis in a var.
return astMatchEllipsis(node.children, pattern.children, config,
function(node, pattern, config) {
if (!astIsEqual(node, pattern, config)) {
return false;
}
// If the pattern has an initializer, it must be equal to the one in
// the source.
if (pattern.initializer && (!node.initializer ||
!astIsEqual(node.initializer, pattern.initializer, config))) {
return false;
}
// If in strict mode and the pattern has no initializer, neither can
// the source.
if (!pattern.initializer && config.strictMatches &&
node.initializer) {
return false;
}
return true;
});
} else {
if (pattern.children.length > node.children.length) {
return false;
}
var keys = _.clone(pattern.children);
for (var i = 0; i < node.children.length; i++) {
for (var j = 0; j < keys.length;) {
config.failOnMetavar = true;
var match = astIsEqual(node.children[i], keys[j], config);
config.failOnMetavar = false;
if (match) {
// If the pattern has an initializer, it must be equal to the one
// in the source.
if (keys[j].initializer &&
(!node.children[i].initializer ||
!astIsEqual(node.children[i].initializer,
keys[j].initializer, config))) {
return false;
}
keys.splice(j, 1);
break;
} else {
j++;
}
}
if (keys.length == 0) {
break;
}
}
// No keys left over -> match.
return keys.length == 0;
}
break;
case tokens.SEMICOLON:
if (!node.expression && !pattern.expression) {
return true;
} else if (node.expression && pattern.expression) {
return astIsEqual(node.expression, pattern.expression, config);
}
return false;
break;
//case tokens.DO:
//forEachNode(node.body, callback);
//forEachNode(node.condition, callback);
break;
//case tokens.WHILE:
//forEachNode(node.condition, callback);
//forEachNode(node.body, callback);
break;
//case tokens.FUNCTION:
//forEachNode(node.body, callback);
break;
//case tokens.RETURN:
if (node.value) {
//forEachNode(node.value, callback);
}
break;
//case tokens.SWITCH:
//forEachNode(node.discriminant, callback);
_.each(node.cases, function(child) {
//forEachNode(child, callback);
});
break;
//case tokens.DEFAULT:
//forEachNode(node.statements, callback);
break;
//case tokens.CASE:
//forEachNode(node.caseLabel, callback);
//forEachNode(node.statements, callback);
break;
//case tokens.LABEL:
//forEachNode(node.statement, callback);
break;
//case tokens.FOR_IN:
//forEachNode(node.iterator, callback);
//forEachNode(node.object, callback);
//forEachNode(node.body, callback);
break;
//case tokens.FOR:
//forEachNode(node.setup, callback);
//forEachNode(node.condition, callback);
//forEachNode(node.update, callback);
//forEachNode(node.body, callback);
break;
//case tokens.IF:
//forEachNode(node.condition, callback);
//forEachNode(node.thenPart, callback);
if (node.elsePart) {
//forEachNode(node.elsePart, callback);
}
break;
//case tokens.TRY:
//forEachNode(node.tryBlock, callback);
_.each(node.catchClauses, function(child) {
//forEachNode(child, callback);
});
if (node.finallyBlock) {
//forEachNode(node.finallyBlock, callback);
}
break;
//case tokens.CATCH:
if (node.guard) {
//forEachNode(node.guard, callback);
}
//forEachNode(node.block);
break;
case tokens.THROW:
return astIsEqual(node.exception, pattern.exception, config);
break;
default:
throw {
message: "Pattern type is not yet supported: " + tokenString(node.type)
};
break;
}
}
var forEachNode = exports.forEachNode = function(node, callback) {
const tokens = Narcissus.definitions.tokenIds;
callback(node);
switch(node.type) {
// Core values
case tokens.FALSE:
case tokens.NULL:
case tokens.NUMBER:
case tokens.REGEXP:
case tokens.STRING:
case tokens.THIS:
case tokens.TRUE:
// 0-child statements
case tokens.BREAK:
case tokens.CONTINUE:
break;
// Unary expressions
case tokens.BITWISE_NOT:
case tokens.DECREMENT:
case tokens.INCREMENT:
case tokens.NEW:
case tokens.NEW_WITH_ARGS:
case tokens.NOT:
case tokens.TYPEOF:
case tokens.UNARY_MINUS:
case tokens.UNARY_PLUS:
case tokens.VOID:
// Binary expressions
case tokens.AND:
case tokens.BITWISE_AND:
case tokens.BITWISE_OR:
case tokens.BITWISE_XOR:
case tokens.DIV:
case tokens.EQ:
case tokens.GE:
case tokens.GT:
case tokens.IN:
case tokens.INSTANCEOF:
case tokens.LE:
case tokens.LSH:
case tokens.LT:
case tokens.MINUS:
case tokens.MOD:
case tokens.MUL:
case tokens.NE:
case tokens.OR:
case tokens.PLUS:
case tokens.RSH:
case tokens.STRICT_EQ:
case tokens.STRICT_NE:
case tokens.ULSH:
case tokens.URSH:
// Other
case tokens.ASSIGN:
case tokens.BLOCK:
case tokens.CALL:
case tokens.COMMA:
case tokens.CONST:
case tokens.DELETE:
case tokens.DOT:
case tokens.HOOK:
case tokens.INDEX:
case tokens.LET:
case tokens.VAR:
// Special
case tokens.ARRAY_INIT:
case tokens.LIST:
case tokens.OBJECT_INIT:
case tokens.PROPERTY_INIT:
case tokens.SCRIPT:
_.each(node.children, function(child) {
forEachNode(child, callback);
});
break;
case tokens.IDENTIFIER:
if (node.initializer) {
forEachNode(node.initializer, callback);
}
break;
case tokens.SEMICOLON:
if (node.expression) {
forEachNode(node.expression, callback);
}
break;
case tokens.DO:
forEachNode(node.body, callback);
forEachNode(node.condition, callback);
break;
case tokens.WHILE:
forEachNode(node.condition, callback);
forEachNode(node.body, callback);
break;
case tokens.FUNCTION:
forEachNode(node.body, callback);
break;
case tokens.RETURN:
if (node.value) {
forEachNode(node.value, callback);
}
break;
case tokens.SWITCH:
forEachNode(node.discriminant, callback);
_.each(node.cases, function(child) {
forEachNode(child, callback);
});
break;
case tokens.DEFAULT:
forEachNode(node.statements, callback);
break;
case tokens.CASE:
forEachNode(node.caseLabel, callback);
forEachNode(node.statements, callback);
break;
case tokens.LABEL:
forEachNode(node.statement, callback);
break;
case tokens.FOR_IN:
forEachNode(node.iterator, callback);
forEachNode(node.object, callback);
forEachNode(node.body, callback);
break;
case tokens.FOR:
if (node.setup) {
forEachNode(node.setup, callback);
}
if (node.condition) {
forEachNode(node.condition, callback);
}
if (node.update) {
forEachNode(node.update, callback);
}
forEachNode(node.body, callback);
break;
case tokens.IF:
forEachNode(node.condition, callback);
forEachNode(node.thenPart, callback);
if (node.elsePart) {
forEachNode(node.elsePart, callback);
}
break;
case tokens.TRY:
forEachNode(node.tryBlock, callback);
_.each(node.catchClauses, function(child) {
forEachNode(child, callback);
});
if (node.finallyBlock) {
forEachNode(node.finallyBlock, callback);
}
break;
case tokens.CATCH:
if (node.guard) {
forEachNode(node.guard, callback);
}
forEachNode(node.block, callback);
break;
case tokens.THROW:
forEachNode(node.exception, callback);
break;
default:
console.error(node.toString());
throw {
message: "forEachNode: " + node.tokenizer.filename + ":" + node.lineno +
": Unimplemented node type " + tokenString(node.type)
};
break;
}
}