-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
431 lines (396 loc) · 9.58 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package runic
import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// token represents a single lexeme returned from the lexer
type token struct {
Typ tokenType `json:"type"` // type of token
Val string `json:"val"` // characters comprising the token
Line int `json:"line"` // line where the token was found
Pos int `json:"pos"` // position in the input text where the token was found
indent int // number of spaces appearing before the token
}
func (t *token) String() string {
var tokenTypeString string
switch t.Typ {
case typeNone:
tokenTypeString = "typeNone"
case typeEOF:
tokenTypeString = "typeEOF"
case typeText:
tokenTypeString = "typeText"
case typeTerminator:
tokenTypeString = "typeTerminator"
case typeHeading:
tokenTypeString = "typeHeading"
case typeTag:
tokenTypeString = "typeTag"
case typeOpeningSquare:
tokenTypeString = "typeOpeningSquare"
case typeClosingSquare:
tokenTypeString = "typeClosingSquare"
case typeBulletpoint:
tokenTypeString = "typeBulletpoint"
}
var indent string
if t.indent > 0 {
indent = ">" + strconv.Itoa(t.indent)
}
return fmt.Sprintf("[%d:%d:%s:'%s'%s]", t.Line, t.Pos, tokenTypeString, t.Val, indent)
}
type tokenType int
const (
typeNone = iota
typeEOF
typeText
typeTerminator
typeHeading
typeTag
typeOpeningSquare
typeClosingSquare
typeBulletpoint
)
const (
eof = -1
void = -2
charNewline = '\n'
charDot = '.'
charColon = ':'
charOpeningSquare = '['
charClosingSquare = ']'
charBackslash = '\\'
charHyphen = '-'
)
// lexer represents the state machine processing the input text
type lexer struct {
input string // input string containing markup
line int // current line number
pos int // current position in the input text
token token // current token
char rune // current character
lexNext func() // next lex function
skippedNewlines int // number of newlines skipped during `skipSpace`
skippedSpace int // number of spaces skipped during `skipSpace`
tag string // current tag accumulated (run of `unicode.isLetter` chars)
continuousNewline bool // don't treat a single newline as a terminator
ctx ctxType // current context
}
type ctxType int
const (
ctxList ctxType = iota + 1
)
// lex returns a lexer, initialised to process the given input text
func lex(input string) *lexer {
l := &lexer{input: input, line: 1}
l.lexNext = l.lexGlobal
return l
}
// collectTokens runs the lexer and accumulates the lexed tokens to return,
// used by the test suite
func collectTokens(input string) (tokens []token) {
lexer := lex(input)
for lexer.nextToken() {
tokens = append(tokens, lexer.token)
}
return
}
// nextToken progresses the lexer by a single token (or to eof). it returns
// true while the lexer is running. this is called by the parser in order to
// read a new l.token value
func (l *lexer) nextToken() bool {
if l.lexNext != nil {
l.lexNext()
if l.token.Typ == typeNone {
return l.nextToken()
}
return true
}
return false
}
// mkToken produces a token of the provided type and value, initialised with
// the current line adnd pos values
func (l *lexer) mkToken(typ tokenType, val string) token {
return token{Typ: typ, Val: val, Line: l.line, Pos: l.pos}
}
// resetToken sets the current token to an empty token
func (l *lexer) resetToken() {
l.token = token{}
l.continuousNewline = false
l.ctx = 0
}
// next progresses the lexer by a single character
func (l *lexer) next() {
if l.pos >= len(l.input) || len(l.input) == 0 {
l.char = eof
return
}
char, byteWidth := utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += byteWidth
if char == charNewline {
l.line++
l.skippedSpace = 0
}
l.char = char
l.addToTag()
l.skipSpace()
}
// backup is used to reverse the action of `l.next`
func (l *lexer) backup() {
if l.pos == 0 {
l.char = void
return
}
if l.char == charNewline {
l.line--
}
char, byteWidth := utf8.DecodeLastRuneInString(l.input[:l.pos])
l.pos -= byteWidth
l.char = char
}
func (l *lexer) nextN(n int) {
for range n {
l.next()
}
}
func (l *lexer) backupN(n int) {
for range n {
l.backup()
}
}
// skipSpace checks that the current and proceeding characters are both
// whitespace. if they are, the lexer progresses to the next character. if a
// newline has been skipped at any point, the final whitespace character is
// replaced in the input with a newline. the result is that any combination of
// whitespace which includes a newline is reduced to a single newline character
func (l *lexer) skipSpace() {
if unicode.IsSpace(l.char) && unicode.IsSpace(l.peek()) {
l.skippedSpace++
if l.char == charNewline {
l.skippedNewlines++
}
l.next()
return
}
if l.skippedNewlines > 0 && unicode.IsSpace(l.char) {
if l.char == charNewline {
l.skippedNewlines++
}
l.input = l.input[:l.pos-1] + "\n" + l.input[l.pos:]
l.char = charNewline
return
}
l.skippedNewlines = 0
}
// addToTag stores the currently lexing tag in `l.tag`. a "tag" is a group of
// a-z characters, typically found before a `charOpeningSquare`
func (l *lexer) addToTag() {
if l.token.Typ != typeText {
return
}
if unicode.IsLetter(l.char) {
l.tag += string(l.char)
return
}
if unicode.IsSpace(l.char) {
l.tag = ""
}
}
// peek reads the following rune in the input but does not progress the lexer
func (l *lexer) peek() rune {
if l.pos == len(l.input) {
return eof
}
char, _ := utf8.DecodeRuneInString(l.input[l.pos:])
return char
}
// peekBehind reads the previous rune in the input but does not backtrack
func (l *lexer) peekBehind() rune {
if l.pos == 0 {
return void
}
if l.pos == len(l.input) {
char, _ := utf8.DecodeLastRuneInString(l.input)
return char
}
char, _ := utf8.DecodeLastRuneInString(l.input[:l.pos-1])
return char
}
func (l *lexer) peekNextNonSpace() rune {
if l.pos == len(l.input) {
return eof
}
char, _ := utf8.DecodeRuneInString(strings.TrimSpace(l.input[l.pos:]))
return char
}
func (l *lexer) addToToken(c rune) {
l.token.Val += string(c)
}
func (l *lexer) truncateToken(n int) {
l.token.Val = l.token.Val[:len(l.token.Val)-n]
}
func (l *lexer) trimTrailingSpace() {
if len(l.token.Val) == 0 {
return
}
if unicode.IsSpace(l.peekBehind()) {
l.token.Val = l.token.Val[:len(l.token.Val)-1]
}
}
func (l *lexer) isHeadingChar() bool {
if l.char == charDot || l.char == charColon {
return true
}
return false
}
// lexGlobal is the starting state of the lexer
func (l *lexer) lexGlobal() {
l.resetToken()
l.next()
if l.char == eof {
l.token = l.mkToken(typeEOF, "")
l.lexNext = nil
return
}
if unicode.IsSpace(l.char) {
l.lexNext = l.lexGlobal
return
}
if l.char == charBackslash {
l.backup()
l.lexNext = l.lexText
return
}
if l.isHeadingChar() {
l.backup()
l.lexNext = l.lexHeading
return
}
if l.char == charHyphen {
l.backup()
l.lexNext = l.lexHypen
return
}
l.backup()
l.continuousNewline = true
l.lexNext = l.lexText
}
// lexText is used to parse standard text
func (l *lexer) lexText() {
l.token = l.mkToken(typeText, "")
defer func() {
if l.token.Val == "" {
l.token.Typ = typeNone
}
}()
for {
l.next()
if l.char == eof {
l.trimTrailingSpace()
l.lexNext = l.lexGlobal
return
}
if l.char == charBackslash && l.peek() != charBackslash {
l.tag = ""
continue
}
if l.peekBehind() == charBackslash {
l.addToToken(l.char)
continue
}
if l.char == charNewline && l.ctx == ctxList {
if l.peekNextNonSpace() == charHyphen {
l.lexNext = l.lexHypen
return
}
l.lexNext = l.lexTerminator
return
}
if l.char == charNewline && (!l.continuousNewline || l.skippedNewlines >= 2) {
l.lexNext = l.lexTerminator
return
}
if l.char == charNewline && l.continuousNewline {
l.addToToken(' ')
continue
}
if l.char == charOpeningSquare {
if len(l.tag) == 0 {
l.trimTrailingSpace()
l.backup()
l.lexNext = l.lexOpeningSquare
return
}
l.backupN(len(l.tag))
l.truncateToken(len(l.tag))
l.trimTrailingSpace()
l.backup()
l.lexNext = l.lexTag
return
}
if l.char == charClosingSquare && l.peekBehind() != charBackslash {
l.trimTrailingSpace()
l.backup()
l.lexNext = l.lexClosingSquare
return
}
if l.token.Val == "" && unicode.IsSpace(l.char) {
l.token = l.mkToken(typeText, "")
continue
}
l.addToToken(l.char)
}
}
// lexHeading lexes the heading symbols and returns to `lexText`
func (l *lexer) lexHeading() {
l.token = l.mkToken(typeHeading, "")
for {
l.next()
if l.char == eof {
l.lexNext = l.lexGlobal
return
}
if !l.isHeadingChar() || len(l.token.Val) == 3 {
l.backup()
l.lexNext = l.lexText
return
}
l.token.Val += string(l.char)
}
}
// lexTerminator is intended to produce a terminator token and return to `lexGlobal`
func (l *lexer) lexTerminator() {
l.backup()
l.token = l.mkToken(typeTerminator, string(charNewline))
l.next()
l.lexNext = l.lexGlobal
}
func (l *lexer) lexTag() {
l.token = l.mkToken(typeTag, l.tag)
l.nextN(len(l.tag))
l.tag = ""
l.lexNext = l.lexOpeningSquare
}
func (l *lexer) lexOpeningSquare() {
l.token = l.mkToken(typeOpeningSquare, string(charOpeningSquare))
l.next()
l.lexNext = l.lexText
}
func (l *lexer) lexClosingSquare() {
l.token = l.mkToken(typeClosingSquare, string(charClosingSquare))
l.next()
l.lexNext = l.lexText
}
func (l *lexer) lexHypen() {
l.token = l.mkToken(typeBulletpoint, "-")
l.token.indent = l.skippedSpace
l.next()
if unicode.IsSpace(l.char) {
l.next()
}
l.ctx = ctxList
l.lexNext = l.lexText
}