-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscanner.go
237 lines (224 loc) · 5.2 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
package bibtex
import (
"bufio"
"bytes"
"io"
"strconv"
"strings"
)
var parseField bool
// scanner is a lexical scanner
type scanner struct {
commentMode bool
r *bufio.Reader
pos tokenPos
}
// newScanner returns a new instance of scanner.
func newScanner(r io.Reader) *scanner {
return &scanner{r: bufio.NewReader(r), pos: tokenPos{Char: 0, Lines: []int{}}}
}
// read reads the next rune from the buffered reader.
// Returns the rune(0) if an error occurs (or io.eof is returned).
func (s *scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
if ch == '\n' {
s.pos.Lines = append(s.pos.Lines, s.pos.Char)
s.pos.Char = 0
} else {
s.pos.Char++
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *scanner) unread() {
_ = s.r.UnreadRune()
if s.pos.Char == 0 {
s.pos.Char = s.pos.Lines[len(s.pos.Lines)-1]
s.pos.Lines = s.pos.Lines[:len(s.pos.Lines)-1]
} else {
s.pos.Char--
}
}
// Scan returns the next token and literal value.
func (s *scanner) Scan() (tok token, lit string, err error) {
ch := s.read()
if isWhitespace(ch) {
s.ignoreWhitespace()
ch = s.read()
}
if isAlphanum(ch) {
s.unread()
return s.scanIdent()
}
switch ch {
case eof:
return 0, "", nil
case '@':
return tATSIGN, string(ch), nil
case ':':
return tCOLON, string(ch), nil
case ',':
parseField = false // reset parseField if reached end of field.
return tCOMMA, string(ch), nil
case '=':
parseField = true // set parseField if = sign outside quoted or ident.
return tEQUAL, string(ch), nil
case '"':
tok, lit := s.scanQuoted()
return tok, lit, nil
case '{':
if parseField {
return s.scanBraced()
}
// If we're reading a comment, return everything after {
// to the next @-sign (exclusive)
if s.commentMode {
s.unread()
commentBodyTok, commentBody := s.scanCommentBody()
return commentBodyTok, commentBody, nil
}
return tLBRACE, string(ch), nil
case '}':
if parseField { // reset parseField if reached end of entry.
parseField = false
}
return tRBRACE, string(ch), nil
case '#':
return tPOUND, string(ch), nil
case ' ':
s.ignoreWhitespace()
}
return tILLEGAL, string(ch), nil
}
// scanIdent categorises a string to one of three categories.
func (s *scanner) scanIdent() (tok token, lit string, err error) {
switch ch := s.read(); ch {
case '"':
tok, lit := s.scanQuoted()
return tok, lit, nil
case '{':
return s.scanBraced()
default:
s.unread() // Not open quote/brace.
tok, lit := s.scanBare()
return tok, lit, nil
}
}
func (s *scanner) scanBare() (token, string) {
var buf bytes.Buffer
for {
if ch := s.read(); ch == eof {
break
} else if !isAlphanum(ch) && !isBareSymbol(ch) || isWhitespace(ch) {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
str := buf.String()
if strings.ToLower(str) == "comment" {
s.commentMode = true
return tCOMMENT, str
} else if strings.ToLower(str) == "preamble" {
return tPREAMBLE, str
} else if strings.ToLower(str) == "string" {
return tSTRING, str
} else if _, err := strconv.Atoi(str); err == nil && parseField { // Special case for numeric
return tIDENT, str
}
return tBAREIDENT, str
}
// scanBraced parses a braced string, like {this}.
func (s *scanner) scanBraced() (token, string, error) {
var buf bytes.Buffer
var macro bool
brace := 1
for {
if ch := s.read(); ch == eof {
break
} else if ch == '\\' {
_, _ = buf.WriteRune(ch)
macro = true
} else if ch == '{' {
_, _ = buf.WriteRune(ch)
brace++
} else if ch == '}' {
brace--
macro = false
if brace == 0 { // Balances open brace.
return tIDENT, buf.String(), nil
}
_, _ = buf.WriteRune(ch)
} else if ch == '@' {
if macro {
_, _ = buf.WriteRune(ch)
} else {
return token(0), buf.String(), ErrUnexpectedAtsign
}
} else if isWhitespace(ch) {
_, _ = buf.WriteRune(ch)
macro = false
} else {
_, _ = buf.WriteRune(ch)
}
}
return tILLEGAL, buf.String(), nil
}
// scanQuoted parses a quoted string, like "this".
func (s *scanner) scanQuoted() (token, string) {
var buf bytes.Buffer
brace := 0
for {
if ch := s.read(); ch == eof {
break
} else if ch == '{' {
brace++
} else if ch == '}' {
brace--
} else if ch == '"' {
if brace == 0 { // Matches open quote, unescaped
return tIDENT, buf.String()
}
_, _ = buf.WriteRune(ch)
} else {
_, _ = buf.WriteRune(ch)
}
}
return tILLEGAL, buf.String()
}
// skipCommentBody is a scan method used for reading bibtex
// comment item by reading all runes until the next @.
//
// e.g.
// @comment{...anything can go here even if braces are unbalanced@
// comment body string will be "...anything can go here even if braces are unbalanced"
func (s *scanner) scanCommentBody() (token, string) {
var buf bytes.Buffer
for {
if ch := s.read(); ch == eof {
break
} else if ch == '@' {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
s.commentMode = false
return tCOMMENTBODY, buf.String()
}
// ignoreWhitespace consumes the current rune and all contiguous whitespace.
func (s *scanner) ignoreWhitespace() {
for {
if ch := s.read(); ch == eof {
break
} else if !isWhitespace(ch) {
s.unread()
break
}
}
}