-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
187 lines (169 loc) · 3.55 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
package regexp
import (
"fmt"
"unicode/utf8"
)
type parser struct {
in string
pos int
width int
}
func (p *parser) next() (r rune) {
r, p.width = utf8.DecodeRuneInString(p.in[p.pos:])
p.pos += p.width
return
}
func (p *parser) backup() {
p.pos -= p.width
p.width = 0
}
func (p *parser) peek() (r rune) {
r = p.next()
p.backup()
return
}
// expr is a graph of nodes with a start and an end
type expr struct{ start, end nodeBuilder }
func (e expr) isEmpty() bool { return e == expr{} }
func parse(in string) (start, end node, err error) {
p := parser{in: in}
e, err := parseClause(&p)
if err != nil {
return
}
if p.pos != len(in) {
p.backup()
err = fmt.Errorf("illegal %c at pos %d", p.peek(), p.pos)
return
}
term := &terminalNode{}
e = concatNode(e, term)
return e.start, e.end, nil
}
// parseClause parses a sequence of terms connected by '|' or concatenation.
func parseClause(p *parser) (e expr, err error) {
for {
switch r := p.peek(); r {
case utf8.RuneError, ')':
return
case '|':
if e, err = parsePipe(p, e); err != nil {
return
}
default:
var next expr
if next, err = parseTerm(p); err != nil {
return
}
e = concat(e, next)
}
}
}
// parsePipe returns an expr which represents the or of lhs and the next clause
func parsePipe(p *parser, lhs expr) (e expr, err error) {
p.next()
if lhs.isEmpty() {
err = fmt.Errorf("invalid empty expression to the left of %c at pos %d %#v %#v", p.peek(), p.pos, lhs.start, lhs.end)
return
}
if e, err = parseClause(p); err != nil {
return
}
e.start.addEpsilon(lhs.start)
n := &pipeNode{}
e = concatNode(e, n)
lhs = concatNode(lhs, n)
return
}
// parseTerm parses the next single expr to add to a concatenation
func parseTerm(p *parser) (e expr, err error) {
var pf func(*parser) (expr, error)
switch r := p.peek(); r {
case '?', '+', '*':
return expr{}, fmt.Errorf("Invalid character %c at pos %d", r, p.pos)
case '(':
pf = parseSubexp
case '\\':
pf = parseEscape
case '.':
pf = parseDot
default:
pf = parseLiteral
}
if e, err = pf(p); err != nil {
return
}
switch r := p.peek(); r {
case '?', '+', '*':
return parseMeta(p, e)
}
return
}
func parseEscape(p *parser) (expr, error) {
p.next()
switch r := p.peek(); r {
case '?', '*', '+', '(', ')', '|':
return parseLiteral(p)
default:
return expr{}, fmt.Errorf("unknown escape sequence \\%c", r)
}
}
func parseSubexp(p *parser) (e expr, err error) {
start := p.pos
p.next()
if e, err = parseClause(p); err != nil {
return
}
if r := p.next(); r != ')' {
err = fmt.Errorf("unterminated subexp starting at pos %d", start)
}
lp := &leftParenNode{}
rp := &rightParenNode{}
e = concatNode(concat(expr{lp, lp}, e), rp)
return
}
func parseDot(p *parser) (expr, error) {
p.next()
n := &dotNode{}
return expr{n, n}, nil
}
func parseLiteral(p *parser) (expr, error) {
n := &literalNode{r: p.next()}
return expr{n, n}, nil
}
func parseMeta(p *parser, term expr) (e expr, err error) {
mc := p.next()
var n nodeBuilder
switch mc {
case '+':
n = &plusNode{}
n.addEpsilon(term.start)
case '*':
n = &starNode{}
n.addEpsilon(term.start)
term.start.addEpsilon(n)
case '?':
n = &qmarkNode{}
term.start.addEpsilon(n)
}
e = concatNode(term, n)
return
}
func concatNode(e expr, n nodeBuilder) expr {
return concat(e, expr{n, n})
}
func concat(e expr, next expr) expr {
if e.isEmpty() {
return next
}
if next.isEmpty() {
return e
}
if mb, ok := e.end.(matchBuilder); ok {
mb.setNext(next.start)
} else {
e.end.addEpsilon(next.start)
}
e.end = next.end
return e
}