@yanqirenshi/model.pad 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018-2026 Satoshi Iwasaki (yanqirenshi@gmail.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,82 @@
1
- # Temporary Holding Version
1
+ # @yanqirenshi/model.pad
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ PAD(Problem Analysis Diagram / 問題分析図。view = `d3.pad`)の**図ごとの知識**を、描画から独立させたパッケージです。
4
+ react / d3 に依存しません(依存は acorn だけ)。
5
+
6
+ | 持つもの | 入口 |
7
+ |---|---|
8
+ | JS のコードのパーサ | `parse_js_code(code)`(`PadNode` の木を **JSON の文字列**で返す) |
9
+ | 型 | `PadNode`(`sequence` / `block` / `if` / `loop` / `command` / `error`) |
10
+ | `PadNode` のスキーマ(JSON Schema 2020-12) | `SCHEMAS.node`(`PAD_NODE_SCHEMA`) |
11
+
12
+ `check`・`fieldsOf`・`applyPatch`・`movePatch` は持ちません(d3.pad の関数のドラッグは画面の中だけで、外に知らせる口が無いため)。
13
+
14
+ ## 位置づけ
15
+
16
+ ```
17
+ 利用側 ──▶ d3.pad (view。calculateLayout と描画) ──▶ model.pad(parse_js_code と PadNode) ──▶ acorn
18
+ ──▶ model.pad(d3 や React を入れずに、パーサだけを使う)
19
+ ```
20
+
21
+ - d3.pad 0.1.2 までは、ここにあるもの(`src/parsers/jsParser.ts`・`src/types.ts`)は d3.pad の中にありました。#201 で、動きを変えずに移しました
22
+ - d3.pad(0.2.0 から)は、ここから読んで、同じ名前で再公開します
23
+ - レイアウト(`calculateLayout`)と描画は、d3.pad に残しています
24
+
25
+ ## ほかの model.* との違い
26
+
27
+ - **ESM だけを出します。CommonJS 版(`dist/cjs/`)はありません**(model.terraform と同じ形)
28
+ - **Node の ESM で、バンドラを通さずにそのまま読めます。** 相対の import に `.js` を付けているためです。
29
+ d3.pad の `dist` は import に拡張子が無いので、Node の ESM では `ERR_MODULE_NOT_FOUND` で読めません(Vite などのバンドラを通せば動きます)。
30
+ model.pad は、d3 や React を入れずにパーサだけを使うためのものなので、Node でも読めるようにしました
31
+ - **model.core に依存しません。** 使う部品が無いためです
32
+
33
+ ## 使い方
34
+
35
+ ```js
36
+ import { parse_js_code } from '@yanqirenshi/model.pad'; // Node でも、ブラウザでも
37
+
38
+ const node = JSON.parse(parse_js_code(`
39
+ function classify(n) {
40
+ if (n > 0) { a(); } else { b(); }
41
+ }`));
42
+ // {
43
+ // type: 'sequence',
44
+ // children: [{ type: 'block', label: 'fn classify()', children: [{ type: 'sequence', children: [
45
+ // { type: 'if', condition: 'n > 0',
46
+ // then_block: { type: 'sequence', children: [{ type: 'command', label: 'a();' }] },
47
+ // else_block: { type: 'sequence', children: [{ type: 'command', label: 'b();' }] } },
48
+ // ] }] }],
49
+ // }
50
+ ```
51
+
52
+ `parse_js_code` の戻り値は、d3.pad 0.1.x と同じく JSON の文字列です(#201 で変えないと決めた)。`JSON.parse` してから使ってください。
53
+
54
+ ### 読み方
55
+
56
+ - acorn(`ecmaVersion: 2020`)で読み、**トップレベルの関数宣言**ごとに `block`(ラベルは `fn 名前()`)を作ります。関数でないトップレベルの文は読みません
57
+ - `if` は `then_block` と `else_block`(`else` が無いときは書かない)、`while` と `for` は `loop`、`{ }` は `sequence`、そのほかの文はソースの切り出しをラベルにした `command` です
58
+ - 関数が 1 つも無いときは `{ type: 'error', message: 'No function found' }`、構文エラーのときは `{ type: 'error', message: 'Parse error: SyntaxError: …' }` を返します(投げません)
59
+
60
+ ## スキーマ
61
+
62
+ `SCHEMAS.node` は、`PadNode` の木の JSON Schema(2020-12)です。
63
+ 6 つの形を `$defs` に書き、`oneOf` で 1 つに決めます。入れ子(`children`・`then_block`・`else_block`・`body`)は `$ref` で同じ形を読みます。
64
+
65
+ | type | 必須 |
66
+ |---|---|
67
+ | `sequence` | `children`(PadNode の配列) |
68
+ | `block` | `label`(文字列)・`children` |
69
+ | `if` | `condition`(文字列)・`then_block`。`else_block` は省ける |
70
+ | `loop` | `condition`・`body` |
71
+ | `command` | `label` |
72
+ | `error` | `message` |
73
+
74
+ 利用側の項目は、`additionalProperties` を閉じずに通します。
75
+
76
+ ## 開発
77
+
78
+ ```bash
79
+ npm run build -w @yanqirenshi/model.pad # tsc(ESM だけ)
80
+ npm test -w @yanqirenshi/model.pad # vitest
81
+ npm run typecheck -w @yanqirenshi/model.pad # tsc --noEmit
82
+ ```
@@ -0,0 +1,4 @@
1
+ export type { PadNode } from './types.js';
2
+ export { parse_js_code } from './parsers/jsParser.js';
3
+ export { PAD_NODE_SCHEMA, SCHEMAS } from './schema.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ // model.pad — d3.pad(PAD = Problem Analysis Diagram)の「図ごとの知識」。描画はしない(react / d3 に依存しない)。
2
+ //
3
+ // パーサ: parse_js_code(code)(acorn で JS を読み、PadNode の木を JSON の文字列で返す)
4
+ // 型: PadNode(sequence / block / if / loop / command / error の 6 つ)
5
+ // スキーマ: SCHEMAS / PAD_NODE_SCHEMA(PadNode の木。JSON Schema 2020-12)
6
+ //
7
+ // d3.pad の src/parsers/jsParser.ts と src/types.ts を、動きを変えずに移した(#201)。d3.pad(0.2.0 から)は、ここから読んで再公開する。
8
+ // レイアウト(calculateLayout)と描画は d3.pad に残している。
9
+ // ESM だけを出す。相対の import には .js を付けて、Node の ESM でもバンドラを通さずに読めるようにしている
10
+ // (d3 や React を入れずにパーサだけを使うためのパッケージなので)。
11
+ // parse_js_code の戻り値は、d3.pad 0.1.x と同じく JSON の文字列のまま(#201 の判断)。
12
+ // check・fieldsOf・applyPatch・movePatch は持たない。
13
+ export { parse_js_code } from './parsers/jsParser.js';
14
+ export { PAD_NODE_SCHEMA, SCHEMAS } from './schema.js';
@@ -0,0 +1,2 @@
1
+ export declare function parse_js_code(code: string): string;
2
+ //# sourceMappingURL=jsParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsParser.d.ts","sourceRoot":"","sources":["../../src/parsers/jsParser.ts"],"names":[],"mappings":"AAGA,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGlD"}
@@ -0,0 +1,101 @@
1
+ import * as acorn from 'acorn';
2
+ export function parse_js_code(code) {
3
+ const parser = new JsParser(code);
4
+ return parser.parse();
5
+ }
6
+ class JsParser {
7
+ code;
8
+ constructor(code) {
9
+ this.code = code;
10
+ }
11
+ parse() {
12
+ try {
13
+ const ast = acorn.parse(this.code, { ecmaVersion: 2020 });
14
+ const nodes = [];
15
+ // @ts-ignore
16
+ for (const node of ast.body) {
17
+ if (node.type === 'FunctionDeclaration') {
18
+ nodes.push(this.parseFunction(node));
19
+ }
20
+ }
21
+ if (nodes.length === 0) {
22
+ return JSON.stringify({ type: 'error', message: 'No function found' });
23
+ }
24
+ return JSON.stringify({ type: 'sequence', children: nodes });
25
+ }
26
+ catch (e) {
27
+ return JSON.stringify({ type: 'error', message: `Parse error: ${e}` });
28
+ }
29
+ }
30
+ parseFunction(node) {
31
+ const name = node.id?.name || 'anonymous';
32
+ const body = this.parseBlock(node.body);
33
+ return {
34
+ type: 'block',
35
+ label: `fn ${name}()`, // Use 'fn' label to match Rust style or just 'function'
36
+ children: [body]
37
+ };
38
+ }
39
+ parseBlock(node) {
40
+ const children = [];
41
+ if (node.body) {
42
+ for (const stmt of node.body) {
43
+ children.push(this.parseStmt(stmt));
44
+ }
45
+ }
46
+ return { type: 'sequence', children };
47
+ }
48
+ parseStmt(stmt) {
49
+ switch (stmt.type) {
50
+ case 'VariableDeclaration':
51
+ return { type: 'command', label: this.snippet(stmt) };
52
+ case 'ExpressionStatement':
53
+ // Remove semicolon if present for cleaner display?
54
+ // Currently just taking snippet.
55
+ return { type: 'command', label: this.snippet(stmt) };
56
+ case 'IfStatement':
57
+ return this.parseIf(stmt);
58
+ case 'WhileStatement':
59
+ return {
60
+ type: 'loop',
61
+ condition: this.snippet(stmt.test),
62
+ body: this.parseStmt(stmt.body)
63
+ };
64
+ case 'ForStatement':
65
+ let condLabel = 'for (;;) ';
66
+ if (stmt.init || stmt.test || stmt.update) {
67
+ const init = stmt.init ? this.snippet(stmt.init) : '';
68
+ const test = stmt.test ? this.snippet(stmt.test) : '';
69
+ const update = stmt.update ? this.snippet(stmt.update) : '';
70
+ // Clean up init if it has semicolon?
71
+ // this.snippet(stmt.init) includes 'let i = 0', no semi usually if it's var decl node inside for
72
+ condLabel = `for (${init}; ${test}; ${update})`;
73
+ }
74
+ return {
75
+ type: 'loop',
76
+ condition: condLabel,
77
+ body: this.parseStmt(stmt.body)
78
+ };
79
+ case 'BlockStatement':
80
+ return this.parseBlock(stmt);
81
+ case 'ReturnStatement':
82
+ return { type: 'command', label: this.snippet(stmt) };
83
+ default:
84
+ return { type: 'command', label: this.snippet(stmt) };
85
+ }
86
+ }
87
+ parseIf(stmt) {
88
+ const condition = this.snippet(stmt.test);
89
+ const thenBlock = this.parseStmt(stmt.consequent);
90
+ const elseBlock = stmt.alternate ? this.parseStmt(stmt.alternate) : undefined;
91
+ return {
92
+ type: 'if',
93
+ condition,
94
+ then_block: thenBlock,
95
+ else_block: elseBlock
96
+ };
97
+ }
98
+ snippet(node) {
99
+ return this.code.substring(node.start, node.end);
100
+ }
101
+ }
@@ -0,0 +1,242 @@
1
+ export declare const PAD_NODE_SCHEMA: {
2
+ readonly $schema: "https://json-schema.org/draft/2020-12/schema";
3
+ readonly title: "PAD のノード(PadNode)";
4
+ readonly description: "parse_js_code が返す木。sequence(並び)・block(関数など、ラベル付きの並び)・if(条件と then / else)・loop(条件と本体)・command(1 文)・error(読めなかったとき)の 6 つ。";
5
+ readonly $ref: "#/$defs/node";
6
+ readonly $defs: {
7
+ readonly node: {
8
+ readonly oneOf: readonly [{
9
+ readonly $ref: "#/$defs/sequence";
10
+ }, {
11
+ readonly $ref: "#/$defs/block";
12
+ }, {
13
+ readonly $ref: "#/$defs/if";
14
+ }, {
15
+ readonly $ref: "#/$defs/loop";
16
+ }, {
17
+ readonly $ref: "#/$defs/command";
18
+ }, {
19
+ readonly $ref: "#/$defs/error";
20
+ }];
21
+ };
22
+ readonly sequence: {
23
+ readonly type: "object";
24
+ readonly properties: {
25
+ readonly type: {
26
+ readonly const: "sequence";
27
+ };
28
+ readonly children: {
29
+ readonly type: "array";
30
+ readonly items: {
31
+ readonly $ref: "#/$defs/node";
32
+ };
33
+ };
34
+ };
35
+ readonly required: readonly ["type", "children"];
36
+ readonly additionalProperties: true;
37
+ };
38
+ readonly block: {
39
+ readonly type: "object";
40
+ readonly properties: {
41
+ readonly type: {
42
+ readonly const: "block";
43
+ };
44
+ readonly label: {
45
+ readonly type: "string";
46
+ };
47
+ readonly children: {
48
+ readonly type: "array";
49
+ readonly items: {
50
+ readonly $ref: "#/$defs/node";
51
+ };
52
+ };
53
+ };
54
+ readonly required: readonly ["type", "label", "children"];
55
+ readonly additionalProperties: true;
56
+ };
57
+ readonly if: {
58
+ readonly type: "object";
59
+ readonly properties: {
60
+ readonly type: {
61
+ readonly const: "if";
62
+ };
63
+ readonly condition: {
64
+ readonly type: "string";
65
+ };
66
+ readonly then_block: {
67
+ readonly $ref: "#/$defs/node";
68
+ };
69
+ readonly else_block: {
70
+ readonly $ref: "#/$defs/node";
71
+ };
72
+ };
73
+ readonly required: readonly ["type", "condition", "then_block"];
74
+ readonly additionalProperties: true;
75
+ };
76
+ readonly loop: {
77
+ readonly type: "object";
78
+ readonly properties: {
79
+ readonly type: {
80
+ readonly const: "loop";
81
+ };
82
+ readonly condition: {
83
+ readonly type: "string";
84
+ };
85
+ readonly body: {
86
+ readonly $ref: "#/$defs/node";
87
+ };
88
+ };
89
+ readonly required: readonly ["type", "condition", "body"];
90
+ readonly additionalProperties: true;
91
+ };
92
+ readonly command: {
93
+ readonly type: "object";
94
+ readonly properties: {
95
+ readonly type: {
96
+ readonly const: "command";
97
+ };
98
+ readonly label: {
99
+ readonly type: "string";
100
+ };
101
+ };
102
+ readonly required: readonly ["type", "label"];
103
+ readonly additionalProperties: true;
104
+ };
105
+ readonly error: {
106
+ readonly type: "object";
107
+ readonly properties: {
108
+ readonly type: {
109
+ readonly const: "error";
110
+ };
111
+ readonly message: {
112
+ readonly type: "string";
113
+ };
114
+ };
115
+ readonly required: readonly ["type", "message"];
116
+ readonly additionalProperties: true;
117
+ };
118
+ };
119
+ };
120
+ /** 名前 → JSON Schema(PadNode は 1 つの木なので、コレクションではなく node の 1 つだけ) */
121
+ export declare const SCHEMAS: {
122
+ readonly node: {
123
+ readonly $schema: "https://json-schema.org/draft/2020-12/schema";
124
+ readonly title: "PAD のノード(PadNode)";
125
+ readonly description: "parse_js_code が返す木。sequence(並び)・block(関数など、ラベル付きの並び)・if(条件と then / else)・loop(条件と本体)・command(1 文)・error(読めなかったとき)の 6 つ。";
126
+ readonly $ref: "#/$defs/node";
127
+ readonly $defs: {
128
+ readonly node: {
129
+ readonly oneOf: readonly [{
130
+ readonly $ref: "#/$defs/sequence";
131
+ }, {
132
+ readonly $ref: "#/$defs/block";
133
+ }, {
134
+ readonly $ref: "#/$defs/if";
135
+ }, {
136
+ readonly $ref: "#/$defs/loop";
137
+ }, {
138
+ readonly $ref: "#/$defs/command";
139
+ }, {
140
+ readonly $ref: "#/$defs/error";
141
+ }];
142
+ };
143
+ readonly sequence: {
144
+ readonly type: "object";
145
+ readonly properties: {
146
+ readonly type: {
147
+ readonly const: "sequence";
148
+ };
149
+ readonly children: {
150
+ readonly type: "array";
151
+ readonly items: {
152
+ readonly $ref: "#/$defs/node";
153
+ };
154
+ };
155
+ };
156
+ readonly required: readonly ["type", "children"];
157
+ readonly additionalProperties: true;
158
+ };
159
+ readonly block: {
160
+ readonly type: "object";
161
+ readonly properties: {
162
+ readonly type: {
163
+ readonly const: "block";
164
+ };
165
+ readonly label: {
166
+ readonly type: "string";
167
+ };
168
+ readonly children: {
169
+ readonly type: "array";
170
+ readonly items: {
171
+ readonly $ref: "#/$defs/node";
172
+ };
173
+ };
174
+ };
175
+ readonly required: readonly ["type", "label", "children"];
176
+ readonly additionalProperties: true;
177
+ };
178
+ readonly if: {
179
+ readonly type: "object";
180
+ readonly properties: {
181
+ readonly type: {
182
+ readonly const: "if";
183
+ };
184
+ readonly condition: {
185
+ readonly type: "string";
186
+ };
187
+ readonly then_block: {
188
+ readonly $ref: "#/$defs/node";
189
+ };
190
+ readonly else_block: {
191
+ readonly $ref: "#/$defs/node";
192
+ };
193
+ };
194
+ readonly required: readonly ["type", "condition", "then_block"];
195
+ readonly additionalProperties: true;
196
+ };
197
+ readonly loop: {
198
+ readonly type: "object";
199
+ readonly properties: {
200
+ readonly type: {
201
+ readonly const: "loop";
202
+ };
203
+ readonly condition: {
204
+ readonly type: "string";
205
+ };
206
+ readonly body: {
207
+ readonly $ref: "#/$defs/node";
208
+ };
209
+ };
210
+ readonly required: readonly ["type", "condition", "body"];
211
+ readonly additionalProperties: true;
212
+ };
213
+ readonly command: {
214
+ readonly type: "object";
215
+ readonly properties: {
216
+ readonly type: {
217
+ readonly const: "command";
218
+ };
219
+ readonly label: {
220
+ readonly type: "string";
221
+ };
222
+ };
223
+ readonly required: readonly ["type", "label"];
224
+ readonly additionalProperties: true;
225
+ };
226
+ readonly error: {
227
+ readonly type: "object";
228
+ readonly properties: {
229
+ readonly type: {
230
+ readonly const: "error";
231
+ };
232
+ readonly message: {
233
+ readonly type: "string";
234
+ };
235
+ };
236
+ readonly required: readonly ["type", "message"];
237
+ readonly additionalProperties: true;
238
+ };
239
+ };
240
+ };
241
+ };
242
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2ElB,CAAC;AAEX,mEAAmE;AACnE,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAEV,CAAC"}
package/dist/schema.js ADDED
@@ -0,0 +1,86 @@
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
+ const node = { $ref: '#/$defs/node' };
7
+ export const PAD_NODE_SCHEMA = {
8
+ $schema: 'https://json-schema.org/draft/2020-12/schema',
9
+ title: 'PAD のノード(PadNode)',
10
+ description: 'parse_js_code が返す木。sequence(並び)・block(関数など、ラベル付きの並び)・if(条件と then / else)・loop(条件と本体)・command(1 文)・error(読めなかったとき)の 6 つ。',
11
+ $ref: '#/$defs/node',
12
+ $defs: {
13
+ node: {
14
+ oneOf: [
15
+ { $ref: '#/$defs/sequence' },
16
+ { $ref: '#/$defs/block' },
17
+ { $ref: '#/$defs/if' },
18
+ { $ref: '#/$defs/loop' },
19
+ { $ref: '#/$defs/command' },
20
+ { $ref: '#/$defs/error' },
21
+ ],
22
+ },
23
+ sequence: {
24
+ type: 'object',
25
+ properties: {
26
+ type: { const: 'sequence' },
27
+ children: { type: 'array', items: node },
28
+ },
29
+ required: ['type', 'children'],
30
+ additionalProperties: true,
31
+ },
32
+ block: {
33
+ type: 'object',
34
+ properties: {
35
+ type: { const: 'block' },
36
+ label: { type: 'string' },
37
+ children: { type: 'array', items: node },
38
+ },
39
+ required: ['type', 'label', 'children'],
40
+ additionalProperties: true,
41
+ },
42
+ if: {
43
+ type: 'object',
44
+ properties: {
45
+ type: { const: 'if' },
46
+ condition: { type: 'string' },
47
+ then_block: node,
48
+ else_block: node,
49
+ },
50
+ required: ['type', 'condition', 'then_block'],
51
+ additionalProperties: true,
52
+ },
53
+ loop: {
54
+ type: 'object',
55
+ properties: {
56
+ type: { const: 'loop' },
57
+ condition: { type: 'string' },
58
+ body: node,
59
+ },
60
+ required: ['type', 'condition', 'body'],
61
+ additionalProperties: true,
62
+ },
63
+ command: {
64
+ type: 'object',
65
+ properties: {
66
+ type: { const: 'command' },
67
+ label: { type: 'string' },
68
+ },
69
+ required: ['type', 'label'],
70
+ additionalProperties: true,
71
+ },
72
+ error: {
73
+ type: 'object',
74
+ properties: {
75
+ type: { const: 'error' },
76
+ message: { type: 'string' },
77
+ },
78
+ required: ['type', 'message'],
79
+ additionalProperties: true,
80
+ },
81
+ },
82
+ };
83
+ /** 名前 → JSON Schema(PadNode は 1 つの木なので、コレクションではなく node の 1 つだけ) */
84
+ export const SCHEMAS = {
85
+ node: PAD_NODE_SCHEMA,
86
+ };
@@ -0,0 +1,24 @@
1
+ export type PadNode = {
2
+ type: 'sequence';
3
+ children: PadNode[];
4
+ } | {
5
+ type: 'block';
6
+ label: string;
7
+ children: PadNode[];
8
+ } | {
9
+ type: 'if';
10
+ condition: string;
11
+ then_block: PadNode;
12
+ else_block?: PadNode;
13
+ } | {
14
+ type: 'loop';
15
+ condition: string;
16
+ body: PadNode;
17
+ } | {
18
+ type: 'command';
19
+ label: string;
20
+ } | {
21
+ type: 'error';
22
+ message: string;
23
+ };
24
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GACb;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,OAAO,EAAE,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,EAAE,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GAC5E;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAClD;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
- "name": "@yanqirenshi/model.pad",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@yanqirenshi/model.pad",
3
+ "version": "0.1.0",
4
+ "description": "Diagram model for PAD (Problem Analysis Diagram, d3.pad) — JS code parser (acorn) into a PadNode tree, and its JSON Schema. ESM only, loadable by Node as is. No React / d3 dependency",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "typecheck": "tsc --noEmit",
22
+ "test": "vitest run",
23
+ "prepublishOnly": "npm run build"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/Foolsgolds/Assholes.git",
28
+ "directory": "packages/model.pad"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/Foolsgolds/Assholes/issues"
32
+ },
33
+ "homepage": "https://github.com/Foolsgolds/Assholes/tree/main/packages/model.pad#readme",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "acorn": "^8.15.0"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "~5.9.3",
42
+ "vitest": "^2.1.8"
43
+ },
44
+ "author": "yanqirenshi@gmail.com",
45
+ "keywords": [
46
+ "diagram",
47
+ "model",
48
+ "json-schema",
49
+ "d3.pad",
50
+ "pad",
51
+ "acorn"
52
+ ],
53
+ "license": "MIT"
54
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ // model.pad — d3.pad(PAD = Problem Analysis Diagram)の「図ごとの知識」。描画はしない(react / d3 に依存しない)。
2
+ //
3
+ // パーサ: parse_js_code(code)(acorn で JS を読み、PadNode の木を JSON の文字列で返す)
4
+ // 型: PadNode(sequence / block / if / loop / command / error の 6 つ)
5
+ // スキーマ: SCHEMAS / PAD_NODE_SCHEMA(PadNode の木。JSON Schema 2020-12)
6
+ //
7
+ // d3.pad の src/parsers/jsParser.ts と src/types.ts を、動きを変えずに移した(#201)。d3.pad(0.2.0 から)は、ここから読んで再公開する。
8
+ // レイアウト(calculateLayout)と描画は d3.pad に残している。
9
+ // ESM だけを出す。相対の import には .js を付けて、Node の ESM でもバンドラを通さずに読めるようにしている
10
+ // (d3 や React を入れずにパーサだけを使うためのパッケージなので)。
11
+ // parse_js_code の戻り値は、d3.pad 0.1.x と同じく JSON の文字列のまま(#201 の判断)。
12
+ // check・fieldsOf・applyPatch・movePatch は持たない。
13
+
14
+ export type { PadNode } from './types.js';
15
+ export { parse_js_code } from './parsers/jsParser.js';
16
+ export { PAD_NODE_SCHEMA, SCHEMAS } from './schema.js';
@@ -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 };