AST in JSON format

David-Sarah Hopwood david-sarah at jacaranda.org
Fri Oct 16 23:17:45 PDT 2009


Kevin Curtis wrote:
> In May there was some discussion on the possibility of a standardized
> format for the ECMAScript AST in JSON/JsonML.
> 
> The V8 engine has an option (in bleeding_edge) where the internal AST
> tree can be output to JsonML for debugging purposes:
> ./shell_g --print_json_ast <file.js>
> 
> This is V8's internal AST type, which necessarily includes some
> implementation-specific artifacts. That said, the V8 AST is very
> nearly straight out of the ECMA 262 spec, so it's pretty generic.
> (Note: it's an initial version e.g doesn't recur into switch statement
> cases). It could be useful as an input as to what a standard JSON AST
> should look like. (Which, i guess, ECMAScript engines could support as
> an new, additional format to any existing AST serialization formats).

The Jacaranda parser (not released yet) also produces a JsonML AST.
Below is the same example for comparison, also with Jacaranda-specific
artefacts removed.

> Here's an example - with some V8 artefact's removed for clarity. Note:
> the script gets wrapped in a FunctionLiteral and VariableProxy ==
> Identifier.
> 
> --- source ---
> 
> x = 1;
> if (x > 0) {
>         y = x + 2;
>         print(y);
> }

["SEQ", {},
  ["EXPRSTMT", {},
    ["=", {},
      ["REF", {"name":"x"}],
      ["NUMBER", {"MV":1}]]],
  ["if", {},
    [">", {},
      ["REF", {"name":"x"}],
      ["NUMBER", {"MV":0}]],
    ["{", {},
      ["SEQ", {},
        ["EXPRSTMT", {},
          ["=", {},
            ["REF", {"name":"y"}],
            ["+", {},
              ["REF", {"name":"x"}],
              ["NUMBER", {"MV":2}]]],
        ["EXPRSTMT", {},
          ["(", {},
            ["REF", {"name":"print"}],
            ["ARGS", {},
              ["REF", {"name":"y"}]]]]]]]]

> --- AST JsonML ---
> 
> ["FunctionLiteral",
>   {"name":""},
>   ["ExpressionStatement",
>     ["Assignment",
>       {"op":"ASSIGN"},
>       ["VariableProxy",
>         {"name":"x"}
>       ],
>       ["Literal",
>         {"handle":1}
>       ]
>     ]
>   ],
>   ["IfStatement",
>     ["CompareOperation",
>       {"op":"GT"},
>       ["VariableProxy",
>         {"name":"x"}
>       ],
>       ["Literal",
>         {"handle":0}
>       ]
>     ],
>     ["Block",
>       ["ExpressionStatement",
>         ["Assignment",
>           {"op":"ASSIGN"},
>           ["VariableProxy",
>             {"name":"y"}
>           ],
>           ["BinaryOperation",
>             {"op":"ADD"},
>             ["VariableProxy",
>               {"name":"x"}
>             ],
>             ["Literal",
>               {"handle":2}
>             ]
>           ]
>         ]
>       ],
>       ["ExpressionStatement",
>         ["Call",
>           ["VariableProxy",
>             {"name":"print"}
>           ],
>           ["VariableProxy",
>             {"name":"y"}
>           ]
>         ]
>       ]
>     ],
>     ["EmptyStatement"]
>   ]
> ]
> 
> 3

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the es-discuss mailing list