...
1
2
3
4
5 package json
6
7 import (
8 "bytes"
9 "fmt"
10 "strconv"
11 )
12
13
14 type Kind uint16
15
16 const (
17 Invalid Kind = (1 << iota) / 2
18 EOF
19 Null
20 Bool
21 Number
22 String
23 Name
24 ObjectOpen
25 ObjectClose
26 ArrayOpen
27 ArrayClose
28
29
30
31 comma
32 )
33
34 func (k Kind) String() string {
35 switch k {
36 case EOF:
37 return "eof"
38 case Null:
39 return "null"
40 case Bool:
41 return "bool"
42 case Number:
43 return "number"
44 case String:
45 return "string"
46 case ObjectOpen:
47 return "{"
48 case ObjectClose:
49 return "}"
50 case Name:
51 return "name"
52 case ArrayOpen:
53 return "["
54 case ArrayClose:
55 return "]"
56 case comma:
57 return ","
58 }
59 return "<invalid>"
60 }
61
62
63
64
65
66
67
68 type Token struct {
69
70 kind Kind
71
72 pos int
73
74
75 raw []byte
76
77 boo bool
78
79 str string
80 }
81
82
83 func (t Token) Kind() Kind {
84 return t.kind
85 }
86
87
88 func (t Token) RawString() string {
89 return string(t.raw)
90 }
91
92
93 func (t Token) Pos() int {
94 return t.pos
95 }
96
97
98 func (t Token) Name() string {
99 if t.kind == Name {
100 return t.str
101 }
102 panic(fmt.Sprintf("Token is not a Name: %v", t.RawString()))
103 }
104
105
106 func (t Token) Bool() bool {
107 if t.kind == Bool {
108 return t.boo
109 }
110 panic(fmt.Sprintf("Token is not a Bool: %v", t.RawString()))
111 }
112
113
114
115 func (t Token) ParsedString() string {
116 if t.kind == String {
117 return t.str
118 }
119 panic(fmt.Sprintf("Token is not a String: %v", t.RawString()))
120 }
121
122
123
124
125
126
127
128
129 func (t Token) Float(bitSize int) (float64, bool) {
130 if t.kind != Number {
131 return 0, false
132 }
133 f, err := strconv.ParseFloat(t.RawString(), bitSize)
134 if err != nil {
135 return 0, false
136 }
137 return f, true
138 }
139
140
141
142
143
144
145 func (t Token) Int(bitSize int) (int64, bool) {
146 s, ok := t.getIntStr()
147 if !ok {
148 return 0, false
149 }
150 n, err := strconv.ParseInt(s, 10, bitSize)
151 if err != nil {
152 return 0, false
153 }
154 return n, true
155 }
156
157
158
159
160
161
162 func (t Token) Uint(bitSize int) (uint64, bool) {
163 s, ok := t.getIntStr()
164 if !ok {
165 return 0, false
166 }
167 n, err := strconv.ParseUint(s, 10, bitSize)
168 if err != nil {
169 return 0, false
170 }
171 return n, true
172 }
173
174 func (t Token) getIntStr() (string, bool) {
175 if t.kind != Number {
176 return "", false
177 }
178 parts, ok := parseNumberParts(t.raw)
179 if !ok {
180 return "", false
181 }
182 return normalizeToIntString(parts)
183 }
184
185
186 func TokenEquals(x, y Token) bool {
187 return x.kind == y.kind &&
188 x.pos == y.pos &&
189 bytes.Equal(x.raw, y.raw) &&
190 x.boo == y.boo &&
191 x.str == y.str
192 }
193
View as plain text