-
Notifications
You must be signed in to change notification settings - Fork 1
/
variable.go
146 lines (117 loc) · 3.22 KB
/
variable.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package liquid
import (
"fmt"
"regexp"
)
var (
variableQuotedFragmentRegexp = regexp.MustCompile(fmt.Sprintf("%s(.*)", quotedFragmentRegexp.String())) // om
)
// Vars get passed in the render step of a request
// and contain values and context information for the input
// variables of the request
type Vars map[string]interface{}
// Variable is a single liquid variable expression
// and any associated filters
type Variable struct {
Name Expression
Filters []Filter
markup string
}
func (v *Variable) Render(vars Vars) (string, error) {
panic("unimplemented")
}
func (v *Variable) Blank() bool {
panic("unimplemented")
}
func (v *Variable) String() string {
return fmt.Sprintf("Liquid::Variable %v filters: %v markup: %v", v.Name.Name(), v.Filters, v.markup)
}
// Filter is used to modify a Variable using the
// liquid pipe syntax "x | f1 | f2"
type Filter struct {
name string
args []Expression
kwargs map[string]Expression
}
func (f Filter) String() string {
return fmt.Sprintf("Liquid::Filter %v", f.name)
}
// CreateVariable performs a parse of the supplied markup
// and returns a Variable object
func CreateVariable(value string) (*Variable, error) {
matches := variableQuotedFragmentRegexp.FindStringSubmatch(value)
if len(matches) != 2 {
return nil, fmt.Errorf("Bad match")
}
return ParseStrict(value)
}
// VariableParser is the signature of function required to perform
// a parse of a liquid variable expression
type VariableParser func(markup string) (*Variable, error)
// ParseStrict performs the strictest form of VariableParser parse
func ParseStrict(markup string) (*Variable, error) {
var filters []Filter
p, err := NewParser(markup)
if err != nil {
return nil, err
}
expr, err := p.expression()
if err != nil {
return nil, err
}
name := ParseExpression(expr)
for p.tryConsume(tPipe) {
filterName, err := p.consume(tIdentifier)
if err != nil {
return nil, err
}
var filterArgs []string
// check if there are arguments
if p.tryConsume(tColon) {
// parse de args
for {
arg, err := p.argument()
if err != nil {
return nil, err
}
filterArgs = append(filterArgs, arg)
if !p.tryConsume(tComma) {
break
}
}
}
filters = append(filters, ParseFilterExpressions(filterName, filterArgs))
}
// consume an end of string, if there
if _, err = p.consume(tEndOfString); err != nil {
return nil, err
}
return &Variable{
Name: name,
Filters: filters,
markup: markup,
}, nil
}
// ParseFilterExpressions parses the filter args passed with a liquid variable
func ParseFilterExpressions(name string, unparsedArgs []string) Filter {
var args []Expression
kwargs := make(map[string]Expression)
for _, a := range unparsedArgs {
// Check for keyword arguments first, anything leftover will be treated like a regular argument
if submatches := tagAttributesRegexp.FindStringSubmatch(a); len(submatches) > 0 {
kwargs[submatches[1]] = ParseExpression(submatches[2])
} else {
args = append(args, ParseExpression(a))
}
}
// null out the kwargs map if it is empty
// this ensures that Filter{name: blah} will match a parsed filter
if len(kwargs) == 0 {
kwargs = nil
}
return Filter{
name: name,
args: args,
kwargs: kwargs,
}
}