-
Notifications
You must be signed in to change notification settings - Fork 66
/
token.go
197 lines (174 loc) · 4.24 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Code generated by gocc; DO NOT EDIT.
package token
import (
"bytes"
"fmt"
"strconv"
"unicode/utf8"
)
type Token struct {
Type
Lit []byte
Pos
}
type Type int
const (
INVALID Type = iota
EOF
)
type Pos struct {
Offset int
Line int
Column int
Context Context
}
func (p Pos) String() string {
// If the context provides a filename, provide a human-readable File:Line:Column representation.
switch src := p.Context.(type) {
case Sourcer:
return fmt.Sprintf("%s:%d:%d", src.Source(), p.Line, p.Column)
default:
return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
}
}
type TokenMap struct {
typeMap []string
idMap map[string]Type
}
func (m TokenMap) Id(tok Type) string {
if int(tok) < len(m.typeMap) {
return m.typeMap[tok]
}
return "unknown"
}
func (m TokenMap) Type(tok string) Type {
if typ, exist := m.idMap[tok]; exist {
return typ
}
return INVALID
}
func (m TokenMap) TokenString(tok *Token) string {
return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
}
func (m TokenMap) StringType(typ Type) string {
return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
}
// Equals returns returns true if the token Type and Lit are matches.
func (t *Token) Equals(rhs interface{}) bool {
switch rhsT := rhs.(type) {
case *Token:
return t == rhsT || (t.Type == rhsT.Type && bytes.Equal(t.Lit, rhsT.Lit))
default:
return false
}
}
// CharLiteralValue returns the string value of the char literal.
func (t *Token) CharLiteralValue() string {
return string(t.Lit[1 : len(t.Lit)-1])
}
// Float32Value returns the float32 value of the token or an error if the token literal does not
// denote a valid float32.
func (t *Token) Float32Value() (float32, error) {
if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil {
return 0, err
} else {
return float32(v), nil
}
}
// Float64Value returns the float64 value of the token or an error if the token literal does not
// denote a valid float64.
func (t *Token) Float64Value() (float64, error) {
return strconv.ParseFloat(string(t.Lit), 64)
}
// IDValue returns the string representation of an identifier token.
func (t *Token) IDValue() string {
return string(t.Lit)
}
// Int32Value returns the int32 value of the token or an error if the token literal does not
// denote a valid float64.
func (t *Token) Int32Value() (int32, error) {
if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil {
return 0, err
} else {
return int32(v), nil
}
}
// Int64Value returns the int64 value of the token or an error if the token literal does not
// denote a valid float64.
func (t *Token) Int64Value() (int64, error) {
return strconv.ParseInt(string(t.Lit), 10, 64)
}
// UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if
// the token literal contains an invalid rune.
func (t *Token) UTF8Rune() (rune, error) {
r, _ := utf8.DecodeRune(t.Lit)
if r == utf8.RuneError {
err := fmt.Errorf("Invalid rune")
return r, err
}
return r, nil
}
// StringValue returns the string value of the token literal.
func (t *Token) StringValue() string {
return string(t.Lit[1 : len(t.Lit)-1])
}
var TokMap = TokenMap{
typeMap: []string{
"INVALID",
"$",
"ws",
"andOp",
"orOp",
"not",
"minus",
"dot",
"lparen",
"rparen",
"text",
"string",
"or",
"and",
"less_equals",
"less_than",
"greater_equals",
"greater_than",
"not_equals",
"equals",
"has",
"matches_regexp",
"not_matches_regexp",
"backslash",
"comma",
"plus",
"tilde",
},
idMap: map[string]Type{
"INVALID": 0,
"$": 1,
"ws": 2,
"andOp": 3,
"orOp": 4,
"not": 5,
"minus": 6,
"dot": 7,
"lparen": 8,
"rparen": 9,
"text": 10,
"string": 11,
"or": 12,
"and": 13,
"less_equals": 14,
"less_than": 15,
"greater_equals": 16,
"greater_than": 17,
"not_equals": 18,
"equals": 19,
"has": 20,
"matches_regexp": 21,
"not_matches_regexp": 22,
"backslash": 23,
"comma": 24,
"plus": 25,
"tilde": 26,
},
}