/
lexer.go
228 lines (208 loc) · 5.1 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
// Package lexer tokenizes the input.
//
// The first step in the Evy compilation process is tokenization. This
// involves breaking the Evy input code into individual tokens, such as
// keywords, operators, and identifiers. The lexer package is
// responsible for this task. It provides a [Lexer] type, which can be
// initialized using the [New] function. The [Lexer.Next] method
// returns the next Token in the input string. A [Token] is a data
// structure that represents a single token in the input code. The
// [EOF] token is a special token that indicates the end of the input
// code.
//
// The [parser] then takes these tokens and creates an Abstract Syntax
// Tree(AST), which is a representation of the Evy code's structure.
// Finally, the [evaluator] walks the AST and executes the program.
//
// [parser]: https://pkg.go.dev/evylang.dev/evy/pkg/parser
// [evaluator]: https://pkg.go.dev/evylang.dev/evy/pkg/evaluator
package lexer
import (
"strconv"
"unicode"
)
// Lexer is a lexical analyzer for Evy source code.
type Lexer struct {
input []rune
cur rune // current rune under examination
pos int // current position in input (points to current rune)
line int
col int
}
// New creates a new Lexer for the given input string.
func New(input string) *Lexer {
return &Lexer{input: []rune(input), pos: -1, line: 1}
}
// Next returns the next [Token] in the input string. When the end of
// the input string is reached Next returns a Token with type [EOF].
func (l *Lexer) Next() *Token {
l.advance()
tok := &Token{
Offset: l.pos,
Line: l.line,
Col: l.col,
}
switch l.cur {
case ' ', '\t':
l.consumeHorizontalWhitespace()
return tok.setType(WS)
case '=':
if l.peekRune() == '=' {
l.advance()
return tok.setType(EQ)
}
return tok.setType(ASSIGN)
case '+':
return tok.setType(PLUS)
case '-':
return tok.setType(MINUS)
case '!':
if l.peekRune() == '=' {
l.advance()
return tok.setType(NOT_EQ)
}
return tok.setType(BANG)
case '/':
if l.peekRune() == '/' {
return tok.setType(COMMENT).setLiteral(l.readComment())
}
return tok.setType(SLASH)
case '*':
return tok.setType(ASTERISK)
case '%':
return tok.setType(PERCENT)
case '<':
if l.peekRune() == '=' {
l.advance()
return tok.setType(LTEQ)
}
return tok.setType(LT)
case '>':
if l.peekRune() == '=' {
l.advance()
return tok.setType(GTEQ)
}
return tok.setType(GT)
case ':':
if l.peekRune() == '=' {
l.advance()
return tok.setType(DECLARE)
}
return tok.setType(COLON)
case '{':
return tok.setType(LCURLY)
case '}':
return tok.setType(RCURLY)
case '(':
return tok.setType(LPAREN)
case ')':
return tok.setType(RPAREN)
case '[':
return tok.setType(LBRACKET)
case ']':
return tok.setType(RBRACKET)
case '\n':
return tok.setType(NL)
case '.':
if l.peekRune() == '.' && l.peekRune2() == '.' {
l.advance()
l.advance()
return tok.setType(DOT3)
}
return tok.setType(DOT)
case '"':
literal, err := l.readString()
// strconv.Unquote error
if err != nil {
return tok.setType(ILLEGAL).setLiteral("invalid string")
}
return tok.setType(STRING_LIT).setLiteral(literal)
case 0:
return tok.setType(EOF)
}
if isLetter(l.cur) {
literal := l.readIdent()
tokenType := lookupKeyword(literal)
if tokenType == IDENT {
return tok.setType(IDENT).setLiteral(literal)
}
return tok.setType(tokenType)
}
if isDigit(l.cur) {
return tok.setType(NUM_LIT).setLiteral(l.readNum())
}
return tok.setType(ILLEGAL).setLiteral(string(l.cur))
}
func (l *Lexer) advance() {
if l.cur == '\n' {
l.line++
l.col = 0
}
l.pos++
l.col++
l.cur = l.lookAt(l.pos)
}
func (l *Lexer) peekRune() rune {
return l.lookAt(l.pos + 1)
}
func (l *Lexer) peekRune2() rune {
return l.lookAt(l.pos + 2)
}
func (l *Lexer) lookAt(pos int) rune {
if pos >= len(l.input) {
return 0
}
return l.input[pos]
}
func (l *Lexer) consumeHorizontalWhitespace() {
for pr := l.peekRune(); isHorizontalWhitespace(pr); pr = l.peekRune() {
l.advance()
}
}
func isHorizontalWhitespace(r rune) bool {
return r == ' ' || r == '\t' || r == '\r'
}
func (l *Lexer) readWhile(pred func(rune) bool) string {
pos := l.pos
for pr := l.peekRune(); pred(pr); pr = l.peekRune() {
l.advance()
}
return string(l.input[pos : l.pos+1])
}
func (l *Lexer) readComment() string {
return l.readWhile(func(r rune) bool { return r != 0 && r != '\n' })
}
func (l *Lexer) readNum() string {
return l.readWhile(func(r rune) bool { return isDigit(r) || r == '.' })
}
func (l *Lexer) readIdent() string {
return l.readWhile(func(r rune) bool { return isLetter(r) || unicode.IsDigit(r) })
}
func (l *Lexer) readString() (string, error) {
pos := l.pos
escaped := false
for {
escaped = l.cur == '\\' && !escaped
pr := l.peekRune()
if pr == '"' && !escaped {
l.advance() // end of string
break
}
if pr == 0 || pr == '\n' {
break // error case
}
l.advance()
}
s := string(l.input[pos : l.pos+1])
r, err := strconv.Unquote(s)
if err != nil {
return "", err
}
return r, nil
}
func isLetter(r rune) bool {
return unicode.IsLetter(r) || r == '_'
}
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}