/
type.go
143 lines (126 loc) · 3.65 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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parse
import (
"fmt"
)
// TypeDef represents a user-defined named type.
type TypeDef struct {
NamePos // name assigned by the user, pos and doc
Type Type // the underlying type of the type definition.
}
// Type is an interface representing symbolic occurrences of types in VDL files.
type Type interface {
// String returns a human-readable description of the type.
String() string
// Kind returns a short human-readable string describing the kind of type.
Kind() string
// Pos returns the position of the first character in the type.
Pos() Pos
}
// TypeNamed captures named references to other types. Both built-in primitives
// and user-defined named types use this representation.
type TypeNamed struct {
Name string
P Pos
}
// TypeEnum represents enum types.
type TypeEnum struct {
Labels []NamePos
P Pos
}
// TypeArray represents array types.
type TypeArray struct {
Len int
Elem Type
P Pos
}
// TypeList represents list types.
type TypeList struct {
Elem Type
P Pos
}
// TypeSet represents set types.
type TypeSet struct {
Key Type
P Pos
}
// TypeMap represents map types.
type TypeMap struct {
Key Type
Elem Type
P Pos
}
// TypeStruct represents struct types.
type TypeStruct struct {
Fields []*Field
P Pos
}
// TypeUnion represents union types.
type TypeUnion struct {
Fields []*Field
P Pos
}
// TypeOptional represents optional types.
type TypeOptional struct {
Base Type
P Pos
}
func (t *TypeNamed) Pos() Pos { return t.P }
func (t *TypeEnum) Pos() Pos { return t.P }
func (t *TypeArray) Pos() Pos { return t.P }
func (t *TypeList) Pos() Pos { return t.P }
func (t *TypeSet) Pos() Pos { return t.P }
func (t *TypeMap) Pos() Pos { return t.P }
func (t *TypeStruct) Pos() Pos { return t.P }
func (t *TypeUnion) Pos() Pos { return t.P }
func (t *TypeOptional) Pos() Pos { return t.P }
func (t *TypeNamed) Kind() string { return "named" }
func (t *TypeEnum) Kind() string { return "enum" }
func (t *TypeArray) Kind() string { return "array" }
func (t *TypeList) Kind() string { return "list" }
func (t *TypeSet) Kind() string { return "set" }
func (t *TypeMap) Kind() string { return "map" }
func (t *TypeStruct) Kind() string { return "struct" }
func (t *TypeUnion) Kind() string { return "union" }
func (t *TypeOptional) Kind() string { return "optional" }
func (t *TypeNamed) String() string { return t.Name }
func (t *TypeEnum) String() string {
result := "enum{"
for index, label := range t.Labels {
if index > 0 {
result += ";"
}
result += label.Name
}
return result + "}"
}
func (t *TypeArray) String() string { return fmt.Sprintf("[%v]%v", t.Len, t.Elem) }
func (t *TypeList) String() string { return fmt.Sprintf("[]%v", t.Elem) }
func (t *TypeSet) String() string { return fmt.Sprintf("set[%v]", t.Key) }
func (t *TypeMap) String() string { return fmt.Sprintf("map[%v]%v", t.Key, t.Elem) }
func (t *TypeStruct) String() string {
result := "struct{"
for index, field := range t.Fields {
if index > 0 {
result += ";"
}
result += field.Name + " " + field.Type.String()
}
return result + "}"
}
func (t *TypeUnion) String() string {
result := "union{"
for index, field := range t.Fields {
if index > 0 {
result += ";"
}
result += field.Name + " " + field.Type.String()
}
return result + "}"
}
func (t *TypeOptional) String() string { return fmt.Sprintf("?%v", t.Base) }
func (t *TypeDef) String() string {
return fmt.Sprintf("(%v %v %v)", t.Pos, t.Name, t.Type)
}