-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathlexer.go
More file actions
315 lines (285 loc) · 5.75 KB
/
lexer.go
File metadata and controls
315 lines (285 loc) · 5.75 KB
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
package lexer
import (
"fmt"
"io"
"strings"
"unicode/utf8"
"github.com/expr-lang/expr/file"
"github.com/expr-lang/expr/internal/ring"
)
const ringChunkSize = 10
// Lex will buffer and return the tokens of a disposable *[Lexer].
func Lex(source file.Source) ([]Token, error) {
tokens := make([]Token, 0, ringChunkSize)
l := New()
l.Reset(source)
for {
t, err := l.Next()
switch err {
case nil:
tokens = append(tokens, t)
case io.EOF:
return tokens, nil
default:
return nil, err
}
}
}
// New returns a reusable lexer.
func New() *Lexer {
return &Lexer{
tokens: ring.New[Token](ringChunkSize),
}
}
type Lexer struct {
state stateFn
source file.Source
tokens *ring.Ring[Token]
err *file.Error
start, end struct {
byte, rune int
}
eof bool
// When true, keywords `if`/`else` are not treated as operators and
// will be emitted as identifiers instead (for compatibility with custom if()).
DisableIfOperator bool
}
func (l *Lexer) Reset(source file.Source) {
l.source = source
l.tokens.Reset()
l.state = root
}
func (l *Lexer) Next() (Token, error) {
for l.state != nil && l.err == nil && l.tokens.Len() == 0 {
l.state = l.state(l)
}
if l.err != nil {
return Token{}, l.err.Bind(l.source)
}
if t, ok := l.tokens.Dequeue(); ok {
return t, nil
}
return Token{}, io.EOF
}
const eof rune = -1
func (l *Lexer) commit() {
l.start = l.end
}
func (l *Lexer) next() rune {
if l.end.byte >= len(l.source.String()) {
l.eof = true
return eof
}
r, sz := utf8.DecodeRuneInString(l.source.String()[l.end.byte:])
l.end.rune++
l.end.byte += sz
return r
}
func (l *Lexer) peek() rune {
if l.end.byte < len(l.source.String()) {
r, _ := utf8.DecodeRuneInString(l.source.String()[l.end.byte:])
return r
}
return eof
}
func (l *Lexer) backup() {
if l.eof {
l.eof = false
} else if l.end.rune > 0 {
_, sz := utf8.DecodeLastRuneInString(l.source.String()[:l.end.byte])
l.end.byte -= sz
l.end.rune--
}
}
func (l *Lexer) emit(t Kind) {
l.emitValue(t, l.word())
}
func (l *Lexer) emitValue(t Kind, value string) {
l.tokens.Enqueue(Token{
Location: file.Location{From: l.start.rune, To: l.end.rune},
Kind: t,
Value: value,
})
l.commit()
}
func (l *Lexer) emitEOF() {
from := l.end.rune - 1
if from < 0 {
from = 0
}
to := l.end.rune - 0
if to < 0 {
to = 0
}
l.tokens.Enqueue(Token{
Location: file.Location{From: from, To: to},
Kind: EOF,
})
l.commit()
}
func (l *Lexer) skip() {
l.commit()
}
func (l *Lexer) word() string {
return l.source.String()[l.start.byte:l.end.byte]
}
func (l *Lexer) accept(valid string) bool {
if strings.ContainsRune(valid, l.peek()) {
l.next()
return true
}
return false
}
func (l *Lexer) acceptRun(valid string) {
for l.accept(valid) {
}
}
func (l *Lexer) skipSpaces() {
l.acceptRun(" ")
l.skip()
}
func (l *Lexer) error(format string, args ...any) stateFn {
if l.err == nil { // show first error
end := l.end.rune
if l.eof {
end++
}
l.err = &file.Error{
Location: file.Location{
From: end - 1,
To: end,
},
Message: fmt.Sprintf(format, args...),
}
}
return nil
}
func digitVal(ch rune) int {
switch {
case '0' <= ch && ch <= '9':
return int(ch - '0')
case 'a' <= lower(ch) && lower(ch) <= 'f':
return int(lower(ch) - 'a' + 10)
}
return 16 // larger than any legal digit val
}
func lower(ch rune) rune { return ('a' - 'A') | ch } // returns lower-case ch iff ch is ASCII letter
func (l *Lexer) scanDigits(ch rune, base, n int) rune {
for n > 0 && digitVal(ch) < base {
ch = l.next()
n--
}
if n > 0 {
l.error("invalid char escape")
}
return ch
}
func (l *Lexer) scanEscape(quote rune) rune {
ch := l.next() // read character after '/'
switch ch {
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', quote:
// nothing to do
ch = l.next()
case '0', '1', '2', '3', '4', '5', '6', '7':
ch = l.scanDigits(ch, 8, 3)
case 'x':
ch = l.scanDigits(l.next(), 16, 2)
case 'u':
// Support variable-length form: \u{XXXXXX}
if l.peek() == '{' {
// consume '{'
l.next()
// read 1-6 hex digits
digits := 0
for {
p := l.peek()
if p == '}' {
break
}
if digitVal(p) >= 16 {
l.error("invalid char escape")
return eof
}
if digits >= 6 {
l.error("invalid char escape")
return eof
}
l.next()
digits++
}
if l.peek() != '}' || digits == 0 {
l.error("invalid char escape")
return eof
}
// consume '}' and continue
l.next()
ch = l.next()
break
}
ch = l.scanDigits(l.next(), 16, 4)
case 'U':
ch = l.scanDigits(l.next(), 16, 8)
default:
l.error("invalid char escape")
}
return ch
}
func (l *Lexer) scanString(quote rune) (n int) {
ch := l.next() // read character after quote
for ch != quote {
if ch == '\n' || ch == eof {
l.error("literal not terminated")
return
}
if ch == '\\' {
ch = l.scanEscape(quote)
} else {
ch = l.next()
}
n++
}
return
}
func (l *Lexer) scanRawString(quote rune) (n int) {
var escapedQuotes int
loop:
for {
ch := l.next()
for ch == quote && l.peek() == quote {
// skip current and next char which are the quote escape sequence
l.next()
ch = l.next()
escapedQuotes++
}
switch ch {
case quote:
break loop
case eof:
l.error("literal not terminated")
return
}
n++
}
str := l.source.String()[l.start.byte+1 : l.end.byte-1]
// handle simple case where no quoted backtick was found, then no allocation
// is needed for the new string
if escapedQuotes == 0 {
l.emitValue(String, str)
return
}
var b strings.Builder
var skipped bool
b.Grow(len(str) - escapedQuotes)
for _, r := range str {
if r == quote {
if !skipped {
skipped = true
continue
}
skipped = false
}
b.WriteRune(r)
}
l.emitValue(String, b.String())
return
}