forked from foxcpp/go-sieve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_generic.go
183 lines (172 loc) · 4.89 KB
/
load_generic.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
package interp
import (
"strings"
"github.com/hilli/go-sieve/lexer"
"github.com/hilli/go-sieve/parser"
)
type SpecTag struct {
NeedsValue bool
MatchStr func(val []string)
MatchNum func(val int)
MatchBool func()
// Checks for used string list.
MinStrCount int
MaxStrCount int
}
type SpecPosArg struct {
Optional bool
MatchStr func(val []string)
MatchNum func(i int)
// Checks for used string list.
MinStrCount int
MaxStrCount int
}
type Spec struct {
Tags map[string]SpecTag
Pos []SpecPosArg
AddBlock func([]Cmd)
BlockOptional bool
AddTest func(Test)
TestOptional bool
MultipleTests bool
}
func LoadSpec(s *Script, spec *Spec, pos lexer.Position, args []parser.Arg, tests []parser.Test, block []parser.Cmd) error {
var lastTag *SpecTag
nextPosArg := 0
for _, a := range args {
switch a := a.(type) {
case parser.StringArg:
if lastTag != nil && lastTag.NeedsValue {
if lastTag.MatchNum != nil {
return lexer.ErrorAt(a, "LoadSpec: tagged argument requires a number, got string-list")
} else if lastTag.MatchStr != nil {
lastTag.MatchStr([]string{a.Value})
} else {
panic("missing matcher for SpecTag")
}
lastTag = nil
continue
}
if nextPosArg >= len(spec.Pos) {
return lexer.ErrorAt(a, "LoadSpec: too many arguments")
}
pos := spec.Pos[nextPosArg]
if pos.MinStrCount > 1 {
return lexer.ErrorAt(a, "LoadSpec: string-list required, got single string")
}
if pos.MatchNum != nil {
return lexer.ErrorAt(a, "LoadSpec: argument requires a number, got string-list")
} else if pos.MatchStr != nil {
pos.MatchStr([]string{a.Value})
} else {
panic("no pos matcher")
}
nextPosArg++
case parser.StringListArg:
if lastTag != nil && lastTag.NeedsValue {
if lastTag.MatchNum != nil {
return lexer.ErrorAt(a, "LoadSpec: tagged argument requires a number, got string-list")
} else if lastTag.MatchStr != nil {
if (lastTag.MinStrCount != 0 && len(a.Value) < lastTag.MinStrCount) || (lastTag.MaxStrCount != 0 && len(a.Value) > lastTag.MaxStrCount) {
return lexer.ErrorAt(a, "LoadSpec: wrong amount of string arguments")
}
lastTag.MatchStr(a.Value)
} else {
panic("missing matcher for SpecTag")
}
lastTag = nil
continue
}
if nextPosArg >= len(spec.Pos) {
return lexer.ErrorAt(a, "LoadSpec: too many arguments")
}
pos := spec.Pos[nextPosArg]
if (pos.MinStrCount != 0 && len(a.Value) < pos.MinStrCount) || (pos.MaxStrCount != 0 && len(a.Value) > pos.MaxStrCount) {
return lexer.ErrorAt(a, "LoadSpec: wrong amount of string arguments")
}
if pos.MatchNum != nil {
return lexer.ErrorAt(a, "LoadSpec: argument requires a number, got string-list")
} else if pos.MatchStr != nil {
pos.MatchStr(a.Value)
} else {
panic("no pos matcher")
}
nextPosArg++
case parser.NumberArg:
if lastTag != nil && lastTag.NeedsValue {
if lastTag.MatchStr != nil {
return lexer.ErrorAt(a, "LoadSpec: tagged argument requires a string-list, got number")
} else if lastTag.MatchNum != nil {
lastTag.MatchNum(a.Value)
} else {
panic("missing matcher for SpecTag")
}
lastTag = nil
continue
}
if nextPosArg >= len(spec.Pos) {
return lexer.ErrorAt(a, "LoadSpec: too many arguments")
}
pos := spec.Pos[nextPosArg]
if pos.MatchStr != nil {
return lexer.ErrorAt(a, "LoadSpec: argument requires a string-list, got number")
} else if pos.MatchNum != nil {
pos.MatchNum(a.Value)
} else {
panic("no pos matcher")
}
nextPosArg++
case parser.TagArg:
if lastTag != nil && lastTag.NeedsValue {
return lexer.ErrorAt(a, "LoadSpec: tagged argument requires a value")
}
tag, ok := spec.Tags[strings.ToLower(a.Value)]
if !ok {
return lexer.ErrorAt(a, "LoadSpec: unknown tagged argument: %v", a)
}
if tag.NeedsValue {
lastTag = &tag
} else {
tag.MatchBool()
}
}
}
for i := nextPosArg; i < len(spec.Pos); i++ {
if !spec.Pos[i].Optional {
return lexer.ErrorAt(pos, "LoadSpec: %d argument is required", i+1)
}
}
if spec.AddTest == nil {
if len(tests) != 0 {
return lexer.ErrorAt(pos, "LoadSpec: no tests allowed")
}
} else {
if len(tests) == 0 && !spec.TestOptional {
return lexer.ErrorAt(pos, "LoadSpec: at least one test required")
}
if len(tests) > 1 && !spec.MultipleTests {
return lexer.ErrorAt(pos, "LoadSpec: only one test allowed")
}
for _, t := range tests {
loadedTest, err := LoadTest(s, t)
if err != nil {
return err
}
spec.AddTest(loadedTest)
}
}
if spec.AddBlock != nil {
if block != nil {
loadedCmds, err := LoadBlock(s, block)
if err != nil {
return err
}
spec.AddBlock(loadedCmds)
} else if !spec.BlockOptional {
return lexer.ErrorAt(pos, "LoadSpec: block is required")
}
} else if block != nil {
return lexer.ErrorAt(pos, "LoadSpec: no block allowed")
}
return nil
}