-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
146 lines (113 loc) · 2.67 KB
/
parser.go
File metadata and controls
146 lines (113 loc) · 2.67 KB
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
package routing
import (
"bytes"
"fmt"
"regexp"
)
const (
tChunkStatic = iota
tChunkDynamic
)
type token struct {
v string
t int
}
type parser struct {
lexer *lexer
last token
chunks []chunk
buf bytes.Buffer
}
type chunk struct {
t int
v string
exp *regexp.Regexp
}
func newParser(path string) *parser {
l := newLexer(path)
return &parser{lexer: l, chunks: make([]chunk, 0, 3)}
}
func (p *parser) parse() (bool, error) {
return p.parseStart()
}
func (p *parser) parseStart() (bool, error) {
token := p.lexer.scan()
if !isSlashToken(token) {
return false, fmt.Errorf("parser error, expected %s but got %s", "/", token.v)
}
p.last = token
p.buf.Write([]byte(token.v))
return p.parseStatic()
}
func (p *parser) parseVar() (bool, error) {
token := p.lexer.scan()
if !isVarToken(token) {
return false, fmt.Errorf("parser error, expected %s but got %s", "var identifier", token.v)
}
p.buf.Write([]byte(token.v))
token = p.lexer.scan()
var regExp *regexp.Regexp
if isRegExpressionToken(token) {
rex, err := regexp.Compile(fmt.Sprintf("^%s$", token.v))
if err != nil {
return false, err
}
regExp = rex
token = p.lexer.scan()
}
if !isCloseVarToken(token) {
return false, fmt.Errorf("parser error, expected %s but got %s", "}", token.v)
}
p.chunks = append(p.chunks, chunk{t: tChunkDynamic, v: p.buf.String(), exp: regExp})
p.buf.Reset()
token = p.lexer.scan()
if isEndToken(token) {
return true, nil
}
if isOpenVarToken(token) || isCloseVarToken(token) || isRegExpressionToken(token) {
return false, fmt.Errorf("parser error, expected %s but got %s", "static", token.v)
}
p.buf.Write([]byte(token.v))
p.last = token
return p.parseStatic()
}
func (p *parser) parseStatic() (bool, error) {
token := p.lexer.scan()
if isEndToken(token) {
p.chunks = append(p.chunks, chunk{t: tChunkStatic, v: p.buf.String()})
p.buf.Reset()
return true, nil
}
if (!isSlashToken(p.last) && isSlashToken(token)) || isStaticToken(token) {
p.buf.Write([]byte(token.v))
p.last = token
return p.parseStatic()
}
if isOpenVarToken(token) {
p.chunks = append(p.chunks, chunk{t: tChunkStatic, v: p.buf.String()})
p.buf.Reset()
return p.parseVar()
}
return false, fmt.Errorf("parser error, unexpected token %s", token.v)
}
func isSlashToken(t token) bool {
return t.t == tSlash
}
func isOpenVarToken(t token) bool {
return t.t == tOpenVar
}
func isCloseVarToken(t token) bool {
return t.t == tCloseVar
}
func isVarToken(t token) bool {
return t.t == tVar
}
func isRegExpressionToken(t token) bool {
return t.t == tExpReg
}
func isEndToken(t token) bool {
return t.t == tEnd
}
func isStaticToken(t token) bool {
return t.t == tStatic
}