-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner.go
More file actions
272 lines (251 loc) · 5.43 KB
/
scanner.go
File metadata and controls
272 lines (251 loc) · 5.43 KB
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
package polai
import (
"bufio"
"bytes"
"io"
)
// Scanner represents a lexical scanner.
type Scanner struct {
r *bufio.Reader
}
// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: bufio.NewReader(r)}
}
// Scan returns the next token and literal value.
func (s *Scanner) Scan() (tok Token, lit string) {
// Read the next rune.
ch := s.read()
lit = string(ch)
// If we see whitespace then consume all contiguous whitespace.
// If we see a letter then consume as an ident or reserved word.
// If we see a digit then consume as a number.
if isWhitespace(ch) {
s.unread()
return s.scanWhitespace()
} else if isLetter(ch) {
s.unread()
return s.scanIdent()
} else if isDigit(ch) {
ch = s.read()
for isDigit(ch) {
lit += string(ch)
ch = s.read()
}
s.unread()
return LONG, lit
}
// Otherwise read the individual character.
switch ch {
case eof:
return EOF, ""
case ',':
return COMMA, lit
case '(':
return LEFT_PAREN, lit
case ')':
return RIGHT_PAREN, lit
case '[':
return LEFT_SQB, lit
case ']':
return RIGHT_SQB, lit
case '{':
return LEFT_BRACE, lit
case '}':
return RIGHT_BRACE, lit
case ';':
return SEMICOLON, lit
case '+':
return PLUS, lit
case '*':
return MULTIPLIER, lit
case '.':
return PERIOD, lit
case '<':
ch = s.read()
if ch == '=' {
lit += string(ch)
return LTE, lit
}
s.unread()
return LT, lit
case '>':
ch = s.read()
if ch == '=' {
lit += string(ch)
return GTE, lit
}
s.unread()
return GT, lit
case '-':
ch = s.read()
if !isDigit(ch) {
s.unread()
return DASH, lit
}
lit += string(ch)
ch = s.read()
for isDigit(ch) {
lit += string(ch)
ch = s.read()
}
s.unread()
return LONG, lit
case '"':
for {
ch = s.read()
lit += string(ch)
if ch == '"' {
break
}
if ch == '\\' {
ch = s.read()
lit += string(ch)
}
if ch == rune(0) {
return ILLEGAL, lit
}
}
return DBLQUOTESTR, lit
case '=':
ch = s.read()
lit += string(ch)
if ch == '=' {
return EQUALITY, lit
}
case '!':
ch = s.read()
if ch == '=' {
lit += string(ch)
return INEQUALITY, lit
}
s.unread()
return EXCLAMATION, lit
case ':':
ch = s.read()
if ch == ':' {
lit += string(ch)
return NAMESPACE, lit
}
s.unread()
return COLON, lit
case '&':
ch = s.read()
lit += string(ch)
if ch == '&' {
return AND, lit
}
case '|':
ch = s.read()
lit += string(ch)
if ch == '|' {
return OR, lit
}
case '/':
ch = s.read()
lit += string(ch)
if ch == '/' {
ch = s.read()
for ch != '\n' && ch != eof {
lit += string(ch)
ch = s.read()
}
s.unread()
return COMMENT, lit
}
}
return ILLEGAL, lit
}
// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *Scanner) scanWhitespace() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent whitespace character into the buffer.
// Non-whitespace characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isWhitespace(ch) {
s.unread()
break
} else {
buf.WriteRune(ch)
}
}
return WHITESPC, buf.String()
}
// scanIdent consumes the current rune and all contiguous ident runes.
func (s *Scanner) scanIdent() (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isLetter(ch) && !isDigit(ch) && ch != '_' {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
// If the string matches a keyword then return that keyword.
switch buf.String() {
case "permit":
return PERMIT, buf.String()
case "forbid":
return FORBID, buf.String()
case "principal":
return PRINCIPAL, buf.String()
case "action":
return ACTION, buf.String()
case "resource":
return RESOURCE, buf.String()
case "context":
return CONTEXT, buf.String()
case "in":
return IN, buf.String()
case "when":
return WHEN, buf.String()
case "unless":
return UNLESS, buf.String()
case "has":
return HAS, buf.String()
case "like":
return LIKE, buf.String()
case "if":
return IF, buf.String()
case "then":
return THEN, buf.String()
case "else":
return ELSE, buf.String()
case "true":
return TRUE, buf.String()
case "false":
return FALSE, buf.String()
}
// Otherwise return as a regular identifier.
return IDENT, buf.String()
}
// 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
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *Scanner) unread() { _ = s.r.UnreadRune() }
// isWhitespace returns true if the rune is a space, tab, or newline.
func isWhitespace(ch rune) bool { return ch == ' ' || ch == '\t' || ch == '\n' }
// isLetter returns true if the rune is a letter.
func isLetter(ch rune) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') }
// isDigit returns true if the rune is a digit.
func isDigit(ch rune) bool { return (ch >= '0' && ch <= '9') }
// eof represents a marker rune for the end of the reader.
var eof = rune(0)