-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
353 lines (287 loc) · 6.08 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
/*
* Scanner/lexer, modelled after Go's.
* (https://github.com/golang/go/blob/master/src/go/scanner/scanner.go)
*/
package main
import (
"unicode"
"unicode/utf8"
)
const (
eof = -1
)
var identifiers = map[string]tokenKind{
"and": tokenAndAnd,
"or": tokenOrOr,
"lambda": tokenLambda,
"let": tokenLet,
"in": tokenIn,
"match": tokenMatch,
"with": tokenWith,
"rec": tokenRec,
"pi": tokenPi,
"true": tokenBool,
"false": tokenBool,
"bool": tokenTBool,
"int": tokenTInt,
"float": tokenTFloat,
// NOTE: we could have used an integer 1 and better
// categorize it during parsing, but this is just simpler.
"unit": tokenTUnit,
// Untested. We're also missing all our gates:
// H (Hadamard) N (not) Vtheta (phase shift)
// X (exchange) N_C (controlled not)
// two more Pauli besides (not)?
// "new" : tokenNew,
// "meas" : tokenMeas,
}
type token struct {
kind tokenKind
ln, cn uint // line/column numbers (1-based)
raw string
}
type scanner struct {
src []byte // input file, fully loaded
fn string // filename
ln, cn uint // line/column numbers (1-based; columns counted in rune)
ch rune // current character/rune; set to eof when done
offset int // ch's offset
nextOff int // offset + "len(ch)"
}
func (s *scanner) init(src []byte, fn string) {
s.src = src
s.fn = fn
s.ln = 1
s.cn = 0
s.ch = ' '
s.offset = 0
s.nextOff = 0
// load first rune
s.next()
}
// grab next rune
//
// XXX/TODO: the cn/ln handling is really clumsy.
func (s *scanner) next() {
// we still have something to read
if s.nextOff < len(s.src) {
s.offset = s.nextOff
// assume common case: ascii character
r, w := rune(s.src[s.offset]), 1
if r >= utf8.RuneSelf {
r, w = utf8.DecodeRune(s.src[s.offset:])
}
if r == utf8.RuneError && w == 1 {
panic("TODO")
}
s.cn++
if r == '\n' {
s.ln++
s.cn = 0
}
s.ch = r
s.nextOff += w
} else if s.ch != eof {
s.offset = len(s.src)
s.ch = eof
if s.cn == 0 {
s.cn = 1
} else {
s.cn++
}
}
}
func (s *scanner) peek() byte {
if s.nextOff < len(s.src) {
return s.src[s.nextOff]
}
return 0
}
// skip whitespaces
func (s *scanner) skipWhites() {
// NOTE: next() handles reseting ln/cn
for s.ch == ' ' || s.ch == '\t' || s.ch == '\n' || s.ch == '\r' {
s.next()
}
}
func (s *scanner) switch2(tok0 tokenKind, ch1 rune, tok1 tokenKind) tokenKind {
if s.ch == ch1 {
s.next()
return tok1
}
return tok0
}
func (s *scanner) switch3(
tok0 tokenKind, ch1 rune, tok1 tokenKind, ch2 rune, tok2 tokenKind,
) tokenKind {
if s.ch == ch1 {
s.next()
return tok1
}
if s.ch == ch2 {
s.next()
return tok2
}
return tok0
}
func (s *scanner) switch4(tok0, tok1, tok2, tok3 tokenKind) tokenKind {
b0, b1 := s.ch, s.peek()
if b0 == '=' && b1 == '.' {
s.next()
s.next()
return tok3
}
if b0 == '=' {
s.next()
return tok2
}
if b0 == '.' {
s.next()
return tok1
}
return tok0
}
// returns lower-case ch iff ch is ASCII letter
func lower(ch rune) rune { return ('a' - 'A') | ch }
func isDigit(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 (s *scanner) idOrName() tokenKind {
off := s.offset
// we know that the first s.ch is a letter ≠ λ already,
// so we can look for numbers already
for (isLetter(s.ch) || isDigit(s.ch)) && s.ch != 'λ' {
s.next()
}
if kind, ok := identifiers[string(s.src[off:s.offset])]; ok {
return kind
}
return tokenName
}
func (s *scanner) skipDigits() {
for isDigit(s.ch) {
s.next()
}
}
func (s *scanner) number() tokenKind {
var kind tokenKind
if s.ch == '.' {
s.next()
kind = tokenFloat
s.skipDigits()
return kind
}
s.skipDigits()
if s.ch == '.' {
s.next()
kind = tokenFloat
s.skipDigits()
return kind
}
kind = tokenInt
return kind
}
// grab next token
func (s *scanner) scan() token {
s.skipWhites()
var kind tokenKind
ln, cn, off := s.ln, s.cn, s.offset
switch ch := s.ch; {
case isLetter(ch) && ch != 'λ':
kind = s.idOrName()
case isDigit(ch) || (ch == '.' && isDigit(rune(s.peek()))):
kind = s.number()
case ch == eof:
kind = tokenEOF
default:
s.next()
switch ch {
case 'λ':
kind = tokenLambda
case '(':
kind = tokenLParen
case ')':
kind = tokenRParen
case '.':
// floats (e.g. ".3") managed by outer switch
kind = tokenDot
case '!':
kind = tokenExcl
case '+':
kind = s.switch2(tokenPlus, '.', tokenFPlus)
case '-':
kind = s.switch3(tokenMinus, '.', tokenFMinus, '>', tokenArrow)
case '*':
kind = s.switch2(tokenStar, '.', tokenFStar)
case '/':
kind = s.switch2(tokenSlash, '.', tokenFSlash)
// TODO: make sure all those are tested
case '<':
kind = s.switch4(
tokenLess, // <
tokenFLess, // <.
tokenLessEq, // <=
tokenFLessEq, // <=.
)
case '>':
kind = s.switch4(
tokenMore, // >
tokenFMore, // >.
tokenMoreEq, // >=
tokenFMoreEq, // >=.
)
case ',':
kind = tokenComa
case '=':
kind = tokenEqual
case '〈':
kind = tokenLBracket
case '〉':
kind = tokenRBracket
case '|':
kind = s.switch2(tokenOr, '|', tokenOrOr)
case '&':
kind = s.switch2(tokenAnd, '&', tokenAndAnd)
case '≤':
kind = s.switch2(tokenLessEq, '.', tokenFLessEq)
case '≥':
kind = s.switch2(tokenMoreEq, '.', tokenFMoreEq)
case ':':
kind = tokenColon
case 'π':
kind = tokenPi
case '→':
kind = tokenArrow
case '×':
kind = tokenProduct
case eof:
kind = tokenEOF
// case '⊸': tokenRMultiMap
// case '⊗': tokenOMult
// case '⊕': tokenOPlus
// case '⊤': tokenTrue
default:
panic("assert TODO")
}
}
return token{kind, ln, cn, string(s.src[off:s.offset])}
}
// grab next token
func (s *scanner) scanAll() ([]token, error) {
var toks []token
for {
tok := s.scan()
toks = append(toks, tok)
if tok.kind == tokenEOF {
return toks, nil
}
}
}
// slurp all tokens
func scanAll(src string, fn string) ([]token, error) {
var s scanner
s.init([]byte(src), fn)
return s.scanAll()
}