-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
76 lines (62 loc) · 1.14 KB
/
token.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
// generated by gocc; DO NOT EDIT.
package token
import (
"fmt"
)
type Token struct {
Type
Lit []byte
Pos
}
type Type int
const (
INVALID Type = iota
EOF
)
type Pos struct {
Offset int
Line int
Column int
}
func (this Pos) String() string {
return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", this.Offset, this.Line, this.Column)
}
type TokenMap struct {
typeMap []string
idMap map[string]Type
}
func (this TokenMap) Id(tok Type) string {
if int(tok) < len(this.typeMap) {
return this.typeMap[tok]
}
return "unknown"
}
func (this TokenMap) Type(tok string) Type {
if typ, exist := this.idMap[tok]; exist {
return typ
}
return INVALID
}
func (this TokenMap) TokenString(tok *Token) string {
//TODO: refactor to print pos & token string properly
return fmt.Sprintf("%s(%d,%s)", this.Id(tok.Type), tok.Type, tok.Lit)
}
func (this TokenMap) StringType(typ Type) string {
return fmt.Sprintf("%s(%d)", this.Id(typ), typ)
}
var TokMap = TokenMap{
typeMap: []string{
"INVALID",
"$",
"arg",
"func",
"var",
},
idMap: map[string]Type{
"INVALID": 0,
"$": 1,
"arg": 2,
"func": 3,
"var": 4,
},
}