Using Esprima with Rhino or Nashorn
With Rhino or Nashorn, Esprima must be loaded from its source using the load
function, such as:
- load('/path/to/esprima.js');
The module esprima
will be available as part of the global object.
The following session with Nashorn shell, jrunscript, demonstrates the usage:
- $ jrunscript
- nashorn> load('esprima.js')
- nashorn> ast = esprima.parseScript('const answer = 42')
- [object Object]
- nashorn> print(JSON.stringify(ast, null, 2))
- {
- "type": "Program",
- "body": [
- {
- "type": "VariableDeclaration",
- "declarations": [
- {
- "type": "VariableDeclarator",
- "id": {
- "type": "Identifier",
- "name": "answer"
- },
- "init": {
- "type": "Literal",
- "value": 42,
- "raw": "42"
- }
- }
- ],
- "kind": "const"
- }
- ],
- "sourceType": "script"
- }