-
Notifications
You must be signed in to change notification settings - Fork 14
/
lexer.go
344 lines (315 loc) · 8.56 KB
/
lexer.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
// Copyright (c) 2014, Kevin Walsh. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This code borrows from the lexer design and implementation described
// by Rob Pike, "Lexical Scanning in Go", GTUG Sydney, Aug 30, 2011.
// See: http://cuddle.googlecode.com/hg/talk/lex.html#slide-40
//
// It also borrows from the lexer in package
// github.com/kevinawalsh/datalog/dlengine.
package auth
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"unicode"
"unicode/utf8"
)
// token is a value returned from the lexer.
type token struct {
typ itemType
val interface{} // string, int64, error, or nil
}
// itemType identifies the type of lex items.
type itemType int
const (
itemError itemType = iota // value contains error
itemUnexpectedRune // value contains the rune
itemEOF // value is nil
itemKeyword // value contains the keyword
itemIdentifier // value contains the identifer
itemStr // value contains the string
itemBytes // value contains the []byte slice
itemInt // value contains the int64
itemLP // value contains '('
itemRP // value contains ')'
itemComma // value contains ','
itemDot // value contains '.'
itemColon // value contains ':'
itemWhitespace // value contains ' ', '\t', '\n', etc.
)
var (
tokenFrom = token{itemKeyword, "from"}
tokenUntil = token{itemKeyword, "until"}
tokenSays = token{itemKeyword, "says"}
tokenSpeaksfor = token{itemKeyword, "speaksfor"}
tokenForall = token{itemKeyword, "forall"}
tokenExists = token{itemKeyword, "exists"}
tokenImplies = token{itemKeyword, "implies"}
tokenOr = token{itemKeyword, "or"}
tokenAnd = token{itemKeyword, "and"}
tokenNot = token{itemKeyword, "not"}
tokenFalse = token{itemKeyword, "false"}
tokenTrue = token{itemKeyword, "true"}
tokenExt = token{itemKeyword, "ext"}
tokenLP = token{itemLP, '('}
tokenRP = token{itemRP, ')'}
tokenComma = token{itemComma, ','}
tokenDot = token{itemDot, '.'}
tokenColon = token{itemColon, ':'}
tokenEOF = token{itemEOF, nil}
)
var reservedKeywordTokens = map[token]bool{
tokenFrom: true,
tokenUntil: true,
tokenSays: true,
tokenSpeaksfor: true,
tokenForall: true,
tokenExists: true,
tokenImplies: true,
tokenOr: true,
tokenAnd: true,
tokenNot: true,
tokenFalse: true,
tokenTrue: true,
tokenExt: true,
}
// isPrinToken checks if the input is a principal token. A principal tokens
// is a keyword not in the set of reserved keywords.
func isPrinToken(i token) bool {
_, ok := reservedKeywordTokens[i]
if !ok && i.typ == itemKeyword && lower(rune(i.val.(string)[0])) {
return true
}
return false
}
// String returns pretty-printed token, e.g. for debugging.
func (i token) String() string {
switch i.typ {
case itemError:
return fmt.Sprintf("Error{%v}", i.val)
case itemUnexpectedRune:
return fmt.Sprintf("UnexpectedRune{%v}", i.val)
case itemEOF:
return "EOF{}"
case itemKeyword:
return fmt.Sprintf("Keyword{%q}", i.val)
case itemIdentifier:
return fmt.Sprintf("Identifier{%q}", i.val)
case itemStr:
return fmt.Sprintf("Str{%q}", i.val)
case itemBytes:
return fmt.Sprintf("Bytes{%02x}", i.val)
case itemInt:
return fmt.Sprintf("Int{%v}", i.val)
case itemLP, itemRP, itemComma, itemDot, itemColon:
return fmt.Sprintf("Punct{%q}", i.val)
default:
panic("not reached")
}
}
// reader provides input to the scanner.
type reader interface {
io.RuneScanner // for ReadRune, UnreadRune
io.Reader // for Fscanf
}
// lexer holds the state of the scanner.
type lexer struct {
input reader // the input being scanned.
val bytes.Buffer // accumulated runes returned from next().
width int // width of last rune returned from next().
done *token // token found at end of input.
}
const eof rune = 0
func (l *lexer) lexMain() token {
for {
switch r := l.next(); {
case r == eof:
return tokenEOF
case unicode.IsSpace(r):
l.reset()
case r == '(':
return tokenLP
case r == ')':
return tokenRP
case r == ',':
return tokenComma
case r == '.':
return tokenDot
case r == ':':
return tokenColon
case r == '"':
l.backup()
return l.lexStr()
case r == '[' || r == '{':
l.backup()
return l.lexBytes()
case r == '-' || digit(r):
l.backup()
return l.lexInt()
case lower(r):
l.backup()
return l.lexKeyword()
case upper(r):
l.backup()
return l.lexIdentifier()
default:
l.backup()
return token{itemUnexpectedRune, r}
}
}
}
func (l *lexer) lexStr() token {
var s string
if _, err := fmt.Fscanf(l.input, "%q", &s); err != nil {
return token{itemError, err}
}
return token{itemStr, s}
}
func (l *lexer) lexBytes() token {
r := l.next()
if r == '[' {
var b []byte
s := ""
for {
r = l.next()
switch {
case hexChar(r):
s += string(r)
case unicode.IsSpace(r) || r == ']':
x, err := hex.DecodeString(s)
if err != nil {
return token{itemError, err}
}
b = append(b, x...)
if r == ']' {
return token{itemBytes, b}
}
default:
return token{itemError, fmt.Errorf("expected bytes, found %q", s)}
}
}
} else if r == '{' {
s := ""
for {
r = l.next()
switch {
case lower(r) || upper(r) || digit(r) || r == '_' || r == '-' || r == '=' || r == '\r' || r == '\n':
s += string(r)
case r == '}':
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return token{itemError, err}
}
return token{itemBytes, b}
default:
return token{itemError, fmt.Errorf("expected base64w, found %q", s)}
}
}
} else {
return token{itemError, fmt.Errorf("expected '[' or '{', found %q", r)}
}
}
func (l *lexer) lexInt() token {
var i int64
if _, err := fmt.Fscanf(l.input, "%d", &i); err != nil {
return token{itemError, err}
}
return token{itemInt, i}
}
func (l *lexer) lexKeyword() token {
for {
r := l.next()
if !lower(r) {
l.backup()
t := token{itemKeyword, l.reset()}
return t
}
}
}
func (l *lexer) lexIdentifier() token {
// precondition: l.next() is [A-Z]
for {
r := l.next()
if !(lower(r) || upper(r) || digit(r) || r == '_') {
l.backup()
return token{itemIdentifier, l.reset()}
}
}
}
func digit(r rune) bool {
return '0' <= r && r <= '9'
}
func lower(r rune) bool {
return 'a' <= r && r <= 'z'
}
func upper(r rune) bool {
return 'A' <= r && r <= 'Z'
}
func hexChar(r rune) bool {
return ('0' <= r && r <= '9') || ('a' <= r && r <= 'f') || ('A' <= r && r <= 'F')
}
// next returns the next rune in the input.
func (l *lexer) next() (r rune) {
r, n, err := l.input.ReadRune()
if err == io.EOF {
l.width = 0
return eof
}
l.val.WriteRune(r)
// BUG(kwalsh) fmt.ScanState.ReadRune() returns incorrect length. See issue
// 8512 here: https://code.google.com/p/go/issues/detail?id=8512
n = utf8.RuneLen(r)
l.width = n
return r
}
// backup steps back one rune. Can be called only once per call of next.
func (l *lexer) backup() {
if l.width > 0 {
l.input.UnreadRune()
l.val.Truncate(l.val.Len() - l.width)
l.width = 0
}
}
// reset consumes accumulated input and resets val and width.
func (l *lexer) reset() string {
s := l.val.String()
l.val.Reset()
l.width = 0
return s
}
// lex creates a new scanner for the input string.
func lex(input reader) *lexer {
return &lexer{input: input}
}
// nextToken returns the next token from the input.
func (l *lexer) nextToken() token {
if l.done != nil {
// only happens after itemEOF, itemError, or itemUnexpectedRune
return *l.done
}
token := l.lexMain()
l.reset()
if token == tokenEOF || token.typ == itemError || token.typ == itemUnexpectedRune {
l.done = &token
}
return token
}
// peek gets the next rune in the input without advancing the input.
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}