Juan571 / js2-mode

Automatically exported from code.google.com/p/js2-mode
0 stars 0 forks source link

find a way to support JSON #50

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The main problem is that JSON content is invalid JavaScript syntax - you
can't have an object-literal as a left-hand side expression.  Need to add
an option and some support to the parser.

Original issue reported on code.google.com by steve.ye...@gmail.com on 19 Apr 2008 at 9:32

GoogleCodeExporter commented 8 years ago
AFAIK, JSON is well formed JavaScript subset and easiest way to support it is to
adapt JavaScript lexer to return bad tokens on non JSON lexems. Not sure if it 
will
work in your case though

Original comment by Maxim.Mo...@gmail.com on 24 Apr 2008 at 12:43

GoogleCodeExporter commented 8 years ago

Original comment by chris.bu...@gmail.com on 7 Aug 2008 at 3:38

GoogleCodeExporter commented 8 years ago
Is this being actively worked on?

Original comment by steven.h...@gmail.com on 7 Oct 2008 at 5:40

GoogleCodeExporter commented 8 years ago
Will this be worked on? (or, alternatively, what would I need to write to make 
this happen?)

Original comment by pnathan....@gmail.com on 9 Jul 2010 at 12:10

GoogleCodeExporter commented 8 years ago
I imagine that you'd have to write a modified version of the object literal 
parser that requires quoted property names. Perhaps the current function could 
act differently based on a parameter that defaults to the current behaviour. 
The second thing is to find the main entry point for the parser and create a 
similar entry point that parses only object literals and arrays. (Having an 
array as the root is apparently not "real" JSON but is used relatively often in 
practice.) Lastly, create a modified version of the expression parsing function 
that doesn't parse variable references, function expressions, and other 
expressions that aren't present in JSON. You'd be left with strings, objects, 
arrays, numbers, booleans, and null.

Oh, and comments are not valid JSON either. Again they are sometimes used in 
practice but they are not part of Crockford's spec. See json.org for details.

It might actually be easier to write a JSON parser from scratch than to modify 
js2-mode, as JSON is so simple. I haven't looked at the js2-mode code yet.

Original comment by sami.sam...@gmail.com on 9 Jul 2010 at 12:46

GoogleCodeExporter commented 8 years ago
I'm not currently working on this feature.

You're right -- JSON has more differences from regular JavaScript literals than 
I remembered.

There is an existing json.el for Emacs that can parse JSON.  It might be a 
possible starting point, but integrating it with js2-mode might be hard.

What functionality do you want?  Is it the editing or the lint errors/warnings? 
 If it's editing, then it might be best to try to convert json.el's parse tree 
format into js2-mode's parse tree format.  If it's linting, then we should 
modify js2-mode's parser to support the peculiarities of JSON.

Original comment by steve.ye...@gmail.com on 9 Jul 2010 at 3:02

GoogleCodeExporter commented 8 years ago
I've prepared workaround that parses json files as expected in js2-mode.
It cannot be perceived as complete, but at least doesn't throw syntax errors.
I've used advice feature so it's fully transparent and you don't need to modify 
or rewrite js2-mode code to apply that.
Fix is applied only to *.json files so it doesn't interfere with regular *.js 
files:

(add-to-list 'auto-mode-alist '("\\.json$" . js2-mode))

(defadvice js2-reparse (before json)
    (setq js2-buffer-file-name buffer-file-name))
(ad-activate 'js2-reparse)

(defadvice js2-parse-statement (around json)
    (if (and (= tt js2-LC)
            js2-buffer-file-name
            (string-equal (substring js2-buffer-file-name -5) ".json")
            (eq (+ (save-excursion
                        (goto-char (point-min))
                        (back-to-indentation)
                        (while (eolp)
                            (next-line)
                            (back-to-indentation))
                        (point)) 1) js2-ts-cursor))
        (setq ad-return-value (js2-parse-assign-expr))
        ad-do-it))
(ad-activate 'js2-parse-statement)

Original comment by marius...@gmail.com on 30 Apr 2011 at 11:54

GoogleCodeExporter commented 8 years ago
@sami.sam: How do you figure that JSON doesn't allow arrays at the root? I had 
always assumed that "value" was meant to be the starting production ...

... and the RFC, http://tools.ietf.org/html/rfc4627, though not quite that 
lenient, does explicitly permit both objects and arrays:

   A JSON text is a serialized object or array.

      JSON-text = object / array

Original comment by naesten on 4 May 2011 at 4:35

GoogleCodeExporter commented 8 years ago
Hmm, I must be mistaken. I don't see anything on json.org about the allowed 
root types at all.

Original comment by sami.sam...@gmail.com on 4 May 2011 at 4:41