/
scanner.go
350 lines (317 loc) · 6.49 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
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package ast
import (
"bytes"
"encoding/hex"
"fmt"
"os"
"strconv"
)
type token int
const (
tokIllegal token = iota
tokComment
tokIdent
tokInclude
tokIncdir
tokDefine
tokResource
tokString
tokStringHex
tokCExpr
tokInt
tokNewLine
tokLParen
tokRParen
tokLBrack
tokRBrack
tokLBrace
tokRBrace
tokEq
tokComma
tokColon
tokBinAnd
tokCmpEq
tokCmpNeq
tokEOF
)
var punctuation = [256]token{
'\n': tokNewLine,
'(': tokLParen,
')': tokRParen,
'[': tokLBrack,
']': tokRBrack,
'{': tokLBrace,
'}': tokRBrace,
'=': tokEq,
',': tokComma,
':': tokColon,
'&': tokBinAnd,
}
var tok2str = [...]string{
tokIllegal: "ILLEGAL",
tokComment: "comment",
tokIdent: "identifier",
tokInclude: "include",
tokIncdir: "incdir",
tokDefine: "define",
tokResource: "resource",
tokString: "string",
tokStringHex: "hex string",
tokCExpr: "CEXPR",
tokInt: "int",
tokNewLine: "NEWLINE",
tokEOF: "EOF",
tokCmpEq: "==",
tokCmpNeq: "!=",
}
func init() {
for ch, tok := range punctuation {
if tok == tokIllegal {
continue
}
tok2str[tok] = fmt.Sprintf("%q", ch)
}
}
var keywords = map[string]token{
"include": tokInclude,
"incdir": tokIncdir,
"define": tokDefine,
"resource": tokResource,
}
func (tok token) String() string {
return tok2str[tok]
}
type scanner struct {
data []byte
filename string
errorHandler ErrorHandler
ch byte
off int
line int
col int
prev1 token
prev2 token
errors int
}
func newScanner(data []byte, filename string, errorHandler ErrorHandler) *scanner {
if errorHandler == nil {
errorHandler = LoggingHandler
}
s := &scanner{
data: data,
filename: filename,
errorHandler: errorHandler,
off: -1,
}
s.next()
return s
}
type ErrorHandler func(pos Pos, msg string)
func LoggingHandler(pos Pos, msg string) {
fmt.Fprintf(os.Stderr, "%v: %v\n", pos, msg)
}
const BuiltinFile = "BUILTINS"
func (pos Pos) Builtin() bool {
return pos.File == BuiltinFile
}
func (pos Pos) String() string {
if pos.Builtin() {
return "builtins"
}
if pos.Col == 0 {
return fmt.Sprintf("%v:%v", pos.File, pos.Line)
}
return fmt.Sprintf("%v:%v:%v", pos.File, pos.Line, pos.Col)
}
func (pos Pos) less(other Pos) bool {
if pos.File != other.File {
return pos.File < other.File
}
if pos.Line != other.Line {
return pos.Line < other.Line
}
return pos.Col < other.Col
}
func (s *scanner) Scan() (tok token, lit string, pos Pos) {
s.skipWhitespace()
pos = s.pos()
switch {
case s.ch == 0:
tok = tokEOF
s.next()
case s.prev2 == tokDefine && s.prev1 == tokIdent:
tok = tokCExpr
for ; s.ch != '\n'; s.next() {
}
lit = string(s.data[pos.Off:s.off])
case s.ch == '#':
tok = tokComment
for s.next(); s.ch != '\n'; s.next() {
}
lit = string(s.data[pos.Off+1 : s.off])
case s.ch == '"' || s.ch == '<':
tok = tokString
lit = s.scanStr(pos)
case s.ch == '`':
tok = tokStringHex
lit = s.scanStr(pos)
case s.ch >= '0' && s.ch <= '9' || s.ch == '-':
tok = tokInt
lit = s.scanInt(pos)
case s.ch == '\'':
tok = tokInt
lit = s.scanChar(pos)
case s.ch == '_' || s.ch >= 'a' && s.ch <= 'z' || s.ch >= 'A' && s.ch <= 'Z':
tok, lit = s.scanIdent(pos)
case s.tryConsume("=="):
tok = tokCmpEq
case s.tryConsume("!="):
tok = tokCmpNeq
default:
tok = punctuation[s.ch]
if tok == tokIllegal {
s.Error(pos, "illegal character %#U", s.ch)
}
s.next()
}
s.prev2 = s.prev1
s.prev1 = tok
return
}
func (s *scanner) scanStr(pos Pos) string {
// TODO(dvyukov): get rid of <...> strings, that's only includes
closing := s.ch
if s.ch == '<' {
closing = '>'
}
for s.next(); s.ch != closing; s.next() {
if s.ch == 0 || s.ch == '\n' {
s.Error(pos, "string literal is not terminated")
return ""
}
}
lit := string(s.data[pos.Off+1 : s.off])
for i := 0; i < len(lit); i++ {
if lit[i] < 0x20 || lit[i] >= 0x80 {
pos1 := pos
pos1.Col += i + 1
pos1.Off += i + 1
s.Error(pos1, "illegal character %#U in string literal", lit[i])
break
}
}
s.next()
if closing != '`' {
return lit
}
decoded, err := hex.DecodeString(lit)
if err != nil {
s.Error(pos, "bad hex string literal: %v", err)
}
return string(decoded)
}
func (s *scanner) scanInt(pos Pos) string {
for s.ch >= '0' && s.ch <= '9' ||
s.ch >= 'a' && s.ch <= 'f' ||
s.ch >= 'A' && s.ch <= 'F' ||
s.ch == 'x' || s.ch == '-' {
s.next()
}
lit := string(s.data[pos.Off:s.off])
if _, err := strconv.ParseUint(lit, 10, 64); err == nil {
return lit
}
if len(lit) > 1 && lit[0] == '-' {
if _, err := strconv.ParseInt(lit, 10, 64); err == nil {
return lit
}
}
if len(lit) > 2 && lit[0] == '0' && lit[1] == 'x' {
if _, err := strconv.ParseUint(lit[2:], 16, 64); err == nil {
return lit
}
}
s.Error(pos, fmt.Sprintf("bad integer %q", lit))
return "0"
}
func (s *scanner) scanChar(pos Pos) string {
s.next()
s.next()
if s.ch != '\'' {
s.Error(pos, "char literal is not terminated")
return "0"
}
s.next()
return string(s.data[pos.Off : pos.Off+3])
}
func (s *scanner) scanIdent(pos Pos) (tok token, lit string) {
tok = tokIdent
for s.ch == '_' || s.ch == '$' ||
s.ch >= 'a' && s.ch <= 'z' ||
s.ch >= 'A' && s.ch <= 'Z' ||
s.ch >= '0' && s.ch <= '9' {
s.next()
}
lit = string(s.data[pos.Off:s.off])
if key, ok := keywords[lit]; ok {
tok = key
}
return
}
func (s *scanner) Error(pos Pos, msg string, args ...interface{}) {
s.errors++
s.errorHandler(pos, fmt.Sprintf(msg, args...))
}
func (s *scanner) Ok() bool {
return s.errors == 0
}
func (s *scanner) next() {
s.off++
for s.off < len(s.data) && s.data[s.off] == '\r' {
s.off++
}
if s.off == len(s.data) {
// Always emit NEWLINE before EOF.
// Makes lots of things simpler as we always
// want to treat EOF as NEWLINE as well.
s.ch = '\n'
return
}
if s.off > len(s.data) {
s.ch = 0
return
}
if s.off == 0 || s.data[s.off-1] == '\n' {
s.line++
s.col = 0
}
s.ch = s.data[s.off]
s.col++
if s.ch == 0 {
s.Error(s.pos(), "illegal character \\x00")
}
}
func (s *scanner) tryConsume(str string) bool {
if !bytes.HasPrefix(s.data[s.off:], []byte(str)) {
return false
}
for i := 0; i < len(str); i++ {
s.next()
}
return true
}
func (s *scanner) skipWhitespace() {
for s.ch == ' ' || s.ch == '\t' {
s.next()
}
}
func (s *scanner) pos() Pos {
return Pos{
File: s.filename,
Off: s.off,
Line: s.line,
Col: s.col,
}
}