https://github.com/dmq/js-parser
JS 代码解析器 ,生成AST、生成代码、解析执行代码
https://github.com/dmq/js-parser
Last synced: 6 months ago
JSON representation
JS 代码解析器 ,生成AST、生成代码、解析执行代码
- Host: GitHub
- URL: https://github.com/dmq/js-parser
- Owner: DMQ
- Created: 2022-07-12T09:58:37.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-07-19T11:46:47.000Z (about 4 years ago)
- Last Synced: 2025-08-01T03:51:36.728Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JS 代码解析器
> 极简版本AST代码解析器,支持的js语法有限,但模块较完整:词法分析、语法分析(生成AST)、生成代码(依赖AST)、执行代码(依赖AST)
1. 本项目供学习使用,仅实现以下AST类型,(完整的AST类型有哪些?👉 [estree](https://github.com/estree/estree))
```javascript
{
Program: 'Program', // 程序
VariableDeclaration: 'VariableDeclaration', // 变量声明,可包含多个 VariableDeclarator,如:var a,b;
VariableDeclarator: 'VariableDeclarator', // 变量声明器
FunctionDeclaration: 'FunctionDeclaration', // 函数声明
Identifier: 'Identifier', // 标识符
NumericLiteral: 'NumericLiteral', // 数字字面量
StringLiteral: 'StringLiteral', // 字符串字面量
BlockStatement: 'BlockStatement', // 块级语句
ExpressionStatement: 'ExpressionStatement', // 表达式语句
AssignmentExpression: 'AssignmentExpression', // 赋值表达式
CallExpression: 'CallExpression', // 函数调用表达式
MemberExpression: 'MemberExpression', // 成员表达式,如:obj.a
};
```
2. 模块指引
- [TokenStream](./ast/token-stream.js)
- [AST Parser](./ast/ast-parser.js)
- [Code Generator](./process-ast/generator.js)
- [Code Excutor](./process-ast/executor.js)
3. 执行Demo
```
node ./demo.js
```