forked from harttle/liquidjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.ts
More file actions
100 lines (90 loc) · 3.64 KB
/
Copy pathexpression.ts
File metadata and controls
100 lines (90 loc) · 3.64 KB
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
import { QuotedToken, RangeToken, OperatorToken, Token, PropertyAccessToken, OperatorType, operatorTypes, GroupedExpressionToken } from '../tokens'
import { isRangeToken, isPropertyAccessToken, isGroupedExpressionToken, UndefinedVariableError, range, isOperatorToken, assert } from '../util'
import type { Context } from '../context'
import type { UnaryOperatorHandler } from '../render'
import { Drop } from '../drop'
export class Expression {
readonly postfix: Token[]
public constructor (tokens: IterableIterator<Token>) {
this.postfix = [...toPostfix(tokens)]
}
public * evaluate (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
assert(ctx, 'unable to evaluate: context not defined')
const operands: any[] = []
for (const token of this.postfix) {
if (isOperatorToken(token)) {
const r = operands.pop()
let result
if (operatorTypes[token.operator] === OperatorType.Unary) {
result = yield (ctx.opts.operators[token.operator] as UnaryOperatorHandler)(r, ctx)
} else {
const l = operands.pop()
result = yield ctx.opts.operators[token.operator](l, r, ctx)
}
operands.push(result)
} else {
operands.push(yield evalToken(token, ctx, lenient))
}
}
return operands[0]
}
public valid () {
return !!this.postfix.length
}
}
export function * evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> {
if (!token) return
if ('content' in token) return token.content
if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient)
if (isRangeToken(token)) return yield evalRangeToken(token, ctx)
if (isGroupedExpressionToken(token)) return yield evalGroupedExpressionToken(token, ctx, lenient)
}
function * evalGroupedExpressionToken (token: GroupedExpressionToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {
assert(token.resolvedFilters, 'grouped expression filters not resolved')
lenient = lenient || (ctx.opts.lenientIf && token.filters.length > 0 && token.filters[0].name === 'default')
let val = yield token.initial.evaluate(ctx, lenient)
for (const filter of token.resolvedFilters!) {
val = yield filter.render(val, ctx)
}
return val
}
function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {
const props: (string | number | Drop)[] = []
for (const prop of token.props) {
props.push((yield evalToken(prop, ctx, false)) as unknown as string | number | Drop)
}
try {
if (token.variable) {
const variable = yield evalToken(token.variable, ctx, lenient)
return yield ctx._getFromScope(variable, props)
} else {
return yield ctx._get(props)
}
} catch (e) {
if (lenient && (e as Error).name === 'InternalUndefinedVariableError') return null
throw (new UndefinedVariableError(e as Error, token))
}
}
export function evalQuotedToken (token: QuotedToken) {
return token.content
}
function * evalRangeToken (token: RangeToken, ctx: Context) {
const low: number = yield evalToken(token.lhs, ctx)
const high: number = yield evalToken(token.rhs, ctx)
ctx.memoryLimit.use(high - low + 1)
return range(+low, +high + 1)
}
function * toPostfix (tokens: IterableIterator<Token>): IterableIterator<Token> {
const ops: OperatorToken[] = []
for (const token of tokens) {
if (isOperatorToken(token)) {
while (ops.length && ops[ops.length - 1].getPrecedence() > token.getPrecedence()) {
yield ops.pop()!
}
ops.push(token)
} else yield token
}
while (ops.length) {
yield ops.pop()!
}
}