-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.go
265 lines (237 loc) · 7.08 KB
/
tokens.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
package token
import (
"errors"
"strings"
"unicode"
"github.com/ionous/tell/charm"
"github.com/ionous/tell/charmed"
"github.com/ionous/tell/runes"
)
// tbd: maybe a channel instead?
type Notifier interface {
Decoded(Pos, Type, any) error
}
type Tokenizer struct {
Notifier Notifier
// configure the upcoming Decode to produce only floating point numbers.
// otherwise it will produce int for integers, and unit for hex specifications.
UseFloats bool // controls number decoding
}
// return a state to parse a stream of runes and notify as they are detected.
func (cfg Tokenizer) Decode() charm.State {
n := tokenizer{Tokenizer: cfg}
return charm.Parallel("tokenizer", n.decode(false), charmed.DecodePos(&n.curr.Y, &n.curr.X))
}
func NewTokenizer(n Notifier) charm.State {
cfg := Tokenizer{Notifier: n}
return cfg.Decode()
}
type tokenizer struct {
Tokenizer
curr, start Pos
}
func (n *tokenizer) decode(afterIndent bool) charm.State {
return charm.Step(n.whitespace(afterIndent), n.tokenize())
}
// tell Notifier of the new token/value pair
// and then process the next rune (q)
// ( combining the two simplifies error handling in some cases )
func (n *tokenizer) notifyRune(q rune, t Type, v any) (ret charm.State) {
if e := n.Notifier.Decoded(n.start, t, v); e != nil {
ret = charm.Error(e)
} else {
ret = send(n.decode(true), q)
}
return
}
// eat whitespace between tokens;
// previously, would error if it didnt detect whitespace between tokens
// however that doesnt work well for arrays. ex: `5,`
func (n *tokenizer) whitespace(afterIndent bool) charm.State {
var spaces int
return charm.Self("whitespace", func(self charm.State, q rune) (ret charm.State) {
switch q {
case runes.Space:
spaces++
ret = self
case runes.Newline:
spaces = 0
afterIndent = false
ret = self
case runes.Eof:
ret = charm.Finished()
}
return
})
}
func (n *tokenizer) tokenize() charm.State {
return charm.Statement("tokenize", func(q rune) (ret charm.State) {
n.start = n.curr
switch q {
case runes.HTab:
ret = charm.Error(errors.New("tabs are invalid whitespace"))
case runes.Hash:
next := n.commentDecoder()
ret = send(next, q)
case runes.QuoteDouble:
ret = n.decodeQuote(charmed.DecodeDouble)
case runes.QuoteSingle:
ret = n.decodeQuote(charmed.DecodeSingle)
case runes.QuoteRaw:
ret = n.decodeQuote(charmed.DecodeRaw)
case runes.QuotePipe:
ret = n.decodeQuote(charmed.DecodePipe)
case runes.Dash: // negative numbers or sequences
ret = n.dashDecoding()
case runes.ArrayOpen, runes.ArrayClose, runes.ArraySeparator:
if e := n.Notifier.Decoded(n.start, Array, q); e != nil {
ret = charm.Error(e)
} else {
ret = n.decode(true)
}
default:
switch {
case runes.IsNumber(q) || q == '+': // a leading negative gets handled by dashDecoding.
next := n.numDecoder()
ret = send(next, q)
case unicode.IsLetter(q): // maps and bools
next := n.wordDecoder()
ret = send(next, q)
}
}
return
})
}
type quoteParser func(*strings.Builder) charm.State
func (n *tokenizer) decodeQuote(which quoteParser) charm.State {
var b strings.Builder
return charm.Step(which(&b), charm.Statement("string", func(q rune) charm.State {
return n.notifyRune(q, String, b.String())
}))
}
// if the passed rune might be start a bool value
// for example, `trouble:` would match `true` temporarily
// and `false:` would match `false` until the colon.
func (n *tokenizer) wordDecoder() charm.State {
return charm.Statement("wordDecoder", func(q rune) (ret charm.State) {
var b boolValue
if q == 't' {
b = boolTrue
} else if q == 'f' {
b = boolFalse
}
if b == boolInvalid {
ret = n.decodeSignature()
} else {
var sig Signature
sign := sig.Decoder()
boolean := charmed.StringMatch(b.String())
ret = charm.Self("parallel", func(self charm.State, q rune) (ret charm.State) {
ret = self
// sign succeeds and turns nil on whitespace after a colon;
// boolean on the rune after its last letter.
if sign = sign.NewRune(q); sign == nil {
ret = n.notifyRune(q, Key, sig.String())
} else if boolean = boolean.NewRune(q); boolean == nil {
// boolean shouldnt match: ex. "falsey"
if !runes.IsWhitespace(q) {
boolean = charm.Error(errors.New("not a boolean"))
} else {
// note: this means a key "true true:" will be interpreted as
// a bool (true) followed by a key (true:)
ret = n.notifyRune(q, Bool, b == boolTrue)
}
} else if terminal(sign) {
// sign is mostly superset of bool; (except for the eof/eol cases)
// if it dies and boolean didnt just succeed; they're both dead.
ret = charm.Error(errWordy)
}
return
})
}
return send(ret, q)
})
}
// negative numbers or sequences
func (n *tokenizer) decodeSignature() charm.State {
var sig Signature
sign := sig.Decoder() // use self, instead of step to customize the error response
return charm.Self("signature", func(self charm.State, q rune) (ret charm.State) {
ret = self // provisionally
if sign = sign.NewRune(q); sign == nil {
ret = n.notifyRune(q, Key, sig.String())
} else if terminal(sign) {
ret = charm.Error(errWordy)
}
return
})
}
// negative numbers or sequences
func (n *tokenizer) dashDecoding() charm.State {
return charm.Statement("dashing", func(q rune) (ret charm.State) {
if runes.IsWhitespace(q) { // a space indicates a sequence `- 5`
ret = n.notifyRune(q, Key, "") // a blank key means a dash
} else { // no space indicates a number ( or an error ) `-5`
next := n.numDecoder()
ret = send(next, runes.Dash, q)
}
return
})
}
func (n *tokenizer) commentDecoder() charm.State {
var b strings.Builder
return charm.Self("comments", func(self charm.State, q rune) (ret charm.State) {
switch q {
default:
b.WriteRune(q)
ret = self
case runes.Newline, runes.Eof:
ret = n.notifyRune(q, Comment, b.String())
}
return
})
}
// fix? returns float64 because json does
// could also return int64 when its int like
func (n *tokenizer) numDecoder() charm.State {
var d charmed.NumParser
return charm.Step(d.Decode(), charm.Statement("numDecoder", func(q rune) (ret charm.State) {
if n.UseFloats {
if v, e := d.GetFloat(); e != nil {
ret = charm.Error(e)
} else {
ret = n.notifyRune(q, Number, v)
}
} else {
if v, e := d.GetNumber(); e != nil {
ret = charm.Error(e)
} else {
ret = n.notifyRune(q, Number, v)
}
}
return
}))
}
// a tri-boolean: 0 is invalid, not false.
type boolValue int
//go:generate stringer -type=boolValue -linecomment
const (
boolInvalid boolValue = iota
boolFalse // false
boolTrue // true
)
var errWordy = errors.New("couldn't read words. strings should be quoted, booleans should be 'true' or 'false', and map keys should start with a letter and end with a colon")
// is the next state an error?
func terminal(next charm.State) (okay bool) {
_, okay = next.(charm.Terminal)
return
}
// send one or more runes to the next state
func send(next charm.State, qs ...rune) charm.State {
for _, q := range qs {
if next = next.NewRune(q); next == nil {
break
}
}
return next
}