This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validatePattern.go
187 lines (170 loc) · 3.46 KB
/
validatePattern.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
package parser
import (
"fmt"
"reflect"
)
func validateRule(
ptr *Rule,
validated map[Pattern]struct{},
) error {
if ptr.Pattern == nil {
return fmt.Errorf("rule %p is missing a pattern", ptr)
}
return validatePattern(ptr.Pattern, validated)
}
func validateSequence(
ptr Sequence,
validated map[Pattern]struct{},
) error {
if len(ptr) < 1 {
return fmt.Errorf("sequence is empty")
}
for _, elem := range ptr {
if err := validatePattern(elem, validated); err != nil {
return err
}
}
return nil
}
func validateRepeated(
ptr *Repeated,
validated map[Pattern]struct{},
) error {
if ptr.Max != 0 && ptr.Min > ptr.Max {
return fmt.Errorf(
"repeated %p min (%d) greater max (%d)",
ptr,
ptr.Min,
ptr.Max,
)
}
if ptr.Pattern == nil {
return fmt.Errorf("repeated %p is missing a pattern", ptr)
}
return validatePattern(ptr.Pattern, validated)
}
func validateEither(
ptr Either,
validated map[Pattern]struct{},
) error {
if len(ptr) < 2 {
return fmt.Errorf("either-combinator has %d option(s)", len(ptr))
}
options := map[Pattern]struct{}{}
ix := 0
for _, opt := range ptr {
// Check for duplicate options
checkDuplicate := false
switch opt.(type) {
case *Rule:
checkDuplicate = true
case *Lexed:
checkDuplicate = true
case *Exact:
checkDuplicate = true
case *Repeated:
checkDuplicate = true
}
if checkDuplicate {
if _, ok := options[opt]; ok {
return fmt.Errorf(
"either-combinator has duplicate options (at index %d)",
ix,
)
}
options[opt] = struct{}{}
}
if err := validatePattern(opt, validated); err != nil {
return err
}
ix++
}
return nil
}
func validateNot(
ptr Not,
validated map[Pattern]struct{},
) error {
if _, ok := ptr.Pattern.(Not); ok {
return fmt.Errorf("not-combinator is nested")
}
return validatePattern(ptr.Pattern, validated)
}
func validateLexed(ptr *Lexed) error {
if ptr.Fn == nil {
return fmt.Errorf("lexed-terminal %p is missing the lexer function", ptr)
}
return nil
}
func validateExact(ptr *Exact) error {
if len(ptr.Expectation) < 1 {
return fmt.Errorf("exact-terminal %p is missing an expectation", ptr)
}
return nil
}
// ValidatePattern recursively validates the given pattern
func ValidatePattern(ptr Pattern) error {
return validatePattern(ptr, nil)
}
func validatePattern(ptr Pattern, validated map[Pattern]struct{}) error {
if validated == nil {
validated = map[Pattern]struct{}{}
}
isValidated := func() bool {
if _, isValidated := validated[ptr]; !isValidated {
validated[ptr] = struct{}{}
return false
}
return true
}
switch ptr := ptr.(type) {
case nil:
return nil
case *Rule:
if isValidated() {
return nil
}
if err := validateRule(ptr, validated); err != nil {
return err
}
case Sequence:
if err := validateSequence(ptr, validated); err != nil {
return err
}
case *Repeated:
if isValidated() {
return nil
}
if err := validateRepeated(ptr, validated); err != nil {
return err
}
case Either:
if err := validateEither(ptr, validated); err != nil {
return err
}
case Not:
if err := validateNot(ptr, validated); err != nil {
return err
}
case *Lexed:
if isValidated() {
return nil
}
if err := validateLexed(ptr); err != nil {
return err
}
case *Exact:
if isValidated() {
return nil
}
if err := validateExact(ptr); err != nil {
return err
}
default:
return fmt.Errorf(
"unsupported pattern type: %s",
reflect.TypeOf(ptr),
)
}
return nil
}