Skip to content

Commit b36db1f

Browse files
committed
Add support for floating point numbers
1 parent 824fda6 commit b36db1f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

gson.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,16 @@ func (l *lexer) next() token {
120120
for l.cursor < len(l.input) && unicode.IsDigit(rune(l.input[l.cursor])) {
121121
l.chop(1)
122122
}
123+
124+
if l.cursor < len(l.input) && rune(l.input[l.cursor]) == '.' {
125+
l.chop(1)
126+
127+
for l.cursor < len(l.input) && unicode.IsDigit(rune(l.input[l.cursor])) {
128+
l.chop(1)
129+
}
130+
}
123131
t.value = string(l.input[tokenStart:l.cursor])
132+
fmt.Println(t.value)
124133
return t
125134
}
126135

@@ -173,17 +182,17 @@ func (p *parser) parseLiteral(expected string) error {
173182
return nil
174183
}
175184

176-
func (p *parser) parseNumber() (int, error) {
185+
func (p *parser) parseNumber() (float64, error) {
177186
if p.idx >= len(p.tokens) {
178-
return -1, fmt.Errorf("expected string, but got end of input")
187+
return -1, fmt.Errorf("expected number, but got end of input")
179188
}
180189

181190
if p.tokens[p.idx].key != tok_number {
182-
return -1, fmt.Errorf("expected string, but got '%s'", p.tokens[p.idx].value)
191+
return -1, fmt.Errorf("expected number, but got '%s'", p.tokens[p.idx].value)
183192
}
184193
value := p.tokens[p.idx].value
185194
p.consume()
186-
return strconv.Atoi(value)
195+
return strconv.ParseFloat(value, 64)
187196
}
188197

189198
func (p *parser) parseString() (string, error) {
@@ -201,11 +210,11 @@ func (p *parser) parseString() (string, error) {
201210

202211
func (p *parser) parseBool() (bool, error) {
203212
if p.idx >= len(p.tokens) {
204-
return false, fmt.Errorf("expected string, but got end of input")
213+
return false, fmt.Errorf("expected boolean, but got end of input")
205214
}
206215

207216
if p.tokens[p.idx].key != tok_bool {
208-
return false, fmt.Errorf("expected string, but got '%s'", p.tokens[p.idx].value)
217+
return false, fmt.Errorf("expected boolean, but got '%s'", p.tokens[p.idx].value)
209218
}
210219
value := p.tokens[p.idx].value
211220
p.consume()

0 commit comments

Comments
 (0)