-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
parser.ts
42 lines (40 loc) · 1.2 KB
/
parser.ts
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
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 Tag from '../template/tag/tag'
import Output from '../template/output'
import HTML from '../template/html'
import ITemplate from '../template/itemplate'
export default class Parser {
liquid: Liquid
constructor (liquid: Liquid) {
this.liquid = liquid
}
parse (tokens: Array<Token>) {
let token
const templates: ITemplate[] = []
while ((token = tokens.shift())) {
templates.push(this.parseToken(token, tokens))
}
return templates
}
parseToken (token: Token, remainTokens: Array<Token>) {
try {
if (token.type === 'tag') {
return new Tag(token as TagToken, remainTokens, this.liquid)
}
if (token.type === 'output') {
return new Output(token as OutputToken, this.liquid.options.strictFilters)
}
return new HTML(token)
} catch (e) {
throw new ParseError(e, token)
}
}
parseStream (tokens: Array<Token>) {
return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens))
}
}