forked from jschaf/bibtex
/
scanner.go
558 lines (510 loc) · 14.6 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Package scanner implements a scanner for bibtex source text.
// It takes a []byte as source which can then be tokenized
// through repeated calls to the Scan method.
package scanner
import (
"fmt"
gotok "go/token"
"path/filepath"
"strings"
"unicode"
"unicode/utf8"
"github.com/stormvirux/bibtex/token"
)
const (
eof = -1
)
// An ErrorHandler may be provided to Scanner.Init. If a syntax error is
// encountered and a handler was installed, the handler is called with a
// position and an error message. The position points to the beginning of
// the offending token.
type ErrorHandler func(pos gotok.Position, msg string)
// A Scanner holds the scanner's internal state while processing
// a given text. It can be allocated as part of another data
// structure but must be initialized via Init before use.
type Scanner struct {
// immutable state
file *gotok.File // source file handle
dir string // directory portion of file.Name()
src []byte // source
err ErrorHandler // error reporting; or nil
mode Mode // scanning mode
// scanning state
ch rune // current character
offset int // character offset
rdOffset int // reading offset (position after current character)
lineOffset int // current line offset
prev token.Token // previous token
endQuoteCh rune // '"' or '}'
braceDepth int // the brace depth in a string; starts at 0
// public state - ok to modify
ErrorCount int // number of errors encountered
}
// Read the next Unicode char into s.ch.
// s.ch < 0 means end-of-file.
func (s *Scanner) next() {
if s.rdOffset < len(s.src) {
s.offset = s.rdOffset
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
r, w := rune(s.src[s.rdOffset]), 1
switch {
case r == 0:
s.error(s.offset, "illegal character NUL")
case r >= utf8.RuneSelf:
// not ASCII
r, w = utf8.DecodeRune(s.src[s.rdOffset:])
if r == utf8.RuneError && w == 1 {
s.error(s.offset, "illegal UTF-8 encoding")
} else if r == bom && s.offset > 0 {
s.error(s.offset, "illegal byte order mark")
}
}
s.rdOffset += w
s.ch = r
} else {
s.offset = len(s.src)
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
s.ch = eof
}
}
func (s *Scanner) error(offs int, msg string) {
if s.err != nil {
s.err(s.file.Position(s.file.Pos(offs)), msg)
}
s.ErrorCount++
}
const bom = 0xFEFF // byte order mark, only permitted as very first character
// peek returns the byte following the most recently read character without
// advancing the scanner. If the scanner is at EOF, peek returns 0.
func (s *Scanner) peek() byte {
if s.rdOffset < len(s.src) {
return s.src[s.rdOffset]
}
return 0
}
// A mode value is a set of flags (or 0).
// They control scanner behavior.
type Mode uint
const (
ScanComments Mode = 1 << iota // return comments as Comment or TexComment tokens
ScanStrings // tokenize the contents of bibtex strings
)
// Init prepares the scanner s to tokenize the text src by setting the
// scanner at the beginning of src. The scanner uses the file set file
// for position information and it adds line information for each line.
// It is ok to re-use the same file when re-scanning the same file as
// line information which is already present is ignored. Init causes a
// panic if the file size does not match the src size.
//
// Calls to Scan will invoke the error handler err if they encounter a
// syntax error and err is not nil. Also, for each error encountered,
// the Scanner field ErrorCount is incremented by one. The mode parameter
// determines how comments are handled.
//
// Note that Init may call err if there is an error in the first character
// of the file.
func (s *Scanner) Init(file *gotok.File, src []byte, err ErrorHandler, mode Mode) {
// Explicitly initialize all fields since a scanner may be reused.
if file.Size() != len(src) {
panic(fmt.Sprintf("file size (%d) does not match src len (%d)", file.Size(), len(src)))
}
s.file = file
s.dir, _ = filepath.Split(file.Name())
s.src = src
s.err = err
s.mode = mode
s.ch = ' '
s.offset = 0
s.rdOffset = 0
s.lineOffset = 0
s.ErrorCount = 0
s.next()
if s.ch == bom {
s.next() // ignore BOM at file beginning
}
}
func (s *Scanner) errorf(offset int, format string, args ...interface{}) {
s.error(offset, fmt.Sprintf(format, args...))
}
func (s *Scanner) skipWhitespace() {
for s.ch == ' ' || s.ch == '\t' || s.ch == '\n' || s.ch == '\r' {
s.next()
}
}
func lower(ch rune) rune { return ('a' - 'A') | ch } // returns lower-case ch iff ch is ASCII letter
func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
func isLetter(ch rune) bool {
return 'a' <= lower(ch) && lower(ch) <= 'z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch)
}
func IsAsciiLetter(ch rune) bool { return 'a' <= lower(ch) && lower(ch) <= 'z' }
// IsName returns true if the char is a valid bibtex cite char.
// Taken from the btparse docs:
// https://metacpan.org/pod/release/AMBS/Text-BibTeX-0.66/btparse/doc/bt_language.pod
// Includes letters, digits, underscores, hyphens and the following:
// ! $ & * + - . / : ; < > ? [ ] ^ _ ` |
func IsName(ch rune) bool {
return ('a' <= ch && ch <= 'z') ||
('A' <= ch && ch <= 'Z') ||
('0' <= ch && ch <= '9') ||
ch == '_' ||
ch == '-' ||
ch == '/' ||
ch == '!' ||
ch == '$' ||
ch == '&' ||
ch == '*' ||
ch == '+' ||
ch == '.' ||
ch == ':' ||
ch == ';' ||
ch == '<' ||
ch == '>' ||
ch == '?' ||
ch == '[' ||
ch == ']' ||
ch == '^' ||
ch == '`' ||
ch == '|'
}
func (s *Scanner) scanCommand() string {
offs := s.offset - 1 // Already consumed @
s.next()
if !isLetter(s.ch) {
s.error(s.offset, "expected letter after @ for a command")
}
for isLetter(s.ch) {
s.next()
}
return string(s.src[offs:s.offset])
}
func (s *Scanner) scanIdent() string {
offs := s.offset
for IsName(s.ch) {
s.next()
}
return string(s.src[offs:s.offset])
}
// scanString parses an bibtex string delimited by double quotes.
func (s *Scanner) scanString() string {
offs := s.offset
for {
ch := s.ch
if ch < 0 {
s.error(offs, "string literal in double quotes not terminated")
break
}
s.next()
if ch == '"' {
break
}
if ch == '{' {
s.scanBraceString()
}
}
return string(s.src[offs : s.offset-1])
}
func (s *Scanner) scanNumber() string {
offs := s.offset
for isDecimal(s.ch) {
s.next()
}
return string(s.src[offs:s.offset])
}
// scanBraceString parses an bibtex string delimited by braces.
func (s *Scanner) scanBraceString() string {
offs := s.offset
for {
ch := s.ch
if ch < 0 {
s.error(offs, "string literal in braces not terminated")
break
}
s.next()
if ch == '}' {
break
}
if ch == '{' {
s.next()
s.scanBraceString()
}
}
return string(s.src[offs : s.offset-1])
}
func (s *Scanner) scanTexComment() string {
offs := s.offset - 1 // initial '%' already consumed
for s.ch != '\n' && s.ch >= 0 {
s.next()
}
return string(s.src[offs:s.offset])
}
func (s *Scanner) scanStringMath() (token.Token, string) {
offs := s.offset
for s.ch != '$' {
if s.ch < 0 || s.ch == '\n' {
s.error(offs, "math in string literal not terminated")
return token.Illegal, string(s.src[offs-1 : s.offset])
}
if s.ch == '\\' {
s.next() // consume the backslash and whatever comes next
}
s.next()
}
s.next() // consume closing '$'
return token.StringMath, string(s.src[offs : s.offset-1])
}
// scanStringEscape scans a string beginning with a backslash.
//
// A string beginning with a backslash can be an:
// - escape sequence for bibtex chars like \{ and \}
// - beginning of a latex command like \url{www.example.com}
// - beginning of a character accent like \^o to represent ô.
//
// See https://tex.stackexchange.com/a/66671/59048.
func (s *Scanner) scanStringEscape() (token.Token, string) {
offs := s.offset - 1 // initial backslash already consumed
switch s.ch {
case '\\', '$', '&', '%', '{', '}', '_':
// a single non-alphabetical character
s.next()
return token.StringBackslash, string(s.src[offs:s.offset])
case '\'', '~', '.', '^', '=', '`':
s.next()
return s.scanSpecialCharStringAccent()
case ',', ';', '[', ']', '(', ')':
// any single non-alphabetical character can be macro.
s.next()
return token.StringMacro, string(s.src[offs:s.offset])
}
// It must be a macro made up of ascii letters.
lo := s.offset
for !s.isSpecialStringChar(s.ch) && s.ch != 0 {
s.next()
}
name := string(s.src[lo:s.offset])
if len(name) == 0 {
s.error(offs, "expected macro name after backslash, got nothing")
return token.Illegal, string(s.src[offs : s.offset-1])
}
// Check that it's only ascii letters.
for _, c := range name {
if !IsAsciiLetter(c) {
s.errorf(offs, "expected command name to only contain ascii letters, got %q", name)
return token.Illegal, string(s.src[offs : s.offset-1])
}
}
return token.StringMacro, name
}
// scanSpecialCharStringAccent scans a string that begins with a backslash
// followed by a special char like \'o, \'{o} or \^{o}.
func (s *Scanner) scanSpecialCharStringAccent() (token.Token, string) {
offs := s.offset - 1 // initial backslash already consumed
s.next() // consume special char
s.next() // consume brace or letter
if s.ch == '{' {
s.next() // consume letter that's accented
if !IsAsciiLetter(s.ch) {
s.errorf(offs, "expected ascii letter after accent sequence %q , got %q", string(s.src[offs:s.offset-1]), s.ch)
return token.Illegal, ""
}
s.next() // consume right brace '}'
if s.ch != '}' {
s.errorf(offs, "expected right brace after accent sequence %q , got %q", string(s.src[offs:s.offset-1]), s.ch)
return token.Illegal, ""
}
} else {
// unbraced accent like \^o
s.next()
if !IsAsciiLetter(s.ch) {
s.errorf(offs, "expected ascii letter after accent sequence %q , got %q", string(s.src[offs:s.offset-1]), s.ch)
return token.Illegal, ""
}
}
if s.next(); s.ch != '}' {
s.errorf(offs, "expected right brace to close accent sequence %q , got %q", string(s.src[offs:s.offset-1]), s.ch)
return token.Illegal, ""
}
return token.StringAccent, string(s.src[offs:s.offset])
}
func (s *Scanner) isSpecialStringChar(ch rune) bool {
if ch == '"' {
// A double quote is only special at brace depth 0 when we started the
// string with a double quote because it terminates the string.
return s.braceDepth == 0 && s.endQuoteCh == '"'
}
return ch == '$' || ch == '{' || ch == '}' ||
ch == eof || ch == ',' ||
ch == '~' || // nbsp
ch == '\\' || // escape chars or begin a macro
ch == '\n' || ch == '\r' || ch == ' ' || ch == '\t' // white space
}
func (s *Scanner) scanStringContents() string {
offs := s.offset
for !s.isSpecialStringChar(s.ch) {
if s.ch == '\\' {
s.next() // consume the backslash and next char
}
s.next()
}
return string(s.src[offs:s.offset])
}
func (s *Scanner) scanInString() (pos gotok.Pos, tok token.Token, lit string) {
if s.endQuoteCh == 0 {
panic("called scanInString but not in quote")
}
pos = s.file.Pos(s.offset)
if !s.isSpecialStringChar(s.ch) {
tok = token.StringContents
lit = s.scanStringContents()
return
}
// It's a special char.
ch := s.ch
s.next()
switch ch {
case '$':
tok, lit = s.scanStringMath()
case '"':
if s.endQuoteCh == '"' && s.braceDepth == 0 {
s.endQuoteCh = 0
tok = token.DoubleQuote
} else {
tok = token.StringContents
lit = `"`
}
case '\\':
tok, lit = s.scanStringEscape()
case '{':
s.braceDepth += 1
tok = token.StringLBrace
case '}':
tok = token.StringRBrace
if s.endQuoteCh == '}' && s.braceDepth == 0 {
s.endQuoteCh = 0
} else {
s.braceDepth -= 1
}
case ' ', '\r', '\n', '\t':
tok = token.StringSpace
s.skipWhitespace()
case ',':
tok = token.StringComma
lit = ","
case '~':
tok = token.StringNBSP
lit = "~"
default:
// next reports unexpected BOMs - don't repeat
if ch != bom {
s.errorf(s.file.Offset(pos), "illegal character %#U in string", ch)
}
tok = token.Illegal
lit = string(ch)
}
return
}
// Scan scans the next token and returns the token position, the token, and its
// literal string if applicable. The source end is indicated by token.EOF.
//
// If the returned token is a literal (token.Ident, token.Number, token.String),
// command, or token.TexComment, the literal string has the corresponding value.
//
// If the returned token is token.Illegal, the literal string is the offending
// character.
//
// In all other cases, Scan returns an empty literal string.
//
// For more tolerant parsing, Scan will return a valid token if possible even
// if a syntax error was encountered. Thus, even if the resulting token sequence
// contains no illegal tokens, a client may not assume that no error has
// occurred. Instead, the client must check the scanner's ErrorCount or the
// number of calls of the error handler, if there was one installed.
//
// Scan adds line information to the file with Init. Token positions are
// relative to the file.
func (s *Scanner) Scan() (pos gotok.Pos, tok token.Token, lit string) {
if s.endQuoteCh == '}' || s.endQuoteCh == '"' {
return s.scanInString()
}
s.skipWhitespace()
pos = s.file.Pos(s.offset)
switch ch := s.ch; {
case isDecimal(ch):
tok = token.Number
lit = s.scanNumber()
case IsName(ch):
tok = token.Ident
lit = s.scanIdent()
default:
s.next() // always make progress
switch ch {
case -1:
tok = token.EOF
case '"':
if s.mode&ScanStrings != 0 {
s.endQuoteCh = '"'
tok = token.DoubleQuote
} else {
tok = token.String
lit = s.scanString()
}
case ',':
tok = token.Comma
case '=':
tok = token.Assign
case '@':
lit = s.scanCommand()
switch cmd := strings.ToLower(lit); cmd {
case "@comment":
tok = token.Comment
case "@string":
tok = token.Abbrev
case "@preamble":
tok = token.Preamble
default:
tok = token.BibEntry
}
case '{':
// Use a heuristic to determine whether this brace is for declaration or
// a brace string. If preceded by '=', it's a string for a tag. If
// preceded by an LBrace, it's a value in a block like:
// @preamble { {foo} }
if s.prev == token.Assign || s.prev == token.LBrace {
if s.mode&ScanStrings != 0 {
s.endQuoteCh = '}'
tok = token.StringLBrace
} else {
tok = token.BraceString
lit = s.scanBraceString()
}
} else {
tok = token.LBrace
}
case '}':
tok = token.RBrace
case '%':
tok = token.TexComment
lit = s.scanTexComment()
case '#':
tok = token.Concat
case '(':
tok = token.LParen
case ')':
tok = token.RParen
default:
// next reports unexpected BOMs - don't repeat
if ch != bom {
s.errorf(s.file.Offset(pos), "illegal character %#U", ch)
}
tok = token.Illegal
lit = string(ch)
}
}
s.prev = tok
return
}