-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer.go
356 lines (319 loc) · 7.84 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
345
346
347
348
349
350
351
352
353
354
355
356
package lexer
import (
"github.com/gabivlj/candice/internals/token"
)
type Lexer struct {
input string
position int // current position
readPosition int // next position after current char
ch byte // current char
line uint32
column uint32
}
// New Returns a new Lexer
func New(input string) *Lexer {
l := &Lexer{input: input, line: 1, column: 0}
// Initialize to first char.
l.readChar()
return l
}
func (l *Lexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) || isDigit(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}
func isLetter(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}
func (l *Lexer) skipWhiteSpace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\r' || l.ch == '\n' {
if l.ch == '\n' {
l.line++
l.column = 1
}
l.readChar()
}
}
func (l *Lexer) skipUntilJL() {
for l.ch != '\n' && l.ch != 0 {
l.readChar()
}
l.readChar()
l.line++
l.column = 1
}
func (l *Lexer) RetrieveLine(t token.Token) string {
currentColumn := t.OverallPosition - 1
if t.Type == token.EOF {
t.OverallPosition = len(l.input) - 1
currentColumn = len(l.input) - 2
}
for currentColumn >= 0 && l.input[currentColumn] != '\n' && l.input[currentColumn] != '\t' {
currentColumn--
}
return l.input[currentColumn+1 : t.OverallPosition+1]
}
// Returns if there is a combination with the next char, else returns otherwise param
func (l *Lexer) peekerForTwoChars(expect byte, otherwise token.Token, t token.TypeToken) token.Token {
// Peek next character
peek := l.peekChar()
// Checks if the peeked character is the expected one
if peek == expect {
return l.readTwoBytesToken(t, peek)
}
// Otherwise return the token that it falls to
return otherwise
}
func (l *Lexer) readTwoBytesToken(t token.TypeToken, peek byte) token.Token {
// Store the current character
ch := l.ch
// Next character
l.readChar()
// Return the token for that combination
return token.Token{Type: t, Literal: string(ch) + string(peek), Line: l.line, Position: l.column - 2, OverallPosition: l.position}
}
func (l *Lexer) readCharLiteral() token.Token {
l.readChar()
var literal byte
if l.peekChar() == '\\' {
// it's a \n or \t
l.readChar()
c := l.ch
l.readChar()
if c == 'n' {
c = '\n'
} else if c == 't' {
c = '\t'
} else if c == 'r' {
c = '\r'
}
literal = c
} else {
literal = l.ch
l.readChar()
}
if l.ch != '\'' {
return l.newToken(token.ILLEGAL, l.ch)
}
// Skip '
l.readChar()
return l.newToken(token.CHAR, literal)
}
func (l *Lexer) getMacroToken() token.Token {
macro := l.ch
l.readChar()
identifier := l.readIdentifier()
switch identifier {
case "if":
return l.newToken(token.MACRO_IF, macro)
}
return l.newToken(token.ILLEGAL, macro)
}
// NextToken Returns the next token of an input
func (l *Lexer) NextToken() token.Token {
var tok token.Token
l.skipWhiteSpace()
for l.ch == '/' && l.peekChar() == '/' {
l.skipUntilJL()
l.skipWhiteSpace()
}
l.skipWhiteSpace()
switch l.ch {
case '\'':
tok = l.readCharLiteral()
return tok
case '#':
tok = l.getMacroToken()
return tok
case '@':
tok = l.newToken(token.AT, l.ch)
case '.':
tok = l.peekerForTwoChars('.', l.newToken(token.DOT, l.ch), token.DOUBLE_DOT)
case ':':
tok = l.newToken(token.COLON, l.ch)
case '[':
tok = l.newToken(token.LBRACKET, l.ch)
case ']':
tok = l.newToken(token.RBRACKET, l.ch)
case '"':
tok.Type = token.STRING
tok.Literal = l.readString()
tok.Line = l.line
tok.Position = l.column - uint32(len(tok.Literal))
tok.OverallPosition = l.position
case '=':
tok = l.peekerForTwoChars('=', l.newToken(token.ASSIGN, '='), token.EQ)
case '!':
tok = l.peekerForTwoChars('=', l.newToken(token.BANG, '!'), token.NOTEQ)
case '%':
tok = l.newToken(token.MODULO, l.ch)
case ';':
tok = l.newToken(token.SEMICOLON, l.ch)
case '(':
tok = l.newToken(token.LPAREN, l.ch)
case '+':
tok = l.peekerForTwoChars('+', l.newToken(token.PLUS, l.ch), token.DOUBLE_PLUS)
case '-':
tok = l.peekerForTwoChars('-', l.newToken(token.MINUS, l.ch), token.DOUBLE_MINUS)
case '/':
tok = l.newToken(token.SLASH, l.ch)
case '*':
tok = l.newToken(token.ASTERISK, l.ch)
case '<':
if l.peekChar() == '<' {
tok = l.readTwoBytesToken(token.LS, l.peekChar())
} else {
tok = l.peekerForTwoChars('=', l.newToken(token.LT, l.ch), token.LTE)
}
case '>':
if l.peekChar() == '>' {
tok = l.readTwoBytesToken(token.RS, l.peekChar())
} else {
tok = l.peekerForTwoChars('=', l.newToken(token.GT, l.ch), token.GTE)
}
case '&':
tok = l.peekerForTwoChars('&', l.newToken(token.ANDBIN, l.ch), token.AND)
case '|':
tok = l.peekerForTwoChars('|', l.newToken(token.ORBIN, l.ch), token.OR)
case '^':
tok = l.newToken(token.XORBIN, l.ch)
case ')':
tok = l.newToken(token.RPAREN, l.ch)
case ',':
tok = l.newToken(token.COMMA, l.ch)
case '{':
tok = l.newToken(token.LBRACE, l.ch)
case '}':
tok = l.newToken(token.RBRACE, l.ch)
case 0:
tok.Line = l.line
tok.Position = l.column
tok.OverallPosition = l.position
tok.Literal = ""
tok.Type = token.EOF
default:
if isLetter(l.ch) {
// returns the whole identifier
tok.Literal = l.readIdentifier()
// lookup the literal in the keyword table, if it doesn't exist it's a IDENT.
tok.Type = token.LookupIdent(tok.Literal)
tok.Position = l.column - uint32(len(tok.Literal))
tok.Line = l.line
tok.OverallPosition = l.position
return tok
}
if isDigit(l.ch) {
tok = l.parseNumericToken()
return tok
}
tok = l.newToken(token.ILLEGAL, l.ch)
}
l.readChar()
return tok
}
func (l *Lexer) parseNumericToken() token.Token {
var tok token.Token
tok.Literal, tok.Type = l.readNumber()
tok.OverallPosition = l.position
tok.Line = l.line
tok.Position = l.column - uint32(len(tok.Literal))
return tok
}
func (l *Lexer) readString() string {
s := ""
l.readChar()
for l.ch != '"' && l.ch != 0 {
if l.ch == '\\' {
// It's a jumpline
if l.peekChar() == 'n' {
l.readChar()
l.readChar()
s += string('\n')
continue
}
// Read it as a literal
l.readChar()
s += string(l.ch)
l.readChar()
continue
}
s += string(l.ch)
l.readChar()
}
return s
}
func (l *Lexer) newToken(tokenType token.TypeToken, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch), Line: l.line, Position: l.column - 1, OverallPosition: l.position}
}
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
func (l *Lexer) readHex() (string, token.TypeToken) {
position := l.position
// '0'
l.readChar()
// 'x'
l.readChar()
// Numbers and characters
for isDigit(l.ch) || (l.ch >= 'A' && l.ch <= 'F') || (l.ch >= 'a' && l.ch <= 'f') {
l.readChar()
}
return l.input[position:l.position], token.HEX
}
func (l *Lexer) readBin() (string, token.TypeToken) {
position := l.position
// '0'
l.readChar()
// 'b'
l.readChar()
// Numbers and characters
for l.ch == '0' || l.ch == '1' {
l.readChar()
}
return l.input[position:l.position], token.BINARY
}
func (l *Lexer) readNumber() (string, token.TypeToken) {
position := l.position
tokenType := token.INT
if l.ch == '0' && l.peekChar() == 'x' {
return l.readHex()
}
if l.ch == '0' && l.peekChar() == 'b' {
return l.readBin()
}
for isDigit(l.ch) {
l.readChar()
}
if l.ch == '.' && l.peekChar() >= '0' && l.peekChar() <= '9' {
l.readChar()
tokenType = token.FLOAT
}
for isDigit(l.ch) {
l.readChar()
}
return l.input[position:l.position], tokenType
}
func (l *Lexer) readChar() {
defer func() {
// next position is current position now
l.position = l.readPosition
l.column++
// point to the next char
l.readPosition++
}()
// EOF
if l.readPosition >= len(l.input) {
l.ch = 0
return
}
// read next position.
l.ch = l.input[l.readPosition]
}
func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
}
return l.input[l.readPosition]
}