@kensnzk/koyu 0.5.0 → 0.13.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.
Files changed (160) hide show
  1. package/README.ja.md +43 -11
  2. package/README.md +51 -11
  3. package/dist/axo.d.ts +16 -0
  4. package/dist/axo.d.ts.map +1 -0
  5. package/dist/axo.js +211 -0
  6. package/dist/axo.js.map +1 -0
  7. package/dist/check.d.ts +109 -0
  8. package/dist/check.d.ts.map +1 -0
  9. package/dist/check.js +957 -0
  10. package/dist/check.js.map +1 -0
  11. package/dist/cli.d.ts +3 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +410 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/diff.d.ts +105 -0
  16. package/dist/diff.d.ts.map +1 -0
  17. package/dist/diff.js +583 -0
  18. package/dist/diff.js.map +1 -0
  19. package/dist/fabric.d.ts +28 -0
  20. package/dist/fabric.d.ts.map +1 -0
  21. package/dist/fabric.js +105 -0
  22. package/dist/fabric.js.map +1 -0
  23. package/dist/graph.d.ts +118 -0
  24. package/dist/graph.d.ts.map +1 -0
  25. package/dist/graph.js +627 -0
  26. package/dist/graph.js.map +1 -0
  27. package/dist/index.d.ts +12 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +13 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/light.d.ts +15 -0
  32. package/dist/light.d.ts.map +1 -0
  33. package/dist/light.js +53 -0
  34. package/dist/light.js.map +1 -0
  35. package/dist/mcp.d.ts +3 -0
  36. package/dist/mcp.d.ts.map +1 -0
  37. package/dist/mcp.js +361 -0
  38. package/dist/mcp.js.map +1 -0
  39. package/dist/model.d.ts +379 -0
  40. package/dist/model.d.ts.map +1 -0
  41. package/dist/model.js +464 -0
  42. package/dist/model.js.map +1 -0
  43. package/dist/parse-file.d.ts +9 -0
  44. package/dist/parse-file.d.ts.map +1 -0
  45. package/dist/parse-file.js +22 -0
  46. package/dist/parse-file.js.map +1 -0
  47. package/dist/parse.d.ts +16 -0
  48. package/dist/parse.d.ts.map +1 -0
  49. package/dist/parse.js +1040 -0
  50. package/dist/parse.js.map +1 -0
  51. package/dist/plan.d.ts +10 -0
  52. package/dist/plan.d.ts.map +1 -0
  53. package/dist/plan.js +354 -0
  54. package/dist/plan.js.map +1 -0
  55. package/dist/poly.d.ts +78 -0
  56. package/dist/poly.d.ts.map +1 -0
  57. package/dist/poly.js +277 -0
  58. package/dist/poly.js.map +1 -0
  59. package/dist/site.d.ts +24 -0
  60. package/dist/site.d.ts.map +1 -0
  61. package/dist/site.js +54 -0
  62. package/dist/site.js.map +1 -0
  63. package/dist/vertical.d.ts +176 -0
  64. package/dist/vertical.d.ts.map +1 -0
  65. package/dist/vertical.js +669 -0
  66. package/dist/vertical.js.map +1 -0
  67. package/examples/basement/main.muro +84 -0
  68. package/examples/comparison/README.md +64 -1
  69. package/examples/comparison/gen-ifc4.ts +1 -1
  70. package/examples/comparison/gen-ifcx.ts +2 -2
  71. package/examples/comparison/two-rooms.ifcx +1 -1
  72. package/examples/complex/L1.muro +132 -0
  73. package/examples/complex/assets.muro +17 -0
  74. package/examples/complex/basement.muro +40 -0
  75. package/examples/complex/core.muro +40 -0
  76. package/examples/complex/hotel.muro +117 -0
  77. package/examples/complex/main.muro +67 -0
  78. package/examples/complex/office.muro +41 -0
  79. package/examples/complex/plant.muro +34 -0
  80. package/examples/complex/podium.muro +140 -0
  81. package/examples/complex/site.muro +18 -0
  82. package/examples/house/L1.muro +16 -0
  83. package/examples/house/L2.muro +19 -0
  84. package/examples/house/assets.muro +9 -0
  85. package/examples/house/main.muro +27 -0
  86. package/examples/house/site.muro +31 -0
  87. package/examples/house.muro +3 -3
  88. package/examples/mansion.muro +18 -13
  89. package/examples/office.muro +1 -1
  90. package/examples/steps/01-one-room.muro +4 -0
  91. package/examples/steps/02-two-rooms.muro +5 -0
  92. package/examples/steps/03-door.muro +9 -0
  93. package/examples/steps/04-exterior.muro +15 -0
  94. package/examples/steps/05-two-storeys.muro +27 -0
  95. package/examples/steps/06-finished.muro +30 -0
  96. package/examples/steps/README.md +25 -0
  97. package/examples/tower/L1.muro +80 -0
  98. package/examples/tower/L11.muro +49 -0
  99. package/examples/tower/L2.muro +71 -0
  100. package/examples/tower/L3.muro +31 -0
  101. package/examples/tower/assets.muro +14 -0
  102. package/examples/tower/main.muro +37 -0
  103. package/examples/tower/site-geometry.muro +7 -0
  104. package/examples/tower/site.muro +33 -0
  105. package/examples/tower/typical.muro +131 -0
  106. package/examples/two-rooms.canonical.json +30 -3
  107. package/examples/two-rooms.muro +11 -7
  108. package/package.json +29 -2
  109. package/src/axo.ts +258 -0
  110. package/src/check.ts +948 -122
  111. package/src/cli.ts +175 -12
  112. package/src/diff.ts +676 -0
  113. package/src/fabric.ts +123 -0
  114. package/src/graph.ts +451 -61
  115. package/src/index.ts +38 -2
  116. package/src/light.ts +4 -7
  117. package/src/mcp.ts +397 -0
  118. package/src/model.ts +462 -67
  119. package/src/parse-file.ts +28 -0
  120. package/src/parse.ts +682 -45
  121. package/src/plan.ts +152 -14
  122. package/src/poly.ts +295 -0
  123. package/src/site.ts +19 -11
  124. package/src/vertical.ts +884 -0
  125. package/.github/workflows/ci.yml +0 -16
  126. package/.zenodo.json +0 -20
  127. package/CITATION.cff +0 -26
  128. package/docs/decisions/0001-notation-v0.md +0 -29
  129. package/docs/decisions/0002-height-and-offsets.md +0 -27
  130. package/docs/decisions/0003-uncounted-divisions.md +0 -36
  131. package/docs/decisions/0004-typical-floors.md +0 -21
  132. package/docs/decisions/0005-zones-and-unions.md +0 -23
  133. package/docs/decisions/0006-voids-and-light.md +0 -31
  134. package/docs/decisions/0007-semi-outdoor-air.md +0 -27
  135. package/docs/decisions/0008-vocabulary-and-level-attr.md +0 -23
  136. package/docs/decisions/0009-site-and-exterior.md +0 -29
  137. package/docs/ifc-coverage.md +0 -82
  138. package/docs/ifcx-notes.md +0 -25
  139. package/docs/img/house-L1.svg +0 -80
  140. package/docs/img/house-L2.svg +0 -53
  141. package/docs/img/mansion-L1.svg +0 -113
  142. package/docs/img/mansion-L10.svg +0 -111
  143. package/docs/img/mansion-L5.svg +0 -160
  144. package/docs/img/office-L1.svg +0 -116
  145. package/docs/img/office-L2.svg +0 -109
  146. package/docs/img/two-rooms.svg +0 -39
  147. package/docs/log/2026-07-22.md +0 -13
  148. package/docs/log/2026-07-23.md +0 -51
  149. package/docs/roadmap.md +0 -55
  150. package/docs/writing-architecture.md +0 -119
  151. package/spec/notation-v0.md +0 -164
  152. package/spec/vocabulary.md +0 -62
  153. package/test/core.test.ts +0 -171
  154. package/test/design2.test.ts +0 -97
  155. package/test/house.test.ts +0 -74
  156. package/test/mansion.test.ts +0 -99
  157. package/test/office.test.ts +0 -71
  158. package/test/uncounted.test.ts +0 -120
  159. package/test/vertical.test.ts +0 -75
  160. package/tsconfig.json +0 -13
package/src/parse.ts CHANGED
@@ -1,79 +1,327 @@
1
1
  // koyu v0 — 記法パーサ
2
2
  // 一行が一文。図面が数百年運んできた抽象度を、そのままテキストにする。
3
+ // import による合成 (ADR-0010): ファイル群を重ねて一つの模型にする。
4
+ // 分担して書き、合成時のコンフリクト (パス・アセット・グリッドの重複) は言葉のエラーになる。
3
5
 
4
6
  import {
5
7
  type Area,
6
8
  type Attrs,
7
9
  type AttrValue,
8
10
  type Boundary,
11
+ DEFAULT_LANGUAGE_VERSION,
9
12
  type Edge,
10
13
  type Model,
11
14
  type Opening,
15
+ type Pt,
12
16
  type Rect,
13
17
  type Seg,
14
18
  SourceError,
15
19
  type Space,
20
+ SUPPORTED_LANGUAGE_VERSIONS,
16
21
  } from "./model.js";
22
+ import { deriveDefaultBoundaries, derivePieces } from "./graph.js";
17
23
 
18
24
  const EDGES = new Set(["N", "E", "S", "W"]);
19
25
 
20
- export function parse(source: string): Model {
21
- const model: Model = {
22
- version: "0.1",
26
+ /**
27
+ * 帯 (band, ADR-0019) の宣言 — parse の局所状態であり Model には入らない。
28
+ * 帯は「位置ではなく寸法と並び」を書く記法で、垂直の矩計 (level の積み上げ) の水平版である。
29
+ * 展開すると通常の Space になるので、下流 (check/plan/graph/diff/light/site) は帯を知らない。
30
+ */
31
+ interface BandDecl {
32
+ axis: "X" | "Y";
33
+ /** 割る向きの区間 mm */
34
+ lo: number;
35
+ hi: number;
36
+ /** 帯の両端と直交方向の両端は「書かれた綴り」のまま要素へ渡す (意味保存の要) */
37
+ loRef: string;
38
+ hiRef: string;
39
+ crossA: string;
40
+ crossB: string;
41
+ members: BandMember[];
42
+ line: number;
43
+ }
44
+
45
+ interface BandMember {
46
+ path: string;
47
+ type: string;
48
+ /** 帯の向きの寸法mm。"rest" は残りを吸収する印 (帯に高々一つ) */
49
+ w: number | "rest";
50
+ attrTokens: string[];
51
+ line: number;
52
+ }
53
+
54
+ function emptyModel(): Model {
55
+ return {
56
+ version: DEFAULT_LANGUAGE_VERSION,
23
57
  unit: "mm",
24
58
  grid: { X: { names: [], coords: [] }, Y: { names: [], coords: [] } },
25
59
  levels: {},
26
60
  spaces: new Map(),
27
61
  zones: new Map(),
62
+ assets: new Map(),
28
63
  boundaries: [],
64
+ polygons: new Map(),
65
+ columns: [],
66
+ layers: [],
29
67
  };
68
+ }
69
+
70
+ /** レイヤーの読み込み口 — import の解決。fs版は parse-file.ts が、仮想版は parseFiles が与える。
71
+ * fromKey が undefined のときは entry 自身の解決 */
72
+ export type LayerLoader = (
73
+ fromKey: string | undefined,
74
+ ref: string,
75
+ ) => { key: string; src: string };
76
+
77
+ export function parse(source: string): Model {
78
+ const model = emptyModel();
79
+ ingest(model, source, undefined, new Set(), undefined);
80
+ // 描かれた線で領域を切り分けてから、既定の壁を導く (ADR-0022 / ADR-0027)。
81
+ // 逆順だと、線で接触が消えた組にも既定境界が生まれ、線分ゼロの境界に
82
+ // 出所の無い BND04 が出る — 書いていない関係を責めることになる
83
+ derivePieces(model);
84
+ deriveDefaultBoundaries(model);
85
+ return model;
86
+ }
87
+
88
+ /** ローダーを介した合成の入口。entry もローダーで読む (ADR-0010) */
89
+ export function parseWith(loader: LayerLoader, entry: string): Model {
90
+ const model = emptyModel();
91
+ let layer: { key: string; src: string };
92
+ try {
93
+ layer = loader(undefined, entry);
94
+ } catch {
95
+ throw new SourceError(0, `ファイルが読めません: ${entry}`);
96
+ }
97
+ ingestLayer(model, layer.key, layer.src, new Set(), loader);
98
+ // 描かれた線で領域を切り分けてから、既定の壁を導く (ADR-0022 / ADR-0027)。
99
+ // 逆順だと、線で接触が消えた組にも既定境界が生まれ、線分ゼロの境界に
100
+ // 出所の無い BND04 が出る — 書いていない関係を責めることになる
101
+ derivePieces(model);
102
+ deriveDefaultBoundaries(model);
103
+ return model;
104
+ }
105
+
106
+ /** 仮想ファイル群からの合成 — fsの無い環境 (ブラウザ等) 向け。
107
+ * キーはPOSIX風の相対パス (`L1.muro`, `floors/L1.muro`)。import はキー空間の中で解決される */
108
+ export function parseFiles(files: Record<string, string>, entry: string): Model {
109
+ const map = new Map(Object.entries(files).map(([k, v]) => [normKey(k), v]));
110
+ return parseWith((from, ref) => {
111
+ const key = from === undefined ? normKey(ref) : joinKey(dirKey(from), ref);
112
+ const src = map.get(key);
113
+ if (src === undefined) throw new Error(key);
114
+ return { key, src };
115
+ }, entry);
116
+ }
117
+
118
+ function normKey(p: string): string {
119
+ return joinKey("", p);
120
+ }
121
+ function dirKey(p: string): string {
122
+ const i = p.lastIndexOf("/");
123
+ return i < 0 ? "" : p.slice(0, i);
124
+ }
125
+ function joinKey(dir: string, rel: string): string {
126
+ const out: string[] = dir ? dir.split("/") : [];
127
+ for (const seg of rel.split("/")) {
128
+ if (seg === "" || seg === ".") continue;
129
+ if (seg === "..") out.pop();
130
+ else out.push(seg);
131
+ }
132
+ return out.join("/");
133
+ }
30
134
 
135
+ function ingestLayer(
136
+ model: Model,
137
+ key: string,
138
+ src: string,
139
+ seen: Set<string>,
140
+ loader: LayerLoader | undefined,
141
+ ): void {
142
+ if (seen.has(key)) return; // 同じレイヤーは一度だけ合成される (USDのsublayerと同じ)
143
+ seen.add(key);
144
+ model.layers.push(key); // 合成への参加を要素の有無によらず記録する (grid/levelだけの層も数える)
145
+ ingest(model, src, key, seen, loader);
146
+ }
147
+
148
+ function ingest(
149
+ model: Model,
150
+ source: string,
151
+ file: string | undefined,
152
+ seen: Set<string>,
153
+ loader: LayerLoader | undefined,
154
+ ): void {
31
155
  let current: Boundary[] = [];
32
156
  let currentSpaces: Space[] = [];
157
+ let band: BandDecl | undefined; // 帯は次の非字下げ行か層の終わりで展開される (ADR-0019)
33
158
  const lines = source.split(/\r?\n/);
34
159
 
35
160
  for (let i = 0; i < lines.length; i++) {
36
- const raw = lines[i]!;
37
- const ln = i + 1;
38
- const tokens = tokenize(raw, ln);
39
- if (tokens.length === 0) continue;
40
- const indented = /^\s/.test(raw);
41
- const [head, ...rest] = tokens as [string, ...string[]];
161
+ try {
162
+ const raw = lines[i]!;
163
+ const ln = i + 1;
164
+ const tokens = tokenize(raw, ln);
165
+ if (tokens.length === 0) continue;
166
+ const indented = /^\s/.test(raw);
167
+ const [head, ...rest] = tokens as [string, ...string[]];
42
168
 
43
169
  if (indented) {
44
170
  if (head === "door" || head === "window") {
45
171
  if (current.length === 0) {
46
172
  throw new SourceError(ln, `${head} は boundary の直下に字下げして書きます`);
47
173
  }
48
- for (const b of current) b.openings.push(parseOpening(head, rest, ln));
174
+ for (const b of current) b.openings.push(parseOpening(head, rest, ln, model));
49
175
  } else if (head === "seg") {
50
176
  if (current.length === 0) {
51
177
  throw new SourceError(ln, "seg は boundary の直下に字下げして書きます");
52
178
  }
53
- for (const b of current) b.segs.push(parseSeg(rest, ln));
179
+ for (const b of current) b.segs.push(parseSeg(rest, ln, model));
180
+ } else if (head === "line") {
181
+ // 描かれた線 (ADR-0022) — 境界の実現を、隣接からの導出ではなく設計の行為で与える
182
+ if (current.length === 0) {
183
+ throw new SourceError(ln, "line は boundary の直下に字下げして書きます");
184
+ }
185
+ const drawn = parseDrawnLine(rest, ln, model);
186
+ for (const b of current) {
187
+ if (b.drawn) throw new SourceError(ln, `一つの境界に線は一本です: ${b.a} | ${b.b}`);
188
+ b.drawn = { ...drawn };
189
+ }
54
190
  } else if (head === "area") {
191
+ if (band) {
192
+ throw new SourceError(
193
+ ln,
194
+ "band の要素に area は書けません (領域が導出のため — area が要る室は位置で書きます)",
195
+ );
196
+ }
55
197
  if (currentSpaces.length === 0) {
56
198
  throw new SourceError(ln, "area は space の直下に字下げして書きます");
57
199
  }
58
200
  for (const s of currentSpaces) s.areas.push(parseArea(rest, ln, model));
201
+ } else if (head === "space") {
202
+ // 帯の要素 — 領域の代わりに幅 w: を持つ space 行 (ADR-0019)
203
+ if (!band) throw new SourceError(ln, "字下げした space は band の直下に書きます");
204
+ band.members.push(parseBandMember(rest, ln));
59
205
  } else {
60
206
  throw new SourceError(
61
207
  ln,
62
- `字下げ行に置けるのは door / window / seg / area のみです: ${head}`,
208
+ `字下げ行に置けるのは door / window / seg / line / area / space (band の要素) のみです: ${head}`,
63
209
  );
64
210
  }
65
211
  continue;
66
212
  }
67
213
 
214
+ // 帯は次の非字下げ行の直前に展開する (パス重複の検出順を宣言順に保つ)
215
+ if (band) {
216
+ expandBand(model, band, file);
217
+ band = undefined;
218
+ }
68
219
  current = [];
69
220
  currentSpaces = [];
70
221
  switch (head) {
71
222
  case "koyu": {
72
- model.version = rest[0] ?? "0.1";
223
+ const v = rest[0];
224
+ if (!v) throw new SourceError(ln, `koyu には版を書きます: koyu ${DEFAULT_LANGUAGE_VERSION}`);
225
+ if (rest.length > 1) {
226
+ throw new SourceError(ln, `koyu の版宣言に余分なトークンがあります: ${rest.slice(1).join(" ")}`);
227
+ }
228
+ if (!SUPPORTED_LANGUAGE_VERSIONS.includes(v)) {
229
+ throw new SourceError(
230
+ ln,
231
+ `対応していないkoyuの版です: ${v} (このツールの対応: ${SUPPORTED_LANGUAGE_VERSIONS.join(", ")})`,
232
+ );
233
+ }
234
+ // 版はbase層 (entry) でのみ・一度だけ宣言する — 合成順による黙った上書きを禁じる (ADR-0017)。
235
+ // gridの規律に合わせ、再宣言は同値でもエラー
236
+ if (file !== undefined && model.layers[0] !== file) {
237
+ throw new SourceError(ln, "koyu の版宣言はbase層 (entry) でのみ書きます");
238
+ }
239
+ if (model.versionDeclared) {
240
+ throw new SourceError(ln, `koyu の版は一度だけ宣言します (既に ${model.version})`);
241
+ }
242
+ model.version = v;
243
+ model.versionDeclared = true;
244
+ break;
245
+ }
246
+ case "import": {
247
+ const rel = rest[0];
248
+ if (!rel) throw new SourceError(ln, "import には相対パスを書きます: import ./assets.muro");
249
+ if (!loader) {
250
+ throw new SourceError(
251
+ ln,
252
+ "import はファイル合成 (parseFile / parseFiles / CLI) でのみ使えます",
253
+ );
254
+ }
255
+ let layer: { key: string; src: string };
256
+ try {
257
+ layer = loader(file, rel);
258
+ } catch {
259
+ throw new SourceError(ln, `ファイルが読めません: ${rel}`);
260
+ }
261
+ ingestLayer(model, layer.key, layer.src, seen, loader);
262
+ break;
263
+ }
264
+ case "asset": {
265
+ // 建具アセット (RevitのFamily / USDのReference — ADR-0010)
266
+ const aname = rest[0];
267
+ const akind = rest[1];
268
+ if (!aname || aname.includes(":") || aname.startsWith("/")) {
269
+ throw new SourceError(ln, "asset は asset <名> door|window [属性...] の形で書きます");
270
+ }
271
+ if (akind !== "door" && akind !== "window") {
272
+ throw new SourceError(ln, `asset の種別は door / window です: ${akind}`);
273
+ }
274
+ const prevA = model.assets.get(aname);
275
+ if (prevA) {
276
+ throw new SourceError(
277
+ ln,
278
+ `アセット名が重複しています: ${aname} (既出: ${prevA.file ?? "同ファイル"}:${prevA.line}行目)`,
279
+ );
280
+ }
281
+ model.assets.set(aname, {
282
+ name: aname,
283
+ kind: akind,
284
+ attrs: parseAttrs(rest.slice(2), ln),
285
+ line: ln,
286
+ ...(file ? { file } : {}),
287
+ });
288
+ break;
289
+ }
290
+ case "polygon": {
291
+ // 敷地形状 (ADR-0011) — 所与のジオメトリ。唯一、書かれる形。
292
+ // polygon /site -2600,-7000 38000,-7000 38000,15600 2000,16800 -2600,12000
293
+ const ppath = rest[0];
294
+ if (!ppath || !ppath.startsWith("/")) {
295
+ throw new SourceError(ln, "polygon は polygon /ゾーンパス x,y x,y x,y ... の形で書きます");
296
+ }
297
+ const pts = rest.slice(1).map((tok) => {
298
+ const m = /^(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$/.exec(tok);
299
+ if (!m) throw new SourceError(ln, `頂点が読めません (x,y のmm座標): ${tok}`);
300
+ return { x: Number(m[1]), y: Number(m[2]) };
301
+ });
302
+ if (pts.length < 3) throw new SourceError(ln, "polygon には頂点を3つ以上書きます");
303
+ const prevP = model.polygons.get(ppath);
304
+ if (prevP) {
305
+ throw new SourceError(
306
+ ln,
307
+ `敷地形状が重複しています: ${ppath} (既出: ${prevP.file ?? "同ファイル"}:${prevP.line}行目)`,
308
+ );
309
+ }
310
+ model.polygons.set(ppath, {
311
+ path: ppath,
312
+ points: pts,
313
+ line: ln,
314
+ ...(file ? { file } : {}),
315
+ });
73
316
  break;
74
317
  }
75
318
  case "name": {
76
- model.name = rest.join(" ");
319
+ const nm = rest.join(" ");
320
+ if (!nm) throw new SourceError(ln, "name には値を書きます");
321
+ if (model.name !== undefined && model.name !== nm) {
322
+ throw new SourceError(ln, `name は一度だけ宣言します (既に「${model.name}」— 合成時はbase層で)`);
323
+ }
324
+ model.name = nm;
77
325
  break;
78
326
  }
79
327
  case "unit": {
@@ -85,6 +333,9 @@ export function parse(source: string): Model {
85
333
  if (axis !== "X" && axis !== "Y") {
86
334
  throw new SourceError(ln, `grid の軸は X か Y です: ${axis}`);
87
335
  }
336
+ if (model.grid[axis].coords.length > 0) {
337
+ throw new SourceError(ln, `grid ${axis} は一度だけ宣言します (合成時はbase層で)`);
338
+ }
88
339
  const coords = rest.slice(1).map((t) => toNumber(t, ln, "gridの座標"));
89
340
  if (coords.length < 2) throw new SourceError(ln, "grid には座標を2つ以上書きます");
90
341
  for (let k = 1; k < coords.length; k++) {
@@ -103,9 +354,14 @@ export function parse(source: string): Model {
103
354
  if (!name) throw new SourceError(ln, "level には名前が要ります");
104
355
  const z = toNumber(rest[1] ?? "", ln, "levelの高さ(z)");
105
356
  const attrs = parseAttrs(rest.slice(2), ln);
106
- const h = takeNumber(attrs, "h");
107
- const slab = takeNumber(attrs, "slab");
108
- const pitch = takeNumber(attrs, "pitch");
357
+ const h = takeNumber(attrs, "h", ln);
358
+ const slab = takeNumber(attrs, "slab", ln);
359
+ const pitch = takeNumber(attrs, "pitch", ln);
360
+ const under = takeNumber(attrs, "underground", ln);
361
+ if (under !== undefined && under !== 0 && under !== 1) {
362
+ throw new SourceError(ln, "underground は 0 / 1 で指定します (1=地下)");
363
+ }
364
+ const ug = under === 1 ? { underground: true } : {};
109
365
 
110
366
  // 範囲宣言: level L3..L9 6700 pitch:2900 — 基準階のレベルを一度に宣言する
111
367
  const range = /^([A-Za-z]+)(\d+)\.\.([A-Za-z]+)(\d+)$/.exec(name);
@@ -123,8 +379,11 @@ export function parse(source: string): Model {
123
379
  model.levels[nm] = {
124
380
  name: nm,
125
381
  z: z + pitch * (k - Number(n1)),
382
+ line: ln,
383
+ ...(file !== undefined ? { file } : {}),
126
384
  ...(h !== undefined ? { h } : {}),
127
385
  ...(slab !== undefined ? { slab } : {}),
386
+ ...ug,
128
387
  };
129
388
  }
130
389
  break;
@@ -136,19 +395,81 @@ export function parse(source: string): Model {
136
395
  model.levels[name] = {
137
396
  name,
138
397
  z,
398
+ line: ln,
399
+ ...(file !== undefined ? { file } : {}),
139
400
  ...(h !== undefined ? { h } : {}),
140
401
  ...(slab !== undefined ? { slab } : {}),
402
+ ...ug,
403
+ };
404
+ break;
405
+ }
406
+ case "column": {
407
+ // 柱 (ADR-0023) — 寸法と階と通りだけを書く。位置は通り芯の交点から導出される
408
+ const size = toNumber(rest[0] ?? "", ln, "columnの寸法");
409
+ if (size <= 0) throw new SourceError(ln, "column の寸法は正のmmで書きます");
410
+ const span = rest[1];
411
+ if (!span) {
412
+ throw new SourceError(ln, "column は column <寸法mm> <L?..L?|レベル名> [x:通り,..] [y:通り,..] の形で書きます");
413
+ }
414
+ const levels = /\.\./.test(span)
415
+ ? resolveSpanLevels(model, span, ln)
416
+ : (() => {
417
+ if (!model.levels[span]) throw new SourceError(ln, `未宣言のレベルです: ${span}`);
418
+ return [span];
419
+ })();
420
+ const attrs = parseAttrs(rest.slice(2), ln);
421
+ const depth = takeNumber(attrs, "d", ln);
422
+ const names = (key: "x" | "y"): string[] | undefined => {
423
+ const v = takeString(attrs, key);
424
+ if (v === undefined) return undefined;
425
+ const list = v.split(",").filter(Boolean);
426
+ for (const n of list) {
427
+ if (!model.grid[key === "x" ? "X" : "Y"].names.includes(n)) {
428
+ throw new SourceError(ln, `未定義の通り名です: ${n}`);
429
+ }
430
+ }
431
+ if (list.length === 0) throw new SourceError(ln, `${key}: に通り名を書きます`);
432
+ return list;
141
433
  };
434
+ const xNames = names("x");
435
+ const yNames = names("y");
436
+ model.columns.push({
437
+ size,
438
+ ...(depth !== undefined ? { depth } : {}),
439
+ levels,
440
+ ...(xNames ? { xNames } : {}),
441
+ ...(yNames ? { yNames } : {}),
442
+ attrs,
443
+ line: ln,
444
+ ...(file ? { file } : {}),
445
+ });
446
+ break;
447
+ }
448
+ case "band": {
449
+ // 帯 (ADR-0019) — 寸法と並びを書き、位置を導く。宣言はここ、展開は次の非字下げ行の直前
450
+ band = parseBandHead(rest, ln, model);
142
451
  break;
143
452
  }
144
453
  case "space": {
145
454
  const path = rest[0];
146
455
  if (!path) throw new SourceError(ln, "space にはパスが要ります");
456
+ // w: は帯の要素の語 — 字下げを落とした要素が「領域なしの空間」として黙って通るのを防ぐ
457
+ if (rest.some((t) => t === "w:" || t.startsWith("w:"))) {
458
+ throw new SourceError(
459
+ ln,
460
+ "space に w: は書けません (幅で書く空間は band の直下に字下げします)",
461
+ );
462
+ }
147
463
  for (const [p] of expandSpan(model, [path], ln)) {
148
464
  const space = parseSpace([p!, ...rest.slice(1)], ln, model);
149
- if (model.spaces.has(space.path)) {
150
- throw new SourceError(ln, `空間パスが重複しています: ${space.path}`);
465
+ const prevS = model.spaces.get(space.path);
466
+ if (prevS) {
467
+ throw new SourceError(
468
+ ln,
469
+ `空間パスが重複しています: ${space.path} (既出: ${prevS.file ?? "同ファイル"}:${prevS.line}行目)`,
470
+ );
151
471
  }
472
+ if (file) space.file = file;
152
473
  model.spaces.set(space.path, space);
153
474
  currentSpaces.push(space);
154
475
  }
@@ -162,6 +483,7 @@ export function parse(source: string): Model {
162
483
  }
163
484
  for (const [ea, eb] of expandSpan(model, [pa, pb], ln)) {
164
485
  const b = parseBoundary([ea!, eb!, ...rest.slice(2)], ln);
486
+ if (file) b.file = file;
165
487
  model.boundaries.push(b);
166
488
  current.push(b);
167
489
  }
@@ -174,10 +496,19 @@ export function parse(source: string): Model {
174
496
  throw new SourceError(ln, "zone は zone /パス [属性...] の形で書きます");
175
497
  }
176
498
  for (const [p] of expandSpan(model, [zpath], ln)) {
177
- if (model.zones.has(p!)) {
178
- throw new SourceError(ln, `ゾーンパスが重複しています: ${p}`);
499
+ const prevZ = model.zones.get(p!);
500
+ if (prevZ) {
501
+ throw new SourceError(
502
+ ln,
503
+ `ゾーンパスが重複しています: ${p} (既出: ${prevZ.file ?? "同ファイル"}:${prevZ.line}行目)`,
504
+ );
179
505
  }
180
- model.zones.set(p!, { path: p!, attrs: parseAttrs(rest.slice(1), ln), line: ln });
506
+ model.zones.set(p!, {
507
+ path: p!,
508
+ attrs: parseAttrs(rest.slice(1), ln),
509
+ line: ln,
510
+ ...(file ? { file } : {}),
511
+ });
181
512
  }
182
513
  break;
183
514
  }
@@ -203,6 +534,7 @@ export function parse(source: string): Model {
203
534
  openings: [],
204
535
  segs: [],
205
536
  line: ln,
537
+ ...(file ? { file } : {}),
206
538
  };
207
539
  model.boundaries.push(b);
208
540
  current.push(b);
@@ -212,8 +544,25 @@ export function parse(source: string): Model {
212
544
  default:
213
545
  throw new SourceError(ln, `未知のキーワードです: ${head}`);
214
546
  }
547
+ } catch (e) {
548
+ // 合成時はどのファイルのエラーかを言葉にする
549
+ if (e instanceof SourceError && !e.file && file) {
550
+ throw new SourceError(e.line, e.raw, file);
551
+ }
552
+ throw e;
553
+ }
554
+ }
555
+ // 層の終わりで閉じる帯 (最終行が帯の要素だった場合)
556
+ if (band) {
557
+ try {
558
+ expandBand(model, band, file);
559
+ } catch (e) {
560
+ if (e instanceof SourceError && !e.file && file) {
561
+ throw new SourceError(e.line, e.raw, file);
562
+ }
563
+ throw e;
564
+ }
215
565
  }
216
- return model;
217
566
  }
218
567
 
219
568
  // ---- 各要素 ----
@@ -248,7 +597,17 @@ function parseSpace(rest: string[], ln: number, model: Model): Space {
248
597
  const seg = path.split("/")[1];
249
598
  const level = explicit ?? (seg && model.levels[seg] ? seg : undefined);
250
599
 
251
- const space: Space = { path, type, level, grids: [], rects: [], areas: [], attrs, line: ln };
600
+ const space: Space = {
601
+ path,
602
+ type,
603
+ level,
604
+ grids: [],
605
+ rects: [],
606
+ pieces: [],
607
+ areas: [],
608
+ attrs,
609
+ line: ln,
610
+ };
252
611
  for (const g of groups) {
253
612
  if (g.length === 0) continue;
254
613
  const r = parseRegion(g, ln, model);
@@ -258,6 +617,193 @@ function parseSpace(rest: string[], ln: number, model: Model): Space {
258
617
  return space;
259
618
  }
260
619
 
620
+ // ---- 帯 (band) — 寸法と並びから位置を導く (ADR-0019) ----
621
+
622
+ /** `band <軸> <X?..X?> <Y?..Y?>` の見出し行。key:value は書けない (帯は残らないので運び先が無い) */
623
+ function parseBandHead(rest: string[], ln: number, model: Model): BandDecl {
624
+ const axis = rest[0];
625
+ if (axis !== "X" && axis !== "Y") {
626
+ throw new SourceError(
627
+ ln,
628
+ `band の割る向きは X か Y です: ${axis ?? "(無し)"} (band X X1..X2 Y1..Y2 の形で書きます)`,
629
+ );
630
+ }
631
+ const tail = rest.slice(1);
632
+ const extra = tail.filter((t) => !t.includes(".."));
633
+ if (extra.length > 0) {
634
+ throw new SourceError(
635
+ ln,
636
+ `band の行に書けるのは 軸と領域だけです (属性は要素の space 行に書きます): ${extra.join(" ")}`,
637
+ );
638
+ }
639
+ // 逆順表記は space では同じ矩形の別綴りだが、帯では並びの向きが意味を持つので許さない
640
+ for (const t of tail) {
641
+ const [p, q] = t.split("..");
642
+ if (!p || !q) throw new SourceError(ln, `領域指定が読めません: ${t}`);
643
+ const rp = resolveRef(model, p, ln);
644
+ const rq = resolveRef(model, q, ln);
645
+ if (rp.axis === rq.axis && rp.coord > rq.coord) {
646
+ throw new SourceError(
647
+ ln,
648
+ `band の範囲は昇順で書きます (要素は 西→東 / 南→北 に並びます): ${t}`,
649
+ );
650
+ }
651
+ }
652
+ const r = parseRegion(tail, ln, model); // 軸の対・幅ゼロ・未定義の通り名は既存の言葉で弾かれる
653
+ return {
654
+ axis,
655
+ lo: axis === "X" ? r.rect.x1 : r.rect.y1,
656
+ hi: axis === "X" ? r.rect.x2 : r.rect.y2,
657
+ loRef: axis === "X" ? r.grid.xa : r.grid.ya,
658
+ hiRef: axis === "X" ? r.grid.xb : r.grid.yb,
659
+ crossA: axis === "X" ? r.grid.ya : r.grid.xa,
660
+ crossB: axis === "X" ? r.grid.yb : r.grid.xb,
661
+ members: [],
662
+ line: ln,
663
+ };
664
+ }
665
+
666
+ /** 帯の要素: `space <パス> <型> w:<mm>|w:rest [属性...]` — 領域の代わりに寸法を持つ space 行 */
667
+ function parseBandMember(rest: string[], ln: number): BandMember {
668
+ const path = rest[0];
669
+ if (!path?.startsWith("/")) {
670
+ throw new SourceError(ln, "band の要素は space /パス 型 w:(mm) の形で書きます");
671
+ }
672
+ const type = rest[1];
673
+ // 型の位置に k:v が来たら「型の書き忘れ」— 幅の欠落として誤報しない
674
+ if (!type || type.includes(":")) {
675
+ throw new SourceError(ln, `band の要素 ${path} に型(語彙)が要ります`);
676
+ }
677
+ let w: number | "rest" | undefined;
678
+ const attrTokens: string[] = [];
679
+ for (const t of rest.slice(2)) {
680
+ if (t === "+" || t.includes("..")) {
681
+ throw new SourceError(ln, `band の要素に領域は書けません (帯と w: が与えます): ${t}`);
682
+ }
683
+ if (t.startsWith("level:")) {
684
+ throw new SourceError(
685
+ ln,
686
+ `band の要素に level: は書けません (帯は一つのレベルの並びです): ${t}`,
687
+ );
688
+ }
689
+ if (t.startsWith("w:")) {
690
+ if (w !== undefined) throw new SourceError(ln, "属性キーが重複しています: w");
691
+ const v = t.slice(2);
692
+ if (v === "rest") w = "rest";
693
+ else if (/^\d+$/.test(v) && Number(v) > 0) w = Number(v);
694
+ else throw new SourceError(ln, `band の要素の幅は正の整数mm か rest で書きます: ${t}`);
695
+ continue;
696
+ }
697
+ attrTokens.push(t);
698
+ }
699
+ if (w === undefined) {
700
+ throw new SourceError(ln, `band の要素には幅 w:(mm) か w:rest が要ります: ${path}`);
701
+ }
702
+ return { path, type, w, attrTokens, line: ln };
703
+ }
704
+
705
+ /**
706
+ * resolveRef の逆 — 導かれた切り位置を通り参照に綴る「床規則」:
707
+ * その座標**以下**で最も大きい通り芯からのオフセット (オフセット0なら通り名だけ)。
708
+ * 上の通り芯から引く綴り (Y2-1800) は導出では生じない。
709
+ */
710
+ function spellRef(model: Model, axis: "X" | "Y", c: number, ln: number): string {
711
+ const g = model.grid[axis];
712
+ if (!Number.isInteger(c)) {
713
+ throw new SourceError(ln, `導かれた切り位置が整数mmになりません: ${c}`);
714
+ }
715
+ let i = 0;
716
+ for (let k = 0; k < g.coords.length; k++) if (g.coords[k]! <= c) i = k;
717
+ const off = c - g.coords[i]!;
718
+ if (off === 0) return g.names[i]!;
719
+ return `${g.names[i]}${off > 0 ? "+" : ""}${off}`;
720
+ }
721
+
722
+ /** 帯を通常の Space へ展開する。一方向・順序付き・決定的 — 足し算と一回の引き算だけ */
723
+ function expandBand(model: Model, band: BandDecl, file: string | undefined): void {
724
+ const { axis, lo, hi, members } = band;
725
+ if (members.length === 0) {
726
+ throw new SourceError(band.line, "band の下に space を字下げして1つ以上書きます");
727
+ }
728
+ const extent = hi - lo;
729
+ let sum = 0;
730
+ let restAt = -1;
731
+ for (let k = 0; k < members.length; k++) {
732
+ const m = members[k]!;
733
+ if (m.w === "rest") {
734
+ if (restAt >= 0) {
735
+ throw new SourceError(
736
+ m.line,
737
+ `残りを吸収する要素 (w:rest) は帯に一つだけです: ${members[restAt]!.path}, ${m.path}`,
738
+ );
739
+ }
740
+ restAt = k;
741
+ } else sum += m.w;
742
+ }
743
+ const list = members.map((m) => ` ${m.path} w:${m.w}`).join("\n");
744
+ if (sum > extent) {
745
+ throw new SourceError(
746
+ band.line,
747
+ `帯の幅 ${extent}mm に対し寸法の合計が ${sum}mm で、${sum - extent}mm 超えています\n${list}`,
748
+ );
749
+ }
750
+ const widths = members.map((m) => (m.w === "rest" ? 0 : m.w));
751
+ if (restAt >= 0) {
752
+ if (sum === extent) {
753
+ throw new SourceError(
754
+ members[restAt]!.line,
755
+ `帯の幅 ${extent}mm を他の寸法が使い切っていて、${members[restAt]!.path} (w:rest) の残りがゼロです`,
756
+ );
757
+ }
758
+ widths[restAt] = extent - sum;
759
+ } else if (sum < extent) {
760
+ throw new SourceError(
761
+ band.line,
762
+ `帯の幅 ${extent}mm に対し寸法の合計が ${sum}mm で、${extent - sum}mm 足りません ` +
763
+ `(寸法を直すか、どれかを w:rest にします)\n${list}`,
764
+ );
765
+ }
766
+
767
+ // レベルスパンは全要素をまとめて一度だけ展開する (帯の中で揃っていることを先に確かめる)
768
+ const iterations = expandSpan(model, members.map((m) => m.path), band.line);
769
+ for (const it of iterations) {
770
+ const lv = new Set(it.map((p) => p.split("/")[1]));
771
+ if (lv.size > 1) {
772
+ throw new SourceError(
773
+ band.line,
774
+ `帯の要素は同じレベルに展開します: ${it.map((p) => `${p} → ${p.split("/")[1]}`).join(", ")}`,
775
+ );
776
+ }
777
+ }
778
+
779
+ for (const it of iterations) {
780
+ let cursor = lo;
781
+ for (let k = 0; k < members.length; k++) {
782
+ const m = members[k]!;
783
+ const a = cursor;
784
+ const b = cursor + widths[k]!;
785
+ cursor = b;
786
+ // 帯の両端は書かれた綴りのまま。内側の切り位置だけを綴る (これが意味保存の要)
787
+ const aRef = a === lo ? band.loRef : spellRef(model, axis, a, m.line);
788
+ const bRef = b === hi ? band.hiRef : spellRef(model, axis, b, m.line);
789
+ const along = `${aRef}..${bRef}`;
790
+ const cross = `${band.crossA}..${band.crossB}`;
791
+ const xTok = axis === "X" ? along : cross;
792
+ const yTok = axis === "X" ? cross : along;
793
+ const space = parseSpace([it[k]!, m.type, xTok, yTok, ...m.attrTokens], m.line, model);
794
+ const prev = model.spaces.get(space.path);
795
+ if (prev) {
796
+ throw new SourceError(
797
+ m.line,
798
+ `空間パスが重複しています: ${space.path} (既出: ${prev.file ?? "同ファイル"}:${prev.line}行目)`,
799
+ );
800
+ }
801
+ if (file) space.file = file;
802
+ model.spaces.set(space.path, space);
803
+ }
804
+ }
805
+ }
806
+
261
807
  /** 領域指定 (X?..X? Y?..Y?) をグリッド参照とmm矩形に解決する */
262
808
  function parseRegion(
263
809
  regionTokens: string[],
@@ -279,12 +825,15 @@ function parseRegion(
279
825
  if (rp.axis !== rq.axis) {
280
826
  throw new SourceError(ln, `領域の両端は同じ軸の通りで指定します: ${t}`);
281
827
  }
828
+ // 逆順表記 (X2..X1) は同じ矩形の別綴り — 座標昇順に正規化して保存する (正準JSON・diffが揃う)
829
+ const [lo, hi] = rp.coord <= rq.coord ? [[rp.coord, p] as const, [rq.coord, q] as const]
830
+ : [[rq.coord, q] as const, [rp.coord, p] as const];
282
831
  if (rp.axis === "X") {
283
- xr = [rp.coord, rq.coord];
284
- xg = [p, q];
832
+ xr = [lo[0], hi[0]];
833
+ xg = [lo[1], hi[1]];
285
834
  } else {
286
- yr = [rp.coord, rq.coord];
287
- yg = [p, q];
835
+ yr = [lo[0], hi[0]];
836
+ yg = [lo[1], hi[1]];
288
837
  }
289
838
  }
290
839
  if (!xr || !yr || !xg || !yg) {
@@ -302,6 +851,42 @@ function parseRegion(
302
851
  };
303
852
  }
304
853
 
854
+ /**
855
+ * 描かれた線 (ADR-0022) — `line X3,Y1 X4+600,Y3`。
856
+ * 端点は通り語の対 (`<X通り>,<Y通り>`、どちらもオフセット可)。
857
+ * **生の座標も角度も書けない** — 位置を定めるのは常に線であり、線の端点は通りである
858
+ */
859
+ function parseDrawnLine(
860
+ rest: string[],
861
+ ln: number,
862
+ model: Model,
863
+ ): { aRef: string; bRef: string; a: Pt; b: Pt; line: number } {
864
+ if (rest.length !== 2) {
865
+ throw new SourceError(ln, "line は line <始点> <終点> の形で書きます (例: line X3,Y1 X4,Y3)");
866
+ }
867
+ const [aRef, bRef] = rest as [string, string];
868
+ const a = resolvePoint(model, aRef, ln);
869
+ const b = resolvePoint(model, bRef, ln);
870
+ if (Math.abs(a.x - b.x) < 0.5 && Math.abs(a.y - b.y) < 0.5) {
871
+ throw new SourceError(ln, `line の両端が同じ点です: ${aRef}`);
872
+ }
873
+ return { aRef, bRef, a, b, line: ln };
874
+ }
875
+
876
+ /** 通り語の対 `X3,Y1` / `X3+600,Y2-900` を点へ */
877
+ function resolvePoint(model: Model, token: string, ln: number): Pt {
878
+ const parts = token.split(",");
879
+ if (parts.length !== 2) {
880
+ throw new SourceError(ln, `点は <X通り>,<Y通り> の形で書きます: ${token}`);
881
+ }
882
+ const p = resolveRef(model, parts[0]!, ln);
883
+ const q = resolveRef(model, parts[1]!, ln);
884
+ if (p.axis !== "X" || q.axis !== "Y") {
885
+ throw new SourceError(ln, `点はX通り,Y通りの順で書きます: ${token}`);
886
+ }
887
+ return { x: p.coord, y: q.coord };
888
+ }
889
+
305
890
  /** 数えない分節: 室内の領域 (床材の切替など) */
306
891
  function parseArea(rest: string[], ln: number, model: Model): Area {
307
892
  const regionTokens = rest.filter((t) => t.includes(".."));
@@ -311,16 +896,37 @@ function parseArea(rest: string[], ln: number, model: Model): Area {
311
896
  }
312
897
 
313
898
  /** 数えない分節: 境界上の区間 (壁材の途中変更など) */
314
- function parseSeg(rest: string[], ln: number): Seg {
899
+ function parseSeg(rest: string[], ln: number, model: Model): Seg {
315
900
  const attrs = parseAttrs(rest, ln);
316
- const w = takeNumber(attrs, "w");
901
+ const w = takeNumber(attrs, "w", ln);
317
902
  if (w === undefined || w <= 0) {
318
903
  throw new SourceError(ln, "seg には幅 w:(mm) が要ります");
319
904
  }
320
- const at = takeNumber(attrs, "at") ?? 0.5;
321
- if (at < 0 || at > 1) throw new SourceError(ln, "at は 0..1 で指定します");
905
+ const at = parseAt(attrs, ln, model);
322
906
  const edge = takeEdge(attrs, ln);
323
- return { w, at, ...(edge ? { edge } : {}), attrs, line: ln };
907
+ return { w, ...at, ...(edge ? { edge } : {}), attrs, line: ln };
908
+ }
909
+
910
+ /**
911
+ * 位置指定: at は 0..1 の比率 (クランプされる) か、通り参照 (at:X2+450 — 明示位置)。
912
+ * 明示位置ははみ出しをクランプせずエラーにする (placeBand)
913
+ */
914
+ function parseAt(
915
+ attrs: Attrs,
916
+ ln: number,
917
+ model: Model,
918
+ ): { at: number; atRef?: string; atAbs?: number; atAxis?: "X" | "Y" } {
919
+ const v = attrs["at"];
920
+ if (v === undefined) return { at: 0.5 };
921
+ delete attrs["at"];
922
+ if (typeof v === "number") {
923
+ if (v < 0 || v > 1) {
924
+ throw new SourceError(ln, "at は 0..1 の比率か、通り参照 (at:X2+450) で指定します");
925
+ }
926
+ return { at: v };
927
+ }
928
+ const r = resolveRef(model, v, ln);
929
+ return { at: 0.5, atRef: v, atAbs: r.coord, atAxis: r.axis };
324
930
  }
325
931
 
326
932
  /** レベルのスパン (L2..L9) を、宣言済みレベルのz順の並びに解決する */
@@ -385,7 +991,7 @@ function parseBoundary(rest: string[], ln: number): Boundary {
385
991
  throw new SourceError(ln, "boundary は boundary /パスA /パスB [属性...] の形で書きます");
386
992
  }
387
993
  const attrs = parseAttrs(rest.slice(2), ln);
388
- const t = takeNumber(attrs, "t");
994
+ const t = takeNumber(attrs, "t", ln);
389
995
  const kindRaw = takeString(attrs, "type") ?? "wall";
390
996
  if (!["wall", "open", "stair", "shaft", "void"].includes(kindRaw)) {
391
997
  throw new SourceError(
@@ -393,7 +999,7 @@ function parseBoundary(rest: string[], ln: number): Boundary {
393
999
  `boundary の type は wall / open / stair / shaft / void です: ${kindRaw}`,
394
1000
  );
395
1001
  }
396
- const air = takeNumber(attrs, "air");
1002
+ const air = takeNumber(attrs, "air", ln);
397
1003
  if (air !== undefined && air !== 0 && air !== 1) {
398
1004
  throw new SourceError(ln, "air は 0 / 1 で指定します (1=遮蔽しない: 手すり・柵など)");
399
1005
  }
@@ -412,15 +1018,37 @@ function parseBoundary(rest: string[], ln: number): Boundary {
412
1018
  };
413
1019
  }
414
1020
 
415
- function parseOpening(kind: "door" | "window", rest: string[], ln: number): Opening {
416
- const attrs = parseAttrs(rest, ln);
417
- const w = takeNumber(attrs, "w");
1021
+ function parseOpening(
1022
+ kind: "door" | "window",
1023
+ rest: string[],
1024
+ ln: number,
1025
+ model: Model,
1026
+ ): Opening {
1027
+ // 先頭の非 key:value トークンは建具アセット参照 (Instance←Reference — ADR-0010)。
1028
+ // アセットの属性を既定とし、インスタンスの属性が上書きする
1029
+ let ref: string | undefined;
1030
+ let tokens = rest;
1031
+ if (rest[0] && !rest[0].includes(":") && !rest[0].startsWith("/")) {
1032
+ ref = rest[0];
1033
+ tokens = rest.slice(1);
1034
+ }
1035
+ const attrs: Attrs = {};
1036
+ if (ref) {
1037
+ const asset = model.assets.get(ref);
1038
+ if (!asset) throw new SourceError(ln, `未定義の建具アセットです: ${ref}`);
1039
+ if (asset.kind !== kind) {
1040
+ throw new SourceError(ln, `アセット ${ref} は ${asset.kind} です (${kind} として使えません)`);
1041
+ }
1042
+ Object.assign(attrs, asset.attrs);
1043
+ }
1044
+ Object.assign(attrs, parseAttrs(tokens, ln));
1045
+
1046
+ const w = takeNumber(attrs, "w", ln);
418
1047
  if (w === undefined || w <= 0) {
419
- throw new SourceError(ln, `${kind} には幅 w:(mm) が要ります`);
1048
+ throw new SourceError(ln, `${kind} には幅 w:(mm) が要ります (アセット側でも可)`);
420
1049
  }
421
- const h = takeNumber(attrs, "h");
422
- const at = takeNumber(attrs, "at") ?? 0.5;
423
- if (at < 0 || at > 1) throw new SourceError(ln, "at は 0..1 で指定します");
1050
+ const h = takeNumber(attrs, "h", ln);
1051
+ const at = parseAt(attrs, ln, model);
424
1052
  const edge = takeEdge(attrs, ln);
425
1053
  const hingeRaw = takeString(attrs, "hinge");
426
1054
  if (hingeRaw !== undefined && !EDGES.has(hingeRaw)) {
@@ -432,9 +1060,10 @@ function parseOpening(kind: "door" | "window", rest: string[], ln: number): Open
432
1060
  }
433
1061
  return {
434
1062
  kind,
1063
+ ...(ref ? { ref } : {}),
435
1064
  w,
436
1065
  ...(h !== undefined ? { h } : {}),
437
- at,
1066
+ ...at,
438
1067
  ...(edge ? { edge } : {}),
439
1068
  ...(hingeRaw ? { hinge: hingeRaw as Edge } : {}),
440
1069
  ...(swingRaw ? { swing: swingRaw as "a" | "b" } : {}),
@@ -476,6 +1105,10 @@ function parseAttrs(tokens: string[], ln: number): Attrs {
476
1105
  const key = t.slice(0, idx);
477
1106
  const rawVal = t.slice(idx + 1);
478
1107
  if (rawVal === "") throw new SourceError(ln, `属性 ${key} に値がありません`);
1108
+ if (attrs[key] !== undefined) {
1109
+ // 後勝ちの黙認はtypoとマージ事故を隠す — 同一行内の重複はエラー (ADR-0013)
1110
+ throw new SourceError(ln, `属性キーが重複しています: ${key}`);
1111
+ }
479
1112
  attrs[key] = maybeNumber(rawVal);
480
1113
  }
481
1114
  return attrs;
@@ -490,11 +1123,15 @@ function toNumber(v: string, ln: number, what: string): number {
490
1123
  return Number(v);
491
1124
  }
492
1125
 
493
- function takeNumber(attrs: Attrs, key: string): number | undefined {
1126
+ function takeNumber(attrs: Attrs, key: string, ln: number): number | undefined {
494
1127
  const v = attrs[key];
495
1128
  if (v === undefined) return undefined;
496
1129
  delete attrs[key];
497
- return typeof v === "number" ? v : Number.NaN;
1130
+ if (typeof v !== "number") {
1131
+ // NaNの黙認はcheck緑のまま導出を壊す (typo h:24O0 など) — その場のエラーにする
1132
+ throw new SourceError(ln, `属性 ${key} は数値で書きます: ${v}`);
1133
+ }
1134
+ return v;
498
1135
  }
499
1136
 
500
1137
  function takeString(attrs: Attrs, key: string): string | undefined {