This repository has been archived by the owner on May 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanner.go
373 lines (336 loc) · 8.7 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
package pre
import (
"bytes"
"fmt"
"unicode"
)
// scanState represents the internal state of the lexical analyzer.
type scanState int
const (
scanStateInit scanState = iota
scanStateBang
scanStateText
scanStateLine
scanStateDirective
scanStateParams
scanStateIdentifier
scanStateSingleComment
scanStateMultiComment
scanStateSlash
scanStateHash
)
// We use a '!' since '#' is reserved for single-line comments.
const directivePrefixRune = '!'
const directivePrefixString = "!"
// Scanner is the lexical analyzer for the preprocessor.
type Scanner struct {
buffer readBuffer
state scanState
lookahead int
nextToken Token
nextText string
nextErr error
line int
comment bool // Is the scanner currently in a multiline comment?
multiline int
prev scanState // The state previous to the comment.
verbose bool
}
func (s *Scanner) reset() {
s.state = scanStateInit
s.lookahead = 0
s.line = 0
s.comment = false
}
func (s *Scanner) SetFile(name string) {
s.reset()
s.buffer = readBuffer{}
s.buffer.readFile(name)
}
func (s *Scanner) SetText(text string) {
s.reset()
s.buffer = readBuffer{}
s.buffer.readText(text)
}
func (s *Scanner) SetVerbose(verbose bool) {
s.verbose = verbose
}
// Line returns the current line that the scanner is evaluating.
// It can be used to supplement error messages.
func (s *Scanner) Line() int {
return s.line
}
// Peek returns k=1 lookahead tokens.
func (s *Scanner) Peek() (Token, string, error) {
if s.lookahead > 1 {
return TokenNone, "", ProcessingError{"Invalid lookahead", s.line, 0, ProcessingInvalidLookahead}
}
if s.lookahead > 0 {
s.lookahead++
return s.nextToken, s.nextText, s.nextErr
}
s.nextToken, s.nextText, s.nextErr = s.Scan()
s.lookahead = 2
return s.nextToken, s.nextText, s.nextErr
}
func (s *Scanner) Push() error {
if s.lookahead == 2 {
s.lookahead = 1
return nil
}
return ProcessingError{"Invalid push", s.line, 0, ProcessingInvalidLookahead}
}
// Scan returns the next token, if available.
func (s *Scanner) Scan() (Token, string, error) {
if s.lookahead > 0 {
s.lookahead = 0
return s.nextToken, s.nextText, s.nextErr
}
if s.buffer.isEnd() {
return TokenEnd, "", nil
}
var text bytes.Buffer
for {
r := s.buffer.next()
switch s.state {
case scanStateInit:
if s.verbose {
fmt.Printf("scanStateInit %#U\n", r)
}
switch r {
case ' ', '\t':
// Ignore any whitespace before '!'
text.WriteRune(r)
case '\r', '\n', unicode.MaxRune:
s.nextLine(r)
return TokenText, text.String(), nil
case '/':
text.WriteRune(r)
if s.buffer.current() == '*' {
s.buffer.next() // Skip *
text.WriteRune('*')
s.prev = scanStateText // We're text now, not init
s.state = scanStateMultiComment
s.multiline = 0
} else {
s.state = scanStateText
}
case directivePrefixRune: // '!'
s.state = scanStateBang
default:
s.state = scanStateText
text.WriteRune(r)
}
case scanStateText:
if s.verbose {
fmt.Printf("scanStateText %#U\n", r)
}
switch r {
case '\r', '\n', unicode.MaxRune:
s.nextLine(r)
s.state = scanStateInit
return TokenText, text.String(), nil
case '/':
if s.buffer.current() == '*' {
text.WriteRune(r)
s.buffer.next() // Skip *
text.WriteRune('*')
s.prev = s.state
s.state = scanStateMultiComment
s.multiline = 0
} else {
text.WriteRune(r)
}
default:
s.state = scanStateText
text.WriteRune(r)
}
case scanStateBang:
if s.verbose {
fmt.Printf("scanStateBang %#U\n", r)
}
switch {
case r == ' ' || r == '\t':
// Skip any whitespace after '!'
case r == '_' || unicode.IsLetter(r):
text.Reset()
text.WriteRune(r)
s.state = scanStateDirective
default:
return TokenNone, text.String(), SyntaxError{"Invalid directive", s.line, 0, SyntaxErrorInvalidDirective}
}
case scanStateDirective:
if s.verbose {
fmt.Printf("scanStateDirective %#U\n", r)
}
switch {
case r == '_' || unicode.IsLetter(r):
text.WriteRune(r)
case r == '\r' || r == '\n' || r == unicode.MaxRune:
s.nextLine(r)
s.state = scanStateLine
return TokenDirective, text.String(), nil
case r == '#':
// Any single-line comments after a directive get eaten.
s.prev = s.state
s.state = scanStateSingleComment
case r == '/':
// Multiline comments also get eaten.
if s.buffer.current() == '*' {
s.buffer.next() // Skip *
s.prev = s.state
s.state = scanStateMultiComment
s.multiline = 0
} else {
return TokenNone, text.String(), SyntaxError{"Invalid / in directive", s.line, 0, SyntaxErrorInvalidDirective}
}
default:
s.state = scanStateParams
s.buffer.push()
return TokenDirective, text.String(), nil
}
case scanStateParams:
if s.verbose {
fmt.Printf("scanStateParams %#U\n", r)
}
switch {
case r == ' ' || r == '\t':
// Ignore interstitial whitespace
case r == '\r' || r == '\n' || r == unicode.MaxRune:
s.nextLine(r)
s.state = scanStateLine
case r == '_' || unicode.IsLetter(r):
s.state = scanStateIdentifier
text.WriteRune(r)
case r == '&':
if s.buffer.current() == '&' {
s.buffer.next() // Skip second &
return TokenAnd, "", nil
}
return TokenNone, text.String(), SyntaxError{"Invalid operator: AND is &&", s.line, 0, SyntaxErrorInvalidOperator}
case r == '|':
if s.buffer.current() == '|' {
s.buffer.next() // Skip second |
return TokenOr, "", nil
}
return TokenNone, text.String(), SyntaxError{"Invalid operator: OR is ||", s.line, 0, SyntaxErrorInvalidOperator}
case r == '!':
return TokenNot, "", nil
case r == '(':
return TokenLParen, "", nil
case r == ')':
return TokenRParen, "", nil
case r == '#':
// Any single-line comments after a directive get eaten.
text.WriteRune(r)
s.prev = s.state
s.state = scanStateSingleComment
case r == '/':
// Multiline comments also get eaten.
if s.buffer.current() == '*' {
s.buffer.next() // Skip *
s.prev = s.state
s.state = scanStateMultiComment
s.multiline = 0
} else {
return TokenNone, text.String(), SyntaxError{"Invalid / following directive", s.line, 0, SyntaxErrorInvalidDirective}
}
default:
return TokenNone, text.String(), SyntaxError{"Invalid directive parameters", s.line, 0, SyntaxErrorInvalidParameters}
}
case scanStateIdentifier:
if s.verbose {
fmt.Printf("scanStateIdentifier %#U\n", r)
}
switch {
case r == '\r' || r == '\n' || r == unicode.MaxRune:
s.nextLine(r)
s.state = scanStateLine
return TokenIdentifier, text.String(), nil
case r == '_' || unicode.IsLetter(r):
text.WriteRune(r)
case r == ' ' || r == '\t':
s.state = scanStateParams
return TokenIdentifier, text.String(), nil
default:
s.buffer.push()
s.state = scanStateParams
return TokenIdentifier, text.String(), nil
}
case scanStateLine:
if s.verbose {
fmt.Printf("scanStateLine\n")
}
// We're emitting a line, so ignore the current rune.
if r != unicode.MaxRune {
s.buffer.push()
}
s.state = scanStateInit
return TokenLine, "", nil
case scanStateSingleComment:
if s.verbose {
fmt.Printf("scanStateSingleComment %#U\n", r)
}
switch {
case r == '\r' || r == '\n' || r == unicode.MaxRune:
s.buffer.push()
s.state = s.prev
default:
text.WriteRune(r)
}
case scanStateMultiComment:
if s.verbose {
fmt.Printf("scanStateMultiComment %#U\n", r)
}
switch r {
case '*':
if s.buffer.current() == '/' {
s.buffer.next() // Skip /
if s.prev == scanStateText {
text.WriteRune(r)
text.WriteRune('/')
} else if s.multiline > 0 {
// If we were in a preprocessor state and the comment
// was on the same line, then we do nothing. If the
// comment spanned multiple lines, then we simulate
// an EOL.
s.prev = scanStateLine
}
s.multiline = 0
s.state = s.prev
} else if s.prev == scanStateText {
text.WriteRune(r)
}
case '\r', '\n', unicode.MaxRune:
s.nextLine(r)
s.multiline++
if s.prev == scanStateText {
return TokenText, text.String(), nil
}
default:
if s.prev == scanStateText {
text.WriteRune(r)
}
}
}
}
}
// chomp will eat any remaining end of line characters.
// This is mainly useful on Windows, which uses CR\LF.
// NOTE: Should not eat any following lines.
func (s *Scanner) chomp(current rune) {
if current == '\r' {
r := s.buffer.current()
if r == '\n' {
s.buffer.next()
}
}
}
// nextLine handles common end of line processing.
func (s *Scanner) nextLine(current rune) {
s.chomp(current)
s.line++
if s.verbose {
fmt.Println("LINE")
}
}