-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
200 lines (179 loc) · 4.28 KB
/
parser.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
package main
import (
"fmt"
"strconv"
"strings"
)
const repeatInfinity = -1
func parse(regex string) *parseContext {
ctx := &parseContext{
pos: 0,
tokens: []token{},
}
for ctx.pos < len(regex) {
process(regex, ctx)
ctx.pos++
}
return ctx
}
//process each element
func process(regex string, ctx *parseContext) {
ch := regex[ctx.pos]
if ch == '(' {
groupCtx := &parseContext{
pos: ctx.pos,
tokens: []token{},
}
parseGroup(regex, groupCtx)
ctx.tokens = append(ctx.tokens, token{
tokenType: group,
value: groupCtx.tokens,
})
} else if ch == '[' { // <2>
parseBracket(regex, ctx)
} else if ch == '|' { // <3>
parseOr(regex, ctx)
} else if ch == '*' || ch == '?' || ch == '+' { // <4>
parseRepeat(regex, ctx)
} else if ch == '{' { // <5>
parseRepeatSpecified(regex, ctx)
} else { // <6>
// literal
t := token{
tokenType: literal,
value: ch,
}
ctx.tokens = append(ctx.tokens, t)
}
}
func parseGroup(regex string, ctx *parseContext) {
ctx.pos++
for regex[ctx.pos] != ')' {
process(regex, ctx)
ctx.pos++
}
}
func parseBracket(regex string, ctx *parseContext) {
ctx.pos++ // get past the LBRACKET
var literals []string
for regex[ctx.pos] != ']' { // <1>
ch := regex[ctx.pos]
if ch == '-' { // <2>
next := regex[ctx.pos+1] // <3-1>
prev := literals[len(literals)-1][0] // <3-1>
literals[len(literals)-1] = fmt.Sprintf("%c%c", prev, next) // <3-2>
ctx.pos++ // to consume the 'next' char
} else { // <4>
literals = append(literals, fmt.Sprintf("%c", ch))
}
ctx.pos++ // <5>
}
literalsSet := map[uint8]bool{}
for _, l := range literals { // <6>
for i := l[0]; i <= l[len(l)-1]; i++ { // <7>
literalsSet[i] = true
}
}
ctx.tokens = append(ctx.tokens, token{ // <8>
tokenType: bracket,
value: literalsSet,
})
}
func parseOr(regex string, ctx *parseContext) {
rhsContext := &parseContext{
pos: ctx.pos,
tokens: []token{},
}
rhsContext.pos += 1 // get past |
for rhsContext.pos < len(regex) && regex[rhsContext.pos] != ')' {
process(regex, rhsContext)
rhsContext.pos += 1
}
// <1:end>
// both sides of the OR expression
left := token{
tokenType: groupUncaptured,
value: ctx.tokens, // <2>
}
right := token{ // <3>
tokenType: groupUncaptured,
value: rhsContext.tokens,
}
ctx.pos = rhsContext.pos // <4>
ctx.tokens = []token{{ // <5>
tokenType: or,
value: []token{left, right},
}}
}
func parseRepeat(regex string, ctx *parseContext) {
ch := regex[ctx.pos]
var min, max int
if ch == '*' {
min = 0
max = repeatInfinity
} else if ch == '?' {
min = 0
max = 1
} else {
// ch == '+'
min = 1
max = repeatInfinity
}
// we need to wrap the last token with the quantifier data
// so that we know what the min and max apply to
lastToken := ctx.tokens[len(ctx.tokens)-1]
ctx.tokens[len(ctx.tokens)-1] = token{
tokenType: repeat,
value: repeatPayload{
min: min,
max: max,
token: lastToken,
},
}
}
func parseRepeatSpecified(regex string, ctx *parseContext) {
// +1 because we skip LCURLY { at the beginning
start := ctx.pos + 1
// proceed until we reach to the end of the curly braces
for regex[ctx.pos] != '}' {
ctx.pos++
}
boundariesStr := regex[start:ctx.pos] // <1>
pieces := strings.Split(boundariesStr, ",") // <2>
var min, max int
if len(pieces) == 1 { // <3>
if value, err := strconv.Atoi(pieces[0]); err != nil {
panic(err.Error())
} else {
min = value
max = value
}
} else if len(pieces) == 2 { // <4>
if value, err := strconv.Atoi(pieces[0]); err != nil {
panic(err.Error())
} else {
min = value
}
if pieces[1] == "" {
max = repeatInfinity
} else if value, err := strconv.Atoi(pieces[1]); err != nil {
panic(err.Error())
} else {
max = value
}
} else {
panic(fmt.Sprintf("There must be either 1 or 2 values specified for the quantifier: provided '%s'", boundariesStr))
}
// we need to wrap the last token with the quantifier data
// so that we know what the min and max apply to
// <5>
lastToken := ctx.tokens[len(ctx.tokens)-1]
ctx.tokens[len(ctx.tokens)-1] = token{
tokenType: repeat,
value: repeatPayload{
min: min,
max: max,
token: lastToken,
},
}
}