-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpunyexpr.js
564 lines (502 loc) · 18 KB
/
punyexpr.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
(function (exports) {
'use strict'
const TOKEN_TYPE_LITERAL = 'literal'
const TOKEN_TYPE_IDENTIFIER = 'identifier'
const TOKEN_TYPE_REGEX = 'regex'
const TOKEN_TYPE_SYMBOL = 'symbol'
const FUNCTION = 'function'
class PunyExprError extends Error {
constructor (name, message, offset) {
super(message)
this.name = name
this.offset = offset
}
}
PunyExprError.throw = (name, message, offset) => {
throw new PunyExprError(name, message, offset)
}
const tokenize = (() => {
// Removed false,null,undefined,true,typeof
const JS_FORBIDDEN_KEYWORDS = 'abstract,arguments,await,boolean,break,byte,case,catch,char,class,const,continue,debugger,default,delete,do,double,else,enum,eval,export,extends,final,finally,float,for,function,goto,if,implements,import,in,instanceof,int,interface,let,long,native,new,package,private,protected,public,return,short,static,super,switch,synchronized,this,throw,throws,transient,try,var,void,volatile,while,with,yield'.split(',')
const TOKEN_REGEXP_SINGLE_QUOTE_STRING = /'((?:[^'\\]|\\.)*)'/
const TOKEN_REGEXP_DOUBLE_QUOTE_STRING = /"((?:[^"\\]|\\.)*)"/
const TOKEN_REGEXP_NUMBER = /((?:\d+(?:\.\d*)?|\.\d+))/
const TOKEN_REGEXP_IDENTIFIER = /([a-zA-Z_][a-zA-Z_0-9]*)/
const TOKEN_REGEXP_REGEX = /(\/(?:[^/\\]|\\.)+\/[gmiysd]*)/
const TOKEN_REGEXP_SYMBOL = /(\+|-|\*|\/|\[|\]|\.|\?|:|%|<|=|>|!|&|\||\(|\)|,)/
const TOKEN_REGEXP_SEPARATOR = /(\s)/
const TOKEN_REGEXP_UNKNOWN = /(.)/
const TOKENIZER_REGEXP = new RegExp([
TOKEN_REGEXP_SINGLE_QUOTE_STRING,
TOKEN_REGEXP_DOUBLE_QUOTE_STRING,
TOKEN_REGEXP_NUMBER,
TOKEN_REGEXP_IDENTIFIER,
TOKEN_REGEXP_REGEX,
TOKEN_REGEXP_SYMBOL,
TOKEN_REGEXP_SEPARATOR,
TOKEN_REGEXP_UNKNOWN
].map(re => { const str = re.toString(); return str.substring(1, str.length - 1) }).join('|'), 'g')
const invalidTokenError = offset => PunyExprError.throw('InvalidTokenError', `Invalid token @${offset}`, offset)
const IDENTIFIER_TO_LITERAL = {
false: false,
null: null,
true: true,
undefined,
Infinity,
NaN
}
const TOKENIZER_CONVERTER = [
value => [TOKEN_TYPE_LITERAL, value.replace(/\\'/g, '\'')],
value => [TOKEN_TYPE_LITERAL, value.replace(/\\"/g, '"')],
value => [TOKEN_TYPE_LITERAL, parseFloat(value)],
(value, offset) => {
if (Object.prototype.hasOwnProperty.call(IDENTIFIER_TO_LITERAL, value)) {
return [TOKEN_TYPE_LITERAL, IDENTIFIER_TO_LITERAL[value]]
}
if (JS_FORBIDDEN_KEYWORDS.includes(value)) {
invalidTokenError(offset)
}
return [TOKEN_TYPE_IDENTIFIER, value]
},
value => {
const endPos = value.lastIndexOf('/')
const source = value.substring(1, endPos)
const flags = value.substring(endPos + 1)
return [TOKEN_TYPE_REGEX, [source, flags]]
},
value => [TOKEN_TYPE_SYMBOL, value],
value => [],
(value, offset) => invalidTokenError(offset)
]
return (string) => {
const tokens = []
let offset = 0
let lastTokenType
const requireSeparator = [TOKEN_TYPE_LITERAL, TOKEN_TYPE_IDENTIFIER]
string.replace(TOKENIZER_REGEXP, ({ length }, ...capturedValues) => {
const rawType = capturedValues.findIndex(capturedValue => capturedValue !== undefined)
const capturedValue = capturedValues[rawType]
const converter = TOKENIZER_CONVERTER[rawType]
const [type, value] = converter(capturedValue, offset)
if (requireSeparator.includes(type) && requireSeparator.includes(lastTokenType)) {
invalidTokenError(offset)
}
lastTokenType = type
if (type !== undefined) {
tokens.push([type, value, offset, length])
}
offset += length
})
return tokens
}
})()
// Stryker disable next-line all
const OP_DETAILS = Symbol('punyexpr')
const parse = (() => {
/*
(Extremely) Simplified grammar (based on https://tc39.es/ecma262/#sec-expressions)
⛔ Not yet implemented
➡️ Shortcut
PrimaryExpression :
Identifier
Literal
[ ⟮Expression ⟮, Expression ⟯*⟯? ]
⛔ObjectLiteral
RegularExpressionLiteral
( Expression )
CallOrMemberExpression : 💬 Supports this call thanks to binding
PrimaryExpression
CallOrMemberExpression ( )
CallOrMemberExpression ( Expression ⟮, Expression ⟯* )
CallOrMemberExpression [ Expression ] 💬 If result is a function, it is bound to the left part
CallOrMemberExpression . Identifier 💬 If result is a function, it is bound to the left part
UnaryExpression :
CallExpression
typeof UnaryExpression
+ UnaryExpression
- UnaryExpression
⛔~ UnaryExpression
! UnaryExpression
ExponentiationExpression :
UnaryExpression ** ExponentiationExpression
MultiplicativeExpression :
MultiplicativeExpression * ExponentiationExpression
MultiplicativeExpression / ExponentiationExpression
MultiplicativeExpression % ExponentiationExpression
AdditiveExpression :
AdditiveExpression + MultiplicativeExpression
AdditiveExpression - MultiplicativeExpression
⛔ShiftExpression : ➡️ AdditiveExpression
⛔ShiftExpression << AdditiveExpression
⛔ShiftExpression >> AdditiveExpression
⛔ShiftExpression >>> AdditiveExpression
RelationalExpression :
RelationalExpression < ShiftExpression
RelationalExpression > ShiftExpression
RelationalExpression <= ShiftExpression
RelationalExpression >= ShiftExpression
⛔RelationalExpression instanceof ShiftExpression
⛔RelationalExpression in ShiftExpression
⛔PrivateIdentifier in ShiftExpression
EqualityExpression :
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression
EqualityExpression === RelationalExpression
EqualityExpression !== RelationalExpression
⛔BitwiseANDExpression :
⛔BitwiseANDExpression & EqualityExpression
⛔BitwiseXORExpression :
⛔BitwiseXORExpression ^ BitwiseANDExpression
⛔BitwiseORExpression : ➡️ EqualityExpression
⛔BitwiseORExpression | BitwiseXORExpression
LogicalANDExpression :
LogicalANDExpression && BitwiseORExpression
LogicalORExpression :
LogicalORExpression || LogicalANDExpression
⛔CoalesceExpression :
⛔CoalesceExpressionHead ?? BitwiseORExpression
ShortCircuitExpression : ➡️ LogicalORExpression
LogicalORExpression
⛔CoalesceExpression
ConditionalExpression :
ShortCircuitExpression
ShortCircuitExpression ? AssignmentExpression : AssignmentExpression
Expression :
ConditionalExpression
*/
const buildOp = (name, impl) => (range, ...args) => Object.assign(impl.bind(null, ...args), { [OP_DETAILS]: [name, args, range] })
const constant = buildOp('constant', value => value)
const array = buildOp('array', (...itemsAndContext) => {
const context = itemsAndContext[itemsAndContext.length - 1]
return itemsAndContext.slice(0, -1).map(item => item(context))
})
const regex = buildOp('regex', (pattern, flags, builder) => builder(pattern, flags))
const regexDefaultBuilder = (pattern, flags) => new RegExp(pattern, flags)
const propertyOfContext = buildOp('context', (name, context) => context[name(context)])
const propertyOf = buildOp('property', (object, name, context) => {
const that = object(context)
const result = that[name(context)]
// eslint-disable-next-line valid-typeof
if (typeof result === FUNCTION) {
return result.bind(that)
}
return result
})
const callFunction = buildOp('call', (func, args, context) => func(context).apply(null, args.map(arg => arg(context))))
const pos = buildOp('pos', (value, context) => +value(context))
const neg = buildOp('neg', (value, context) => -value(context))
const not = buildOp('not', (value, context) => !value(context))
const getTypeof = buildOp('getTypeof', (value, context) => typeof value(context))
const exp = buildOp('exp', (value1, value2, context) => value1(context) ** value2(context))
const mul = buildOp('mul', (value1, value2, context) => value1(context) * value2(context))
const div = buildOp('div', (value1, value2, context) => value1(context) / value2(context))
const remainder = buildOp('remainder', (value1, value2, context) => value1(context) % value2(context))
const add = buildOp('add', (value1, value2, context) => value1(context) + value2(context))
const sub = buildOp('sub', (value1, value2, context) => value1(context) - value2(context))
const lt = buildOp('lt', (value1, value2, context) => value1(context) < value2(context))
const gt = buildOp('gt', (value1, value2, context) => value1(context) > value2(context))
const lte = buildOp('lte', (value1, value2, context) => value1(context) <= value2(context))
const gte = buildOp('gte', (value1, value2, context) => value1(context) >= value2(context))
// eslint-disable-next-line eqeqeq
const eq = buildOp('eq', (value1, value2, context) => value1(context) == value2(context))
// eslint-disable-next-line eqeqeq
const neq = buildOp('neq', (value1, value2, context) => value1(context) != value2(context))
const eqq = buildOp('eqq', (value1, value2, context) => value1(context) === value2(context))
const neqq = buildOp('neqq', (value1, value2, context) => value1(context) !== value2(context))
const and = buildOp('and', (value1, value2, context) => value1(context) && value2(context))
const or = buildOp('or', (value1, value2, context) => value1(context) || value2(context))
const ternary = buildOp('ternary', (condition, trueValue, falseValue, context) => {
if (condition(context)) {
return trueValue(context)
}
return falseValue(context)
})
let tokens
let options
const checkNotEndOfExpression = () => {
if (tokens.length === 0) {
PunyExprError.throw('EndOfExpressionError', 'Unexpected end of expression')
}
}
const current = () => {
checkNotEndOfExpression()
return tokens[0]
}
const offset = () => current()[2]
const range = (from, op) => {
let currentRange
if (op) {
currentRange = op[OP_DETAILS][2]
} else {
currentRange = current().splice(2)
}
const [offset, length] = currentRange
return [from, offset + length - from]
}
const shift = (steps = 1) => tokens.splice(0, steps)
const isSymbol = (expected = undefined) => {
if (tokens.length === 0) {
return false
}
const [type, value] = tokens[0]
return (type === TOKEN_TYPE_SYMBOL) && (!expected || expected.includes(value))
}
const shiftOnSymbols = (expected) => {
const nextSymbols = []
for (const token of tokens) {
const [type, value] = token
if (type !== TOKEN_TYPE_SYMBOL) {
break
}
nextSymbols.push(value)
}
const nextSymbolsAggregated = nextSymbols.join('')
const matching = expected
.filter(symbol => nextSymbolsAggregated.startsWith(symbol))
.sort((a, b) => b.length - a.length)[0]
if (matching) {
shift(matching.length)
return matching
}
return false
}
const unexpected = () => PunyExprError.throw('UnexpectedTokenError', `Unexpected token @${offset(tokens)}`, offset(tokens))
const arrayLiteral = () => {
const from = offset()
shift()
const items = []
while (!isSymbol(']')) {
const item = expression()
items.push(item)
if (isSymbol(',')) { // accepts trailing comma
shift()
}
}
const arrayRange = range(from)
shift()
return array(arrayRange, ...items)
}
const primaryExpression = () => {
checkNotEndOfExpression()
if (isSymbol('(')) {
shift()
const result = expression()
if (!isSymbol(')')) {
unexpected()
}
shift()
return result
}
if (isSymbol('[')) {
return arrayLiteral()
}
if (isSymbol()) {
unexpected()
}
const [[type, value, ...valueRange]] = shift()
if (type === TOKEN_TYPE_IDENTIFIER) {
return propertyOfContext(valueRange, constant(valueRange, value))
}
if (type === TOKEN_TYPE_REGEX) {
if (!options.regex) {
const [from] = valueRange
PunyExprError.throw('RegExpDisabledError', `Regular expressions are disabled @${from}`, from)
}
const [pattern, flags] = value
let builder = options.regex
// eslint-disable-next-line valid-typeof
if (typeof builder !== FUNCTION) {
builder = regexDefaultBuilder
}
return regex(valueRange, pattern, flags, builder)
}
return constant(valueRange, value)
}
const CallOrMemberExpression = () => {
const from = offset()
let result = primaryExpression()
const operators = {
'(': () => {
const args = []
while (!isSymbol(')')) {
if (args.length > 0) {
if (!isSymbol(',')) {
unexpected()
}
shift()
}
args.push(expression())
}
const callRange = range(from)
shift()
result = callFunction(callRange, result, args)
},
'[': () => {
const name = expression()
if (!isSymbol(']')) {
unexpected()
}
const propertyRange = range(from)
shift()
result = propertyOf(propertyRange, result, name)
},
'.': () => {
const [type, value, ...valueRange] = current()
if (type !== TOKEN_TYPE_IDENTIFIER) {
unexpected()
}
const propertyRange = range(from)
shift()
result = propertyOf(propertyRange, result, constant(valueRange, value))
}
}
while (isSymbol('([.')) {
const [[, symbol]] = shift()
operators[symbol]()
}
return result
}
const unaryMapper = {
'+': pos,
'-': neg,
'!': not,
typeof: getTypeof
}
const unaryExpression = () => {
const [type, value, from] = current()
const postProcess = isSymbol('+-!') || ((type === TOKEN_TYPE_IDENTIFIER) && value === 'typeof')
if (!postProcess) {
return CallOrMemberExpression()
}
const unaryRange = range(from)
shift()
const func = unaryMapper[value]
return func(unaryRange, unaryExpression())
}
const _recursiveExpression = (subExpression, operators) => {
const expectedSymbols = Object.keys(operators)
return () => {
const from = offset()
let result = subExpression()
let symbol = shiftOnSymbols(expectedSymbols)
while (symbol) {
const sub = subExpression()
const recursiveRange = range(from, sub)
result = operators[symbol](recursiveRange, result, sub)
symbol = shiftOnSymbols(expectedSymbols)
}
return result
}
}
const exponentiationExpression = _recursiveExpression(unaryExpression, {
'**': exp
})
const multiplicativeExpression = _recursiveExpression(exponentiationExpression, {
'*': mul,
'/': div,
'%': remainder
})
const additiveExpression = _recursiveExpression(multiplicativeExpression, {
'+': add,
'-': sub
})
const relationalExpression = _recursiveExpression(additiveExpression, {
'<': lt,
'>': gt,
'<=': lte,
'>=': gte
})
const equalityExpression = _recursiveExpression(relationalExpression, {
'==': eq,
'!=': neq,
'===': eqq,
'!==': neqq
})
const logicalANDExpression = _recursiveExpression(equalityExpression, {
'&&': and
})
const logicalORExpression = _recursiveExpression(logicalANDExpression, {
'||': or
})
const conditionalExpression = () => {
const from = offset()
const condition = logicalORExpression()
if (shiftOnSymbols(['?'])) {
const trueResult = conditionalExpression()
checkNotEndOfExpression()
if (!isSymbol(':')) {
unexpected()
}
shift()
const falseResult = conditionalExpression()
const conditionRange = range(from, falseResult)
return ternary(conditionRange, condition, trueResult, falseResult)
}
return condition
}
const expression = conditionalExpression
return (...args) => {
[tokens, options] = args
const result = expression()
if (tokens.length !== 0) {
PunyExprError.throw('UnexpectedRemainderError', `Unexpected left over tokens @${offset()}`, offset())
}
return result
}
})()
const ro = value => ({
value,
writable: false
})
const assignROProperties = (object, properties) => {
Object.defineProperties(
object,
Object.keys(properties).reduce((dict, property) => {
dict[property] = ro(properties[property])
return dict
}, {})
)
}
const toJSON = expr => {
const [op, args, [at, length]] = expr[OP_DETAILS]
const filtered = {}
return {
op,
at,
length,
args: args
.map(arg => {
// eslint-disable-next-line valid-typeof
if (typeof arg === FUNCTION) {
if (arg[OP_DETAILS]) {
return toJSON(arg)
}
return filtered
}
if (Array.isArray(arg)) {
return arg.map(toJSON)
}
return arg
})
.filter(arg => arg !== filtered)
}
}
const punyexpr = (str, options = {}) => {
const impl = parse(tokenize(str), {
regex: false,
...options
})
const expr = (context = {}) => impl(context)
assignROProperties(expr, {
toJSON: toJSON.bind(null, impl),
toString: () => str
})
return expr
}
assignROProperties(punyexpr, {
tokenize,
Error: PunyExprError,
version: '0.0.0'
})
exports.punyexpr = punyexpr
}(this))