AST in JSON format

Kevin Curtis kevinc1846 at googlemail.com
Wed Oct 14 23:26:50 PDT 2009


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).

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);
}


--- 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


More information about the es-discuss mailing list