@yanqirenshi/model.pad 0.0.0-stage → 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 +149 -2
- package/dist/dhn/dhnDocumentToPadNodes.d.ts +27 -0
- package/dist/dhn/dhnDocumentToPadNodes.d.ts.map +1 -0
- package/dist/dhn/dhnDocumentToPadNodes.js +435 -0
- package/dist/ids.d.ts +3 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +34 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/pad/padTreeToPadNodes.d.ts +8 -0
- package/dist/pad/padTreeToPadNodes.d.ts.map +1 -0
- package/dist/pad/padTreeToPadNodes.js +43 -0
- package/dist/parsers/jsParser.d.ts +2 -0
- package/dist/parsers/jsParser.d.ts.map +1 -0
- package/dist/parsers/jsParser.js +101 -0
- package/dist/schema.d.ts +242 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +86 -0
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +54 -5
- package/src/dhn/dhnDocumentToPadNodes.ts +491 -0
- package/src/ids.ts +36 -0
- package/src/index.ts +20 -0
- package/src/pad/padTreeToPadNodes.ts +74 -0
- package/src/parsers/jsParser.ts +122 -0
- package/src/schema.ts +89 -0
- package/src/types.ts +7 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as acorn from 'acorn';
|
|
2
|
+
import type { PadNode } from '../types.js'; // Node の ESM でそのまま読めるよう、拡張子を付ける(#201)
|
|
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/schema.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// PadNode(parse_js_code の戻り値を JSON.parse したもの)の JSON Schema(2020-12)。
|
|
2
|
+
// type で分かれる 6 つの形(sequence / block / if / loop / command / error)を $defs に書き、入れ子は $ref で読む。
|
|
3
|
+
//
|
|
4
|
+
// 項目は PadNode の型(src/types.ts)に合わせる。else_block だけが省ける(parse_js_code は else が無いと書かない)。
|
|
5
|
+
// 利用側の項目は、additionalProperties を閉じずに通す(ほかの model.* と同じ)。
|
|
6
|
+
|
|
7
|
+
const node = { $ref: '#/$defs/node' } as const;
|
|
8
|
+
|
|
9
|
+
export const PAD_NODE_SCHEMA = {
|
|
10
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
11
|
+
title: 'PAD のノード(PadNode)',
|
|
12
|
+
description: 'parse_js_code が返す木。sequence(並び)・block(関数など、ラベル付きの並び)・if(条件と then / else)・loop(条件と本体)・command(1 文)・error(読めなかったとき)の 6 つ。',
|
|
13
|
+
$ref: '#/$defs/node',
|
|
14
|
+
$defs: {
|
|
15
|
+
node: {
|
|
16
|
+
oneOf: [
|
|
17
|
+
{ $ref: '#/$defs/sequence' },
|
|
18
|
+
{ $ref: '#/$defs/block' },
|
|
19
|
+
{ $ref: '#/$defs/if' },
|
|
20
|
+
{ $ref: '#/$defs/loop' },
|
|
21
|
+
{ $ref: '#/$defs/command' },
|
|
22
|
+
{ $ref: '#/$defs/error' },
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
sequence: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
type: { const: 'sequence' },
|
|
29
|
+
children: { type: 'array', items: node },
|
|
30
|
+
},
|
|
31
|
+
required: ['type', 'children'],
|
|
32
|
+
additionalProperties: true,
|
|
33
|
+
},
|
|
34
|
+
block: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
type: { const: 'block' },
|
|
38
|
+
label: { type: 'string' },
|
|
39
|
+
children: { type: 'array', items: node },
|
|
40
|
+
},
|
|
41
|
+
required: ['type', 'label', 'children'],
|
|
42
|
+
additionalProperties: true,
|
|
43
|
+
},
|
|
44
|
+
if: {
|
|
45
|
+
type: 'object',
|
|
46
|
+
properties: {
|
|
47
|
+
type: { const: 'if' },
|
|
48
|
+
condition: { type: 'string' },
|
|
49
|
+
then_block: node,
|
|
50
|
+
else_block: node,
|
|
51
|
+
},
|
|
52
|
+
required: ['type', 'condition', 'then_block'],
|
|
53
|
+
additionalProperties: true,
|
|
54
|
+
},
|
|
55
|
+
loop: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
type: { const: 'loop' },
|
|
59
|
+
condition: { type: 'string' },
|
|
60
|
+
body: node,
|
|
61
|
+
},
|
|
62
|
+
required: ['type', 'condition', 'body'],
|
|
63
|
+
additionalProperties: true,
|
|
64
|
+
},
|
|
65
|
+
command: {
|
|
66
|
+
type: 'object',
|
|
67
|
+
properties: {
|
|
68
|
+
type: { const: 'command' },
|
|
69
|
+
label: { type: 'string' },
|
|
70
|
+
},
|
|
71
|
+
required: ['type', 'label'],
|
|
72
|
+
additionalProperties: true,
|
|
73
|
+
},
|
|
74
|
+
error: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
properties: {
|
|
77
|
+
type: { const: 'error' },
|
|
78
|
+
message: { type: 'string' },
|
|
79
|
+
},
|
|
80
|
+
required: ['type', 'message'],
|
|
81
|
+
additionalProperties: true,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
/** 名前 → JSON Schema(PadNode は 1 つの木なので、コレクションではなく node の 1 つだけ) */
|
|
87
|
+
export const SCHEMAS = {
|
|
88
|
+
node: PAD_NODE_SCHEMA,
|
|
89
|
+
} as const;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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 };
|