AST in JSON format

Kevin Curtis kevinc1846 at
Mon Dec 7 16:28:21 PST 2009

> (This is as considered separately from the possibility of programatically
> manipulating a parsed AST - the use cases for that are clear. Though there
> may still be verification issues depending on the nature of the manipulation
> API. It seems like the possibilities are either specialized objects that
> enforce validity on every individual manipulation, or something that accepts
> JSON-like objects and verifies validity after the fact, or something that
> accepts JSON-like objects and verifies validity by converting to JavaScript
> source code and then parsing it).
> Regards,
> Maciej

I asked something similar here:

Are there 2 approaches to adding AST functionality in ecmascript:

1) The AST as JSON
Either Brendan's original example or the S-expression-ish jsonml.
This covers both the in memory representation of the AST and its
serialization format.

2) API
Similar to the ast module in python where multiple api calls build the
AST nodes.

Given that there are no takers for 'scheme by the back door', then,
with the object hardening features of ES5 - 2) should be considered.
//  assume an ast module factory
var x = ast.BinOp("+", 4, 5);
x.left = 6;
x.right = 7
x.whatever = 8 // error

The Python ast module has a dump method which dumps the AST tree of
Python objects to a string of nested functions calls - which can be
eval'ed to recreate in memory.

More information about the es-discuss mailing list