-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregexps.go
213 lines (194 loc) · 5.02 KB
/
regexps.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 crun
import (
"fmt"
"log"
"math/rand"
"regexp/syntax"
"time"
)
// MoreTimes Maximum omitted default value
const MoreTimes = 18
// Regexp syntax tree translated from regexp/syntax
type Regexp struct {
Op Op
Sub []Regexps
Rune []rune
Min, Max int
}
// Regexps syntax tree translated from regexp/syntax
type Regexps []*Regexp
// Compile parses a regular expression and returns.
func Compile(str string) (Regexps, error) {
reg, err := syntax.Parse(str, syntax.Perl)
if err != nil {
return nil, fmt.Errorf("crun: Compile(%q): %w", str, err)
}
return NewRegexps(reg), nil
}
// MustCompile is like Compile but panics if the expression cannot be parsed.
// It simplifies safe initialization of global variables holding compiled regular
// expressions.
func MustCompile(str string) Regexps {
reg, err := Compile(str)
if err != nil {
panic(err)
}
return reg
}
// NewRegexps returns regexps translated from regexp/syntax
func NewRegexps(reg *syntax.Regexp) (out Regexps) {
return std.NewRegexps(reg)
}
var std = &Optional{
MoreTimes: MoreTimes,
AnyCharNotNL: []rune{33, 126},
AnyChar: []rune{33, 126},
}
// Optional is optional related option for regexps
type Optional struct {
MoreTimes int
AnyCharNotNL []rune
AnyChar []rune
Rand Rand
}
// NewRegexps returns regexps translated from regexp/syntax
func (o *Optional) NewRegexps(reg *syntax.Regexp) (out Regexps) {
ff := func(rs ...*Regexp) {
out = append(out, rs...)
}
switch reg.Op {
case syntax.OpNoMatch: // matches no strings
case syntax.OpEmptyMatch: // matches empty string
case syntax.OpLiteral: // matches Runes sequence
ff(&Regexp{
Op: OpLiteral,
Rune: reg.Rune,
})
case syntax.OpCharClass: // matches Runes interpreted as range pair list
ff(&Regexp{
Op: OpRepeat,
Rune: reg.Rune,
Max: 1,
Min: 1,
})
case syntax.OpAnyCharNotNL: // matches any character except newline
ff(&Regexp{
Op: OpRepeat,
Rune: o.AnyCharNotNL,
Max: 1,
Min: 1,
})
case syntax.OpAnyChar: // matches any character
ff(&Regexp{
Op: OpRepeat,
Rune: o.AnyChar,
Max: 1,
Min: 1,
})
case syntax.OpBeginLine: // matches empty string at beginning of line
case syntax.OpEndLine: // matches empty string at end of line
case syntax.OpBeginText: // matches empty string at beginning of text
case syntax.OpEndText: // matches empty string at end of text
case syntax.OpWordBoundary: // matches word boundary `\b`
case syntax.OpNoWordBoundary: // matches word non-boundary `\B`
case syntax.OpCapture: // capturing subexpression with index Cap, optional name Name
for _, v := range reg.Sub {
ff(o.NewRegexps(v)...)
}
case syntax.OpStar: // matches Sub[0] zero or more times
sub := make([]Regexps, 0, len(reg.Sub))
for _, v := range reg.Sub {
sub = append(sub, o.NewRegexps(v))
}
ff(&Regexp{
Op: OpRepeat,
Sub: sub,
Max: o.MoreTimes,
Min: 0,
})
case syntax.OpPlus: // matches Sub[0] one or more times
sub := make([]Regexps, 0, len(reg.Sub))
for _, v := range reg.Sub {
sub = append(sub, o.NewRegexps(v))
}
ff(&Regexp{
Op: OpRepeat,
Sub: sub,
Max: o.MoreTimes,
Min: 1,
})
case syntax.OpQuest: // matches Sub[0] zero or one times
sub := make([]Regexps, 0, len(reg.Sub))
for _, v := range reg.Sub {
sub = append(sub, o.NewRegexps(v))
}
ff(&Regexp{
Op: OpRepeat,
Sub: sub,
Max: 1,
Min: 0,
})
case syntax.OpRepeat: // matches Sub[0] at least Min times, at most Max (Max == -1 is no limit)
sub := make([]Regexps, 0, len(reg.Sub))
for _, v := range reg.Sub {
sub = append(sub, o.NewRegexps(v))
}
ff(&Regexp{
Op: OpRepeat,
Sub: sub,
Max: reg.Max,
Min: reg.Min,
})
case syntax.OpConcat: // matches concatenation of Subs
for _, v := range reg.Sub {
ff(o.NewRegexps(v)...)
}
case syntax.OpAlternate: // matches alternation of Subs
sub := make([]Regexps, 0, len(reg.Sub))
for _, v := range reg.Sub {
sub = append(sub, o.NewRegexps(v))
}
ff(&Regexp{
Op: OpAlternate,
Sub: sub,
})
default:
log.Printf("crun: unsupported op %v", reg.Op)
}
return out
}
// Size The number of possibilities that can match regularity
func (r Regexps) Size() int {
s := 0
size(r, &s)
return s
}
// Range all possibilities
func (r Regexps) Range(f func(string) bool) bool {
return r.RangeWithRunes(func(s []rune) bool {
return f(string(s))
})
}
// RangeWithRuns all possibilities
func (r Regexps) RangeWithRunes(f func([]rune) bool) bool {
return ranges(r, []rune{}, 0, func(s []rune) bool {
return f(s)
})
}
// Rand possibilities
func (r Regexps) Rand() string {
return string(r.RandWithRunes())
}
// RandWithRunes possibilities
func (r Regexps) RandWithRunes() []rune {
return rands(r, stdRandSource, []rune{})
}
// RandSource possibilities
func (r Regexps) RandSource(rand Rand) string {
return string(r.RandSourceWithRunes(rand))
}
// RandSourceWithRunes possibilities
func (r Regexps) RandSourceWithRunes(rand Rand) []rune {
return rands(r, rand, []rune{})
}
var stdRandSource = rand.New(rand.NewSource(time.Now().UnixNano()))