-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.go
69 lines (58 loc) · 1.39 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
package token
import (
"fmt"
"github.com/pkg/errors"
)
type Type uint8
const (
EOF Type = iota // 0
Space
Comment
SpecialSymbol
Identifier
QualifiedIdentifier
ReservedWord
NumeralInt
NumeralReal
Label
CharacterString
)
var TypeNames = map[Type]string{
EOF: "EOF",
Space: "space",
Comment: "comment",
SpecialSymbol: "special symbol",
Identifier: "identifier",
QualifiedIdentifier: "qualified identifier",
ReservedWord: "reserved word",
NumeralInt: "int",
NumeralReal: "real",
Label: "label",
CharacterString: "character string",
}
func (t Type) String() string {
return TypeNames[t]
}
// As Predicator
func (typ Type) Name() string {
return TypeNames[typ]
}
func (typ Type) Predicate(t *Token) bool {
return t.Type == typ
}
func (typ Type) HasText(s string) Predicator {
return &PredicatorImpl{
name: fmt.Sprintf("%s has %q", typ.String(), s),
predicate: func(t *Token) bool { return t.Type == typ && t.Value() == s },
}
}
// kw must be upper case
func (typ Type) HasKeyword(kw string) Predicator {
if typ == ReservedWord && !isReservedWord(kw) {
panic(errors.Errorf("%q is not a reserved word", kw))
}
return &PredicatorImpl{
name: fmt.Sprintf("%s has %q", typ.String(), kw),
predicate: func(t *Token) bool { return t.Type == typ && t.Value() == kw },
}
}