1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 10:59:04 +01:00
liquid/chunks/parser.go

104 lines
2.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-26 18:41:41 +02:00
// Parse creates an AST from a sequence of Chunks.
func Parse(chunks []Chunk) (ASTNode, error) {
2017-06-25 17:23:20 +02:00
type frame struct {
cd *controlTagDefinition
2017-06-25 17:23:20 +02:00
cn *ASTControlTag
2017-06-26 18:41:41 +02:00
ap *[]ASTNode
2017-06-25 17:23:20 +02:00
}
var (
2017-06-27 19:47:14 +02:00
root = &ASTSeq{}
ap = &root.Children // pointer to current node accumulation slice
ccd *controlTagDefinition
ccn *ASTControlTag
stack []frame // stack of control structures
2017-06-27 23:40:15 +02:00
rawTag *ASTRaw
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 {
case inComment:
if c.Type == TagChunkType && c.Tag == "endcomment" {
inComment = false
}
2017-06-27 23:40:15 +02:00
case inRaw:
if c.Type == TagChunkType && c.Tag == "endraw" {
inComment = false
} else {
rawTag.slices = append(rawTag.slices, c.Source)
}
2017-06-27 19:47:14 +02:00
case c.Type == ObjChunkType:
expr, err := expressions.Parse(c.Args)
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:
if cd, ok := findControlTagDefinition(c.Tag); ok {
2017-06-25 17:23:20 +02:00
switch {
2017-06-27 19:47:14 +02:00
case c.Tag == "comment":
inComment = true
2017-06-27 23:40:15 +02:00
case c.Tag == "raw":
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-26 21:36:05 +02:00
} else if td, ok := FindTagDefinition(c.Tag); ok {
f, err := td(c.Args)
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-26 21:36:05 +02:00
return nil, fmt.Errorf("unknown tag: %s", c.Tag)
2017-06-25 17:23:20 +02:00
}
2017-06-26 21:36:05 +02:00
// } else if len(*ap) > 0 {
// switch n := ((*ap)[len(*ap)-1]).(type) {
// case *ASTChunks:
// n.chunks = append(n.chunks, c)
// default:
// *ap = append(*ap, &ASTChunks{chunks: []Chunk{c}})
// }
// } else {
// *ap = append(*ap, &ASTChunks{chunks: []Chunk{c}})
// }
2017-06-25 17:23:20 +02:00
}
}
if ccd != nil {
return nil, fmt.Errorf("unterminated %s tag", ccd.name)
2017-06-25 17:23:20 +02:00
}
if len(root.Children) == 1 {
return root.Children[0], nil
}
return root, nil
}