1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-03 13:37:46 +01:00
liquid/render/compiler.go

104 lines
2.3 KiB
Go
Raw Normal View History

2017-07-06 14:07:53 +02:00
package render
2017-07-06 14:59:10 +02:00
import (
"fmt"
)
// A CompilationError is a parse error during template compilation.
type CompilationError string
func (e CompilationError) Error() string { return string(e) }
func compilationErrorf(format string, a ...interface{}) CompilationError {
return CompilationError(fmt.Sprintf(format, a...))
}
2017-07-06 14:07:53 +02:00
// Compile parses a source template. It returns an AST root, that can be evaluated.
2017-07-06 14:59:10 +02:00
func (c Config) Compile(source string) (ASTNode, error) {
root, err := c.Parse(source)
2017-07-06 14:07:53 +02:00
if err != nil {
return nil, err
}
2017-07-06 14:59:10 +02:00
return c.compileNode(root)
2017-07-06 14:07:53 +02:00
}
// nolint: gocyclo
func (c Config) compileNode(n ASTNode) (Node, error) {
2017-07-06 14:07:53 +02:00
switch n := n.(type) {
case *ASTBlock:
2017-07-06 14:59:10 +02:00
body, err := c.compileNodes(n.Body)
if err != nil {
return nil, err
2017-07-06 14:07:53 +02:00
}
2017-07-06 14:59:10 +02:00
branches, err := c.compileBlocks(n.Branches)
if err != nil {
return nil, err
}
cd, ok := c.findBlockDef(n.Name)
if !ok {
return nil, compilationErrorf("undefined tag %q", n.Name)
2017-07-06 14:07:53 +02:00
}
node := BlockNode{
Chunk: n.Chunk,
Body: body,
Branches: branches,
}
2017-07-06 14:59:10 +02:00
if cd.parser != nil {
r, err := cd.parser(node)
2017-07-06 14:07:53 +02:00
if err != nil {
return nil, err
}
node.renderer = r
2017-07-06 14:07:53 +02:00
}
return &node, nil
2017-07-06 14:59:10 +02:00
case *ASTRaw:
return &RawNode{n.slices}, nil
2017-07-06 14:07:53 +02:00
case *ASTSeq:
2017-07-06 14:59:10 +02:00
children, err := c.compileNodes(n.Children)
if err != nil {
return nil, err
}
return &SeqNode{children}, nil
2017-07-06 15:35:12 +02:00
case *ASTTag:
if td, ok := c.FindTagDefinition(n.Name); ok {
f, err := td(n.Args)
if err != nil {
return nil, err
}
2017-07-06 15:40:37 +02:00
return &TagNode{n.Chunk, f}, nil
2017-07-06 15:35:12 +02:00
}
2017-07-06 15:40:37 +02:00
return nil, compilationErrorf("unknown tag: %s", n.Name)
2017-07-06 14:59:10 +02:00
case *ASTText:
return &TextNode{n.Chunk}, nil
2017-07-06 14:59:10 +02:00
case *ASTObject:
return &ObjectNode{n.Chunk, n.expr}, nil
2017-07-06 14:59:10 +02:00
default:
panic(fmt.Errorf("un-compilable node type %T", n))
}
}
func (c Config) compileBlocks(blocks []*ASTBlock) ([]*BlockNode, error) {
out := make([]*BlockNode, 0, len(blocks))
2017-07-06 14:59:10 +02:00
for _, child := range blocks {
compiled, err := c.compileNode(child)
if err != nil {
return nil, err
}
out = append(out, compiled.(*BlockNode))
2017-07-06 14:59:10 +02:00
}
return out, nil
}
func (c Config) compileNodes(nodes []ASTNode) ([]Node, error) {
out := make([]Node, 0, len(nodes))
2017-07-06 14:59:10 +02:00
for _, child := range nodes {
compiled, err := c.compileNode(child)
if err != nil {
return nil, err
2017-07-06 14:07:53 +02:00
}
2017-07-06 14:59:10 +02:00
out = append(out, compiled)
2017-07-06 14:07:53 +02:00
}
2017-07-06 14:59:10 +02:00
return out, nil
2017-07-06 14:07:53 +02:00
}