-
Notifications
You must be signed in to change notification settings - Fork 30
/
lex.go
368 lines (334 loc) · 7.78 KB
/
lex.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// The bulk of this lexer code is taken from http://golang.org/src/pkg/text/template/parse/lex.go
// Described in a talk by Rob Pike: http://cuddle.googlecode.com/hg/talk/lex.html#title-slide and http://www.youtube.com/watch?v=HxaD_trXwRE
//
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. (Available here: http://golang.org/LICENSE)
//
// For the remainder of the file:
// Copyright 2014 Richard Lehane. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pronom
import (
"fmt"
"strings"
"unicode/utf8"
)
type item struct {
typ itemType
pos int
val string
}
func (i item) String() string {
switch {
case i.typ == itemEOF:
return "EOF"
case i.typ == itemError:
return i.val
}
return fmt.Sprintf("%q", i.val)
}
type itemType int
const (
itemError itemType = iota
itemEOF
itemCurlyLeft
itemCurlyRight
itemWildStart
itemSlash
itemWildEnd
itemWildSingle //??
itemWild //*
itemUnprocessedText
itemEnterGroup
itemExitGroup
itemChoiceMarker
itemNotMarker
itemRangeMarker
itemMaskMarker
itemAnyMaskMarker
itemHexText
itemQuoteText
itemQuote
itemSpace
)
const (
leftBracket = '['
rightBracket = ']'
leftParens = '('
rightParens = ')'
leftCurly = '{'
rightCurly = '}'
wildSingle = '?'
wild = '*'
not = '!'
colon = ':'
slash = '-'
pipe = '|'
quot = '\''
space = ' '
tab = '\t'
amp = '&'
tilda = '~'
newline = '\n'
carriage = '\r'
)
const digits = "0123456789"
const hexadecimal = digits + "abcdefABCDEF"
const hexnonquote = hexadecimal + " " + "\n" + "\r"
const digitswild = digits + "*"
const eof = -1
type stateFn func(*lexer) stateFn
// lexer holds the state of the scanner.
type lexer struct {
name string
input string
state stateFn
pos int
start int
width int
lastPos int
items chan item
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if int(l.pos) >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
// peek returns but does not consume the next rune in the input.
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
// backup steps back one rune. Can only be called once per call of next.
func (l *lexer) backup() {
l.pos -= l.width
}
// emit passes an item back to the client.
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.start, l.input[l.start:l.pos]}
l.start = l.pos
}
// acceptRun consumes a run of runes from the valid set.
func (l *lexer) acceptRun(valid string) {
for strings.IndexRune(valid, l.next()) >= 0 {
}
l.backup()
}
// acceptText consumes a run of runes that are deemed to be plain sequences (hex or quoted values)
func (l *lexer) acceptText(group bool) error {
valid := hexnonquote
if group {
valid = hexadecimal
}
for {
l.acceptRun(valid)
switch l.peek() {
default:
return nil
case quot:
r := l.next()
for r = l.next(); r != eof && r != quot; r = l.next() {
}
if r != quot {
return fmt.Errorf("expected closing quote, got %v", r)
}
}
}
}
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextItem.
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- item{itemError, l.start, fmt.Sprintf("Lex error in "+l.name+": "+format, args...)}
return nil
}
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
item := <-l.items
l.lastPos = item.pos
return item
}
// lex creates a new scanner for the input string.
func lex(name, input string, start stateFn) *lexer {
l := &lexer{
name: name,
input: input,
items: make(chan item),
}
go l.run(start)
return l
}
// run runs the state machine for the lexer.
func (l *lexer) run(start stateFn) {
for l.state = start; l.state != nil; {
l.state = l.state(l)
}
}
// lexer for PRONOM signature files - reports, container and droid
func lexPRONOM(name, input string) *lexer {
return lex(name, input, insideText)
}
func insideText(l *lexer) stateFn {
if err := l.acceptText(false); err != nil {
return l.errorf(err.Error())
}
if l.pos > l.start {
l.emit(itemUnprocessedText)
}
r := l.next()
switch r {
default:
return l.errorf("encountered invalid character %q", r)
case eof:
l.emit(itemEOF)
return nil
case leftBracket:
l.emit(itemEnterGroup)
return insideLeftBracket
case leftParens:
l.emit(itemEnterGroup)
return insideLeftParens
case leftCurly:
l.emit(itemCurlyLeft)
return insideWild
case wildSingle:
return insideWildSingle
case wild:
l.emit(itemWild)
return insideText
}
}
func (l *lexer) insideGroup(boundary itemType) stateFn {
depth := 1
for {
if err := l.acceptText(true); err != nil {
return l.errorf(err.Error())
}
if l.pos > l.start {
l.emit(itemUnprocessedText)
}
r := l.next()
switch r {
default:
return l.errorf("encountered invalid character %q", r)
case leftBracket:
l.emit(itemEnterGroup)
depth++
case rightBracket:
l.emit(itemExitGroup)
depth--
if depth == 0 {
if boundary != rightBracket {
return l.errorf("expected group to close with %q, got %q", boundary, r)
}
return insideText
}
case rightParens:
if boundary != rightParens {
return l.errorf("expected group to close with %q, got %q", boundary, r)
}
l.emit(itemExitGroup)
return insideText
case not:
l.emit(itemNotMarker)
case pipe, space, tab:
l.emit(itemChoiceMarker)
case colon, slash:
l.emit(itemRangeMarker)
case amp:
l.emit(itemMaskMarker)
case tilda:
l.emit(itemAnyMaskMarker)
}
}
}
func insideLeftBracket(l *lexer) stateFn {
return l.insideGroup(rightBracket)
}
func insideLeftParens(l *lexer) stateFn {
return l.insideGroup(rightParens)
}
func insideWildSingle(l *lexer) stateFn {
r := l.next()
if r == wildSingle {
l.emit(itemWildSingle)
return insideText
}
return l.errorf("expecting a double '?', got %q", r)
}
func insideWild(l *lexer) stateFn {
l.acceptRun(digits) // don't accept a '*' as start of range
if l.pos > l.start {
l.emit(itemWildStart)
}
r := l.next()
if r == slash {
l.emit(itemSlash)
l.acceptRun(digitswild)
if l.pos > l.start {
l.emit(itemWildEnd)
}
r = l.next()
}
if r == rightCurly {
l.emit(itemCurlyRight)
return insideText
}
return l.errorf("expecting a closing bracket, got %q", r)
}
// text lexer
func lexText(input string) *lexer {
return lex("textProcessor", input, insideUnprocessedText)
}
func insideUnprocessedText(l *lexer) stateFn {
for {
l.acceptRun(hexadecimal)
if l.pos > l.start {
l.emit(itemHexText)
}
switch l.next() {
default:
l.backup()
return l.errorf("unexpected character in text: %q", l.next())
case eof:
l.emit(itemEOF)
return nil
case quot:
l.emit(itemQuote)
return insideQuoteText
case space, tab, newline, carriage:
l.emit(itemSpace)
}
}
}
func insideQuoteText(l *lexer) stateFn {
r := l.next()
for ; r != eof && r != quot; r = l.next() {
}
if r == quot {
l.backup()
l.emit(itemQuoteText)
l.next()
l.emit(itemQuote)
return insideUnprocessedText
}
return l.errorf("expected closing quote, reached end of string")
}