-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlexer.go
254 lines (216 loc) · 4.76 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
// Copyright 2016 Steven Oud. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package mathcat
import (
"fmt"
"strings"
"unicode"
)
// eol indicates the end of an expression
const eol rune = -1
// lexer holds the lexer's state while scanning an expression. If any error
// occurs, the scanning stops immediately and returns the error.
type lexer struct {
expr []rune // the input expression
ch rune // current character
pos int // current character position
start int // current read offset
tokens Tokens // tokenized lexemes
}
func isIdent(c rune) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= 0x80 && unicode.IsLetter(c))
}
func isNumber(c rune) bool {
return (c >= '0' && c <= '9') || c == '.'
}
func isHex(c rune) bool {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func isBinary(c rune) bool {
return c == '0' || c == '1'
}
func isOctal(c rune) bool {
return c >= '0' && c <= '7'
}
func isWhitespace(c rune) bool {
return c == '\t' || c == ' ' || c == '\r' || c == '\n'
}
// IsValidIdent checks if a string qualifies as a valid identifier.
func IsValidIdent(s string) bool {
checkIdent := func(c rune) bool { return isIdent(c) || isNumber(c) }
return isIdent(rune(s[0])) && strings.IndexFunc(s, checkIdent) != -1
}
// Lex starts lexing an expression, converting an input string into a stream
// of tokens later passed on to the parser.
//
// Returns the generated tokens and any error found.
func Lex(expr string) (Tokens, error) {
l := &lexer{
expr: append([]rune(expr), eol), // add eol as padding
pos: 0,
start: 0,
}
return l.lex()
}
func (l *lexer) lex() (Tokens, error) {
loop:
for l.ch != eol {
l.start = l.pos
l.eat()
switch {
case isIdent(l.ch):
l.readIdent()
case isNumber(l.ch):
l.readNumber()
case isWhitespace(l.ch):
l.skipWhitespace()
default:
switch l.ch {
case '+':
l.switchEq(Add, AddEq)
case '-':
// Check for unary minus. We decide unaryness at lexer level to
// make it easier for the parser to know the difference.
if l.isNegation() && l.peek() != '=' {
l.emit(UnaryMin)
break
}
l.switchEq(Sub, SubEq)
case '/':
l.switchEq(Div, DivEq)
case '*':
if l.peek() == '*' {
l.eat()
l.switchEq(Pow, PowEq)
} else {
l.switchEq(Mul, MulEq)
}
case '%':
l.switchEq(Rem, RemEq)
case '&':
l.switchEq(And, AndEq)
case '|':
l.switchEq(Or, OrEq)
case '^':
l.switchEq(Xor, XorEq)
case '<':
if l.peek() == '<' {
l.eat()
l.switchEq(Lsh, LshEq)
} else {
l.switchEq(Lt, LtEq)
}
case '>':
if l.peek() == '>' {
l.eat()
l.switchEq(Rsh, RshEq)
} else {
l.switchEq(Gt, GtEq)
}
case '~':
l.emit(Not)
case '=':
l.switchEq(Eq, EqEq)
case '!':
if l.peek() != '=' {
return nil, fmt.Errorf("Invalid operation ‘%s’", string(l.ch))
}
l.eat()
l.emit(NotEq)
case '(':
l.emit(Lparen)
case ')':
l.emit(Rparen)
case ',':
l.emit(Comma)
case '#', eol:
// Comment or EOL, stop scanning for tokens
l.emit(Eol)
break loop
default:
return nil, fmt.Errorf("Invalid token ‘%s’", string(l.ch))
}
}
}
return l.tokens, nil
}
func (l lexer) peek() rune {
return l.expr[l.pos]
}
func (l lexer) prev() *Token {
return l.tokens[len(l.tokens)-1]
}
func (l *lexer) eat() rune {
l.ch = l.peek()
l.pos++
return l.ch
}
func (l *lexer) emit(toktype TokenType) {
l.tokens = append(l.tokens, &Token{
Type: toktype,
Value: string(l.expr[l.start:l.pos]),
Pos: l.start,
})
}
func (l lexer) skipWhitespace() {
for isWhitespace(l.peek()) {
l.eat()
}
}
func (l *lexer) readIdent() {
for isIdent(l.peek()) || isNumber(l.peek()) {
l.eat()
}
l.emit(Ident)
}
func (l *lexer) readNumber() {
if l.ch == '0' {
// Hex literals
if l.peek() == 'x' || l.peek() == 'X' {
l.eat()
for isHex(l.peek()) {
l.eat()
}
l.emit(Hex)
return
}
// Binary literals
if l.peek() == 'b' || l.peek() == 'B' {
l.eat()
for isBinary(l.peek()) {
l.eat()
}
l.emit(Binary)
return
}
// Octal literals
if l.peek() == 'o' || l.peek() == 'O' {
l.eat()
for isOctal(l.peek()) {
l.eat()
}
l.emit(Octal)
return
}
}
// Decimal literals
for isNumber(l.peek()) || l.peek() == 'e' || l.peek() == 'E' {
l.eat()
if (l.ch == 'e' || l.ch == 'E') && l.peek() == '-' {
l.eat()
}
}
l.emit(Decimal)
}
func (l lexer) isNegation() bool {
return l.tokens == nil || l.prev().Is(Lparen) || l.prev().IsOperator()
}
func (l *lexer) switchEq(tokA, tokB TokenType) {
if l.peek() == '=' {
l.eat()
l.emit(tokB)
} else {
l.emit(tokA)
}
}