forked from admpub/confl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_check.go
82 lines (72 loc) · 2.11 KB
/
type_check.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
package confl
// represents any Go type that corresponds to a internal type.
type confType interface {
typeString() string
}
// typeEqual accepts any two types and returns true if they are equal.
func typeEqual(t1, t2 confType) bool {
if t1 == nil || t2 == nil {
return false
}
return t1.typeString() == t2.typeString()
}
func typeIsHash(t confType) bool {
return typeEqual(t, confHash) || typeEqual(t, confArrayHash)
}
type confBaseType string
func (btype confBaseType) typeString() string {
return string(btype)
}
func (btype confBaseType) String() string {
return btype.typeString()
}
var (
confInteger confBaseType = "Integer"
confFloat confBaseType = "Float"
confDatetime confBaseType = "Datetime"
confString confBaseType = "String"
confBool confBaseType = "Bool"
confArray confBaseType = "Array"
confHash confBaseType = "Hash"
confArrayHash confBaseType = "ArrayHash"
)
// typeOfPrimitive returns a confType of any primitive value in conf.
// Primitive values are: Integer, Float, Datetime, String and Bool.
//
// Passing a lexer item other than the following will cause a BUG message
// to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
func (p *parser) typeOfPrimitive(lexItem item) confType {
switch lexItem.typ {
case itemInteger:
return confInteger
case itemFloat:
return confFloat
case itemDatetime:
return confDatetime
case itemString:
return confString
case itemBool:
return confBool
}
p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
panic("unreachable")
}
// typeOfArray returns a confType for an array given a list of types of its
// values.
//
// In the current spec, if an array is homogeneous, then its type is always
// "Array". If the array is not homogeneous, an error is generated.
func (p *parser) typeOfArray(types []confType) confType {
// Empty arrays are cool.
if len(types) == 0 {
return confArray
}
theType := types[0]
for _, t := range types[1:] {
if !typeEqual(theType, t) {
p.panicf("Array contains values of type '%s' and '%s', but arrays "+
"must be homogeneous.", theType, t)
}
}
return confArray
}