-
Notifications
You must be signed in to change notification settings - Fork 9
/
scanner.go
334 lines (306 loc) · 7.07 KB
/
scanner.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
/*
This is free and unencumbered software released into the public domain. For more
information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
*/
package gedcom
import (
"fmt"
"io"
"strconv"
)
type Line struct {
Level int
Tag string
Value string
Xref string
LineNumber int // the line number of the input file
Offset int // the character offset in the input file
}
func (l *Line) String() string {
if l.Xref != "" {
return fmt.Sprintf("%d @%s@ %s %s", l.Level, l.Xref, l.Tag, l.Value)
}
return fmt.Sprintf("%d %s %s", l.Level, l.Tag, l.Value)
}
// A Scanner is a GEDCOM scanning state machine.
type Scanner struct {
r io.RuneScanner
err error
state int
line int
offset int
level int
buf []rune
tag string
value string
xref string
}
// NewScanner creates a new Scanner ready for use.
func NewScanner(r io.RuneScanner) *Scanner {
return &Scanner{
r: r,
state: stateBegin,
buf: make([]rune, 0, 4),
}
}
const (
stateBegin = iota
stateLevel
stateSeekTagOrXref
stateSeekTag
stateTag
stateXref
stateSeekValue
stateValue
stateEnd
stateError
)
// Next advances the scanner to the next line. It returns false if there are no more lines
// or if an error is encountered. The caller should check the Err method whenever this
// method returns false.
func (s *Scanner) Next() bool {
s.state = stateBegin
s.level = 0
s.buf = s.buf[:0]
s.xref = ""
s.tag = ""
s.value = ""
s.offset = 0
s.line++
for {
c, n, err := s.r.ReadRune()
if err != nil {
if err != io.EOF {
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("read: %w", err),
}
}
if s.state != stateEnd && s.state != stateBegin {
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: io.ErrUnexpectedEOF,
}
}
return false
}
s.offset += n
switch s.state {
case stateBegin:
switch {
case c >= '0' && c <= '9':
s.buf = append(s.buf, c)
s.state = stateLevel
case isSpace(c):
continue
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("found non-whitespace %q (%#[1]x) before level", c),
}
return false
}
case stateLevel:
switch {
case c >= '0' && c <= '9':
s.buf = append(s.buf, c)
continue
case c == ' ':
parsedLevel, perr := strconv.ParseInt(string(s.buf), 10, 64)
if perr != nil {
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("parse level: %w", perr),
}
return false
}
s.level = int(parsedLevel)
s.buf = s.buf[:0]
s.state = stateSeekTagOrXref
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("level contained non-numerics"),
}
return false
}
case stateSeekTag:
switch {
case isAlphaNumeric(c):
s.buf = append(s.buf, c)
s.state = stateTag
case c == ' ':
continue
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("tag contained non-alphanumeric (%#x)", c),
}
return false
}
case stateSeekTagOrXref:
switch {
case isAlphaNumeric(c):
s.buf = append(s.buf, c)
s.state = stateTag
case c == '@':
s.state = stateXref
case c == ' ':
continue
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("tag or xref contained non-alphanumeric (%#x)", c),
}
return false
}
case stateTag:
switch {
case isAlphaNumeric(c):
s.buf = append(s.buf, c)
continue
case c == '\n' || c == '\r':
s.swallowCr(c)
s.tag = string(s.buf)
s.buf = s.buf[:0]
s.state = stateEnd
return true
case c == ' ':
s.tag = string(s.buf)
s.buf = s.buf[:0]
s.state = stateSeekValue
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("tag contained non-alphanumeric (%#x)", c),
}
return false
}
case stateXref:
switch {
case isAlphaNumeric(c):
s.buf = append(s.buf, c)
continue
case c == '@':
continue
case c == ' ':
s.xref = string(s.buf)
s.buf = s.buf[:0]
s.state = stateSeekTag
default:
s.state = stateError
s.err = &ScanErr{
LineNumber: s.line,
Offset: s.offset,
Err: fmt.Errorf("xref contained non-alphanumeric (%#x)", c),
}
return false
}
case stateSeekValue:
switch {
case c == '\n' || c == '\r':
s.swallowCr(c)
s.state = stateEnd
return true
case c == ' ':
continue
default:
s.buf = append(s.buf, c)
s.state = stateValue
}
case stateValue:
switch {
case c == '\n' || c == '\r':
s.swallowCr(c)
// Check to see if there is a malformed NOTE that contains an embedded newline
// For example, Ancestry GEDCOM exports that include source "London, England, Church of England Births and Baptisms, 1813-1917"
// have the following NOTE tag split over two lines (yet the CONC tag is correctly formatted!)
//
// 1 NOTE Board of Guardian Records and Church of England Parish Registers. London Metropolitan Archives, London.
// <p>Images produced by permission of the City of London Corporation. The City of London gives n
if s.tag == "NOTE" {
next, _, err := s.r.ReadRune()
s.r.UnreadRune()
if err == nil {
if !isNumeric(next) {
// Looks like it might be a malformed note, so continue parsing
s.buf = append(s.buf, '\n')
continue
}
}
}
s.value = string(s.buf)
s.buf = s.buf[:0]
s.state = stateEnd
return true
default:
s.buf = append(s.buf, c)
continue
}
}
}
}
// swallowCr skips a carriage return if it is followed by a newline
func (s *Scanner) swallowCr(c rune) {
if c == '\r' {
next, _, _ := s.r.ReadRune()
if next == '\n' {
s.offset++
} else {
s.r.UnreadRune()
}
}
}
// Line returns the most recent line tokenized by a call to Next.
func (s *Scanner) Line() Line {
return Line{
Level: s.level,
Tag: s.tag,
Value: s.value,
Xref: s.xref,
LineNumber: s.line,
Offset: s.offset,
}
}
// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
if s.err == nil {
return nil
}
return s.err
}
type ScanErr struct {
Err error
LineNumber int
Offset int
}
func (e *ScanErr) Error() string {
return fmt.Sprintf("scan error (line:%d, position:%d): %v", e.LineNumber, e.Offset, e.Err)
}
func (e *ScanErr) Unwrap() error {
return e.Err
}
func isSpace(c rune) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'
}
func isAlphaNumeric(c rune) bool {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
}
func isNumeric(c rune) bool {
return (c >= '0' && c <= '9')
}