Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package json |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 9 | "fmt" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 10 | "io" |
| 11 | "regexp" |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 12 | "strconv" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 13 | "unicode/utf8" |
| 14 | |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 15 | "github.com/golang/protobuf/v2/internal/errors" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 18 | // call specifies which Decoder method was invoked. |
| 19 | type call uint8 |
| 20 | |
| 21 | const ( |
| 22 | readCall call = iota |
| 23 | peekCall |
| 24 | ) |
| 25 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 26 | // Decoder is a token-based JSON decoder. |
| 27 | type Decoder struct { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 28 | // lastCall is last method called, eiterh readCall or peekCall. |
| 29 | lastCall call |
| 30 | |
| 31 | // value contains the last read value. |
| 32 | value Value |
| 33 | |
| 34 | // err contains the last read error. |
| 35 | err error |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 36 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 37 | // startStack is a stack containing StartObject and StartArray types. The |
| 38 | // top of stack represents the object or the array the current value is |
| 39 | // directly located in. |
| 40 | startStack []Type |
| 41 | |
| 42 | // orig is used in reporting line and column. |
| 43 | orig []byte |
| 44 | // in contains the unconsumed input. |
| 45 | in []byte |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 48 | // NewDecoder returns a Decoder to read the given []byte. |
| 49 | func NewDecoder(b []byte) *Decoder { |
| 50 | return &Decoder{orig: b, in: b} |
| 51 | } |
| 52 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 53 | // Peek looks ahead and returns the next JSON type without advancing a read. |
| 54 | func (d *Decoder) Peek() Type { |
| 55 | defer func() { d.lastCall = peekCall }() |
| 56 | if d.lastCall == readCall { |
| 57 | d.value, d.err = d.Read() |
| 58 | } |
| 59 | return d.value.typ |
| 60 | } |
| 61 | |
| 62 | // Read returns the next JSON value. It will return an error if there is no |
| 63 | // valid value. For String types containing invalid UTF8 characters, a |
| 64 | // non-fatal error is returned and caller can call Read for the next value. |
| 65 | func (d *Decoder) Read() (Value, error) { |
| 66 | defer func() { d.lastCall = readCall }() |
| 67 | if d.lastCall == peekCall { |
| 68 | return d.value, d.err |
| 69 | } |
| 70 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 71 | var nerr errors.NonFatal |
| 72 | value, n, err := d.parseNext() |
| 73 | if !nerr.Merge(err) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 74 | return Value{}, err |
| 75 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 76 | |
| 77 | switch value.typ { |
| 78 | case EOF: |
| 79 | if len(d.startStack) != 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 80 | d.value.typ&Null|Bool|Number|String|EndObject|EndArray == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 81 | return Value{}, io.ErrUnexpectedEOF |
| 82 | } |
| 83 | |
| 84 | case Null: |
| 85 | if !d.isValueNext() { |
| 86 | return Value{}, d.newSyntaxError("unexpected value null") |
| 87 | } |
| 88 | |
| 89 | case Bool, Number: |
| 90 | if !d.isValueNext() { |
| 91 | return Value{}, d.newSyntaxError("unexpected value %v", value) |
| 92 | } |
| 93 | |
| 94 | case String: |
| 95 | if d.isValueNext() { |
| 96 | break |
| 97 | } |
| 98 | // Check if this is for an object name. |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 99 | if d.value.typ&(StartObject|comma) == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 100 | return Value{}, d.newSyntaxError("unexpected value %q", value) |
| 101 | } |
| 102 | d.in = d.in[n:] |
| 103 | d.consume(0) |
| 104 | if c := d.in[0]; c != ':' { |
| 105 | return Value{}, d.newSyntaxError(`unexpected character %v, missing ":" after object name`, string(c)) |
| 106 | } |
| 107 | n = 1 |
| 108 | value.typ = Name |
| 109 | |
| 110 | case StartObject, StartArray: |
| 111 | if !d.isValueNext() { |
| 112 | return Value{}, d.newSyntaxError("unexpected character %v", value) |
| 113 | } |
| 114 | d.startStack = append(d.startStack, value.typ) |
| 115 | |
| 116 | case EndObject: |
| 117 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 118 | d.value.typ == comma || |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 119 | d.startStack[len(d.startStack)-1] != StartObject { |
| 120 | return Value{}, d.newSyntaxError("unexpected character }") |
| 121 | } |
| 122 | d.startStack = d.startStack[:len(d.startStack)-1] |
| 123 | |
| 124 | case EndArray: |
| 125 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 126 | d.value.typ == comma || |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 127 | d.startStack[len(d.startStack)-1] != StartArray { |
| 128 | return Value{}, d.newSyntaxError("unexpected character ]") |
| 129 | } |
| 130 | d.startStack = d.startStack[:len(d.startStack)-1] |
| 131 | |
| 132 | case comma: |
| 133 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 134 | d.value.typ&(Null|Bool|Number|String|EndObject|EndArray) == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 135 | return Value{}, d.newSyntaxError("unexpected character ,") |
| 136 | } |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 137 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 138 | |
| 139 | // Update lastType only after validating value to be in the right |
| 140 | // sequence. |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 141 | d.value.typ = value.typ |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 142 | d.in = d.in[n:] |
| 143 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 144 | if d.value.typ == comma { |
| 145 | return d.Read() |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 146 | } |
| 147 | return value, nerr.E |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 150 | var ( |
| 151 | literalRegexp = regexp.MustCompile(`^(null|true|false)`) |
| 152 | // Any sequence that looks like a non-delimiter (for error reporting). |
| 153 | errRegexp = regexp.MustCompile(`^([-+._a-zA-Z0-9]{1,32}|.)`) |
| 154 | ) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 155 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 156 | // parseNext parses for the next JSON value. It returns a Value object for |
| 157 | // different types, except for Name. It also returns the size that was parsed. |
| 158 | // It does not handle whether the next value is in a valid sequence or not, it |
| 159 | // only ensures that the value is a valid one. |
| 160 | func (d *Decoder) parseNext() (value Value, n int, err error) { |
| 161 | // Trim leading spaces. |
| 162 | d.consume(0) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 163 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 164 | in := d.in |
| 165 | if len(in) == 0 { |
| 166 | return d.newValue(EOF, nil, nil), 0, nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 167 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 168 | |
| 169 | switch in[0] { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 170 | case 'n', 't', 'f': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 171 | n := matchWithDelim(literalRegexp, in) |
| 172 | if n == 0 { |
| 173 | return Value{}, 0, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 174 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 175 | switch in[0] { |
| 176 | case 'n': |
| 177 | return d.newValue(Null, in[:n], nil), n, nil |
| 178 | case 't': |
| 179 | return d.newValue(Bool, in[:n], true), n, nil |
| 180 | case 'f': |
| 181 | return d.newValue(Bool, in[:n], false), n, nil |
| 182 | } |
| 183 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 184 | case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 185 | num, n := parseNumber(in) |
| 186 | if num == nil { |
| 187 | return Value{}, 0, d.newSyntaxError("invalid number %s", errRegexp.Find(in)) |
| 188 | } |
| 189 | return d.newValue(Number, in[:n], num), n, nil |
| 190 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 191 | case '"': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 192 | var nerr errors.NonFatal |
| 193 | s, n, err := d.parseString(in) |
| 194 | if !nerr.Merge(err) { |
| 195 | return Value{}, 0, err |
| 196 | } |
| 197 | return d.newValue(String, in[:n], s), n, nerr.E |
| 198 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 199 | case '{': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 200 | return d.newValue(StartObject, in[:1], nil), 1, nil |
| 201 | |
| 202 | case '}': |
| 203 | return d.newValue(EndObject, in[:1], nil), 1, nil |
| 204 | |
| 205 | case '[': |
| 206 | return d.newValue(StartArray, in[:1], nil), 1, nil |
| 207 | |
| 208 | case ']': |
| 209 | return d.newValue(EndArray, in[:1], nil), 1, nil |
| 210 | |
| 211 | case ',': |
| 212 | return d.newValue(comma, in[:1], nil), 1, nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 213 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 214 | return Value{}, 0, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 217 | // position returns line and column number of parsed bytes. |
| 218 | func (d *Decoder) position() (int, int) { |
| 219 | // Calculate line and column of consumed input. |
| 220 | b := d.orig[:len(d.orig)-len(d.in)] |
| 221 | line := bytes.Count(b, []byte("\n")) + 1 |
| 222 | if i := bytes.LastIndexByte(b, '\n'); i >= 0 { |
| 223 | b = b[i+1:] |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 224 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 225 | column := utf8.RuneCount(b) + 1 // ignore multi-rune characters |
| 226 | return line, column |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 229 | // newSyntaxError returns an error with line and column information useful for |
| 230 | // syntax errors. |
| 231 | func (d *Decoder) newSyntaxError(f string, x ...interface{}) error { |
| 232 | e := errors.New(f, x...) |
| 233 | line, column := d.position() |
| 234 | return errors.New("syntax error (line %d:%d): %v", line, column, e) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 237 | // matchWithDelim matches r with the input b and verifies that the match |
| 238 | // terminates with a delimiter of some form (e.g., r"[^-+_.a-zA-Z0-9]"). |
| 239 | // As a special case, EOF is considered a delimiter. |
| 240 | func matchWithDelim(r *regexp.Regexp, b []byte) int { |
| 241 | n := len(r.Find(b)) |
| 242 | if n < len(b) { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 243 | // Check that the next character is a delimiter. |
| 244 | if isNotDelim(b[n]) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 245 | return 0 |
| 246 | } |
| 247 | } |
| 248 | return n |
| 249 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 250 | |
| 251 | // isNotDelim returns true if given byte is a not delimiter character. |
| 252 | func isNotDelim(c byte) bool { |
| 253 | return (c == '-' || c == '+' || c == '.' || c == '_' || |
| 254 | ('a' <= c && c <= 'z') || |
| 255 | ('A' <= c && c <= 'Z') || |
| 256 | ('0' <= c && c <= '9')) |
| 257 | } |
| 258 | |
| 259 | // consume consumes n bytes of input and any subsequent whitespace. |
| 260 | func (d *Decoder) consume(n int) { |
| 261 | d.in = d.in[n:] |
| 262 | for len(d.in) > 0 { |
| 263 | switch d.in[0] { |
| 264 | case ' ', '\n', '\r', '\t': |
| 265 | d.in = d.in[1:] |
| 266 | default: |
| 267 | return |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // isValueNext returns true if next type should be a JSON value: Null, |
| 273 | // Number, String or Bool. |
| 274 | func (d *Decoder) isValueNext() bool { |
| 275 | if len(d.startStack) == 0 { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 276 | return d.value.typ == 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | start := d.startStack[len(d.startStack)-1] |
| 280 | switch start { |
| 281 | case StartObject: |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 282 | return d.value.typ&Name != 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 283 | case StartArray: |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 284 | return d.value.typ&(StartArray|comma) != 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 285 | } |
| 286 | panic(fmt.Sprintf( |
| 287 | "unreachable logic in Decoder.isValueNext, lastType: %v, startStack: %v", |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 288 | d.value.typ, start)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // newValue constructs a Value. |
| 292 | func (d *Decoder) newValue(typ Type, input []byte, value interface{}) Value { |
| 293 | line, column := d.position() |
| 294 | return Value{ |
| 295 | input: input, |
| 296 | line: line, |
| 297 | column: column, |
| 298 | typ: typ, |
| 299 | value: value, |
| 300 | } |
| 301 | } |
| 302 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame^] | 303 | // Value contains a JSON type and value parsed from calling Decoder.Read. |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 304 | type Value struct { |
| 305 | input []byte |
| 306 | line int |
| 307 | column int |
| 308 | typ Type |
| 309 | // value will be set to the following Go type based on the type field: |
| 310 | // Bool => bool |
| 311 | // Number => *numberParts |
| 312 | // String => string |
| 313 | // Name => string |
| 314 | // It will be nil if none of the above. |
| 315 | value interface{} |
| 316 | } |
| 317 | |
| 318 | func (v Value) newError(f string, x ...interface{}) error { |
| 319 | e := errors.New(f, x...) |
| 320 | return errors.New("error (line %d:%d): %v", v.line, v.column, e) |
| 321 | } |
| 322 | |
| 323 | // Type returns the JSON type. |
| 324 | func (v Value) Type() Type { |
| 325 | return v.typ |
| 326 | } |
| 327 | |
| 328 | // Position returns the line and column of the value. |
| 329 | func (v Value) Position() (int, int) { |
| 330 | return v.line, v.column |
| 331 | } |
| 332 | |
| 333 | // Bool returns the bool value if token is Bool, else it will return an error. |
| 334 | func (v Value) Bool() (bool, error) { |
| 335 | if v.typ != Bool { |
| 336 | return false, v.newError("%s is not a bool", v.input) |
| 337 | } |
| 338 | return v.value.(bool), nil |
| 339 | } |
| 340 | |
| 341 | // String returns the string value for a JSON string token or the read value in |
| 342 | // string if token is not a string. |
| 343 | func (v Value) String() string { |
| 344 | if v.typ != String { |
| 345 | return string(v.input) |
| 346 | } |
| 347 | return v.value.(string) |
| 348 | } |
| 349 | |
| 350 | // Name returns the object name if token is Name, else it will return an error. |
| 351 | func (v Value) Name() (string, error) { |
| 352 | if v.typ != Name { |
| 353 | return "", v.newError("%s is not an object name", v.input) |
| 354 | } |
| 355 | return v.value.(string), nil |
| 356 | } |
| 357 | |
| 358 | // Float returns the floating-point number if token is Number, else it will |
| 359 | // return an error. |
| 360 | // |
| 361 | // The floating-point precision is specified by the bitSize parameter: 32 for |
| 362 | // float32 or 64 for float64. If bitSize=32, the result still has type float64, |
| 363 | // but it will be convertible to float32 without changing its value. It will |
| 364 | // return an error if the number exceeds the floating point limits for given |
| 365 | // bitSize. |
| 366 | func (v Value) Float(bitSize int) (float64, error) { |
| 367 | if v.typ != Number { |
| 368 | return 0, v.newError("%s is not a number", v.input) |
| 369 | } |
| 370 | f, err := strconv.ParseFloat(string(v.input), bitSize) |
| 371 | if err != nil { |
| 372 | return 0, v.newError("%v", err) |
| 373 | } |
| 374 | return f, nil |
| 375 | } |
| 376 | |
| 377 | // Int returns the signed integer number if token is Number, else it will |
| 378 | // return an error. |
| 379 | // |
| 380 | // The given bitSize specifies the integer type that the result must fit into. |
| 381 | // It returns an error if the number is not an integer value or if the result |
| 382 | // exceeds the limits for given bitSize. |
| 383 | func (v Value) Int(bitSize int) (int64, error) { |
| 384 | s, err := v.getIntStr() |
| 385 | if err != nil { |
| 386 | return 0, err |
| 387 | } |
| 388 | n, err := strconv.ParseInt(s, 10, bitSize) |
| 389 | if err != nil { |
| 390 | return 0, v.newError("%v", err) |
| 391 | } |
| 392 | return n, nil |
| 393 | } |
| 394 | |
| 395 | // Uint returns the signed integer number if token is Number, else it will |
| 396 | // return an error. |
| 397 | // |
| 398 | // The given bitSize specifies the unsigned integer type that the result must |
| 399 | // fit into. It returns an error if the number is not an unsigned integer value |
| 400 | // or if the result exceeds the limits for given bitSize. |
| 401 | func (v Value) Uint(bitSize int) (uint64, error) { |
| 402 | s, err := v.getIntStr() |
| 403 | if err != nil { |
| 404 | return 0, err |
| 405 | } |
| 406 | n, err := strconv.ParseUint(s, 10, bitSize) |
| 407 | if err != nil { |
| 408 | return 0, v.newError("%v", err) |
| 409 | } |
| 410 | return n, nil |
| 411 | } |
| 412 | |
| 413 | func (v Value) getIntStr() (string, error) { |
| 414 | if v.typ != Number { |
| 415 | return "", v.newError("%s is not a number", v.input) |
| 416 | } |
| 417 | pnum := v.value.(*numberParts) |
| 418 | num, ok := normalizeToIntString(pnum) |
| 419 | if !ok { |
| 420 | return "", v.newError("cannot convert %s to integer", v.input) |
| 421 | } |
| 422 | return num, nil |
| 423 | } |