aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2008-08-28 20:19:17 +0000
committerChong Yidong2008-08-28 20:19:17 +0000
commit7712319db8db97391059925b27ae71f304eee7d2 (patch)
tree849b2c2549c75949fa7254aa05ee03510406279c
parent15e102f6a05fa6c1846949e188ab073f39fe1fc8 (diff)
downloademacs-7712319db8db97391059925b27ae71f304eee7d2.tar.gz
emacs-7712319db8db97391059925b27ae71f304eee7d2.zip
(json-read-number): New arg. Handle explicitly signed numbers.
(json-readtable): Add `+' and `.'.
-rw-r--r--lisp/json.el28
1 files changed, 18 insertions, 10 deletions
diff --git a/lisp/json.el b/lisp/json.el
index 3d4c02c588d..38ec16f5db7 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -221,19 +221,27 @@ KEYWORD is the keyword expected."
221 221
222;; Number parsing 222;; Number parsing
223 223
224(defun json-read-number () 224(defun json-read-number (&optional sign)
225 "Read the JSON number following point. 225 "Read the JSON number following point.
226The optional SIGN argument is for internal use.
227
226N.B.: Only numbers which can fit in Emacs Lisp's native number 228N.B.: Only numbers which can fit in Emacs Lisp's native number
227representation will be parsed correctly." 229representation will be parsed correctly."
228 (if (char-equal (json-peek) ?-) 230 ;; If SIGN is non-nil, the number is explicitly signed.
229 (progn 231 (let ((number-regexp
230 (json-advance) 232 "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
231 (- 0 (json-read-number))) 233 (cond ((and (null sign) (char-equal (json-peek) ?-))
232 (if (looking-at "[0-9]+\\([.][0-9]+\\)?\\([eE][+-]?[0-9]+\\)?") 234 (json-advance)
233 (progn 235 (- (json-read-number t)))
236 ((and (null sign) (char-equal (json-peek) ?+))
237 (json-advance)
238 (json-read-number t))
239 ((and (looking-at number-regexp)
240 (or (match-beginning 1)
241 (match-beginning 2)))
234 (goto-char (match-end 0)) 242 (goto-char (match-end 0))
235 (string-to-number (match-string 0))) 243 (string-to-number (match-string 0)))
236 (signal 'json-number-format (list (point)))))) 244 (t (signal 'json-number-format (list (point)))))))
237 245
238;; Number encoding 246;; Number encoding
239 247
@@ -470,7 +478,7 @@ become JSON objects."
470 (?\" json-read-string)))) 478 (?\" json-read-string))))
471 (mapc (lambda (char) 479 (mapc (lambda (char)
472 (push (list char 'json-read-number) table)) 480 (push (list char 'json-read-number) table))
473 '(?- ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)) 481 '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
474 table) 482 table)
475 "Readtable for JSON reader.") 483 "Readtable for JSON reader.")
476 484