-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenstream.go
185 lines (160 loc) · 4.5 KB
/
tokenstream.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
package runevm
import (
"fmt"
"strings"
"unicode"
)
type Token struct {
Type string
Value string
File string
Line int
Col int
Length int
}
type TokenStream struct {
input *InputStream
current *Token
last *Token
keywords map[string]bool
}
func newTokenStream(input *InputStream) *TokenStream {
keywords := map[string]bool{
"if": true, "then": true, "elif": true, "else": true, "while": true, "break": true, "continue": true, "fun": true, "return": true,
"true": true, "false": true, "array": true, "table": true, "import": true, "not": true,
}
return &TokenStream{input: input, keywords: keywords}
}
func (ts *TokenStream) isKeyword(x string) bool {
return ts.keywords[x]
}
func (ts *TokenStream) isDigit(ch byte) bool {
return unicode.IsDigit(rune(ch))
}
func (ts *TokenStream) isIdStart(ch byte) bool {
return unicode.IsLetter(rune(ch)) || ch == '_'
}
func (ts *TokenStream) isId(ch byte) bool {
return ts.isIdStart(ch) || strings.ContainsRune("?!-<>=0123456789", rune(ch))
}
func (ts *TokenStream) isOpChar(ch byte) bool {
return strings.ContainsRune("+-*/%=&|<>!", rune(ch))
}
func (ts *TokenStream) isPunc(ch byte) bool {
return strings.ContainsRune(".,:(){}[]", rune(ch))
}
func (ts *TokenStream) isWhitespace(ch byte) bool {
return strings.ContainsRune(" \r\t\n", rune(ch))
}
func (ts *TokenStream) readWhile(predicate func(byte) bool) (string, int) {
var str strings.Builder
startPos := ts.input.Pos
for !ts.input.eof() && predicate(ts.input.peek()) {
str.WriteByte(ts.input.next())
}
return str.String(), ts.input.Pos - startPos
}
func (ts *TokenStream) readNumber() *Token {
number, length := ts.readWhile(func(ch byte) bool {
if ch == '.' {
return true
}
return ts.isDigit(ch)
})
return &Token{Type: "num", Value: number, File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
}
func (ts *TokenStream) readIdent() *Token {
id, length := ts.readWhile(ts.isId)
if ts.isKeyword(id) {
return &Token{Type: "kw", Value: id, File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
}
return &Token{Type: "var", Value: id, File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
}
func (ts *TokenStream) readEscaped(end byte) (string, int) {
var escaped bool
var str strings.Builder
startPos := ts.input.Pos
ts.input.next() // Consume initial quote
for !ts.input.eof() {
ch := ts.input.next()
if escaped {
str.WriteByte(ch)
escaped = false
} else if ch == '\\' {
escaped = true
} else if ch == end {
break
} else {
str.WriteByte(ch)
}
}
return str.String(), ts.input.Pos - startPos
}
func (ts *TokenStream) readString() *Token {
str, length := ts.readEscaped('"')
return &Token{Type: "str", Value: str, File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
}
func (ts *TokenStream) skipComment() {
ts.readWhile(func(ch byte) bool { return ch != '\n' })
ts.input.next()
}
func (ts *TokenStream) readNext() *Token {
ts.readWhile(ts.isWhitespace)
if ts.input.eof() {
return nil
}
ch := ts.input.peek()
switch {
case ch == '#':
ts.skipComment()
return ts.readNext()
case ch == '"':
return ts.readString()
case ts.isDigit(ch):
return ts.readNumber()
case ts.isIdStart(ch):
return ts.readIdent()
case ts.isPunc(ch):
length := 1
return &Token{Type: "punc", Value: string(ts.input.next()), File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
case ts.isOpChar(ch):
op, length := ts.readWhile(ts.isOpChar)
return &Token{Type: "op", Value: op, File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col - length, Length: length}
default:
errTok := &Token{Type: "", Value: "", File: ts.input.filepath, Line: ts.input.line, Col: ts.input.Col, Length: 0}
ts.input.error(errTok, fmt.Sprintf("invalid character: %c", ch))
return nil
}
}
func (ts *TokenStream) peek() *Token {
if ts.current == nil {
ts.current = ts.readNext()
}
return ts.current
}
func (ts *TokenStream) next() *Token {
tok := ts.current
ts.current = nil
if tok == nil {
tok = ts.readNext()
}
ts.last = ts.copyToken(tok)
return tok
}
func (ts *TokenStream) eof() bool {
return ts.peek() == nil
}
func (ts *TokenStream) error(tok *Token, msg string) {
ts.input.error(tok, msg)
}
func (ts *TokenStream) copyToken(tok *Token) *Token {
t := &Token{
Type: tok.Type,
Value: tok.Value,
File: tok.File,
Line: tok.Line,
Col: tok.Col,
Length: tok.Length,
}
return t
}