Created
May 11, 2018 12:24
-
-
Save 9wick/dbeae4e41e55ed9947e38d305c7f0db2 to your computer and use it in GitHub Desktop.
javascriptの構文解析をしてみる ref: https://qiita.com/wicket/items/816c8703d23945462dfd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* comment | |
*/ | |
function printTips() { | |
var myHeading = document.querySelector('h1'); | |
myHeading.textContent = 'Hello world!'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printTips() { | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "FunctionDeclaration", | |
"start": 20, | |
"end": 134, | |
"id": { | |
"type": "Identifier", | |
"start": 29, | |
"end": 38, | |
"name": "printTips" | |
}, | |
"generator": false, | |
"expression": false, | |
"params": [], | |
"body": { | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <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