/
gen.go
248 lines (219 loc) · 5.99 KB
/
gen.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package revregex
import (
"fmt"
"regexp"
"regexp/syntax"
"strings"
)
const VERSION = "0.3.1"
// MaxUnicode is the maximum Unicode character that can be generated.
const MaxUnicode = '\U0010ffff'
// Gen can generate deterministic or random strings that match a given regexp.
// Gen is thread safe.
type Gen struct {
// source string for the regexp.
source string
// root parsed tree
tree *syntax.Regexp
}
// Same as NewGen, but in addition, the tree is simplified.
func NewGenSimpl(source string) (*Gen, error) {
g, err := NewGen(source)
g.tree = g.tree.Simplify()
return g, err
}
// NewGen creates a new generator.
// It returns an error if the regexp provided is not syntaxicaly correct.
// Use POSIX syntax. No implicit parse tree simplification.
func NewGen(source string) (*Gen, error) {
var err error
g := new(Gen)
g.source = cleanPattern(source)
_, err = regexp.Compile(g.source)
if err != nil {
return nil, err
}
g.tree, err = syntax.Parse(g.source, syntax.POSIX)
if err != nil {
return nil, err
}
return g, nil
}
// Must is a utilty to panic on error, when creating a Gen.
// Typicla use is :
// g := Must(NewGen(pattern))
func Must(g *Gen, e error) *Gen {
if e != nil {
panic(e)
}
return g
}
func (g *Gen) String() string {
var b strings.Builder
fmt.Fprintf(&b, "%q\t->\t", g.source)
toString(&b, g.tree)
return b.String()
}
func toString(b *strings.Builder, re *syntax.Regexp) {
if re == nil {
fmt.Fprint(b, nil)
return
}
switch re.Op {
case
syntax.OpLiteral, syntax.OpCharClass:
fmt.Fprintf(b, "%s(%q)", re.Op, re.Rune)
case syntax.OpRepeat:
fmt.Fprintf(b, "%s{%d,%d}(", re.Op, re.Min, re.Max)
for _, rs := range re.Sub {
toString(b, rs)
}
fmt.Fprint(b, ")")
default:
fmt.Fprint(b, re.Op, "(")
for _, rs := range re.Sub {
toString(b, rs)
}
fmt.Fprint(b, ")")
}
}
var ErrVerificationFailed = fmt.Errorf("verification failed")
// Verify if a string match the regexp used to create g.
func (g *Gen) Verify(s string) error {
ok, err := regexp.Match(g.source, []byte(s))
if err != nil {
return err
}
if !ok {
return ErrVerificationFailed
}
return nil
}
// Next generate a string that match the provided regexp, using the provided Chooser to make its choices.
func (g *Gen) Next(it Chooser) string {
var b strings.Builder
next(&b, it, g.tree)
return b.String()
}
func next(b *strings.Builder, it Chooser, re *syntax.Regexp) {
if re == nil {
return
}
switch re.Op {
case syntax.OpNoMatch, // matches no strings
syntax.OpBeginLine, // matches empty string at beginning of line
syntax.OpEndLine, // matches empty string at end of line
syntax.OpBeginText, // matches empty string at beginning of text
syntax.OpEndText, // matches empty string at end of text
syntax.OpWordBoundary, // matches word boundary `\b`
syntax.OpNoWordBoundary: // matches word non-boundary `\B`
var bb strings.Builder
toString(&bb, re)
panic(re.Op.String() + " is not implemented in : " + bb.String())
case syntax.OpEmptyMatch: // matches empty string
return
case syntax.OpLiteral: // matches Runes sequence
fmt.Fprintf(b, "%s", string(re.Rune))
return
case syntax.OpCharClass: // matches Runes interpreted as range pair list
// count choices ?
nn := 0
for i := 0; i+1 < len(re.Rune); i += 2 {
nn += int(re.Rune[i+1]-re.Rune[i]) + 1
}
if nn == 0 {
return
}
n := it.Intn(nn)
for i := 0; i+1 < len(re.Rune); i += 2 {
if n < int(re.Rune[i+1]-re.Rune[i])+1 { // match this pair !
fmt.Fprintf(b, "%c", re.Rune[i]+rune(n))
return // done !
} else {
// adjust n and continue to next pair
n = n - (int(re.Rune[i+1]-re.Rune[i]) + 1)
}
}
panic("internal error OpCharClass")
case syntax.OpAnyCharNotNL: // matches any character except newline
n := uint(it.Intn(MaxUnicode - 1))
if n == '\n' {
n++
}
fmt.Fprintf(b, "%c", rune(n))
return
case syntax.OpAnyChar: // matches any character
n := uint(it.Intn(MaxUnicode))
fmt.Fprintf(b, "%c", rune(n))
return
case syntax.OpCapture: // capturing subexpression with index Cap, optional name Name
for _, sub := range re.Sub {
next(b, it, sub)
}
return
case syntax.OpStar: // matches Sub[0] zero or more times
n := exp(it)
for i := 0; i < n; i++ {
for _, sub := range re.Sub { // the choice may differ between the repetitions !
next(b, it, sub)
}
}
case syntax.OpPlus: // matches Sub[0] one or more times
n := exp(it) + 1
for i := 0; i < n; i++ {
for _, sub := range re.Sub { // the choice may differ between the repetitions !
next(b, it, sub)
}
}
case syntax.OpQuest: // matches Sub[0] zero or one times
if it.Intn(2) == 0 {
return
} else {
for _, sub := range re.Sub {
next(b, it, sub)
}
return
}
case syntax.OpRepeat: // matches Sub[0] at least Min times, at most Max (Max == -1 is no limit)
n := re.Min + it.Intn(re.Max-re.Min+1)
for i := 0; i < n; i++ {
for _, sub := range re.Sub { // the choice may differ between the repetitions !
next(b, it, sub)
}
}
case syntax.OpConcat: // matches concatenation of Subs
for _, sub := range re.Sub {
next(b, it, sub)
}
return
case syntax.OpAlternate: // matches alternation of Subs
n := it.Intn(len(re.Sub))
next(b, it, re.Sub[n])
return
default:
panic("unimplemented regexp parse tree operation")
}
}
// cleanPattern from valid but meaningless directives.
func cleanPattern(pattern string) string {
for {
pat := pattern
pat = strings.ReplaceAll(pat, "^", "")
pat = strings.ReplaceAll(pat, "$", "")
pat = strings.ReplaceAll(pat, "\\A", "")
pat = strings.ReplaceAll(pat, "\\B", "")
pat = strings.ReplaceAll(pat, "\\a", "")
pat = strings.ReplaceAll(pat, "\\b", "")
pat = strings.ReplaceAll(pat, "\\z", "")
pat = strings.ReplaceAll(pat, "+?", "+")
pat = strings.ReplaceAll(pat, "*?", "*")
pat = strings.ReplaceAll(pat, "??", "?")
pat = strings.ReplaceAll(pat, "}?", "}")
pat = strings.ReplaceAll(pat, "**", "*")
pat = strings.ReplaceAll(pat, "+*", "+")
if pat == pattern {
return pat
}
pattern = pat
}
}