1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 06:44:42 +01:00
liquid/chunks/parser.go

137 lines
3.7 KiB
Go
Raw Normal View History

2017-06-26 15:36:52 +02:00
package chunks
2017-06-25 17:23:20 +02:00
import (
"fmt"
"github.com/osteele/liquid/expressions"
2017-06-25 17:23:20 +02:00
)
2017-06-30 22:51:39 +02:00
func (s Settings) Parse(source string) (ASTNode, error) {
tokens := Scan(source, "")
return s.parseChunks(tokens)
}
2017-06-26 18:41:41 +02:00
// Parse creates an AST from a sequence of Chunks.
2017-06-30 22:51:39 +02:00
func (s Settings) parseChunks(chunks []Chunk) (ASTNode, error) {
2017-06-30 14:42:11 +02:00
// a stack of control tag state, for matching nested {%if}{%endif%} etc.
2017-06-25 17:23:20 +02:00
type frame struct {
2017-06-30 14:42:11 +02:00
cd *controlTagDefinition // saved local ccd
cn *ASTControlTag // saved local cn
ap *[]ASTNode // saved local ap
2017-06-25 17:23:20 +02:00
}
var (
2017-06-30 14:42:11 +02:00
root = &ASTSeq{} // root of AST; will be returned
ap = &root.Children // newly-constructed nodes are appended here
ccd *controlTagDefinition // current control tag definition
ccn *ASTControlTag // current control node
stack []frame // stack of control structures
rawTag *ASTRaw // current raw tag
2017-06-27 19:47:14 +02:00
inComment = false
2017-06-27 23:40:15 +02:00
inRaw = false
2017-06-25 17:23:20 +02:00
)
for _, c := range chunks {
2017-06-27 19:47:14 +02:00
switch {
2017-06-30 14:42:11 +02:00
// The parser needs to know about comment and raw, because tags inside
// needn't match each other e.g. {%comment%}{%if%}{%endcomment%}
// TODO is this true?
2017-06-27 19:47:14 +02:00
case inComment:
2017-06-29 18:20:16 +02:00
if c.Type == TagChunkType && c.Name == "endcomment" {
2017-06-27 19:47:14 +02:00
inComment = false
}
2017-06-27 23:40:15 +02:00
case inRaw:
2017-06-29 18:20:16 +02:00
if c.Type == TagChunkType && c.Name == "endraw" {
2017-06-30 14:45:22 +02:00
inRaw = false
2017-06-27 23:40:15 +02:00
} else {
rawTag.slices = append(rawTag.slices, c.Source)
}
2017-06-27 19:47:14 +02:00
case c.Type == ObjChunkType:
2017-06-29 18:20:16 +02:00
expr, err := expressions.Parse(c.Parameters)
if err != nil {
return nil, err
}
*ap = append(*ap, &ASTObject{c, expr})
2017-06-27 19:47:14 +02:00
case c.Type == TextChunkType:
2017-06-27 17:19:12 +02:00
*ap = append(*ap, &ASTText{Chunk: c})
2017-06-27 19:47:14 +02:00
case c.Type == TagChunkType:
2017-06-30 22:46:17 +02:00
if cd, ok := s.findControlTagDefinition(c.Name); ok {
2017-06-25 17:23:20 +02:00
switch {
2017-06-29 18:20:16 +02:00
case c.Name == "comment":
2017-06-27 19:47:14 +02:00
inComment = true
2017-06-29 18:20:16 +02:00
case c.Name == "raw":
2017-06-27 23:40:15 +02:00
inRaw = true
rawTag = &ASTRaw{}
*ap = append(*ap, rawTag)
case cd.requiresParent() && !cd.compatibleParent(ccd):
2017-06-25 17:23:20 +02:00
suffix := ""
if ccd != nil {
suffix = "; immediate parent is " + ccd.name
2017-06-25 17:23:20 +02:00
}
return nil, fmt.Errorf("%s not inside %s%s", cd.name, cd.parent.name, suffix)
case cd.isStartTag():
2017-06-25 17:23:20 +02:00
stack = append(stack, frame{cd: ccd, cn: ccn, ap: ap})
2017-06-27 17:19:12 +02:00
ccd, ccn = cd, &ASTControlTag{Chunk: c, cd: cd}
2017-06-25 17:23:20 +02:00
*ap = append(*ap, ccn)
2017-06-27 17:39:32 +02:00
ap = &ccn.Body
case cd.isBranchTag:
2017-06-27 17:19:12 +02:00
n := &ASTControlTag{Chunk: c, cd: cd}
2017-06-27 17:39:32 +02:00
ccn.Branches = append(ccn.Branches, n)
ap = &n.Body
case cd.isEndTag:
2017-06-25 17:23:20 +02:00
f := stack[len(stack)-1]
stack = stack[:len(stack)-1]
ccd, ccn, ap = f.cd, f.cn, f.ap
2017-06-25 17:23:20 +02:00
}
2017-06-30 22:46:17 +02:00
} else if td, ok := s.FindTagDefinition(c.Name); ok {
2017-06-29 18:20:16 +02:00
f, err := td(c.Parameters)
2017-06-26 21:36:05 +02:00
if err != nil {
return nil, err
2017-06-25 17:23:20 +02:00
}
*ap = append(*ap, &ASTFunctional{c, f})
2017-06-25 17:23:20 +02:00
} else {
2017-06-29 18:20:16 +02:00
return nil, fmt.Errorf("unknown tag: %s", c.Name)
2017-06-25 17:23:20 +02:00
}
}
}
if ccd != nil {
2017-06-30 14:42:11 +02:00
return nil, fmt.Errorf("unterminated %s tag at %s", ccd.name, ccn.SourceInfo)
2017-06-25 17:23:20 +02:00
}
2017-06-30 22:46:17 +02:00
if err := s.evaluateBuilders(root); err != nil {
2017-06-29 03:45:24 +02:00
return nil, err
}
2017-06-25 17:23:20 +02:00
if len(root.Children) == 1 {
return root.Children[0], nil
}
return root, nil
}
2017-06-29 03:45:24 +02:00
2017-06-30 22:46:17 +02:00
func (s Settings) evaluateBuilders(n ASTNode) error {
2017-06-29 03:45:24 +02:00
switch n := n.(type) {
case *ASTControlTag:
for _, child := range n.Body {
2017-06-30 22:46:17 +02:00
if err := s.evaluateBuilders(child); err != nil {
2017-06-29 03:45:24 +02:00
return err
}
}
for _, branch := range n.Branches {
2017-06-30 22:46:17 +02:00
if err := s.evaluateBuilders(branch); err != nil {
2017-06-29 03:45:24 +02:00
return err
}
}
2017-06-30 22:46:17 +02:00
cd, ok := s.findControlTagDefinition(n.Name)
2017-06-29 03:45:24 +02:00
if ok && cd.parser != nil {
renderer, err := cd.parser(*n)
if err != nil {
return err
}
n.renderer = renderer
}
case *ASTSeq:
for _, child := range n.Children {
2017-06-30 22:46:17 +02:00
if error := s.evaluateBuilders(child); error != nil {
2017-06-29 03:45:24 +02:00
return error
}
}
}
return nil
}