-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
213 lines (184 loc) · 4.87 KB
/
filter.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"github.com/johan-bolmsjo/saft"
"regexp"
"strconv"
"strings"
)
type filter struct {
name string
regexp *regexp.Regexp
regexpFrom *filter
props map[int]properties // Properites indexed by regexp group
filters filterList
state *filterState
}
type properties struct {
fgcolor, bgcolor color
modifiers modifierSet
}
func (props *properties) mergeWith(other properties) {
if other.fgcolor != colorNone {
props.fgcolor = other.fgcolor
}
if other.bgcolor != colorNone {
props.bgcolor = other.bgcolor
}
props.modifiers |= other.modifiers
}
const filterSep = "/"
func elemParseFilter(elem saft.Elem, prog *program) (*filter, error) {
assoc, err := elem.ExpectAssoc()
if err != nil {
return nil, err
}
if err = assocCheckDuplicates(assoc, parFilterName, parFilterRegexp, parFilterRegexpFrom); err != nil {
return nil, err
}
if err = assocCheckExclusive(assoc, parFilterRegexp, parFilterRegexpFrom); err != nil {
return nil, err
}
filter := filter{props: map[int]properties{}}
var str *saft.String
for _, p := range assoc.L {
key := p.K.V
switch key {
case parFilterName:
if str, err = elemExpectString(p.V, key); err != nil {
return nil, err
}
if strings.Contains(str.V, filterSep) {
return nil, posErrorf(str.Pos(), "filter name must not contain %q", filterSep)
}
filter.name = str.V
case parFilterRegexp:
if str, err = elemExpectString(p.V, key); err != nil {
return nil, err
}
if filter.regexp, err = regexp.Compile(str.V); err != nil {
return nil, posWrapError(err, str.Pos())
}
case parFilterRegexpFrom:
if str, err = elemExpectString(p.V, key); err != nil {
return nil, err
}
if filter.regexpFrom = prog.findFilter(str.V); filter.regexpFrom == nil {
return nil, posErrorf(str.Pos(), "referenced filter %q does not exist", str.V)
}
if filter.regexpFrom.regexp == nil {
return nil, posErrorf(str.Pos(), "referenced filter %q miss regexp", str.V)
}
case parFilterProperties:
if err = elemParseFilterProperties(p.V, key, &filter); err != nil {
return nil, err
}
case parFilter:
nestedFilter, err := elemParseFilter(p.V, prog)
if err != nil {
return nil, err
}
if filter.filters.find(nestedFilter.name) != nil {
return nil, posErrorf(p.V.Pos(), "duplicate filter %q", nestedFilter.name)
}
filter.filters = append(filter.filters, nestedFilter)
default:
return nil, unknownParameterError(&p)
}
}
filter.state = prog.globalFilterState.allocState()
return &filter, nil
}
func elemParseFilterProperties(elem saft.Elem, param string, filter *filter) error {
assoc, err := elemExpectAssoc(elem, param)
if err != nil {
return err
}
for _, p := range assoc.L {
var group int
if group, err = strconv.Atoi(p.K.V); err != nil || group <= 0 {
return posErrorf(p.K.Pos(), "invalid regexp group %q", p.K.V)
}
var props properties
if props, err = elemParseProperties(p.V); err != nil {
return err
}
filter.props[group] = props
}
return nil
}
func elemParseProperties(elem saft.Elem) (properties, error) {
assoc, err := elem.ExpectAssoc()
if err != nil {
return properties{}, err
}
if err = assocCheckDuplicates(assoc, parPropertyColor, parPropertyBGColor, parPropertyModifiers); err != nil {
return properties{}, err
}
var props properties
for _, p := range assoc.L {
key := p.K.V
switch key {
case parPropertyColor:
if props.fgcolor, err = elemParseColor(p.V, key); err != nil {
return properties{}, err
}
case parPropertyBGColor:
if props.bgcolor, err = elemParseColor(p.V, key); err != nil {
return properties{}, err
}
case parPropertyModifiers:
var modifiers []modifier
if modifiers, err = elemParseModifierList(p.V, key); err != nil {
return properties{}, err
}
for _, modifier := range modifiers {
props.modifiers.set(modifier)
}
default:
return properties{}, unknownParameterError(&p)
}
}
return props, nil
}
func elemParseColor(elem saft.Elem, param string) (color, error) {
str, err := elemExpectString(elem, param)
if err != nil {
return colorNone, err
}
color, err := parseColor(str.V)
if err != nil {
return colorNone, posWrapError(err, str.Pos())
}
return color, nil
}
func elemParseModifierList(elem saft.Elem, param string) ([]modifier, error) {
strList, err := elemExpectListOfString(elem, param)
if err != nil {
return nil, err
}
var modifiers []modifier
for _, str := range strList {
modifier, err := parseModifier(str.V)
if err != nil {
return nil, posWrapError(err, str.Pos())
}
modifiers = append(modifiers, modifier)
}
return modifiers, nil
}
type filterList []*filter
func (l *filterList) find(name string) *filter {
if name != "" {
for _, f := range *l {
if name == f.name {
return f
}
}
}
return nil
}
func (l *filterList) apply(f func(*filter)) {
for _, v := range *l {
f(v)
}
}