Skip to content

Instantly share code, notes, and snippets.

@9wick
Created May 11, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9wick/dbeae4e41e55ed9947e38d305c7f0db2 to your computer and use it in GitHub Desktop.
Save 9wick/dbeae4e41e55ed9947e38d305c7f0db2 to your computer and use it in GitHub Desktop.
javascriptの構文解析をしてみる ref: https://qiita.com/wicket/items/816c8703d23945462dfd
/**
* comment
*/
function printTips() {
var myHeading = document.querySelector('h1');
myHeading.textContent = 'Hello world!';
}
{
"type": "Program",
"start": 0,
"end": 135,
"body": [
{
"type": "FunctionDeclaration",
"start": 20,
"end": 134,
"id": {
"type": "Identifier",
"start": 29,
"end": 38,
"name": "printTips"
},
"generator": false,
"expression": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 41,
"end": 134,
"body": [
{
"type": "VariableDeclaration",
"start": 45,
"end": 90,
"declarations": [
{
"type": "VariableDeclarator",
"start": 49,
"end": 89,
"id": {
"type": "Identifier",
"start": 49,
"end": 58,
"name": "myHeading"
},
"init": {
"type": "CallExpression",
"start": 61,
"end": 89,
"callee": {
"type": "MemberExpression",
"start": 61,
"end": 83,
"object": {
"type": "Identifier",
"start": 61,
"end": 69,
"name": "document"
},
"property": {
"type": "Identifier",
"start": 70,
"end": 83,
"name": "querySelector"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"start": 84,
"end": 88,
"value": "h1",
"raw": "'h1'"
}
]
}
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start": 93,
"end": 132,
"expression": {
"type": "AssignmentExpression",
"start": 93,
"end": 131,
"operator": "=",
"left": {
"type": "MemberExpression",
"start": 93,
"end": 114,
"object": {
"type": "Identifier",
"start": 93,
"end": 102,
"name": "myHeading"
},
"property": {
"type": "Identifier",
"start": 103,
"end": 114,
"name": "textContent"
},
"computed": false
},
"right": {
"type": "Literal",
"start": 117,
"end": 131,
"value": "Hello world!",
"raw": "'Hello world!'"
}
}
}
]
}
}
],
"sourceType": "module"
}
function printTips() {
...
}
{
"type": "FunctionDeclaration",
"start": 20,
"end": 134,
"id": {
"type": "Identifier",
"start": 29,
"end": 38,
"name": "printTips"
},
"generator": false,
"expression": false,
"params": [],
"body": {
...
}
}
// <script src="https://cdnjs.cloudflare.com/ajax/libs/acorn/5.5.3/acorn.js"></script>
// <script src="https://cdnjs.cloudflare.com/ajax/libs/acorn/5.5.3/acorn_loose.js"></script>
// <script src="https://cdnjs.cloudflare.com/ajax/libs/acorn/5.5.3/walk.js"></script>
let code = "...";
let ast;
try{
ast = acorn.parse(code, {ecmaVersion: 8}); //構文解析をしてみる(パースできないとエラー)
}catch(err){
ast = acorn.parse_dammit(code, {ecmaVersion: 8}); //構文解析をしてみる(パースできなくても無理やりパースしてエラーにならない)
}
function findAwaitKeyword(node, st, ancestors) {
// ...
}
acorn.walk.ancestor(ast, {
AwaitExpression: findAwaitKeyword
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment