This repository was archived by the owner on Jan 26, 2024. It is now read-only.
forked from rustyoz/genericlexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenericlexer.go
250 lines (220 loc) · 4.26 KB
/
genericlexer.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
package genericLexer
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type Item struct {
Type ItemType
Value string
pos int
lname *string
}
func (i Item) String() string {
s := fmt.Sprint(*i.lname, " ", i.pos, " ")
switch i.Type {
case ItemError:
return s + "Error"
case ItemComma:
return s + "Comma"
case ItemParan:
return s + "Parentheses" + i.Value
case ItemLetter:
return s + fmt.Sprintf("Letter \"%s\"", i.Value)
case ItemWord:
return s + fmt.Sprintf("Word \"%s\"", i.Value)
case ItemNumber:
return s + fmt.Sprint("Number ", i.Value)
case ItemWSP:
return s + "WSP"
default:
return fmt.Sprintf("%s \"%s\"", s, i.Value)
}
}
type Lexer struct {
name string
input string
start int
pos int
width int
Items chan Item
buffer [3]Item
peekcount int
}
type ItemType int
const (
ItemError ItemType = iota
ItemDot
ItemEOS
ItemLetter
ItemWord
ItemNumber
ItemComma
ItemFlag
ItemWSP
ItemParan
)
type stateFn func(*Lexer) stateFn
func Lex(name, input string) (*Lexer, chan Item) {
l := &Lexer{
name: name,
input: input,
Items: make(chan Item),
}
go l.run() // Concurrently run state machine.
return l, l.Items
}
const eof = -1
func (l *Lexer) run() {
for state := lexD; state != nil; {
state = state(l)
}
l.Items <- Item{Type: ItemEOS}
close(l.Items) // No more tokens will be delivered.
}
func (l *Lexer) next() (r rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return r
}
func (l *Lexer) peek() rune {
rune := l.next()
l.backup()
return rune
}
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) backup() {
l.pos -= l.width
}
func (l *Lexer) NextItem() Item {
if l.peekcount > 0 {
l.peekcount--
// fmt.Println("NextItem got peeked Item", l.buffer[0].String())
} else {
l.buffer[0] = <-l.Items
// fmt.Println("NextItem got new Item", l.buffer[0].String())
}
return l.buffer[0]
}
func (l *Lexer) PeekItem() Item {
if l.peekcount > 0 {
// fmt.Println("peekItem got already peeked Item", l.buffer[0].String())
return l.buffer[0]
}
// fmt.Println("peekItem needs new Item")
l.buffer[0] = l.NextItem()
l.peekcount = 1
// fmt.Println("peekItem got new Item", l.buffer[0].String())
return l.buffer[0]
}
func lexNumber(l *Lexer) stateFn {
// Optional leading sign.
l.accept("+-")
// Is it hex?
digits := "0123456789"
if l.accept("0") && l.accept("xX") {
digits = "0123456789abcdefABCDEF"
}
l.acceptRun(digits)
if l.accept(".") {
l.acceptRun(digits)
}
if l.accept("e") {
l.accept("+-")
l.acceptRun(digits)
}
l.emit(ItemNumber)
return lexD
}
func (l *Lexer) ignore() {
l.start = l.pos
}
func (l *Lexer) emit(t ItemType) {
i := Item{t, l.input[l.start:l.pos], l.start, &l.name}
l.Items <- i
l.start = l.pos
}
func lexWord(l *Lexer) stateFn {
l.acceptRun("abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ")
l.emit(ItemWord)
return lexD
}
func lexLetter(l *Lexer) stateFn {
l.accept("abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ")
if unicode.IsLetter(l.peek()) {
return lexWord
}
l.emit(ItemLetter)
return lexD
}
func lexComma(l *Lexer) stateFn {
l.accept(",")
l.emit(ItemComma)
return lexD
}
func isWSP(r rune) bool {
return r == ' ' || r == '\t' || r == '\n'
}
func lexWSP(l *Lexer) stateFn {
l.accept(" \t\r\n\f")
l.emit(ItemWSP)
return lexD
}
func lexD(l *Lexer) stateFn {
for {
r := l.next()
switch {
case r == eof:
l.emit(ItemEOS)
return nil
case isWSP(r):
return lexWSP
case unicode.IsLetter(r):
return lexLetter
case r == '-' || r == '+':
return lexNumber
case unicode.IsNumber(r):
return lexNumber
case r == ',':
return lexComma
case r == '(' || r == ')':
return lexParan
default:
return nil
}
}
return nil
}
func lexParan(l *Lexer) stateFn {
l.accept("()")
l.emit(ItemParan)
return lexD
}
func (l *Lexer) ConsumeWhiteSpace() error {
for l.PeekItem().Type == ItemWSP {
l.NextItem()
}
return nil
}
func (l *Lexer) ConsumeComma() error {
for l.PeekItem().Type == ItemComma {
l.NextItem()
}
return nil
}