-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
277 lines (247 loc) · 5.8 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
package gocalc
type lexer interface {
token() *token
peekToken() *token
}
type state int
const (
stErr state = iota
stStart
stWhitespace
stId
stInt
stZero
stZeroB
stBinInt
stZeroX
stHexInt
stOctInt
stFloat
stLparen
stRparen
stComma
stLogicalNot
stNotEqual
stBitwiseNot
stStar
stPercent
stSlash
stPlus
stMinus
stLeftShift
stRightShift
stLessThan
stLessOrEqual
stGreaterThan
stGreaterOrEqual
stEqual
stBitwiseAnd
stLogicalAnd
stBitwiseXor
stBitwiseOr
stLogicalOr
stT
stTr
stTru
stTrue
stF
stFa
stFal
stFals
stFalse
)
const whitespace = "\t\n\r "
const letters = "abcdefABCDEFghijklmnopqrstuvwxyzGHIJKLMNOPQRSTUVWXYZ"
const digits = "0123456789"
const hexDigits = "0123456789abcdefABCDEF"
const binaryDigits = "01"
const octalDigits = "01234567"
const oneToNine = "123456789"
const stateCount uint8 = 44
const maxTransitionCount uint16 = 256
var trans = [stateCount][maxTransitionCount]state{}
var stateTokens = [...]tokenType{
tokenError, // stErr
tokenError, // stStart
tokenWhitespace, // stWhitespace
tokenIdentifier, // stId
tokenInt, // stInt
tokenInt, // stZero
tokenError, // stZeroB
tokenInt, // stBinInt
tokenError, // stZeroX
tokenInt, // stHexInt
tokenInt, // stOctInt
tokenFloat, // stFloat
tokenLeftParen, // stLparen
tokenRightParen, // stRparen
tokenComma, // stComma
tokenLogicalNot, // stLogicalNot
tokenNotEqual, // stNotEqual
tokenBitwiseNot, // stBitwiseNot
tokenStar, // stStar
tokenPercent, // stPercent
tokenSlash, // stSlash
tokenPlus, // stPlus
tokenMinus, // stMinus
tokenLeftShift, // stLeftShift
tokenRightShift, // stRightShift
tokenLessThan, // stLessThan
tokenLessOrEqual, // stLessOrEqual
tokenGreaterThan, // stGreaterThan
tokenGreaterOrEqual, // stGreaterOrEqual
tokenEqual, // stEqual
tokenBitwiseAnd, // stBitwiseAnd
tokenLogicalAnd, // stLogicalAnd
tokenBitwiseXor, // stBitwiseXor
tokenBitwiseOr, // stBitwiseOr
tokenLogicalOr, // stLogicalOr
tokenIdentifier, // stT
tokenIdentifier, // stTr
tokenIdentifier, // stTru
tokenTrue, // stTrue
tokenIdentifier, // stF
tokenIdentifier, // stFa
tokenIdentifier, // stFal
tokenIdentifier, // stFals
tokenFalse, // stFalse
}
func setTrans(current state, transition string, next state) {
for _, r := range transition {
trans[current][r] = next
}
}
func init() {
setTrans(stStart, whitespace, stWhitespace)
setTrans(stWhitespace, whitespace, stWhitespace)
// Single character tokens
setTrans(stStart, "(", stLparen)
setTrans(stStart, ")", stRparen)
setTrans(stStart, ",", stComma)
setTrans(stStart, "~", stBitwiseNot)
setTrans(stStart, "*", stStar)
setTrans(stStart, "%", stPercent)
setTrans(stStart, "/", stSlash)
setTrans(stStart, "+", stPlus)
setTrans(stStart, "-", stMinus)
setTrans(stStart, "=", stEqual)
setTrans(stStart, letters, stId)
setTrans(stId, letters+digits, stId)
// Int
setTrans(stStart, oneToNine, stInt)
setTrans(stInt, digits, stInt)
// Zero
setTrans(stStart, "0", stZero)
setTrans(stZero, ".", stFloat)
// Binary
setTrans(stZero, "b", stZeroB)
setTrans(stZeroB, binaryDigits, stBinInt)
setTrans(stBinInt, binaryDigits, stBinInt)
// Hex
setTrans(stZero, "xX", stZeroX)
setTrans(stZeroX, hexDigits, stHexInt)
setTrans(stHexInt, hexDigits, stHexInt)
// Octal
setTrans(stZero, octalDigits, stOctInt)
setTrans(stOctInt, octalDigits, stOctInt)
// Float
setTrans(stInt, ".", stFloat)
setTrans(stFloat, digits, stFloat)
// Comparisons
setTrans(stStart, "!", stLogicalNot)
setTrans(stLogicalNot, "=", stNotEqual)
setTrans(stStart, "<", stLessThan)
setTrans(stLessThan, "=", stLessOrEqual)
setTrans(stStart, ">", stGreaterThan)
setTrans(stGreaterThan, "=", stGreaterOrEqual)
setTrans(stStart, "&", stBitwiseAnd)
setTrans(stBitwiseAnd, "&", stLogicalAnd)
setTrans(stStart, "^", stBitwiseXor)
setTrans(stStart, "|", stBitwiseOr)
setTrans(stBitwiseOr, "|", stLogicalOr)
// Shift
setTrans(stLessThan, "<", stLeftShift)
setTrans(stGreaterThan, ">", stRightShift)
// Boolean literals
setTrans(stStart, "t", stT)
setTrans(stT, "r", stTr)
setTrans(stTr, "u", stTru)
setTrans(stTru, "e", stTrue)
setTrans(stTrue, letters+digits, stId)
setTrans(stStart, "f", stF)
setTrans(stF, "a", stFa)
setTrans(stFa, "l", stFal)
setTrans(stFal, "s", stFals)
setTrans(stFals, "e", stFalse)
setTrans(stFalse, letters+digits, stId)
}
func newLexer(input string) lexer {
return &gocalcLexer{
input: input,
tokens: queue{},
state: stStart,
}
}
func (l *gocalcLexer) push() {
if len(l.input) == 0 {
l.emit(tokenEOF)
return
}
for len(l.tokens) < 1 {
nextState := stErr
if l.pos < len(l.input) {
nextState = trans[l.state][l.input[l.pos]]
}
if nextState == stErr {
curTokenType := stateTokens[l.state]
if curTokenType == tokenError {
for l.pos < len(l.input) && trans[l.state][l.input[l.pos]] == stErr {
l.pos++
}
l.emit(tokenError)
return
}
if curTokenType != tokenWhitespace {
l.emit(curTokenType)
}
l.start = l.pos
l.state = stStart
if l.start >= len(l.input) {
l.emit(tokenEOF)
}
} else {
l.state = nextState
l.pos++
}
}
}
func (l *gocalcLexer) token() *token {
if len(l.tokens) < 1 {
l.push()
}
return l.tokens.pop()
}
func (l *gocalcLexer) peekToken() *token {
if len(l.tokens) < 1 {
l.push()
}
return l.tokens.first()
}
const eof = -1
type gocalcLexer struct {
input string
start int
pos int
width int
tokens queue
state state
}
func (l *gocalcLexer) emit(t tokenType) {
l.tokens.push(&token{
typ: t,
val: l.input[l.start:l.pos],
pos: l.start,
end: l.pos,
})
l.start = l.pos
}