Skip to content

Commit

Permalink
refactor: rewrite expression evaluation, fix #130
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 25, 2019
1 parent bd8a1fe commit e0965c6
Show file tree
Hide file tree
Showing 31 changed files with 407 additions and 246 deletions.
2 changes: 1 addition & 1 deletion src/builtin/filters/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray, last } from '../../util/underscore'
import { isTruthy } from '../../render/syntax'
import { isTruthy } from '../../render/boolean'

export default {
'join': (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg),
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/filters/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFalsy } from '../../render/syntax'
import { isFalsy } from '../../render/boolean'
import { toValue } from '../../util/underscore'

export default {
Expand Down
4 changes: 1 addition & 3 deletions src/builtin/tags/assign.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { ITagImplOptions, TagToken, Context } from '../../types'

const re = new RegExp(`(${identifier.source})\\s*=([^]*)`)

Expand Down
7 changes: 3 additions & 4 deletions src/builtin/tags/case.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { evalExp } from '../../render/syntax'
import { Expression, Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand Down Expand Up @@ -28,8 +27,8 @@ export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = await evalExp(branch.val, ctx)
const cond = await evalExp(this.cond, ctx)
const val = new Expression(branch.val).value(ctx)
const cond = new Expression(this.cond).value(ctx)
if (val === cond) {
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
Expand Down
8 changes: 4 additions & 4 deletions src/builtin/tags/cycle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import { Expression } from '../../render/expression'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
Expand All @@ -13,7 +13,7 @@ export default {
let match: RegExpExecArray | null = groupRE.exec(tagToken.args) as RegExpExecArray
assert(match, `illegal tag: ${tagToken.raw}`)

this.group = match[1] || ''
this.group = new Expression(match[1])
const candidates = match[2]

this.candidates = []
Expand All @@ -25,7 +25,7 @@ export default {
},

render: async function (ctx: Context) {
const group = await evalValue(this.group, ctx)
const group = this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]
Expand All @@ -38,6 +38,6 @@ export default {
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx

return evalValue(candidate, ctx)
return new Expression(candidate).value(ctx)
}
} as ITagImplOptions
4 changes: 2 additions & 2 deletions src/builtin/tags/for.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { isString, isObject, isArray } from '../../util/underscore'
import { parseExp } from '../../render/syntax'
import { Expression } from '../../render/expression'
import { assert } from '../../util/assert'
import { identifier, value, hash } from '../../parser/lexical'
import { ForloopDrop } from '../../drop/forloop-drop'
Expand Down Expand Up @@ -37,7 +37,7 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await parseExp(this.collection, ctx)
let collection = new Expression(this.collection).value(ctx)

if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/if.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hash, Emitter, evalExp, isTruthy, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { Hash, Emitter, isTruthy, Expression, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand Down Expand Up @@ -29,7 +29,7 @@ export default {

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (const branch of this.branches) {
const cond = await evalExp(branch.cond, ctx)
const cond = new Expression(branch.cond).value(ctx)
if (isTruthy(cond)) {
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
Expand Down
7 changes: 3 additions & 4 deletions src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from '../../util/assert'
import { Hash, Emitter, TagToken, Context, ITagImplOptions } from '../../types'
import { Expression, Hash, Emitter, TagToken, Context, ITagImplOptions } from '../../types'
import { value, quotedLine } from '../../parser/lexical'
import { evalValue, parseValue } from '../../render/syntax'
import BlockMode from '../../context/block-mode'

const staticFileRE = /[^\s,]+/
Expand All @@ -25,7 +24,7 @@ export default {
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
} else {
filepath = await evalValue(this.value, ctx)
filepath = new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
Expand All @@ -38,7 +37,7 @@ export default {
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = await parseValue(this.with, ctx)
hash[filepath] = new Expression(this.with).evaluate(ctx)
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
Expand Down
5 changes: 2 additions & 3 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import { TagToken, Token, Context, ITagImplOptions } from '../../types'
import { Expression, TagToken, Token, Context, ITagImplOptions } from '../../types'
import BlockMode from '../../context/block-mode'
import { Hash } from '../../template/tag/hash'

Expand All @@ -23,7 +22,7 @@ export default {
},
render: async function (ctx: Context, hash: Hash) {
const layout = ctx.opts.dynamicPartials
? await evalValue(this.layout, ctx)
? await (new Expression(this.layout).value(ctx))
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)

Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/tablerow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from '../../util/assert'
import { evalExp, Emitter, Hash, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { Expression, Emitter, Hash, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { identifier, value, hash } from '../../parser/lexical'
import { TablerowloopDrop } from '../../drop/tablerowloop-drop'

Expand Down Expand Up @@ -29,7 +29,7 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await evalExp(this.collection, ctx) || []
let collection = new Expression(this.collection).value(ctx) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit

Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/unless.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Emitter, evalExp, isFalsy, ParseStream, Context, ITagImplOptions, Token, Hash, TagToken } from '../../types'
import { Emitter, Expression, isFalsy, ParseStream, Context, ITagImplOptions, Token, Hash, TagToken } from '../../types'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand All @@ -21,7 +21,7 @@ export default {
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const cond = await evalExp(this.cond, ctx)
const cond = new Expression(this.cond).value(ctx)
isFalsy(cond)
? await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
: await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
Expand Down
18 changes: 18 additions & 0 deletions src/parser/literal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { last } from '../util/underscore'
import { NullDrop } from '../drop/null-drop'
import { EmptyDrop } from '../drop/empty-drop'
import { BlankDrop } from '../drop/blank-drop'

type literal = true | false | NullDrop | EmptyDrop | BlankDrop | number | string

export function parseLiteral (str: string): literal | undefined {
str = str.trim()

if (str === 'true') return true
if (str === 'false') return false
if (str === 'nil' || str === 'null') return new NullDrop()
if (str === 'empty') return new EmptyDrop()
if (str === 'blank') return new BlankDrop()
if (!isNaN(Number(str))) return Number(str)
if ((str[0] === '"' || str[0] === "'") && str[0] === last(str)) return str.slice(1, -1)
}
6 changes: 6 additions & 0 deletions src/render/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function isTruthy (val: any): boolean {
return !isFalsy(val)
}
export function isFalsy (val: any): boolean {
return val === false || undefined === val || val === null
}
82 changes: 82 additions & 0 deletions src/render/expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { assert } from '../util/assert'
import { isRange, rangeValue } from './range'
import { Value } from './value'
import { Context } from '../context/context'
import { toValue } from '../util/underscore'
import { isOperator, precedence, operatorImpls } from './operator'

export class Expression {
private str: string

public constructor (str: string = '') {
this.str = str
}
public evaluate (ctx: Context): any {
assert(ctx, 'unable to evaluate: context not defined')

const operands = []
for (const token of toPostfix(this.str)) {
if (isOperator(token)) {
const r = operands.pop()
const l = operands.pop()
const result = operatorImpls[token](l, r)
operands.push(result)
continue
}
if (isRange(token)) {
operands.push(rangeValue(token, ctx))
continue
}
operands.push(new Value(token).evaluate(ctx))
}
return operands[0]
}
public value (ctx: Context): any {
return toValue(this.evaluate(ctx))
}
}

function * tokenize (expr: string): IterableIterator<string> {
const N = expr.length
let str = ''
const pairs = { '"': '"', "'": "'", '[': ']', '(': ')' }

for (let i = 0; i < N; i++) {
const c = expr[i]
switch (c) {
case '[':
case '"':
case "'":
str += c
while (i + 1 < N) {
str += expr[++i]
if (expr[i] === pairs[c]) break
}
break
case ' ':
case '\t':
case '\n':
if (str) yield str
str = ''
break
default:
str += c
}
}
if (str) yield str
}

function * toPostfix (expr: string): IterableIterator<string> {
const ops = []
for (const token of tokenize(expr)) {
if (isOperator(token)) {
while (ops.length && precedence[ops[ops.length - 1]] > precedence[token]) {
yield ops.pop()!
}
ops.push(token)
} else yield token
}
while (ops.length) {
yield ops.pop()!
}
}
59 changes: 59 additions & 0 deletions src/render/operator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { isComparable } from '../drop/icomparable'
import { isFunction } from '../util/underscore'
import { isTruthy } from '../render/boolean'

export const precedence = {
'==': 1,
'!=': 1,
'>': 1,
'<': 1,
'>=': 1,
'<=': 1,
'contains': 1,
'and': 0,
'or': 0
}

export const operatorImpls: {[key: string]: (lhs: any, rhs: any) => boolean} = {
'==': (l: any, r: any) => {
if (isComparable(l)) return l.equals(r)
if (isComparable(r)) return r.equals(l)
return l === r
},
'!=': (l: any, r: any) => {
if (isComparable(l)) return !l.equals(r)
if (isComparable(r)) return !r.equals(l)
return l !== r
},
'>': (l: any, r: any) => {
if (isComparable(l)) return l.gt(r)
if (isComparable(r)) return r.lt(l)
return l > r
},
'<': (l: any, r: any) => {
if (isComparable(l)) return l.lt(r)
if (isComparable(r)) return r.gt(l)
return l < r
},
'>=': (l: any, r: any) => {
if (isComparable(l)) return l.geq(r)
if (isComparable(r)) return r.leq(l)
return l >= r
},
'<=': (l: any, r: any) => {
if (isComparable(l)) return l.leq(r)
if (isComparable(r)) return r.geq(l)
return l <= r
},
'contains': (l: any, r: any) => {
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
},
'and': (l: any, r: any) => isTruthy(l) && isTruthy(r),
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
}

const list = Object.keys(precedence)

export function isOperator (token: string) {
return list.includes(token)
}
17 changes: 17 additions & 0 deletions src/render/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { rangeLine } from '../parser/lexical'
import { Context } from '../context/context'
import { range } from '../util/underscore'
import { Value } from './value'

export function isRange (token: string = '') {
return token[0] === '(' && token[token.length - 1] === ')'
}

export function rangeValue (token: string = '', ctx: Context) {
let match
if ((match = token.match(rangeLine))) {
const low = new Value(match[1]).value(ctx)
const high = new Value(match[2]).value(ctx)
return range(+low, +high + 1)
}
}

0 comments on commit e0965c6

Please sign in to comment.