-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_parse_utils.go
314 lines (256 loc) · 8.13 KB
/
lexer_parse_utils.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
package golex
import (
"fmt"
"iter"
)
// ---------------------------------------------------------------
// Lookahead Helpers
// ---------------------------------------------------------------
// NextTokenIs checks if the next token is the same as
// the provided token without consuming the token
func (l *Lexer) NextTokenIs(token Token) bool {
return l.Lookahead(1).Is(token)
}
// NextTokenIsAnyOf checks if the next token is of any of
// the provided tokens without consuming the token
func (l *Lexer) NextTokenIsAnyOf(tokens ...Token) bool {
return l.Lookahead(1).IsAnyOf(tokens...)
}
// NextTokenSequenceIs checks if the next sequence of tokens
// in the lexer matches the provided token sequence
// without consuming the tokens
func (l *Lexer) NextTokenSequenceIs(tokens ...Token) bool {
i := 0
for token := range l.LookaheadIterator(len(tokens)) {
if !token.Is(tokens[i]) {
return false
}
i += 1
}
return true
}
// ---------------------------------------------------------------
// Iterators
// ---------------------------------------------------------------
// IterateTokensBetweenParentheses returns an iterator that iterates over all
// the tokens between the opening and closing parentheses starting at the offset start
// It assumes that the offset start contains the opening parenthesis.
// Nested parenthesis will be contained in the output tokens until
// the matching closing parenthesis to the start is found.
// In addition it returns the start and end cursor position for the iterated portion
func (l *Lexer) IterateTokensBetweenParentheses() (iter.Seq2[Token, error], *int, *int, error) {
return l.IterateTokensBetween(TypeOpenParen, TypeCloseParen)
}
// IterateTokensBetweenCurlyBraced returns an iterator that iterates over all
// the tokens between the opening and closing curly braces starting at the offset start
// It assumes that the offset start contains the opening curly brace.
// Nested curly braces will be contained in the output tokens until
// the matching closing curly brace to the start is found.
// In addition it returns the start and end cursor position for the iterated portion
func (l *Lexer) IterateTokensBetweenCurlyBraces() (iter.Seq2[Token, error], *int, *int, error) {
return l.IterateTokensBetween(TypeOpenCurly, TypeCloseCurly)
}
// IterateTokensBetween returns an iterator that iterates over all
// the tokens between the open and close type starting at the offset start
// it assumes that the offset start contains the opening token.
// Nested openers and closers will be contained in the output
// tokens until the matching closer is found.
// In addition it returns the start and end cursor position for the iterated portion
func (l *Lexer) IterateTokensBetween(open TokenType, close TokenType) (iter.Seq2[Token, error], *int, *int, error) {
var err error
token := l.CurrentToken()
start := l.GetCursor() + len(token.Literal)
end := l.GetCursor()
level := 1
if !token.TypeIs(open) {
return nil, &start, &end, fmt.Errorf("Current token is not of opener type %s", open)
}
return func(yield func(Token, error) bool) {
for !token.TypeIs(TypeEof) {
end = l.GetCursor()
token, err = l.NextToken()
if err != nil {
yield(token, err)
}
if token.TypeIs(TypeEof) {
if !yield(token, nil) {
return
}
return
}
if token.TypeIs(close) {
level -= 1
if level == 0 {
end = l.GetCursor() - len(token.Literal)
return
}
}
if token.TypeIs(open) {
level += 1
}
if !yield(token, nil) {
return
}
}
return
}, &start, &end, nil
}
func (l *Lexer) IterateTokensDelimited(tokenType TokenType, delimiter TokenType) iter.Seq2[Token, error] {
var err error
token := l.CurrentToken()
return func(yield func(Token, error) bool) {
for !token.TypeIs(TypeEof) {
if !token.TypeIs(tokenType) {
yield(token, fmt.Errorf("expected %s but found %s", tokenType, token.Type))
}
if !yield(token, nil) {
return
}
if !l.Lookahead(1).TypeIs(delimiter) {
return
}
token, err = l.NextToken() // Just consume the delimiter
if err != nil {
if !yield(token, err) {
return
}
}
token, err = l.NextToken()
if err != nil {
if !yield(token, err) {
return
}
}
}
return
}
}
func (l *Lexer) IterateAnyTokenDelimited(delimiter TokenType) iter.Seq2[Token, error] {
var err error
token := l.CurrentToken()
return func(yield func(Token, error) bool) {
for !token.TypeIs(TypeEof) {
if !yield(token, nil) {
return
}
if !l.Lookahead(1).TypeIs(delimiter) {
return
}
token, err = l.NextToken() // Just consume the delimiter
if err != nil {
if !yield(token, err) {
return
}
}
token, err = l.NextToken()
if err != nil {
if !yield(token, err) {
return
}
}
}
return
}
}
// ---------------------------------------------------------------
// Collectors
// ---------------------------------------------------------------
// CollectTokensBetweenParentheses collects all the tokens between
// the opening and closing parentheses starting at the offset start
// It assumes that the offset start contains the opening parenthesis.
// Nested parenthesis will be contained in the output tokens until
// the matching closing parenthesis to the start is found.
// In addition it returns the start and end cursor position for the collected portion
func (l *Lexer) CollectTokensBetweenParentheses() (Tokens, int, int, error) {
return l.CollectTokensBetween(TypeOpenParen, TypeCloseParen)
}
// CollectTokensBetweenCurlyBraced collects all the tokens between
// the opening and closing curly braces starting at the offset start
// It assumes that the offset start contains the opening curly brace.
// Nested curly braces will be contained in the output tokens until
// the matching closing curly brace to the start is found.
// In addition it returns the start and end cursor position for the collected portion
func (l *Lexer) CollectTokensBetweenCurlyBraces() (Tokens, int, int, error) {
return l.CollectTokensBetween(TypeOpenCurly, TypeCloseCurly)
}
// CollectTokensBetween collects all the tokens between the open
// and close type starting at the offset start
// it assumes that the offset start contains the opening token.
// Nested openers and closers will be contained in the output
// tokens until the matching closer is found.
// In addition it returns the start and end cursor position for the collected portion
func (l *Lexer) CollectTokensBetween(open TokenType, close TokenType) (Tokens, int, int, error) {
var err error
tokens := Tokens{}
token := l.CurrentToken()
if !token.TypeIs(open) {
return tokens, -1, -1, fmt.Errorf("Current token is not of opener type %s", open)
}
start := l.GetCursor()
end := start
level := 1
for !token.TypeIs(TypeEof) {
end = l.GetCursor()
token, err = l.NextToken()
if err != nil {
return tokens, start, end, err
}
if token.TypeIs(TypeEof) {
return tokens, start, end, fmt.Errorf("Unexpected EndOfFile")
}
if token.TypeIs(close) {
level -= 1
if level == 0 {
break
}
}
if token.TypeIs(open) {
level += 1
}
tokens = append(tokens, token)
}
return tokens, start, end, nil
}
func (l *Lexer) CollectTokensDelimited(tokenType TokenType, delimiter TokenType) (Tokens, error) {
var err error
tokens := Tokens{}
token := l.CurrentToken()
for !token.TypeIs(TypeEof) {
if !token.TypeIs(tokenType) {
return tokens, fmt.Errorf("expected %s but found %s", tokenType, token.Type)
}
tokens = append(tokens, token)
if !l.Lookahead(1).TypeIs(delimiter) {
break
}
token, err = l.NextToken() // Just consume the delimiter
if err != nil {
return tokens, err
}
token, err = l.NextToken()
if err != nil {
return tokens, err
}
}
return tokens, nil
}
func (l *Lexer) CollectAnyTokenDelimited(delimiter TokenType) (Tokens, error) {
var err error
tokens := Tokens{}
token := l.CurrentToken()
for !token.TypeIs(TypeEof) {
tokens = append(tokens, token)
if !l.Lookahead(1).TypeIs(delimiter) {
break
}
token, err = l.NextToken() // Just consume the delimiter
if err != nil {
return tokens, err
}
token, err = l.NextToken()
if err != nil {
return tokens, err
}
}
return tokens, nil
}