-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
263 lines (245 loc) · 4.01 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
package main
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
const (
eof = -1
)
const (
ERR = iota
EOF
VAR
NUM
LP
RP
PLUS
MINUS
MUL
DIV
REM
EXP
ASSIGN
SEMI
LB
RB
COMMENT
COMMA
CHAR
STR
EQ
NE
LE
LT
GE
GT
Keyword
Return
Else
If
Do
For
While
Char
Int
)
var key = map[string]int{
"return": Return,
"else": Else,
"if": If,
"do": Do,
"for": For,
"while": While,
"char": Char,
"int": Int,
}
type lexer struct {
input string
pos int
start int
width int
}
func newLexer(input string) *lexer {
return &lexer{
input: input,
}
}
func (l *lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
func (l *lexer) backup() {
l.pos -= l.width
}
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *lexer) error(msg string) *Token {
return &Token{
Type: ERR,
Text: msg,
}
}
func (l *lexer) emit(typ int) *Token {
t := &Token{typ, l.start, l.input[l.start:l.pos]}
if typ == EOF {
t.Text = "EOF"
}
l.start = l.pos
return t
}
func (l *lexer) jump(pos int) {
l.start = pos
l.pos = pos
}
func (l *lexer) nextToken() *Token {
for c := l.next(); c != eof; c = l.next() {
switch c {
case ' ', '\t', '\n', '\r':
l.ws()
case '+':
return l.emit(PLUS)
case '-':
return l.emit(MINUS)
case '*':
return l.emit(MUL)
case '/':
if next := l.peek(); next == '*' {
l.next()
if i := strings.Index(l.input[l.pos:], "*/"); i > 0 {
l.pos += i + 2
l.start = l.pos
break
} else {
return l.error("unclosed comment")
}
}
return l.emit(DIV)
case '%':
return l.emit(REM)
case '^':
return l.emit(EXP)
case '=':
if next := l.peek(); next == '=' {
l.next()
return l.emit(EQ)
}
return l.emit(ASSIGN)
case '!':
if next := l.peek(); next == '=' {
l.next()
return l.emit(NE)
}
case '<':
if next := l.peek(); next == '=' {
l.next()
return l.emit(LE)
} else {
return l.emit(LT)
}
case '>':
if next := l.peek(); next == '=' {
l.next()
return l.emit(GE)
} else {
return l.emit(GT)
}
case '"':
next := l.next()
for ; next != '"' && next != '\n' && next != eof; next = l.next() {
}
if next == '\n' || next == eof {
return l.error("string literal unterminated")
} else {
return l.emit(STR)
}
case '\'':
next := l.next()
for ; next != '\'' && next != '\n' && next != eof; next = l.next() {
}
if next == '\n' || next == eof {
return l.error("char literal unterminated")
} else {
return l.emit(CHAR)
}
case ',':
return l.emit(COMMA)
case '(':
return l.emit(LP)
case ')':
return l.emit(RP)
case ';':
return l.emit(SEMI)
case '{':
return l.emit(LB)
case '}':
return l.emit(RB)
default:
if unicode.IsLetter(c) || c == '_' {
return varOrKey(l.collect(0))
} else if unicode.IsDigit(c) {
return l.collect(1)
} else {
return l.error(fmt.Sprintf("invalid character: %c", c))
}
}
}
return l.emit(EOF)
}
func varOrKey(tok *Token) *Token {
if key[tok.Text] > Keyword {
tok.Type = key[tok.Text]
}
return tok
}
func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}
func (l *lexer) collect(typ int) *Token {
predict := [2]func(r rune) bool{
isAlphaNumeric,
unicode.IsDigit,
}
for n := l.next(); predict[typ](n); n = l.next() {
}
l.backup()
if typ == 0 {
return l.emit(VAR)
} else {
return l.emit(NUM)
}
}
func (l *lexer) ws() {
for c := l.next(); c == ' ' || c == '\t' || c == '\n' || c == '\r'; c = l.next() {
}
l.backup()
l.start = l.pos
}
type Token struct {
Type int
Pos int
Text string
}
func (t Token) String() string {
switch {
case t.Type == EOF:
return "EOF"
case t.Type == ERR:
return t.Text
case t.Type > Keyword:
return fmt.Sprintf("<%s>", t.Text)
case len(t.Text) > 10:
return fmt.Sprintf("%.10q...", t.Text)
}
return fmt.Sprintf("%q", t.Text)
}