-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
53 lines (46 loc) 路 1.2 KB
/
options.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
package reader
import (
"github.com/spy16/slurp/builtin"
"github.com/spy16/slurp/core"
)
var defaultSymTable = map[string]core.Any{
"nil": builtin.Nil{},
"true": builtin.Bool(true),
"false": builtin.Bool(false),
}
// Option values can be used with New() to configure the reader during init.
type Option func(*Reader)
// WithNumReader sets the number reader macro to be used by the Reader. Uses
// the default number reader if nil.
func WithNumReader(m Macro) Option {
if m == nil {
m = readNumber
}
return func(rd *Reader) {
rd.numReader = m
}
}
// WithSymbolReader sets the symbol reader macro to be used by the Reader.
// Builds a slurp.Symbol if nil.
func WithSymbolReader(m Macro) Option {
if m == nil {
return WithBuiltinSymbolReader(defaultSymTable)
}
return func(rd *Reader) {
rd.symReader = m
}
}
// WithBuiltinSymbolReader configures the default symbol reader with given
// symbol table.
func WithBuiltinSymbolReader(symTable map[string]core.Any) Option {
m := symbolReader(symTable)
return func(rd *Reader) {
rd.symReader = m
}
}
func withDefaults(opt []Option) []Option {
return append([]Option{
WithNumReader(nil),
WithBuiltinSymbolReader(defaultSymTable),
}, opt...)
}