/
lex.go
328 lines (286 loc) · 6.5 KB
/
lex.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2022 Franklin "Snaipe" Mathieu.
//
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
package syntax
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"unicode/utf8"
)
// Cursor represents a cursor position within a document, i.e. a line and
// a column number, both starting at 1.
type Cursor struct {
Line, Column int
}
type Token struct {
// The type of this token.
Type TokenType
// The original string representation of this token.
Raw string
// The value interpreted from Raw (may be nil).
Value interface{}
// The starting position of this token.
Start Cursor
// The end position of this token.
End Cursor
}
// IsAny returns true if the token is one of the specified token types.
func (tok *Token) IsAny(types ...TokenType) bool {
for _, typ := range types {
if tok.Type == typ {
return true
}
}
return false
}
type TokenType string
const (
TokenEOF TokenType = ""
TokenError TokenType = "<error>"
TokenComment TokenType = "<comment>"
TokenInlineComment TokenType = "<inline-comment>"
TokenString TokenType = "<string>"
TokenNumber TokenType = "<number>"
TokenBool TokenType = "<bool>"
TokenIdentifier TokenType = "<identifier>"
TokenNil TokenType = "<nil>"
TokenNewline TokenType = "<newline>"
TokenWhitespace TokenType = "<whitespace>"
)
func (typ TokenType) String() string {
if typ == TokenEOF {
return "<eof>"
}
return string(typ)
}
type backbuffer struct {
buf [2]struct {
r rune
w int
next Cursor
pos Cursor
}
ridx int
rlen int
widx int
}
func (b *backbuffer) inc(i, inc int) int {
i = (i + inc) % len(b.buf)
if i < 0 {
i += len(b.buf)
}
return i
}
func (b *backbuffer) cap() int {
return cap(b.buf)
}
func (b *backbuffer) write(r rune, w int, next, pos Cursor) {
if b.rlen != 0 {
panic("programming error: can't write into backbuffer while there are unread runes")
}
e := &b.buf[b.widx]
e.r, e.w, e.next, e.pos = r, w, next, pos
b.widx = b.inc(b.widx, 1)
}
func (b *backbuffer) read() (rune, int, Cursor, Cursor) {
if b.rlen == 0 {
panic("programming error: no runes in backbuffer")
}
e := &b.buf[b.inc(b.widx, -b.rlen)]
b.rlen--
return e.r, e.w, e.next, e.pos
}
func (b *backbuffer) unread() (rune, int, Cursor, Cursor) {
if b.rlen >= len(b.buf) {
panic("programming error: can't unread more bytes than backbuffer capacity")
}
b.rlen++
ret := &b.buf[b.inc(b.widx, -b.rlen)]
if ret.w == 0 {
panic("programming error: can't unread more bytes than backbuffer length")
}
return ret.r, ret.w, ret.next, ret.pos
}
type StateFunc func(*Lexer) StateFunc
type Lexer struct {
// The input of this lexer. Typically a bufio.Reader.
Input io.RuneReader
// The cursor position marking the start of the current token.
TokenPosition Cursor
// The cursor position at which the lexer will be reading next.
NextPosition Cursor
// The cursor position of the current rune.
Position Cursor
init StateFunc // initial state
state StateFunc // current state
token bytes.Buffer // current token
tokens chan Token // token ring buffer
prev backbuffer // stashed runes for UnreadRune
unread int // number of unread bytes
}
func NewLexer(input io.Reader, init StateFunc) *Lexer {
rscan, ok := input.(io.RuneReader)
if !ok {
rscan = bufio.NewReader(input)
}
l := Lexer{
Input: rscan,
init: init,
}
l.Reset()
return &l
}
func (l *Lexer) Reset() {
l.state = l.init
l.NextPosition = Cursor{1, 1}
l.Position = l.NextPosition
l.TokenPosition = l.NextPosition
l.tokens = make(chan Token, 2)
l.token.Reset()
}
func (l *Lexer) Next() Token {
for {
select {
case token := <-l.tokens:
return token
default:
l.state = l.state(l)
}
}
}
func (l *Lexer) Error(err error) StateFunc {
typ := TokenError
if err == io.EOF {
typ = TokenEOF
}
if _, ok := err.(*Error); !ok {
err = &Error{Cursor: l.TokenPosition, Err: err}
}
token := Token{
Type: typ,
Value: err,
Raw: l.Token(),
Start: l.TokenPosition,
End: l.Position,
}
l.tokens <- token
l.TokenPosition = l.NextPosition
close(l.tokens)
return nil
}
func (l *Lexer) Errorf(format string, args ...interface{}) StateFunc {
return l.Error(fmt.Errorf(format, args...))
}
func (l *Lexer) Emit(typ TokenType, val interface{}) {
token := Token{
Type: typ,
Raw: l.Token(),
Value: val,
Start: l.TokenPosition,
End: l.Position,
}
l.token.Reset()
l.tokens <- token
l.TokenPosition = l.NextPosition
}
func (l *Lexer) Discard() {
l.token.Reset()
l.TokenPosition = l.NextPosition
}
func (l *Lexer) ReadRune() (r rune, w int, err error) {
if l.unread > 0 {
r, w, l.NextPosition, l.Position = l.prev.read()
l.unread--
} else {
r, w, err = l.Input.ReadRune()
if err != nil {
return 0, 0, err
}
if r == utf8.RuneError {
return 0, 0, fmt.Errorf("bad UTF-8 character")
}
l.prev.write(r, w, l.NextPosition, l.Position)
}
l.token.WriteRune(r)
l.Position = l.NextPosition
switch r {
case '\n':
l.NextPosition.Line++
l.NextPosition.Column = 1
default:
l.NextPosition.Column++
}
return r, w, nil
}
func (l *Lexer) UnreadRune() error {
_, w, next, pos := l.prev.unread()
l.unread++
l.NextPosition = next
l.Position = pos
l.token.Truncate(l.token.Len() - w)
return nil
}
func (l *Lexer) PeekRune() (rune, int, error) {
r, w, err := l.ReadRune()
if err != nil {
return 0, 0, err
}
l.UnreadRune()
return r, w, nil
}
func (l *Lexer) Token() string {
return string(l.token.Bytes())
}
func (l *Lexer) AcceptRune(exp rune) (rune, error) {
r, _, err := l.ReadRune()
switch {
case err != nil:
return 0, err
case r != exp:
return r, fmt.Errorf("expected character %q, got %q", exp, r)
}
return r, nil
}
func (l *Lexer) AcceptString(exp string) (string, error) {
for _, rexp := range exp {
r, _, err := l.ReadRune()
switch {
case err != nil:
return "", err
case r != rexp:
return "", fmt.Errorf("unexpected character %q in expected string %q", r, exp)
}
}
return exp, nil
}
func (l *Lexer) AcceptFunc(fn func(rune) bool) (rune, error) {
r, _, err := l.ReadRune()
switch {
case err != nil:
return 0, err
case !fn(r):
return r, fmt.Errorf("unexpected character %q", r)
}
return r, nil
}
func (l *Lexer) AcceptUntil(fn func(rune) bool) (string, error) {
var out strings.Builder
for {
r, _, err := l.ReadRune()
if err == io.EOF {
return out.String(), nil
}
if err != nil {
return "", err
}
if !fn(r) {
l.UnreadRune()
return out.String(), nil
}
out.WriteRune(r)
}
}