Add 2010 to copyright years.
[bpt/emacs.git] / lisp / json.el
index 3d4c02c..39c1fd3 100644 (file)
@@ -1,6 +1,6 @@
 ;;; json.el --- JavaScript Object Notation parser / generator
 
-;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: Edward O'Connor <ted@oconnor.cx>
 ;; Version: 1.2
@@ -123,11 +123,7 @@ this around your call to `json-read' instead of `setq'ing it.")
 
 (defsubst json-advance (&optional n)
   "Skip past the following N characters."
-  (unless n (setq n 1))
-  (let ((goal (+ (point) n)))
-    (goto-char goal)
-    (when (< (point) goal)
-      (signal 'end-of-file nil))))
+  (forward-char n))
 
 (defsubst json-peek ()
   "Return the character at point."
@@ -144,8 +140,7 @@ this around your call to `json-read' instead of `setq'ing it.")
 
 (defun json-skip-whitespace ()
   "Skip past the whitespace at point."
-  (while (looking-at "[\t\r\n\f\b ]")
-    (goto-char (match-end 0))))
+  (skip-chars-forward "\t\r\n\f\b "))
 
 \f
 
@@ -221,19 +216,27 @@ KEYWORD is the keyword expected."
 
 ;; Number parsing
 
-(defun json-read-number ()
-  "Read the JSON number following point.
+(defun json-read-number (&optional sign)
+ "Read the JSON number following point.
+The optional SIGN  argument is for internal use.
+
 N.B.: Only numbers which can fit in Emacs Lisp's native number
 representation will be parsed correctly."
-  (if (char-equal (json-peek) ?-)
-      (progn
-        (json-advance)
-        (- 0 (json-read-number)))
-    (if (looking-at "[0-9]+\\([.][0-9]+\\)?\\([eE][+-]?[0-9]+\\)?")
-        (progn
+ ;; If SIGN is non-nil, the number is explicitly signed.
+ (let ((number-regexp
+        "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
+   (cond ((and (null sign) (char-equal (json-peek) ?-))
+          (json-advance)
+          (- (json-read-number t)))
+         ((and (null sign) (char-equal (json-peek) ?+))
+          (json-advance)
+          (json-read-number t))
+         ((and (looking-at number-regexp)
+               (or (match-beginning 1)
+                   (match-beginning 2)))
           (goto-char (match-end 0))
           (string-to-number (match-string 0)))
-      (signal 'json-number-format (list (point))))))
+         (t (signal 'json-number-format (list (point)))))))
 
 ;; Number encoding
 
@@ -470,7 +473,7 @@ become JSON objects."
            (?\" json-read-string))))
     (mapc (lambda (char)
             (push (list char 'json-read-number) table))
-          '(?- ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
+          '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
     table)
   "Readtable for JSON reader.")