Repository navigation
Expand file tree
/
Copy pathlexer.go
More file actions
382 lines (316 loc) · 6.31 KB
/
Copy pathlexer.go
File metadata and controls
382 lines (316 loc) · 6.31 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
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
374
375
376
377
378
379
380
381
package main
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type itemType int
const eof = -1
const identifierRunes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_?"
const (
itemError itemType = iota
itemEOF
itemIdentifier
itemIdentifierName
itemIdentifierReference
itemIdentifierReferenceAt
itemIdentifierCall
itemOpenBlock
itemEndBlock
itemNumber
itemString
itemPlus
itemMinus
itemTimes
itemDividedBy
itemExecute
itemAt
itemNot
itemCondition
itemEmptyList
itemCons
)
type item struct {
typ itemType
val string
}
func (item *item) print() {
itemTypeString := "unknown"
printValue := true
switch item.typ {
case itemError:
itemTypeString = "itemError"
case itemEOF:
itemTypeString = "EOF"
printValue = false
case itemIdentifier:
itemTypeString = "itemIdentifier"
case itemIdentifierReference:
itemTypeString = "itemIdentifierReference"
case itemIdentifierReferenceAt:
itemTypeString = "itemIdentifierReferenceAt"
case itemIdentifierCall:
itemTypeString = "itemIdentifierCall"
case itemOpenBlock:
itemTypeString = "itemOpenBlock"
case itemEndBlock:
itemTypeString = "itemEndBlock"
case itemNumber:
itemTypeString = "itemNumber"
case itemString:
itemTypeString = "itemString"
case itemPlus:
itemTypeString = "itemPlus"
case itemMinus:
itemTypeString = "itemMinus"
case itemTimes:
itemTypeString = "itemTimes"
case itemDividedBy:
itemTypeString = "itemDividedBy"
case itemEmptyList:
itemTypeString = "itemEmptyList"
case itemCons:
itemTypeString = "itemCons"
}
if printValue {
fmt.Printf("%s: %s\n", itemTypeString, item.val)
} else {
fmt.Printf("%s\n", itemTypeString)
}
}
type lexer struct {
name string
input string
start int
pos int
width int
items chan item
}
type stateFn func(*lexer) stateFn
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.input[l.start:l.pos]}
l.start = l.pos
}
func (l *lexer) next() (rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, width := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = width
l.pos += l.width
return r
}
func (l *lexer) ignore() {
l.start = l.pos
}
func (l *lexer) backup() {
l.pos -= l.width
}
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *lexer) accept(valid string) bool {
if strings.IndexRune(valid, l.next()) >= 0 {
return true
}
l.backup()
return false
}
func (l *lexer) acceptRun(valid string) {
for strings.IndexRune(valid, l.next()) >= 0 {
}
l.backup()
}
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- item {
itemError,
fmt.Sprintf(format, args...),
}
return nil
}
func (l *lexer) run() {
for state := lexCode; state != nil; {
state = state(l)
}
close(l.items)
}
func lexIdentifier(l *lexer) stateFn {
identifierType := itemIdentifier
switch prefix := l.next(); {
case prefix == '\'':
identifierType = itemIdentifierReference
case prefix == '!':
identifierType = itemIdentifierCall
case prefix == '@':
identifierType = itemIdentifierReferenceAt
case prefix == '.':
identifierType = itemIdentifierName
}
if identifierType == itemIdentifier {
l.backup()
} else {
l.ignore() // We don't want the ' or the ! in the identifier name
}
l.acceptRun(identifierRunes)
if l.start == l.pos {
return l.errorf("Empty identifier at position %d", l.start)
}
l.emit(identifierType)
return lexCode
}
func lexNumber(l *lexer) stateFn {
l.accept("-")
digits := "0123456789"
if l.accept("0") && l.accept("xX") {
digits = "0123456789ABCDEFabcdef"
}
l.acceptRun(digits)
if l.accept(".") {
l.acceptRun(digits)
}
l.emit(itemNumber)
return lexCode
}
func lexQuotedString(l *lexer) stateFn {
escape_sequence := false
for c := l.next(); c != '"' || escape_sequence; c = l.next() {
if c == eof {
return l.errorf("Unexpected end of file.")
}
if c == '\\' {
escape_sequence = true
} else {
escape_sequence = false
}
}
l.backup()
l.emit(itemString)
l.next()
l.ignore() // We don't want the end quote
return lexCode
}
func lexComment(l *lexer) stateFn {
for true {
c := l.next()
if c == eof {
return l.errorf("Unexpected end of file.")
}
if c == '*' {
switch l.next() {
case eof:
return l.errorf("Unexpected end of file.")
case ')':
return lexCode
default:
// Continue in the comment
}
}
}
return lexCode
}
func lexCode(l *lexer) stateFn {
switch r := l.next(); {
case r == eof:
l.emit(itemEOF)
return nil
case unicode.IsSpace(r):
l.ignore()
return lexCode
case r == '+':
l.emit(itemPlus)
return lexCode
case r == '*':
l.emit(itemTimes)
return lexCode
case r == '/':
l.emit(itemDividedBy)
return lexCode
case r == '-':
if n := l.peek(); '0' <= n && n <= '9' {
l.backup()
return lexNumber
}
l.emit(itemMinus)
return lexCode
case r == '{':
l.emit(itemOpenBlock)
return lexCode
case r == '}':
l.emit(itemEndBlock)
return lexCode
case r == '@':
l.emit(itemAt)
return lexCode
case r == '~':
l.emit(itemNot)
return lexCode
case r == '=':
l.emit(itemCondition)
return lexCode
case r == ':':
if l.next() != ':' {
return l.errorf("Unexpected ':' at position %d", l.start)
}
l.emit(itemCons)
return lexCode
case r == '<':
if l.next() == '>' {
l.emit(itemEmptyList)
return lexCode
}
l.backup()
if l.next() != '=' {
l.backup()
}
l.emit(itemCondition)
return lexCode
case r == '>':
if l.next() != '=' {
l.backup()
}
l.emit(itemCondition)
return lexCode
case r == '"':
l.ignore() // We don't want the beginning quote
return lexQuotedString
case '0' <= r && r <= '9':
l.backup()
return lexNumber
case strings.IndexRune(identifierRunes, r) >= 0:
l.backup()
return lexIdentifier
case r == '.':
l.backup()
return lexIdentifier
case r == '\'':
l.backup()
return lexIdentifier
case r == '!':
if i := l.peek(); strings.IndexRune(identifierRunes, i) >= 0 {
l.backup()
return lexIdentifier
}
l.emit(itemExecute)
return lexCode
case r == '(':
if l.next() != '*' {
return l.errorf("Unexpected '(' at position %d", l.start)
}
return lexComment
default:
return l.errorf("Unexpected %q at position %d", r, l.start)
}
}
func lex(name, input string) (*lexer, chan item) {
l := &lexer {
name: name,
input: input,
items: make(chan item),
}
go l.run()
return l, l.items
}