https://github.com/chengluyu/makeast
A general JavaScript/TypeScript abstract syntax tree generator.
https://github.com/chengluyu/makeast
Last synced: 2 months ago
JSON representation
A general JavaScript/TypeScript abstract syntax tree generator.
- Host: GitHub
- URL: https://github.com/chengluyu/makeast
- Owner: chengluyu
- Created: 2019-12-03T19:02:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T05:26:23.000Z (over 2 years ago)
- Last Synced: 2025-04-02T08:36:15.742Z (2 months ago)
- Language: JavaScript
- Size: 246 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Makeast
Makeast is a transpiler that helps you quickly generate abstract syntax tree classes and interfaces in JavaScript/TypeScript.
## Why Use Makeast
* Quickly generate usable classes and interfaces.
* Fast prototype your idea when building parsers.
* Painless visitor pattern and factory class.## Example
Suppose you're going to build a arithmetic calculator. You can write the abstract syntax tree strucutre in a top-down manner.
```
@tag("kind", "SyntaxTreeKind")
tree SyntaxTreeNode {
node Token {
text: string
}
tree Expression {
node BinaryExpression {
operator: Token
left: Expression
right: Expression
}
node UnaryExpression {
operator: Token
operand: Expression
}
node NumericLiteral {
raw: string
parsed: number
}
}
}
```
Take a closer look of the description above.* Keyword `node` indicates a syntax tree node in a leaf position. It will be translated into an interface.
* The keyword `tree` indicate a container for one or more nodes and trees. It will be transpiled into an union type of all sub nodes.
* The decorator `@tag("kind", "SyntaxTreeKind")` means generate an enum named `SyntaxTreeNode` together with the tree and every node inside the tree will be included as an enum constant.Makeast will generate following TypeScript interfaces and enums for you.
```typescript
export enum SyntaxTreeKind {
Token,
BinaryExpression,
UnaryExpression,
NumericLiteral,
}export interface Token {
kind: SyntaxTreeKind.Token;
text: string;
}export interface BinaryExpression {
kind: SyntaxTreeKind.BinaryExpression;
operator: Token;
left: Expression;
right: Expression;
}export interface UnaryExpression {
kind: SyntaxTreeKind.UnaryExpression;
operator: Token;
operand: Expression;
}export interface NumericLiteral {
kind: SyntaxTreeKind.NumericLiteral;
raw: string;
parsed: number;
}type Expression = BinaryExpression | UnaryExpression | NumericLiteral;
type SyntaxTreeNode = Token | Expression;
```