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