@yanqirenshi/d3.pad 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/renderers/D3PadRenderer.d.ts +1 -1
- package/dist/renderers/D3PadRenderer.d.ts.map +1 -1
- package/dist/renderers/blocks/Point.d.ts +1 -4
- package/dist/renderers/blocks/Point.d.ts.map +1 -1
- package/dist/utils/layout.d.ts +1 -1
- package/dist/utils/layout.d.ts.map +1 -1
- package/package.json +16 -5
- package/src/index.ts +12 -12
- package/src/renderers/D3PadRenderer.tsx +145 -145
- package/src/renderers/PadNodeRenderer.ts +517 -517
- package/src/renderers/blocks/EndBlockGeometry.ts +52 -52
- package/src/renderers/blocks/EndRenderer.tsx +48 -48
- package/src/renderers/blocks/EndRendererProps.ts +5 -5
- package/src/renderers/blocks/FunctionContainerGeometry.ts +54 -54
- package/src/renderers/blocks/FunctionContainerRenderer.tsx +62 -62
- package/src/renderers/blocks/FunctionHeaderGeometry.ts +43 -43
- package/src/renderers/blocks/FunctionHeaderProps.ts +6 -6
- package/src/renderers/blocks/FunctionHeaderRenderer.tsx +47 -47
- package/src/renderers/blocks/IfBlockGeometry.ts +98 -98
- package/src/renderers/blocks/IfRenderer.tsx +88 -88
- package/src/renderers/blocks/IfRendererProps.ts +7 -7
- package/src/renderers/blocks/LoopRenderer.tsx +50 -50
- package/src/renderers/blocks/LoopRendererProps.ts +7 -7
- package/src/renderers/blocks/Point.ts +4 -5
- package/src/renderers/blocks/README.md +29 -29
- package/src/renderers/blocks/StartBlockGeometry.ts +66 -66
- package/src/renderers/blocks/StartRenderer.tsx +49 -49
- package/src/renderers/blocks/StartRendererProps.ts +5 -5
- package/src/utils/layout.ts +247 -247
- package/dist/parsers/jsParser.d.ts +0 -2
- package/dist/parsers/jsParser.d.ts.map +0 -1
- package/dist/parsers/jsParser.js +0 -101
- package/dist/types.d.ts +0 -24
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
- package/src/parsers/jsParser.ts +0 -122
- package/src/types.ts +0 -7
package/src/parsers/jsParser.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import * as acorn from 'acorn';
|
|
2
|
-
import type { PadNode } from '../types';
|
|
3
|
-
|
|
4
|
-
export function parse_js_code(code: string): string {
|
|
5
|
-
const parser = new JsParser(code);
|
|
6
|
-
return parser.parse();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class JsParser {
|
|
10
|
-
private code: string;
|
|
11
|
-
|
|
12
|
-
constructor(code: string) {
|
|
13
|
-
this.code = code;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
parse(): string {
|
|
17
|
-
try {
|
|
18
|
-
const ast = acorn.parse(this.code, { ecmaVersion: 2020 });
|
|
19
|
-
const nodes: PadNode[] = [];
|
|
20
|
-
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
for (const node of ast.body) {
|
|
23
|
-
if (node.type === 'FunctionDeclaration') {
|
|
24
|
-
nodes.push(this.parseFunction(node));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (nodes.length === 0) {
|
|
29
|
-
return JSON.stringify({ type: 'error', message: 'No function found' });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return JSON.stringify({ type: 'sequence', children: nodes });
|
|
33
|
-
} catch (e) {
|
|
34
|
-
return JSON.stringify({ type: 'error', message: `Parse error: ${e}` });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private parseFunction(node: any): PadNode {
|
|
39
|
-
const name = node.id?.name || 'anonymous';
|
|
40
|
-
const body = this.parseBlock(node.body);
|
|
41
|
-
return {
|
|
42
|
-
type: 'block',
|
|
43
|
-
label: `fn ${name}()`, // Use 'fn' label to match Rust style or just 'function'
|
|
44
|
-
children: [body]
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private parseBlock(node: any): PadNode {
|
|
49
|
-
const children: PadNode[] = [];
|
|
50
|
-
if (node.body) {
|
|
51
|
-
for (const stmt of node.body) {
|
|
52
|
-
children.push(this.parseStmt(stmt));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return { type: 'sequence', children };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private parseStmt(stmt: any): PadNode {
|
|
59
|
-
switch (stmt.type) {
|
|
60
|
-
case 'VariableDeclaration':
|
|
61
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
62
|
-
|
|
63
|
-
case 'ExpressionStatement':
|
|
64
|
-
// Remove semicolon if present for cleaner display?
|
|
65
|
-
// Currently just taking snippet.
|
|
66
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
67
|
-
|
|
68
|
-
case 'IfStatement':
|
|
69
|
-
return this.parseIf(stmt);
|
|
70
|
-
|
|
71
|
-
case 'WhileStatement':
|
|
72
|
-
return {
|
|
73
|
-
type: 'loop',
|
|
74
|
-
condition: this.snippet(stmt.test),
|
|
75
|
-
body: this.parseStmt(stmt.body)
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
case 'ForStatement':
|
|
79
|
-
let condLabel = 'for (;;) ';
|
|
80
|
-
if (stmt.init || stmt.test || stmt.update) {
|
|
81
|
-
const init = stmt.init ? this.snippet(stmt.init) : '';
|
|
82
|
-
const test = stmt.test ? this.snippet(stmt.test) : '';
|
|
83
|
-
const update = stmt.update ? this.snippet(stmt.update) : '';
|
|
84
|
-
// Clean up init if it has semicolon?
|
|
85
|
-
// this.snippet(stmt.init) includes 'let i = 0', no semi usually if it's var decl node inside for
|
|
86
|
-
condLabel = `for (${init}; ${test}; ${update})`;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
type: 'loop',
|
|
91
|
-
condition: condLabel,
|
|
92
|
-
body: this.parseStmt(stmt.body)
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
case 'BlockStatement':
|
|
96
|
-
return this.parseBlock(stmt);
|
|
97
|
-
|
|
98
|
-
case 'ReturnStatement':
|
|
99
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
100
|
-
|
|
101
|
-
default:
|
|
102
|
-
return { type: 'command', label: this.snippet(stmt) };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private parseIf(stmt: any): PadNode {
|
|
107
|
-
const condition = this.snippet(stmt.test);
|
|
108
|
-
const thenBlock = this.parseStmt(stmt.consequent);
|
|
109
|
-
const elseBlock = stmt.alternate ? this.parseStmt(stmt.alternate) : undefined;
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
type: 'if',
|
|
113
|
-
condition,
|
|
114
|
-
then_block: thenBlock,
|
|
115
|
-
else_block: elseBlock
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private snippet(node: any): string {
|
|
120
|
-
return this.code.substring(node.start, node.end);
|
|
121
|
-
}
|
|
122
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export type PadNode =
|
|
2
|
-
| { type: 'sequence'; children: PadNode[] }
|
|
3
|
-
| { type: 'block'; label: string; children: PadNode[] }
|
|
4
|
-
| { type: 'if'; condition: string; then_block: PadNode; else_block?: PadNode }
|
|
5
|
-
| { type: 'loop'; condition: string; body: PadNode }
|
|
6
|
-
| { type: 'command'; label: string }
|
|
7
|
-
| { type: 'error'; message: string };
|