...
1 package parser
2
3 import (
4 "github.com/noirbizarre/gonja/nodes"
5 "github.com/noirbizarre/gonja/tokens"
6 )
7
8 type TemplateParser func(string) (*nodes.Template, error)
9
10
11 func (p *Parser) parseDocElement() (nodes.Node, error) {
12 t := p.Current()
13
14 switch t.Type {
15 case tokens.Data:
16 n := &nodes.Data{Data: t}
17 p.Consume()
18 return n, nil
19 case tokens.EOF:
20 p.Consume()
21 return nil, nil
22 case tokens.CommentBegin:
23 return p.ParseComment()
24 case tokens.VariableBegin:
25 return p.ParseExpressionNode()
26 case tokens.BlockBegin:
27 return p.ParseStatementBlock()
28 }
29 return nil, p.Error("Unexpected token (only HTML/tags/filters in templates allowed)", t)
30 }
31
32 func (p *Parser) ParseTemplate() (*nodes.Template, error) {
33 tpl := &nodes.Template{
34 Name: p.Name,
35 Blocks: nodes.BlockSet{},
36 Macros: map[string]*nodes.Macro{},
37 }
38 p.Template = tpl
39
40 for !p.Stream.End() {
41 node, err := p.parseDocElement()
42 if err != nil {
43 return nil, err
44 }
45 if node != nil {
46 tpl.Nodes = append(tpl.Nodes, node)
47 }
48 }
49 return tpl, nil
50 }
51
View as plain text