unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
To: bug-gnu-emacs@gnu.org
Cc: emacs-pretest-bug@gnu.org
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	[thread overview]
Message-ID: <g7tjc2$r5l$1@ger.gmane.org> (raw)
In-Reply-To: <20080809070528.26869.qmail@priss.frightenedpiglet.com>

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








  parent reply	other threads:[~2008-08-13  3:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87sksoc2kv.fsf@cyd.mit.edu>
2008-08-09  7:05 ` bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec sand
2008-08-09 10:57   ` OFFICE ZERO
     [not found]     ` <handler.679.B679.12182794784103.ackinfo@emacsbugs.donarmstrong.com>
2008-08-09 12:13       ` bug#679: Info received (bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec) OFFICE ZERO
     [not found]         ` <handler.679.B679.121828398627739.ackinfo@emacsbugs.donarmstrong.com>
2008-08-09 13:17           ` bug#679: Info received (bug#679: Info received (bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec)) OFFICE ZERO
2008-08-09 14:14   ` bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec OFFICE ZERO
2008-08-13  3:11   ` Kevin Rodgers [this message]
2008-08-28 20:30   ` bug#679: marked as done (23.0.60; Function json-read-number does not handle complete Javascript spec) Emacs bug Tracking System
2008-08-13  3:42 bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec Chong Yidong
2008-08-13  5:11 ` Edward O'Connor
2008-08-14  2:25   ` Kevin Rodgers
2008-08-19  5:37     ` Edward O'Connor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='g7tjc2$r5l$1@ger.gmane.org' \
    --to=kevin.d.rodgers@gmail.com \
    --cc=679@emacsbugs.donarmstrong.com \
    --cc=bug-gnu-emacs@gnu.org \
    --cc=emacs-pretest-bug@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).