-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
151 lines (134 loc) · 2.48 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
package parser
import (
"fmt"
"unicode"
"golang.org/x/text/unicode/rangetable"
)
const eof rune = -1
type tokType int
const (
tokEOF tokType = iota
tokSpoiler
tokPunct
tokWhitespace
tokWord
tokBacktick
tokColon
tokRAngle
tokAt
tokRSlash
tokEscapeSeq
)
var tokNames = map[tokType]string{
tokEOF: "EOF",
tokSpoiler: "Spoiler",
tokPunct: "Punct",
tokWhitespace: "Whitespace",
tokWord: "Word",
tokBacktick: "Backtick",
tokColon: "Colon",
tokRAngle: "RAngle",
tokAt: "At",
tokRSlash: "RSlash",
tokEscapeSeq: "EscapeSeq",
}
func (i tokType) String() string {
return tokNames[i]
}
type token struct {
typ tokType
pos int
val []rune
}
func (i token) String() string {
return fmt.Sprintf("(%s %d %s)", i.typ, i.pos, string(i.val))
}
func NewLexer(input string) lexer {
return lexer{
input: []rune(input),
pos: -1,
}
}
type lexer struct {
input []rune
start, pos int
}
func (l *lexer) next() rune {
l.pos++
if l.pos < len(l.input) {
return l.input[l.pos]
}
return eof
}
func (l *lexer) backup() {
l.pos--
}
func (l *lexer) accept(test func(r rune) bool) bool {
if test(l.next()) {
return true
}
l.backup()
return false
}
func (l *lexer) emit(t tokType) (tok token) {
tok = token{
typ: t,
pos: l.start,
val: l.input[l.start : l.pos+1],
}
l.start = l.pos + 1
return
}
var nonWord = rangetable.Merge(
unicode.Dash,
unicode.Hyphen,
unicode.Other_Math,
unicode.Pattern_Syntax,
unicode.Pattern_White_Space,
unicode.Quotation_Mark,
unicode.Sentence_Terminal,
unicode.Terminal_Punctuation,
unicode.White_Space,
)
func (l *lexer) Next() token {
r := l.next()
switch r {
case eof:
l.backup()
return l.emit(tokEOF)
case '`':
return l.emit(tokBacktick)
case ':':
return l.emit(tokColon)
case '>':
return l.emit(tokRAngle)
case '@':
return l.emit(tokAt)
case '/':
return l.emit(tokRSlash)
case '\\':
if l.accept(func(r rune) bool { return r != eof }) {
return l.emit(tokEscapeSeq)
} else {
return l.emit(tokPunct)
}
case '|':
if l.accept(func(r rune) bool { return r == '|' }) {
return l.emit(tokSpoiler)
} else {
return l.emit(tokPunct)
}
default:
if unicode.IsSpace(r) {
for l.accept(func(r rune) bool { return unicode.IsSpace(r) }) {
}
return l.emit(tokWhitespace)
} else if unicode.Is(nonWord, r) {
return l.emit(tokPunct)
} else {
for l.accept(func(r rune) bool { return r != eof && !unicode.Is(nonWord, r) }) {
}
return l.emit(tokWord)
}
}
}