-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
312 lines (270 loc) · 4.84 KB
/
lexer.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
package patman
type token struct {
Type tokenType
Value string
Line int
Col int
Pos int
// PosEnd is Pos + len(value)
}
type tokenType int
const (
EOF tokenType = iota
IDENT // used for matching against user-defined operators
STRING // argument
SLASH // used as argument delimiter
L_PARENS
R_PARENS
PIPE
ERROR
)
// TODO: Can use chroma with custom (tiny) lexer for syntax highlight
// 👉 https://github.com/alecthomas/chroma/blob/master/lexers/lexers.go#L48:6
// 👉 https://github.com/charmbracelet/glamour/blob/master/ansi/codeblock.go
type lexer struct {
buf []rune
ch rune // current char
pos int
nextpos int
line int
col int
}
func NewLexer(code string) lexer {
return lexer{
buf: []rune(code),
ch: rune(code[0]),
line: 1,
col: 0,
pos: 0,
nextpos: 1,
}
}
func (l *lexer) NextToken() token {
for l.isWhitespace() {
if l.isNewLine() {
l.line += 1
l.col = 0
}
l.next()
}
if l.isEOF() {
return token{
Type: EOF,
Value: string("EOF"),
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
// OPERATOR
if l.isPrevPipe() || l.pos == 0 || l.isPrevWhitespace() {
identIndex := l.pos
for l.isAlpha() {
l.next()
}
if identIndex != l.pos {
return token{
Type: IDENT,
Value: string(l.buf[identIndex:l.pos]),
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
}
if l.isLparens() {
l.next()
return token{
Type: L_PARENS,
Value: "(",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
// ARGUMENTS
if l.isPrevLparens() {
argIndex := l.pos
counter := 1
for {
// End anyway at EOF to prevent infinite loop
if l.isEOF() {
return token{
Type: ERROR,
Value: "EOF",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
// Should stop at unescaped new line delimiters OR at PIPE
// e.g. replace(a/2 |> 👉 should break
// e.g. replace(a/2
// ..other chars 👉 should break
if l.isPipe() {
return token{
Type: ERROR,
Value: "|>",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
if l.isNewLine() {
return token{
Type: ERROR,
Value: "\n",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
// Ignore all escaped L_PARENS
if l.isLparens() && !l.isPrevBackSlash() {
counter++
}
if !l.isRparens() {
l.next()
continue
}
// Ignore all escaped R_PARENS
if l.isRparens() && !l.isPrevBackSlash() {
counter--
}
if counter == 0 {
break
}
l.next()
}
if argIndex != l.pos {
return token{
Type: STRING,
Value: string(l.buf[argIndex:l.pos]),
Pos: l.pos,
Line: l.line,
Col: l.col,
}
} else {
// There's no matching parens. Syntax error.
return token{
Type: ERROR,
Value: string(l.ch),
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
}
if l.isRparens() {
l.next()
return token{
Type: R_PARENS,
Value: ")",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
if l.isPipe() {
// Pipe operator is 2 charachters
l.next()
l.next()
return token{
Type: PIPE,
Value: "|>",
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
return token{
Type: ERROR,
Value: string(l.ch),
Pos: l.pos,
Line: l.line,
Col: l.col,
}
}
func (l *lexer) next() {
l.pos = l.nextpos
l.nextpos += 1
l.col += 1
if l.pos < 0 {
l.ch = -1 // EOF
return
}
if l.pos < len(l.buf) {
l.ch = rune(l.buf[l.pos])
return
}
l.pos = len(l.buf)
l.ch = -1 // EOF
}
func (l *lexer) rewind() {
l.pos -= 1
l.nextpos -= 1
l.col -= 1
if l.pos < 0 {
l.ch = -1
} else {
l.ch = rune(l.buf[l.pos])
}
}
func (l *lexer) isEOF() bool {
return l.ch == -1
}
func (l *lexer) isWhitespace() bool {
return l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r'
}
func (l *lexer) isLparens() bool {
return l.ch == '('
}
func (l *lexer) isRparens() bool {
return l.ch == ')'
}
func (l *lexer) isBackSlash() bool {
return l.ch == '\\'
}
// Allowed charachter set for defining identifiers
// TODO: Add digits as allowed
func (l *lexer) isAlpha() bool {
return 'a' <= l.ch && l.ch <= 'z' ||
'A' <= l.ch && l.ch <= 'Z' ||
l.ch == '_' ||
l.ch == '$'
}
func (l *lexer) isPipe() bool {
if len(l.buf)-(l.pos+2) > 0 && l.pos > 0 {
return string(l.buf[l.pos:l.pos+2]) == "|>"
}
return false
}
func (l *lexer) isNewLine() bool {
return l.ch == '\n'
}
func (l *lexer) isPrevPipe() bool {
l.rewind()
l.rewind()
is := l.isPipe()
l.next()
l.next()
return is
}
func (l *lexer) isPrevLparens() bool {
l.rewind()
is := l.isLparens()
l.next()
return is
}
func (l *lexer) isPrevWhitespace() bool {
l.rewind()
is := l.isWhitespace()
l.next()
return is
}
func (l *lexer) isPrevBackSlash() bool {
l.rewind()
is := l.isBackSlash()
l.next()
return is
}