Description Usage Arguments Details References Examples
Esprima is a high performance, standard-compliant ECMAScript parser. It has full support for ECMAScript 2017 and returns a sensible syntax tree format as standardized by ESTree project.
1 2 3 4 |
text |
a character vector with JavaScript code |
range |
Annotate each token with its zero-based start and end location |
loc |
Annotate each token with its column and row-based location |
comment |
Include every line and block comment in the output |
jsx |
Support JSX syntax |
tolerant |
Tolerate a few cases of syntax errors |
tokens |
Collect every token |
The esprima_tokenize
function returns a data frame with JavaScript tokens. The
esprima_parse
function returns the Syntax Tree in JSON format. This can be parsed to R
using e.g. jsonlite::fromJSON
.
Esprima documentation: http://esprima.readthedocs.io/en/4.0/.
1 2 3 | code <- "function test(x, y){ x = x || 1; y = y || 1; return x*y;}"
esprima_tokenize(code)
esprima_parse(code)
|
type value
1 Keyword function
2 Identifier test
3 Punctuator (
4 Identifier x
5 Punctuator ,
6 Identifier y
7 Punctuator )
8 Punctuator {
9 Identifier x
10 Punctuator =
11 Identifier x
12 Punctuator ||
13 Numeric 1
14 Punctuator ;
15 Identifier y
16 Punctuator =
17 Identifier y
18 Punctuator ||
19 Numeric 1
20 Punctuator ;
21 Keyword return
22 Identifier x
23 Punctuator *
24 Identifier y
25 Punctuator ;
26 Punctuator }
{
"type": "Program",
"body": [
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "test"
},
"params": [
{
"type": "Identifier",
"name": "x"
},
{
"type": "Identifier",
"name": "y"
}
],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "Identifier",
"name": "x"
},
"right": {
"type": "LogicalExpression",
"operator": "||",
"left": {
"type": "Identifier",
"name": "x"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
}
}
},
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "Identifier",
"name": "y"
},
"right": {
"type": "LogicalExpression",
"operator": "||",
"left": {
"type": "Identifier",
"name": "y"
},
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
}
}
},
{
"type": "ReturnStatement",
"argument": {
"type": "BinaryExpression",
"operator": "*",
"left": {
"type": "Identifier",
"name": "x"
},
"right": {
"type": "Identifier",
"name": "y"
}
}
}
]
},
"generator": false,
"expression": false,
"async": false
}
],
"sourceType": "script"
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.