forked from Dwarfartisan/gisp
/
type.go
154 lines (141 loc) · 3.9 KB
/
type.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
package gisp
import (
p "github.com/Dwarfartisan/goparsec"
"reflect"
)
type Type struct {
reflect.Type
option bool
}
func (typ Type) String() string {
str := typ.Type.String()
if typ.option {
return str + "?"
} else {
return str
}
}
func (typ Type) Option() bool {
return typ.option
}
func stop(st p.ParseState) (interface{}, error) {
pos := st.Pos()
defer st.SeekTo(pos)
r, err := p.Choice(
p.Try(p.Space),
p.Try(p.NewLine),
p.Try(p.OneOf(":.()[]{}?")),
p.Try(p.Eof),
)(st)
if err != nil {
return nil, err
}
return r, nil
}
func stopWord(x interface{}) p.Parser {
return p.Bind_(stop, p.Return(x))
}
func typeName(word string) p.Parser {
return p.Bind(p.String(word), stopWord)
}
var anyType = p.Bind(p.Bind(p.Many1(p.Either(p.Try(p.Digit), p.Letter)), stopWord), p.ReturnString)
func SliceTypeParserExt(env Env) p.Parser {
return func(st p.ParseState) (interface{}, error) {
t, err := p.Bind_(p.String("[]"), ExtTypeParser(env))(st)
if err != nil {
return nil, err
}
return reflect.SliceOf(t.(Type).Type), nil
}
}
func MapTypeParserExt(env Env) p.Parser {
return func(st p.ParseState) (interface{}, error) {
key, err := p.Between(p.String("map["), p.Rune(']'), ExtTypeParser(env))(st)
if err != nil {
return nil, err
}
value, err := ExtTypeParser(env)(st)
if err != nil {
return nil, err
}
return reflect.MapOf(key.(Type).Type, value.(Type).Type), nil
}
}
func MapTypeParser(st p.ParseState) (interface{}, error) {
key, err := p.Between(p.String("map["), p.Rune(']'), TypeParser)(st)
if err != nil {
return nil, err
}
value, err := TypeParser(st)
if err != nil {
return nil, err
}
return reflect.MapOf(key.(Type).Type, value.(Type).Type), nil
}
func ExtTypeParser(env Env) p.Parser {
return func(st p.ParseState) (interface{}, error) {
_, err := p.String("::")(st)
if err != nil {
return nil, err
}
buildin := p.Choice(
p.Try(p.Bind_(typeName("bool"), p.Return(BOOL))),
p.Try(p.Bind_(typeName("float"), p.Return(FLOAT))),
p.Try(p.Bind_(typeName("int"), p.Return(INT))),
p.Try(p.Bind_(typeName("string"), p.Return(STRING))),
p.Try(p.Bind_(typeName("time"), p.Return(TIME))),
p.Try(p.Bind_(typeName("duration"), p.Return(DURATION))),
p.Try(p.Bind_(typeName("any"), p.Return(ANY))),
p.Try(p.Bind_(typeName("atom"), p.Return(ATOM))),
p.Try(p.Bind_(p.String("list"), p.Return(LIST))),
p.Try(p.Bind_(typeName("quote"), p.Return(QUOTE))),
p.Try(p.Bind_(p.String("dict"), p.Return(DICT))),
p.Try(MapTypeParserExt(env)),
)
ext := func(st p.ParseState) (interface{}, error) {
n, err := anyType(st)
if err != nil {
return nil, err
}
t, ok := env.Lookup(n.(string))
if !ok {
return nil, st.Trap("type %v not found", n)
}
if typ, ok := t.(reflect.Type); ok {
return typ, nil
} else {
return nil, st.Trap("var %v is't a type. It is %v", n, reflect.TypeOf(t))
}
}
t, err := p.Either(buildin, ext)(st)
if err != nil {
return nil, err
}
_, err = p.Try(p.Rune('?'))(st)
option := err == nil
return Type{t.(reflect.Type), option}, nil
}
}
func TypeParser(st p.ParseState) (interface{}, error) {
t, err := p.Bind_(p.String("::"),
p.Choice(
p.Try(p.Bind_(p.String("bool"), p.Return(BOOL))),
p.Try(p.Bind_(p.String("float"), p.Return(FLOAT))),
p.Try(p.Bind_(p.String("int"), p.Return(INT))),
p.Try(p.Bind_(p.String("string"), p.Return(STRING))),
p.Try(p.Bind_(p.String("time"), p.Return(TIME))),
p.Try(p.Bind_(p.String("duration"), p.Return(DURATION))),
p.Try(p.Bind_(p.String("any"), p.Return(ANY))),
p.Try(p.Bind_(p.String("atom"), p.Return(ATOM))),
p.Try(p.Bind_(p.String("list"), p.Return(LIST))),
p.Try(p.Bind_(p.String("quote"), p.Return(QUOTE))),
p.Try(p.Bind_(p.String("dict"), p.Return(DICT))),
MapTypeParser,
))(st)
if err != nil {
return nil, err
}
_, err = p.Try(p.Rune('?'))(st)
option := err == nil
return Type{t.(reflect.Type), option}, nil
}