-
Notifications
You must be signed in to change notification settings - Fork 7
/
lexer.go
266 lines (251 loc) · 5.35 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
// Turbo Pascal lexer
//
// The lexer turns a string of Pascal source code into a stream of
// tokens for parsing.
//
// To tokenize some source, create a new lexer with NewLexer(src) and
// then call Scan() until the token type is EOF or ILLEGAL.
package main
import (
"fmt"
)
// Lexer tokenizes a byte string of source code. Use NewLexer to
// actually create a lexer, and Scan() to get tokens.
type Lexer struct {
src []byte
offset int
ch byte
pos Position
nextPos Position
}
// Position stores the source line and column where a token starts.
type Position struct {
// Line number of the token (starts at 1).
Line int
// Column on the line (starts at 1). Note that this is the byte
// offset into the line, not rune offset.
Column int
}
// NewLexer creates a new lexer that will tokenize the given source
// code.
func NewLexer(src []byte) *Lexer {
l := &Lexer{src: src}
l.nextPos.Line = 1
l.nextPos.Column = 1
l.next()
return l
}
// Scan scans the next token and returns its position (line/column),
// token value (one of the uppercased token constants), and the
// string value of the token. For most tokens, the token value is
// empty. For IDENT, NUM, and STR tokens, it's the token's value.
// For an ILLEGAL token, it's the error message.
func (l *Lexer) Scan() (Position, Token, string) {
// Skip whitespace and comments
for l.ch == ' ' || l.ch == '\t' || l.ch == '\r' || l.ch == '\n' || l.ch == '{' {
if l.ch == '{' {
l.next()
for l.ch != '}' && l.ch != 0 {
l.next()
}
}
l.next()
}
if l.ch == 0 {
// l.next() reached end of input
return l.pos, EOF, ""
}
pos := l.pos
tok := ILLEGAL
val := ""
ch := l.ch
l.next()
// Names: keywords and functions
if isNameStart(ch) {
start := l.offset - 2
for isNameStart(l.ch) || (l.ch >= '0' && l.ch <= '9') || l.ch == '_' {
l.next()
}
name := string(l.src[start : l.offset-1])
tok := KeywordToken(name)
if tok == ILLEGAL {
tok = IDENT
val = name
}
return pos, tok, val
}
switch ch {
case '@':
tok = AT
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// integer: ('0' .. '9')+
// real: ('0' .. '9')+ (('.' ('0' .. '9')+ (EXPONENT)?)? | EXPONENT)
// exponent: ('e' | 'E') ('+' | '-')? ('0' .. '9')+
start := l.offset - 2
for l.ch >= '0' && l.ch <= '9' {
l.next()
}
if l.ch == '.' && l.peekNext() != '.' {
l.next()
gotDigit := false
for l.ch >= '0' && l.ch <= '9' {
l.next()
gotDigit = true
}
if !gotDigit {
return l.pos, ILLEGAL, "expected digits after '.'"
}
}
if l.ch == 'e' || l.ch == 'E' {
l.next()
if l.ch == '+' || l.ch == '-' {
l.next()
}
gotDigit := false
for l.ch >= '0' && l.ch <= '9' {
l.next()
gotDigit = true
}
if !gotDigit {
return l.pos, ILLEGAL, "expected digits after 'e'"
}
}
tok = NUM
val = string(l.src[start : l.offset-1])
case '$':
l.next()
start := l.offset - 2
for isHexDigit(l.ch) {
l.next()
}
tok = HEX
val = string(l.src[start : l.offset-1])
case ':':
tok = l.choice('=', COLON, ASSIGN)
case '=':
tok = EQUALS
case '<':
switch l.ch {
case '=':
l.next()
tok = LTE
case '>':
l.next()
tok = NOT_EQUALS
default:
tok = LESS
}
case '>':
tok = l.choice('=', GREATER, GTE)
case '\'', '#':
chars := make([]byte, 0, 32) // most won't require heap allocation
for {
if ch == '\'' {
for {
c := l.ch
if c == 0 {
return l.pos, ILLEGAL, "didn't find end quote in string"
}
if c == '\r' || c == '\n' {
return l.pos, ILLEGAL, "can't have newline in string"
}
if c == '\'' {
l.next()
if l.ch != '\'' {
break
}
}
chars = append(chars, c)
l.next()
}
} else { // '#'
num := 0
for l.ch >= '0' && l.ch <= '9' {
num = num*10 + int(l.ch-'0')
if num > 255 {
return l.pos, ILLEGAL, "#char greater than 255"
}
l.next()
}
chars = append(chars, byte(num))
}
if l.ch != '\'' && l.ch != '#' {
break
}
ch = l.ch
l.next()
}
tok = STR
val = string(chars)
case '(':
tok = LPAREN
case ')':
tok = RPAREN
case ',':
tok = COMMA
case ';':
tok = SEMICOLON
case '+':
tok = PLUS
case '-':
tok = MINUS
case '*':
tok = STAR
case '/':
tok = SLASH
case '[':
tok = LBRACKET
case ']':
tok = RBRACKET
case '^':
tok = POINTER
case '.':
tok = l.choice('.', DOT, DOT_DOT)
default:
tok = ILLEGAL
val = fmt.Sprintf("unexpected char %q", ch)
}
return pos, tok, val
}
// Load the next character into l.ch (or 0 on end of input) and update
// line and column position.
func (l *Lexer) next() {
l.pos = l.nextPos
if l.offset >= len(l.src) {
// For last character, move offset 1 past the end as it
// simplifies offset calculations in IDENT and NUMBER
if l.ch != 0 {
l.ch = 0
l.offset++
}
return
}
ch := l.src[l.offset]
if ch == '\n' {
l.nextPos.Line++
l.nextPos.Column = 1
} else {
l.nextPos.Column++
}
l.ch = ch
l.offset++
}
func isNameStart(ch byte) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func isHexDigit(ch byte) bool {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func (l *Lexer) choice(ch byte, one, two Token) Token {
if l.ch == ch {
l.next()
return two
}
return one
}
func (l *Lexer) peekNext() byte {
if l.offset >= len(l.src) {
return 0
}
return l.src[l.offset]
}