forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
105 lines (101 loc) · 3.08 KB
/
parser.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Package parser is an internal package that parses template source into an abstract syntax tree.
package parser
import (
"fmt"
"strings"
"github.com/osteele/liquid/expressions"
)
// Parse parses a source template. It returns an AST root, that can be compiled and evaluated.
func (c Config) Parse(source string, loc SourceLoc) (ASTNode, Error) {
tokens := Scan(source, loc, c.Delims)
return c.parseTokens(tokens)
}
// Parse creates an AST from a sequence of tokens.
func (c Config) parseTokens(tokens []Token) (ASTNode, Error) { // nolint: gocyclo
// a stack of control tag state, for matching nested {%if}{%endif%} etc.
type frame struct {
syntax BlockSyntax
node *ASTBlock
ap *[]ASTNode
}
var (
g = c.Grammar
root = &ASTSeq{} // root of AST; will be returned
ap = &root.Children // newly-constructed nodes are appended here
sd BlockSyntax // current block syntax definition
bn *ASTBlock // current block node
stack []frame // stack of blocks
rawTag *ASTRaw // current raw tag
inComment = false
inRaw = false
)
for _, tok := range tokens {
switch {
// 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?
case inComment:
if tok.Type == TagTokenType && tok.Name == "endcomment" {
inComment = false
}
case inRaw:
if tok.Type == TagTokenType && tok.Name == "endraw" {
inRaw = false
} else {
rawTag.Slices = append(rawTag.Slices, tok.Source)
}
case tok.Type == ObjTokenType:
expr, err := expressions.Parse(tok.Args)
if err != nil {
return nil, WrapError(err, tok)
}
*ap = append(*ap, &ASTObject{tok, expr})
case tok.Type == TextTokenType:
*ap = append(*ap, &ASTText{Token: tok})
case tok.Type == TagTokenType:
if cs, ok := g.BlockSyntax(tok.Name); ok {
switch {
case tok.Name == "comment":
inComment = true
case tok.Name == "raw":
inRaw = true
rawTag = &ASTRaw{}
*ap = append(*ap, rawTag)
case cs.RequiresParent() && (sd == nil || !cs.CanHaveParent(sd)):
suffix := ""
if sd != nil {
suffix = "; immediate parent is " + sd.TagName()
}
return nil, Errorf(tok, "%s not inside %s%s", tok.Name, strings.Join(cs.ParentTags(), " or "), suffix)
case cs.IsBlockStart():
push := func() {
stack = append(stack, frame{syntax: sd, node: bn, ap: ap})
sd, bn = cs, &ASTBlock{Token: tok, syntax: cs}
*ap = append(*ap, bn)
}
push()
ap = &bn.Body
case cs.IsClause():
n := &ASTBlock{Token: tok, syntax: cs}
bn.Clauses = append(bn.Clauses, n)
ap = &n.Body
case cs.IsBlockEnd():
pop := func() {
f := stack[len(stack)-1]
stack = stack[:len(stack)-1]
sd, bn, ap = f.syntax, f.node, f.ap
}
pop()
default:
panic(fmt.Errorf("block type %q", tok.Name))
}
} else {
*ap = append(*ap, &ASTTag{tok})
}
}
}
if bn != nil {
return nil, Errorf(bn, "unterminated %q block", bn.Name)
}
return root, nil
}