-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
282 lines (254 loc) · 5.54 KB
/
scanner.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
package jaess
import (
"fmt"
"io"
"bytes"
)
// a scanner of tokens
type TokenScanner struct {
input io.RuneScanner
offset int64
Location Cursor
lastToken *Token
unToken *Token
capture *SourceCapture
Trace bool
}
// location within the source input
type Cursor struct {
line int
column int
}
// used for capturing source blocks like functions
type SourceCapture struct {
buf bytes.Buffer
parent *SourceCapture
}
// creates a new token scanner
func NewTokenScanner(input io.RuneScanner) *TokenScanner {
ts := new(TokenScanner)
ts.input = input
ts.Location = Cursor{0, 0}
return ts
}
func (self *Cursor) _IncrementByRune(r rune) {
if r == '\n' {
self.line += 1
self.column = 0
} else {
self.column += 1
}
}
func (self *SourceCapture) WriteRune(r rune) {
_, err := self.buf.WriteRune(r)
if err != nil {
panic(err)
}
if self.parent != nil {
self.parent.WriteRune(r)
}
}
func (self SourceCapture) String() string {
return string(self.buf.Bytes())
}
// begins a capture of a source block
func (self *TokenScanner) BeginCapture() {
sc := new(SourceCapture)
if self.capture != nil {
sc.parent = self.capture
}
self.capture = sc
}
// ends and returns a capture
func (self *TokenScanner) FinishCapture() *SourceCapture {
sc := self.capture
if sc == nil {
panic(fmt.Errorf("cant finish capture before starting one"))
}
self.capture = sc.parent
return sc
}
// gets the next token available
// returns a token or nil, or nil with an error
func (self *TokenScanner) Next() (*Token, error) {
var token *Token
// check undo cache
if self.unToken != nil {
token = self.unToken
self.unToken = nil
return token, nil
}
// scan
for {
r, rlen, err := self.input.ReadRune()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if token == nil {
token = new(Token)
token.Location = self.Location
}
ok, sntxErr := token.ConsumeRune(r)
if sntxErr != nil {
sntxErr.Location = self.Location
return nil, sntxErr
}
if ok {
self.offset += int64(rlen)
self.Location._IncrementByRune(r)
if self.capture != nil {
self.capture.WriteRune(r)
}
continue
}
self.input.UnreadRune()
if _SPACE != token.Type {
break
}
token = nil
}
// correct type of single line comment at eof
if token != nil && _COMMENT_SINGLE_LINE == token.Type {
token.Type = COMMENT
}
// check for internal enums
if token != nil && token.Type >= _HIDDEN {
if token.Type == _COMMENT_MULTI_LINE ||
token.Type == _COMMENT_MULTI_LINE_MAY_END {
return nil, &SyntaxError{"incomplete multiline comment", token.Location}
} else {
return nil, &SyntaxError{"unexpected eof", self.Location}
}
}
if self.Trace {
fmt.Printf("\x1b[90m%v\x1b[0m\n", token)
}
// cache last token
self.lastToken = token
return token, nil
}
// moves the scanner back one. cannot go back more than one.
func (self *TokenScanner) UnNext() error {
if self.unToken != nil {
return ScannerError{"consecutive UnNext calls unsupported, must call Next between each"}
}
self.unToken = self.lastToken
return nil
}
// peeks at the next value. cannot call UnNext after peeking.
func (self *TokenScanner) Peek() (*Token, error) {
if self.unToken == nil {
var err error
self.unToken, err = self.Next()
if err != nil {
return nil, err
}
}
return self.unToken, nil
}
// reads the run into the token
// returning true if rune is accepted, false if rune is rejected
// error is nil unless a syntax error is detected
func (self *Token) ConsumeRune(r rune) (bool, *SyntaxError) {
switch self.Type {
// stage 1: lexical identification
case TOKEN_UNKNOWN:
switch {
case IsInlineWhitespaceRune(r):
self.Type = _SPACE
case '\n' == r:
self.Type = NEWLINE
// case '\'' == r:
// self.Type = _STRING_SINGLE_QUOTE
case '"' == r:
self.Type = _STRING_DOUBLE_QUOTE
case '/' == r:
self.Type = _ONE_SLASH
case IsDelimeterRune(r):
self.Type = DELIMITER
case IsOperatorRune(r):
self.Type = OPERATOR
case IsAtomRune(r):
self.Type = ATOM
case IsDigitRune(r):
self.Type = NUMBER
default:
return false, &SyntaxError{fmt.Sprintf("Invalid Rune %c", r), Cursor{-1, -1}}
}
self.Value += string(r)
return true, nil
// stage 2: token scan
case _SPACE:
if IsInlineWhitespaceRune(r) {
self.Value += string(r)
return true, nil
}
case _ONE_SLASH:
if r == '/' {
self.Value += string(r)
self.Type = _COMMENT_SINGLE_LINE
return true, nil
}
if r == '*' {
self.Value += string(r)
self.Type = _COMMENT_MULTI_LINE
return true, nil
}
case _COMMENT_SINGLE_LINE:
if r == '\n' {
self.Type = COMMENT
return false, nil
} else {
self.Value += string(r)
return true, nil
}
case _COMMENT_MULTI_LINE:
self.Value += string(r)
if r == '*' {
self.Type = _COMMENT_MULTI_LINE_MAY_END
}
return true, nil
case _COMMENT_MULTI_LINE_MAY_END:
switch r {
case '/':
self.Type = COMMENT
case '*':
self.Type = _COMMENT_MULTI_LINE_MAY_END
default:
self.Type = _COMMENT_MULTI_LINE
}
self.Value += string(r)
return true, nil
case _STRING_DOUBLE_QUOTE:
self.Value += string(r)
if r == '"' {
self.Type = STRING
}
return true, nil
case COMMENT:
return false, nil
case DELIMITER:
return false, nil
case STRING:
return false, nil
case OPERATOR:
if IsOperatorRune(r) {
self.Value += string(r)
return true, nil
}
case ATOM:
if IsAtomRune(r) || IsDigitRune(r) {
self.Value += string(r)
return true, nil
}
case NUMBER:
if r == '.' || IsDigitRune(r) {
self.Value += string(r)
return true, nil
}
}
return false, nil
}