feat: renderSync, parseAndRenderSync and renderFileSync, see #48

This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 8028f82499
commit 7fb01ad69a
66 changed files with 896 additions and 272 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
export class Emitter {
public html: string = '';
public html = '';
public break = false;
public continue = false;
public write (html: string) {
this.html += html
+38 -21
View File
@@ -1,38 +1,55 @@
import { assert } from '../util/assert'
import { isRange, rangeValue } from './range'
import { isRange, rangeValue, rangeValueSync } 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
private operands: any[] = []
private postfix: string[]
public constructor (str: string = '') {
this.str = str
public constructor (str = '') {
this.postfix = [...toPostfix(str)]
}
public evaluate (ctx: Context): any {
public async evaluate (ctx: Context): Promise<any> {
assert(ctx, 'unable to evaluate: context not defined')
const operands = []
for (const token of toPostfix(this.str)) {
for (const token of this.postfix) {
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))
this.evaluateOnce(token)
} else if (isRange(token)) {
this.operands.push(await rangeValue(token, ctx))
} else this.operands.push(await new Value(token).evaluate(ctx))
}
return operands[0]
return this.operands[0]
}
public value (ctx: Context): any {
return toValue(this.evaluate(ctx))
public evaluateSync (ctx: Context): any {
assert(ctx, 'unable to evaluate: context not defined')
for (const token of this.postfix) {
if (isOperator(token)) {
this.evaluateOnce(token)
} else if (isRange(token)) {
this.operands.push(rangeValueSync(token, ctx))
} else {
const val = new Value(token).evaluateSync(ctx)
this.operands.push(val)
}
}
return this.operands[0]
}
public async value (ctx: Context): Promise<any> {
return toValue(await this.evaluate(ctx))
}
public valueSync (ctx: Context): any {
return toValue(this.evaluateSync(ctx))
}
private evaluateOnce (token: string) {
const r = this.operands.pop()
const l = this.operands.pop()
const result = operatorImpls[token](l, r)
this.operands.push(result)
}
}
+13 -4
View File
@@ -3,15 +3,24 @@ import { Context } from '../context/context'
import { range } from '../util/underscore'
import { Value } from './value'
export function isRange (token: string = '') {
export function isRange (token: string) {
return token[0] === '(' && token[token.length - 1] === ')'
}
export function rangeValue (token: string = '', ctx: Context) {
export async 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)
const low = await new Value(match[1]).value(ctx)
const high = await new Value(match[2]).value(ctx)
return range(+low, +high + 1)
}
}
export function rangeValueSync (token: string, ctx: Context) {
let match
if ((match = token.match(rangeLine))) {
const low = new Value(match[1]).valueSync(ctx)
const high = new Value(match[2]).valueSync(ctx)
return range(+low, +high + 1)
}
}
+17 -4
View File
@@ -4,13 +4,26 @@ import { ITemplate } from '../template/itemplate'
import { Emitter } from './emitter'
export class Render {
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()) {
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()): Promise<string> {
for (const tpl of templates) {
try {
await tpl.render(ctx, emitter)
const html = await tpl.render(ctx, emitter)
html && emitter.write(html)
if (emitter.break || emitter.continue) break
} catch (e) {
if (e.name === 'RenderBreakError') throw e
throw e.name === 'RenderError' ? e : new RenderError(e, tpl)
throw RenderError.is(e) ? e : new RenderError(e, tpl)
}
}
return emitter.html
}
public renderTemplatesSync (templates: ITemplate[], ctx: Context, emitter = new Emitter()): string {
for (const tpl of templates) {
try {
const html = tpl.renderSync(ctx, emitter)
html && !(html instanceof Promise) && emitter.write(html)
if (emitter.break || emitter.continue) break
} catch (e) {
throw RenderError.is(e) ? e : new RenderError(e, tpl)
}
}
return emitter.html
+12 -4
View File
@@ -5,11 +5,15 @@ import { parseLiteral } from '../parser/literal'
export class Value {
private str: string
public constructor (str: string = '') {
public constructor (str: string) {
this.str = str
}
public evaluate (ctx: Context) {
public async evaluate (ctx: Context) {
return this.evaluateSync(ctx)
}
public evaluateSync (ctx: Context) {
const literalValue = parseLiteral(this.str)
if (literalValue !== undefined) {
return literalValue
@@ -17,7 +21,11 @@ export class Value {
return ctx.get(this.str)
}
public value (ctx: Context) {
return toValue(this.evaluate(ctx))
public async value (ctx: Context) {
return toValue(await this.evaluate(ctx))
}
public valueSync (ctx: Context) {
return toValue(this.evaluateSync(ctx))
}
}