mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
perf: introduce AST to avoid reparse
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { ParseError } from '../util/error'
|
||||
import { Liquid } from '../liquid'
|
||||
import { ParseStream } from './parse-stream'
|
||||
import { Token } from './token'
|
||||
import { TagToken } from './tag-token'
|
||||
import { OutputToken } from './output-token'
|
||||
import { isTagToken, isOutputToken } from '../util/type-guards'
|
||||
import { OutputToken } from '../tokens/output-token'
|
||||
import { Tag } from '../template/tag/tag'
|
||||
import { Output } from '../template/output'
|
||||
import { HTML } from '../template/html'
|
||||
import { Template } from '../template/template'
|
||||
import { TopLevelToken } from '../tokens/toplevel-token'
|
||||
|
||||
export default class Parser {
|
||||
private liquid: Liquid
|
||||
@@ -15,7 +15,7 @@ export default class Parser {
|
||||
public constructor (liquid: Liquid) {
|
||||
this.liquid = liquid
|
||||
}
|
||||
public parse (tokens: Token[]) {
|
||||
public parse (tokens: TopLevelToken[]) {
|
||||
let token
|
||||
const templates: Template[] = []
|
||||
while ((token = tokens.shift())) {
|
||||
@@ -23,12 +23,12 @@ export default class Parser {
|
||||
}
|
||||
return templates
|
||||
}
|
||||
public parseToken (token: Token, remainTokens: Token[]) {
|
||||
public parseToken (token: TopLevelToken, remainTokens: TopLevelToken[]) {
|
||||
try {
|
||||
if (TagToken.is(token)) {
|
||||
if (isTagToken(token)) {
|
||||
return new Tag(token, remainTokens, this.liquid)
|
||||
}
|
||||
if (OutputToken.is(token)) {
|
||||
if (isOutputToken(token)) {
|
||||
return new Output(token as OutputToken, this.liquid.filters)
|
||||
}
|
||||
return new HTML(token)
|
||||
@@ -36,7 +36,7 @@ export default class Parser {
|
||||
throw new ParseError(e, token)
|
||||
}
|
||||
}
|
||||
public parseStream (tokens: Token[]) {
|
||||
public parseStream (tokens: TopLevelToken[]) {
|
||||
return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user