json: preserve old behavior in terminating a number

The json parser has allowed "1x" as "1" and "1e-0x" as "1.0", so
keep that behavior for now.
This commit is contained in:
Matthew Flatt 2019-07-06 05:59:14 -06:00
parent bbb8707a4a
commit 79d6b9bc18
2 changed files with 7 additions and 7 deletions

View File

@ -95,6 +95,7 @@
(define (parse-tests)
(test (string->jsexpr @T{ 0 }) => 0
(string->jsexpr @T{ 0x }) => 0 ; not clearly a good idea, but preserve old behavior
(string->jsexpr @T{ 1 }) => 1
(string->jsexpr @T{ -1 }) => -1 ; note: `+' is forbidden
(string->jsexpr @T{ -12 }) => -12
@ -109,6 +110,7 @@
(string->jsexpr @T{-10.34e03}) => -10340.0
(string->jsexpr @T{-10.34e+3}) => -10340.0
(string->jsexpr @T{-10.34e+03}) => -10340.0
(string->jsexpr @T{-10.34e+0x}) => -10.340 ; preserve old behavior
(string->jsexpr @T{-10.34e-3}) => -1.034e-2
(string->jsexpr @T{-10.34e-03}) => -1.034e-2
(string->jsexpr @T{-10.34e+31}) => -1.034e32

View File

@ -398,7 +398,7 @@
(define c (read-byte i))
(cond
[(digit-byte? c)
(read-exponent-rest n exp (to-number c))]
(read-exponent-rest n exp (to-number c) 1)]
[(eqv? c (char->integer #\+))
(read-exponent-more n mark #"+" exp 1)]
[(eqv? c (char->integer #\-))
@ -411,23 +411,21 @@
(define (read-exponent-more n mark mark2 exp sgn)
(define c (read-byte i))
(cond
[(and (digit-byte? c) (zero? (to-number c)))
(read-exponent-more n mark mark2 exp sgn)]
[(digit-byte? c)
(read-exponent-rest n exp (* sgn (to-number c)))]
(read-exponent-rest n exp (to-number c) sgn)]
[else (bad-input (bytes-append (n->string n exp)
(bytes mark)
mark2
(maybe-bytes c))
#:eof? (eof-object? c))]))
;; more digits:
(define (read-exponent-rest n exp exp2)
(define (read-exponent-rest n exp exp2 sgn)
(define c (peek-byte i))
(cond
[(digit-byte? c)
(read-byte i)
(read-exponent-rest n exp (+ (* 10 exp2) (to-number c)))]
[else (exact->inexact (* n (expt 10 (+ exp exp2))))]))
(read-exponent-rest n exp (+ (* 10 exp2) (to-number c)) sgn)]
[else (exact->inexact (* n (expt 10 (+ exp (* sgn exp2)))))]))
(start))
;;
(define (read-json [top? #f])