From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kevin Rodgers Newsgroups: gmane.emacs.bugs,gmane.emacs.pretest.bugs Subject: bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec Date: Tue, 12 Aug 2008 21:11:01 -0600 Message-ID: References: <20080809070528.26869.qmail@priss.frightenedpiglet.com> Reply-To: Kevin Rodgers , 679@emacsbugs.donarmstrong.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1218598092 29828 80.91.229.12 (13 Aug 2008 03:28:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 13 Aug 2008 03:28:12 +0000 (UTC) Cc: emacs-pretest-bug@gnu.org To: bug-gnu-emacs@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Aug 13 05:29:02 2008 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KT72h-0006rW-HJ for geb-bug-gnu-emacs@m.gmane.org; Wed, 13 Aug 2008 05:29:02 +0200 Original-Received: from localhost ([127.0.0.1]:41674 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KT71Z-0003jh-3e for geb-bug-gnu-emacs@m.gmane.org; Tue, 12 Aug 2008 23:27:49 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KT71U-0003jF-4N for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:27:44 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KT71O-0003eK-Kp for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:27:43 -0400 Original-Received: from [199.232.76.173] (port=50589 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KT71O-0003eF-Dh for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:27:38 -0400 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:50918) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KT71N-0002b9-0q for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:27:39 -0400 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m7D3RPSH018723; Tue, 12 Aug 2008 20:27:25 -0700 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.13.8/8.13.8/Submit) id m7D3K362015981; Tue, 12 Aug 2008 20:20:03 -0700 X-Loop: don@donarmstrong.com Resent-From: Kevin Rodgers Original-Sender: news Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs Resent-Date: Wed, 13 Aug 2008 03:20:03 +0000 Resent-Message-ID: Resent-Sender: don@donarmstrong.com X-Emacs-PR-Message: report 679 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: Original-Received: via spool by submit@emacsbugs.donarmstrong.com id=B.121859708114148 (code B ref -1); Wed, 13 Aug 2008 03:20:03 +0000 Original-Received: (at submit) by emacsbugs.donarmstrong.com; 13 Aug 2008 03:11:21 +0000 Original-Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m7D3BFTs014142 for ; Tue, 12 Aug 2008 20:11:17 -0700 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KT6lX-0007R3-3x for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:11:15 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KT6lW-0007Qd-Nn for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:11:14 -0400 Original-Received: from [199.232.76.173] (port=59268 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KT6lW-0007QX-I3 for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:11:14 -0400 Original-Received: from main.gmane.org ([80.91.229.2]:53285 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KT6lW-0004pS-Qh for bug-gnu-emacs@gnu.org; Tue, 12 Aug 2008 23:11:15 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1KT6lQ-00034Z-Au for bug-gnu-emacs@gnu.org; Wed, 13 Aug 2008 03:11:08 +0000 Original-Received: from c-67-161-145-183.hsd1.co.comcast.net ([67.161.145.183]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 13 Aug 2008 03:11:08 +0000 Original-Received: from kevin.d.rodgers by c-67-161-145-183.hsd1.co.comcast.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 13 Aug 2008 03:11:08 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 105 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: c-67-161-145-183.hsd1.co.comcast.net User-Agent: Thunderbird 2.0.0.16 (Macintosh/20080707) In-Reply-To: <20080809070528.26869.qmail@priss.frightenedpiglet.com> X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 3) Resent-Date: Tue, 12 Aug 2008 23:27:43 -0400 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:19360 gmane.emacs.pretest.bugs:22824 Archived-At: sand@blarg.net wrote: > In json.el, the 'json-read-number' function is intended to read the > textual representation of Javascript numbers. > > (defun json-read-number () > "Read the JSON number following point. > 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 > (goto-char (match-end 0)) > (string-to-number (match-string 0))) > (signal 'json-number-format (list (point)))))) > > The Javascript spec > (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#Floating-Point_Literals) > says the following about floating-point numbers: > > | A floating-point literal can have the following parts: > | > | * A decimal integer which can be signed (preceded by "+" or "-"), > | * A decimal point ("."), > | * A fraction (another decimal number), > | * An exponent. > | > | The exponent part is an "e" or "E" followed by an integer, which can > | be signed (preceded by "+" or "-"). A floating-point literal must have > | at least one digit and either a decimal point or "e" (or "E"). > > This means that > > 1. > > is a valid floating point-literal per the spec, but the regular > expression above does not match the complete text---the regular > expression requires digits after the decimal point. This ends up > matched as a simple integer, leaving the decimal point in the input > stream and causing a later syntax error. Similarly > > .1 > > is a valid floating point-literal illegal that Emacs rejects, because > the regular expression requires digits before the decimal point. > Testing with the error console in Firefox confirms that the two > examples above are valid syntax. > > The parser function needs to be fixed to handle the complete syntax. It also means that "+1" is valid but not recognized. That's easy to fix: (cond ((char-equal (json-peek) ?-) (json-advance) (- (json-read-number))) ((char-equal (json-peek) ?+) (json-advance) (json-read-number)) ((looking-at "[0-9]+\\([.][0-9]+\\)?\\([eE][+-]?[0-9]+\\)?") ...) (t (signal 'json-number-format (list (point))))) The spec you cite also says: | More succinctly, the syntax is: | | [digits][.digits][(E|e)[(+|-)]digits] which would be translated literally as "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?" The only difference between that and the regexp in json-read-number is the optionality of the leading digits and the representation of the decimal point. But the potential problem with changing the regexp like that is that it now matches the empty string, which the spec does not allow: | A floating-point literal must have at least one digit and either a decimal point or "e" (or "E"). So, does this give the desired results: (defun json-read-number () "Read the JSON number following point. N.B.: Only numbers which can fit in Emacs Lisp's native number representation will be parsed correctly." (cond ((char-equal (json-peek) ?-) (json-advance) (- (json-read-number))) ((char-equal (json-peek) ?+) (json-advance) (json-read-number)) ((and (looking-at "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?") (or (match-beginning 1) (match-beginning 2) (match-beginning 3))) (goto-char (match-end 0)) (string-to-number (match-string 0))) (t (signal 'json-number-format (list (point)))))) -- Kevin Rodgers Denver, Colorado, USA