-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
242 lines (233 loc) · 4.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"errors"
"io"
"regexp"
"strconv"
"unicode"
)
// parse parses the user input for valid addresses and returns if the
// current token is not a valid range command or EOF.
func (ed *Editor) parse() error {
var (
addr int
err error
)
ed.addrc = 0
ed.start = ed.dot
ed.end = ed.dot
for {
addr, err = ed.nextAddress()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
if addr < 1 {
break
}
ed.addrc++
ed.start = ed.end
ed.end = addr
if ed.tok != ',' && ed.tok != ';' {
break
} else if ed.consume() == ';' {
ed.dot = addr
}
}
if ed.addrc > 2 {
ed.addrc = 2
}
if ed.addrc == 1 || ed.end != addr {
ed.start = ed.end
}
return nil
}
// nextAddress extracts the next address in the user input.
func (ed *Editor) nextAddress() (int, error) {
var (
addr = ed.dot
err error
first = true
)
ed.skipWhitespace()
startpos := ed.tokpos
for starttok := ed.tok; ; first = false {
switch {
case unicode.IsDigit(ed.tok) || ed.tok == '+' || ed.tok == '-' || ed.tok == '^':
mod := ed.tok
if !unicode.IsDigit(mod) {
ed.consume()
}
var n int
ed.skipWhitespace()
if unicode.IsDigit(ed.tok) {
n, err = ed.scanNumber()
if err != nil {
return -1, err
}
} else if !unicode.IsSpace(mod) {
n = 1
}
switch mod {
case '-', '^':
addr -= n
case '+':
addr += n
default:
addr = n
}
case ed.tok == '.' || ed.tok == '$':
if ed.tokpos != startpos {
return -1, ErrInvalidAddress
}
addr = len(ed.lines)
if ed.tok == '.' {
addr = ed.dot
}
ed.consume()
case ed.tok == '?', ed.tok == '/':
if !first {
return -1, ErrInvalidAddress
}
var mod = ed.tok
ed.consume()
var search = ed.scanStringUntil(mod)
re, err := regexp.Compile(search)
if err != nil {
return -1, err
}
if search == "" {
if ed.re == nil {
return -1, ErrNoPrevPattern
}
re = ed.re
}
ed.re = re
var (
found bool
i = ed.dot
)
if len(ed.lines) < 1 {
return -1, ErrNoMatch
}
if mod != '/' {
i--
}
for {
if mod == '/' {
i++
} else {
i--
if i < 0 {
i = len(ed.lines) - 1
}
}
i %= len(ed.lines)
if re.MatchString(ed.lines[i]) {
addr = i + 1
found = true
break
}
if i == ed.dot {
break
}
}
if !found {
return -1, ErrNoMatch
}
case ed.tok == '\'':
if !first {
return -1, ErrInvalidAddress
}
var r = ed.consume()
if r == EOF || !unicode.IsLower(r) {
return -1, ErrInvalidMark
}
var mark = int(r) - 'a'
if mark < 0 || mark >= len(ed.mark) {
return -1, ErrInvalidMark
}
var maddr = ed.mark[mark]
if maddr < 1 || maddr > len(ed.lines) {
return -1, ErrInvalidAddress
}
addr = maddr
ed.consume()
case ed.tok == '%' || ed.tok == ',' || ed.tok == ';':
if first {
ed.addrc++
ed.end = 1
if ed.tok == ';' {
ed.end = ed.dot
}
ed.consume()
if addr, err = ed.nextAddress(); err != nil {
addr = len(ed.lines)
}
}
fallthrough
default:
if ed.tok == starttok {
return -1, io.EOF
}
if addr < 0 || addr > len(ed.lines) {
ed.addrc++
return -1, ErrInvalidAddress
}
return addr, nil
}
}
}
// check validates if n, m are valid depending on how many addresses
// were previously parsed. check returns error "invalid address" if
// the positions are out of bounds.
func (ed *Editor) check(n, m int) error {
if ed.addrc == 0 {
ed.start = n
ed.end = m
}
if ed.start > ed.end || ed.start < 1 || ed.end > len(ed.lines) {
return ErrInvalidAddress
}
return nil
}
// scanNumber scans the user input for a number and will advance until
// the current token is not a valid digit.
func (ed *Editor) scanNumber() (int, error) {
var s string
for unicode.IsDigit(ed.tok) {
s += string(ed.tok)
ed.consume()
}
return strconv.Atoi(s)
}
// scanString scans the user input until EOF or new line.
func (ed *Editor) scanString() string {
var str string
for ed.tok != EOF && ed.tok != '\n' {
str += string(ed.tok)
ed.consume()
}
return str
}
// scanStringUntil works like `scanString` but will continue until it
// sees (and consumes) `delim`. If `delim` is not found it continues
// until EOF.
func (ed *Editor) scanStringUntil(delim rune) string {
var str string
for ed.tok != EOF && ed.tok != '\n' && ed.tok != delim {
str += string(ed.tok)
ed.consume()
}
if ed.tok == delim {
ed.consume()
}
return str
}
func (e *Editor) skipWhitespace() {
for e.tok == ' ' || e.tok == '\t' {
e.consume()
}
}