This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
regexp.go
169 lines (150 loc) · 3.66 KB
/
regexp.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
package main
import (
"container/list"
"fmt"
"regexp"
"strings"
)
// Parses flex-style regular expressions.
type flexParser struct {
p *Parser
line string
i int
stateFunc func(fp *flexParser) bool
qStart int
tcStart int
rangeStarts *list.List
lastElement int
}
func (p *Parser) ParseFlex(line string) (startConds []string, expr string, trailing string, remainder string) {
fp := &flexParser{p: p,
line: line,
i: 0,
stateFunc: (*flexParser).stateRoot,
qStart: 0,
tcStart: -1,
rangeStarts: list.New(),
lastElement: -1}
if fp.line[0] == '<' {
sce := strings.Index(fp.line, ">")
scs := fp.line[1:sce]
startConds = strings.Split(scs, ",")
fp.line = fp.line[sce+1:]
} else {
startConds = []string{}
}
for ; fp.i < len(fp.line); fp.i++ {
if fp.line[fp.i] == '\\' {
fp.i++
continue
}
if fp.stateFunc(fp) {
break
}
}
if fp.tcStart != -1 {
expr = fp.line[:fp.tcStart]
trailing = fp.line[fp.tcStart+1 : fp.i]
} else {
expr = fp.line[:fp.i]
trailing = ""
}
remainder = fp.line[fp.i:]
return
}
func (fp *flexParser) stateRoot() bool {
switch fp.line[fp.i] {
case ' ', '\t':
return true
case '[':
fp.stateFunc = (*flexParser).stateClass
fp.lastElement = fp.i
case '"':
fp.stateFunc = (*flexParser).stateQuotes
fp.qStart = fp.i
case '{':
fp.stateFunc = (*flexParser).stateSubst
fp.qStart = fp.i
case '/':
if fp.tcStart != -1 {
panic("multiple trailing contexts '/'")
}
fp.tcStart = fp.i
case '.':
repl := "[^\\n]"
fp.line = fp.line[:fp.i] + repl + fp.line[fp.i+1:]
fp.lastElement = fp.i
fp.i += len(repl) - 1
case '^':
if fp.i != 0 {
// ^ to be treated as non-special if not at start
// of fp.line
fp.line = fp.line[:fp.i] + "\\^" + fp.line[fp.i+1:]
fp.lastElement = fp.i
fp.i += 1
}
case '$':
if fp.tcStart != -1 {
panic("unescaped '$' in pattern found after trailing context '/'")
} else if fp.i != len(fp.line)-1 && fp.line[fp.i+1] != ' ' && fp.line[fp.i+1] != '\t' {
// $ to be treated as non-special if not last char
fp.line = fp.line[:fp.i] + "\\$" + fp.line[fp.i+1:]
fp.lastElement = fp.i
fp.i += 1
} else {
// last char.
fp.tcStart = fp.i
// fp.line[fp.i+1:] should be empty anyway.
fp.line = fp.line[:fp.i] + "/\\n|$" + fp.line[fp.i+1:]
fp.i += 5 - 1
}
case '(':
if len(fp.line) > fp.i+3 && fp.line[fp.i+1:fp.i+3] == "?#" {
// Regular expression comment.
end := strings.Index(fp.line[fp.i:], ")")
fp.line = fp.line[:fp.i] + fp.line[end+fp.i+1:]
fp.i--
} else {
fp.rangeStarts.PushFront(fp.i)
}
case ')':
f := fp.rangeStarts.Front()
fp.lastElement = f.Value.(int)
fp.rangeStarts.Remove(f)
default:
fp.lastElement = fp.i
}
return false
}
func (fp *flexParser) stateClass() bool {
if fp.line[fp.i] == ']' {
fp.stateFunc = (*flexParser).stateRoot
}
return false
}
func (fp *flexParser) stateQuotes() bool {
if fp.line[fp.i] != '"' {
return false
}
origQuoted := fp.line[fp.qStart+1 : fp.i]
quoted := strings.Replace(origQuoted, "\\\"", "\"", -1)
quoted = regexp.QuoteMeta(quoted)
fp.line = fp.line[:fp.qStart] + quoted + fp.line[fp.i+1:]
fp.i += len(quoted) - len(origQuoted) - 2
fp.stateFunc = (*flexParser).stateRoot
return false
}
func (fp *flexParser) stateSubst() bool {
if fp.line[fp.i] != '}' {
return false
}
name := fp.line[fp.qStart+1 : fp.i]
repl, found := fp.p.parseSubs[name]
if !found {
panic(fmt.Sprintf("substitution {%s} found, but no such sub found!", name))
}
fp.line = fp.line[:fp.qStart] + "(" + repl + ")" + fp.line[fp.i+1:]
fp.i += 2 + len(repl) - len(name) - 2
fp.stateFunc = (*flexParser).stateRoot
return false
}