This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runes.go
219 lines (184 loc) · 4.32 KB
/
runes.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
// Copyright Noel Cower 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
package leks
import (
"io"
"sort"
"unicode"
"unicode/utf8"
)
// Writing runes to writers (typically only a bytes.Buffer)
type runeWriter interface {
WriteRune(rune) (int, error)
}
func writeRune(w io.Writer, r rune) (int, error) {
if w, ok := w.(runeWriter); ok {
return w.WriteRune(r)
}
var encoded [4]byte
subset := encoded[:utf8.EncodeRune(encoded[:], r)]
if len(subset) == 0 {
return 0, nil
}
return w.Write(subset)
}
// Rune sorting
type runeSort []rune
func (r runeSort) Len() int {
return len(r)
}
func (r runeSort) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r runeSort) Less(i, j int) bool {
return r[i] < r[j]
}
// Rune tests (i.e., matching runes for bits of the lexer)
func runeNeverMatches(rune) bool { return false }
func isWhitespace(r rune) bool { return r != '\n' && unicode.IsSpace(r) }
func isNewline(r rune) bool { return r == '\n' }
func isPeriod(r rune) bool { return r == '.' }
func isExp(r rune) bool { return r == 'e' || r == 'E' }
func isSign(r rune) bool { return r == '+' || r == '-' }
func isBinSep(r rune) bool { return r == 'b' || r == 'B' }
func isHexSep(r rune) bool { return r == 'x' || r == 'X' }
// Only permit ASCII subset of numbers because I don't want to try to read anything else.
var isDec = runeInRange('0', '9')
var isAlpha = unicode.IsLetter
var isAlnum = runeMatchesSome(
unicode.IsLetter,
isDec,
)
var isWordInitial = runeMatchesSome(
isAlpha,
runeInSeq([]rune("~_@$")),
)
var isWord = runeMatchesSome(
isAlnum,
runeInSeq([]rune("~_@$-<>=")),
)
var isWordSentinel = runeInSeq([]rune("?!"))
var isBin = runeInRange('0', '1')
var isOct = runeInRange('0', '7')
var isHex = runeMatchesSome(
isDec,
runeInRange('a', 'f'),
runeInRange('A', 'F'),
)
var isSingleSym = runeInSeq([]rune("\n#@$()[]{}\\,;'"))
var isDoubleSym = runeInSeq([]rune("!&*+-:<=>?|/%^~"))
var symMap = map[rune]TokenType{
'\'': TQuote,
'.': TDot,
'#': THash,
'%': TPercent,
'(': TParenOpen,
')': TParenClose,
'[': TBracketOpen,
']': TBracketClose,
'{': TCurlOpen,
'}': TCurlClose,
'^': TCaret,
'\\': TBackslash,
'/': TSlash,
',': TComma,
';': TSemicolon,
'?': TQuestion,
'!': TBang,
'>': TGreaterThan,
'<': TLessThan,
'=': TEquals,
'|': TPipe,
'&': TAmp,
':': TColon,
'-': TMinus,
'+': TPlus,
'*': TMul,
'\n': TNewline,
}
func runeMatchesSome(fns ...func(rune) bool) func(rune) bool {
if len(fns) == 0 {
return runeNeverMatches
}
fns = append([]func(rune) bool(nil), fns...)
return func(r rune) bool {
for _, fn := range fns {
if fn(r) {
return true
}
}
return false
}
}
func runesMatchSeq(seq string) func(rune) bool {
if len(seq) == 0 {
return runeNeverMatches
}
runes := []rune(seq)
if len(runes) == 1 {
match := runes[0]
return func(r rune) bool { return r == match }
}
window := make([]rune, len(runes))
wh := 0
return func(r rune) bool {
// Move the window ahead
copy(window, window[1:])
window[len(window)-1] = r
if wh != len(window) {
wh++
return false
}
for i := range window {
if window[i] != runes[i] {
return false
}
}
return true
}
}
// runeInRange returns a rune test that tests whether the input rune is in the range of min and max, inclusive.
func runeInRange(min, max rune) func(rune) bool {
if max < min {
min, max = max, min
}
return func(r rune) bool {
return r >= min && r <= max
}
}
func runeInSeq(runes []rune) func(rune) bool {
if len(runes) == 0 {
return runeNeverMatches
}
runes = append([]rune(nil), runes...)
sort.Sort(runeSort(runes))
rangeTest := runeInRange(runes[0], runes[len(runes)-1])
return func(r rune) bool {
if !rangeTest(r) {
return false
}
n := len(runes)
i := sort.Search(n, func(i int) bool { return runes[i] >= r })
return n != i && runes[i] == r
}
}
func escapeRuneTest(escaped bool, test func(rune) bool) func(rune) bool {
isEscaped := false
return func(r rune) bool {
if isEscaped {
isEscaped = false
return escaped
}
if r == '\\' {
isEscaped = true
return escaped
}
return test(r)
}
}
func negateRuneTest(test func(rune) bool) func(rune) bool {
return func(r rune) bool {
return !test(r)
}
}