1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 14:57:49 +01:00
liquid/render/compiler.go

97 lines
2.2 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"
2017-07-07 11:41:37 +02:00
"github.com/osteele/liquid/parser"
2017-07-06 14:59:10 +02:00
)
2017-07-06 14:07:53 +02:00
// Compile parses a source template. It returns an AST root, that can be evaluated.
func (c Config) Compile(source string, loc parser.SourceLoc) (Node, parser.Error) {
root, err := c.Parse(source, loc)
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 parser.ASTNode) (Node, parser.Error) {
2017-07-06 14:07:53 +02:00
switch n := n.(type) {
2017-07-07 11:41:37 +02:00
case *parser.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
}
branches, err := c.compileBlocks(n.Clauses)
2017-07-06 14:59:10 +02:00
if err != nil {
return nil, err
}
cd, ok := c.findBlockDef(n.Name)
if !ok {
return nil, parser.Errorf(n, "undefined tag %q", n.Name)
2017-07-06 14:07:53 +02:00
}
node := BlockNode{
2017-07-12 14:57:43 +02:00
Token: n.Token,
Body: body,
Clauses: 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, parser.WrapError(err, n)
2017-07-06 14:07:53 +02:00
}
node.renderer = r
2017-07-06 14:07:53 +02:00
}
return &node, nil
2017-07-07 11:41:37 +02:00
case *parser.ASTRaw:
2017-07-10 17:49:14 +02:00
return &RawNode{n.Slices, sourcelessNode{}}, nil
2017-07-07 11:41:37 +02:00
case *parser.ASTSeq:
2017-07-06 14:59:10 +02:00
children, err := c.compileNodes(n.Children)
if err != nil {
return nil, err
}
2017-07-10 17:49:14 +02:00
return &SeqNode{children, sourcelessNode{}}, nil
2017-07-07 11:41:37 +02:00
case *parser.ASTTag:
2017-07-06 15:35:12 +02:00
if td, ok := c.FindTagDefinition(n.Name); ok {
f, err := td(n.Args)
if err != nil {
return nil, parser.Errorf(n, "%s", err)
2017-07-06 15:35:12 +02:00
}
2017-07-09 17:18:35 +02:00
return &TagNode{n.Token, f}, nil
2017-07-06 15:35:12 +02:00
}
2017-07-19 15:42:01 +02:00
return nil, parser.Errorf(n, "undefined tag %q", n.Name)
2017-07-07 11:41:37 +02:00
case *parser.ASTText:
2017-07-09 17:18:35 +02:00
return &TextNode{n.Token}, nil
2017-07-07 11:41:37 +02:00
case *parser.ASTObject:
2017-07-09 17:18:35 +02:00
return &ObjectNode{n.Token, 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 []*parser.ASTBlock) ([]*BlockNode, parser.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 []parser.ASTNode) ([]Node, parser.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
}