Looking for a JS parser (written in JS) that preserves all comments?

410 views
Skip to first unread message

John Lenz

unread,
Feb 27, 2013, 5:50:12 PM2/27/13
to js-t...@googlegroups.com
It looks like neither the esprima or uglify parsers do this.

Peter van der Zee

unread,
Feb 27, 2013, 5:57:17 PM2/27/13
to js-t...@googlegroups.com
On Wed, Feb 27, 2013 at 11:50 PM, John Lenz <conca...@gmail.com> wrote:
> It looks like neither the esprima or uglify parsers do this.

http://github.com/qfox/zeparser
http://github.com/qfox/zeparser2

Returns a tokenstream (black and white, where white means the
whitespace). First version also delivers full parse tree with it, but
is much slower than v2. V2 just delivers a token stream (if you
uncomment one line), but is highly customizable.

</selfpromotion>

There's also acorn: http://marijnhaverbeke.nl/acorn/ and some slower ones.

If you find any new ones not listed on http://gonzales.qfox.nl/ please
tell me :)

- peter

Peter van der Zee

unread,
Feb 27, 2013, 5:59:18 PM2/27/13
to js-t...@googlegroups.com
On Wed, Feb 27, 2013 at 11:57 PM, Peter van der Zee <qfo...@gmail.com> wrote:
> On Wed, Feb 27, 2013 at 11:50 PM, John Lenz <conca...@gmail.com> wrote:
>> It looks like neither the esprima or uglify parsers do this.

I'd actually be surprised if they can't. But I'm sure Ariya will
clarify that soon :)

- peter

John J Barton

unread,
Feb 27, 2013, 6:00:30 PM2/27/13
to js-t...@googlegroups.com
Traceur seems to be able to be adapted to that purpose:
https://code.google.com/p/traceur-compiler/source/browse/demo/classifier.js

jjb


On Wed, Feb 27, 2013 at 2:50 PM, John Lenz <conca...@gmail.com> wrote:
It looks like neither the esprima or uglify parsers do this.

--
--
http://clausreinke.github.com/js-tools/resources.html - tool information
http://groups.google.com/group/js-tools - mailing list information

---
You received this message because you are subscribed to the Google Groups "js-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to js-tools+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Ariya Hidayat

unread,
Feb 27, 2013, 9:46:34 PM2/27/13
to js-t...@googlegroups.com
Right now Esprima collects the comment in a separate array, try e.g.
esprima.parse(code, { range: true, comment: true });.

If you can wait for the next version, it will attach the comment(s) to
the syntax node.

If you can't wait, it should be rather easy to attach the comments via
a post-processing step. I remember someone did that, I can't recall
who/where.


Regards,

--
Ariya Hidayat, http://ariya.ofilabs.com
http://twitter.com/ariyahidayat
http://gplus.to/ariyahidayat

Yusuke SUZUKI

unread,
Feb 27, 2013, 10:02:37 PM2/27/13
to js-t...@googlegroups.com
If you can't wait, it should be rather easy to attach the comments via
a post-processing step. I remember someone did that, I can't recall
who/where.

escodegen.attachComments do this :)


--
--
http://clausreinke.github.com/js-tools/resources.html - tool information
http://groups.google.com/group/js-tools - mailing list information

---
You received this message because you are subscribed to the Google Groups "js-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to js-tools+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Regards,
Yusuke Suzuki

Ariya Hidayat

unread,
Feb 27, 2013, 10:09:33 PM2/27/13
to js-t...@googlegroups.com
On Wed, Feb 27, 2013 at 7:02 PM, Yusuke SUZUKI <utata...@gmail.com> wrote:
>> If you can't wait, it should be rather easy to attach the comments via
>> a post-processing step. I remember someone did that, I can't recall
>> who/where.
>
>
> escodegen.attachComments do this :)

Thanks, Yusuke :) Now I remember everything!

Here's a simple example program (run with Node.js):

var esprima = require('esprima');
var escodegen = require('escodegen');

var code = 'function foo() { // Answer\n return 42 }';
var syntax = esprima.parse(code, { tokens: true, range: true,
comment: true });
syntax = escodegen.attachComments(syntax, syntax.comments, syntax.tokens);

console.log(JSON.stringify(syntax.body, null, 2));

You would see that ReturnStatement node will have "// Answer" in its
leadingComments.

Claus Reinke

unread,
Feb 28, 2013, 8:16:28 AM2/28/13
to js-t...@googlegroups.com
> It looks like neither the esprima or uglify parsers do this.
>
If you check that 'tool information' link at the bottom of every message
here, in group parsers

http://clausreinke.github.com/js-tools/resources.html#group:parsers

it lists esprima as preserving comments.

I cling to the hope that some day JS tool developers will open tickets
on that github repo to keep information about their tools up to date;-)

Claus

Ariya Hidayat

unread,
Mar 21, 2013, 11:24:53 AM3/21/13
to js-t...@googlegroups.com
For a complete example, i.e. lossless code regeneration (parsing +
generating) including all the comments, see
http://esprima.org/demo/rewrite.html.

John Lenz

unread,
Mar 21, 2013, 2:04:14 PM3/21/13
to js-t...@googlegroups.com
Thanks


--
Reply all
Reply to author
Forward
0 new messages