@kensnzk/koyu 0.5.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 (61) hide show
  1. package/.github/workflows/ci.yml +16 -0
  2. package/.zenodo.json +20 -0
  3. package/CITATION.cff +26 -0
  4. package/LICENSE +73 -0
  5. package/NOTICE +2 -0
  6. package/README.ja.md +71 -0
  7. package/README.md +72 -0
  8. package/docs/decisions/0001-notation-v0.md +29 -0
  9. package/docs/decisions/0002-height-and-offsets.md +27 -0
  10. package/docs/decisions/0003-uncounted-divisions.md +36 -0
  11. package/docs/decisions/0004-typical-floors.md +21 -0
  12. package/docs/decisions/0005-zones-and-unions.md +23 -0
  13. package/docs/decisions/0006-voids-and-light.md +31 -0
  14. package/docs/decisions/0007-semi-outdoor-air.md +27 -0
  15. package/docs/decisions/0008-vocabulary-and-level-attr.md +23 -0
  16. package/docs/decisions/0009-site-and-exterior.md +29 -0
  17. package/docs/ifc-coverage.md +82 -0
  18. package/docs/ifcx-notes.md +25 -0
  19. package/docs/img/house-L1.svg +80 -0
  20. package/docs/img/house-L2.svg +53 -0
  21. package/docs/img/mansion-L1.svg +113 -0
  22. package/docs/img/mansion-L10.svg +111 -0
  23. package/docs/img/mansion-L5.svg +160 -0
  24. package/docs/img/office-L1.svg +116 -0
  25. package/docs/img/office-L2.svg +109 -0
  26. package/docs/img/two-rooms.svg +39 -0
  27. package/docs/log/2026-07-22.md +13 -0
  28. package/docs/log/2026-07-23.md +51 -0
  29. package/docs/roadmap.md +55 -0
  30. package/docs/writing-architecture.md +119 -0
  31. package/examples/comparison/README.md +38 -0
  32. package/examples/comparison/gen-ifc4.ts +133 -0
  33. package/examples/comparison/gen-ifcx.ts +134 -0
  34. package/examples/comparison/two-rooms.ifc +152 -0
  35. package/examples/comparison/two-rooms.ifcx +1030 -0
  36. package/examples/comparison/validate-ifc.ts +33 -0
  37. package/examples/house.muro +89 -0
  38. package/examples/mansion.muro +187 -0
  39. package/examples/office.muro +110 -0
  40. package/examples/two-rooms.canonical.json +111 -0
  41. package/examples/two-rooms.muro +22 -0
  42. package/package.json +33 -0
  43. package/spec/notation-v0.md +164 -0
  44. package/spec/vocabulary.md +62 -0
  45. package/src/check.ts +302 -0
  46. package/src/cli.ts +281 -0
  47. package/src/graph.ts +306 -0
  48. package/src/index.ts +8 -0
  49. package/src/light.ts +66 -0
  50. package/src/model.ts +367 -0
  51. package/src/parse.ts +512 -0
  52. package/src/plan.ts +334 -0
  53. package/src/site.ts +82 -0
  54. package/test/core.test.ts +171 -0
  55. package/test/design2.test.ts +97 -0
  56. package/test/house.test.ts +74 -0
  57. package/test/mansion.test.ts +99 -0
  58. package/test/office.test.ts +71 -0
  59. package/test/uncounted.test.ts +120 -0
  60. package/test/vertical.test.ts +75 -0
  61. package/tsconfig.json +13 -0
package/src/parse.ts ADDED
@@ -0,0 +1,512 @@
1
+ // koyu v0 — 記法パーサ
2
+ // 一行が一文。図面が数百年運んできた抽象度を、そのままテキストにする。
3
+
4
+ import {
5
+ type Area,
6
+ type Attrs,
7
+ type AttrValue,
8
+ type Boundary,
9
+ type Edge,
10
+ type Model,
11
+ type Opening,
12
+ type Rect,
13
+ type Seg,
14
+ SourceError,
15
+ type Space,
16
+ } from "./model.js";
17
+
18
+ const EDGES = new Set(["N", "E", "S", "W"]);
19
+
20
+ export function parse(source: string): Model {
21
+ const model: Model = {
22
+ version: "0.1",
23
+ unit: "mm",
24
+ grid: { X: { names: [], coords: [] }, Y: { names: [], coords: [] } },
25
+ levels: {},
26
+ spaces: new Map(),
27
+ zones: new Map(),
28
+ boundaries: [],
29
+ };
30
+
31
+ let current: Boundary[] = [];
32
+ let currentSpaces: Space[] = [];
33
+ const lines = source.split(/\r?\n/);
34
+
35
+ 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[]];
42
+
43
+ if (indented) {
44
+ if (head === "door" || head === "window") {
45
+ if (current.length === 0) {
46
+ throw new SourceError(ln, `${head} は boundary の直下に字下げして書きます`);
47
+ }
48
+ for (const b of current) b.openings.push(parseOpening(head, rest, ln));
49
+ } else if (head === "seg") {
50
+ if (current.length === 0) {
51
+ throw new SourceError(ln, "seg は boundary の直下に字下げして書きます");
52
+ }
53
+ for (const b of current) b.segs.push(parseSeg(rest, ln));
54
+ } else if (head === "area") {
55
+ if (currentSpaces.length === 0) {
56
+ throw new SourceError(ln, "area は space の直下に字下げして書きます");
57
+ }
58
+ for (const s of currentSpaces) s.areas.push(parseArea(rest, ln, model));
59
+ } else {
60
+ throw new SourceError(
61
+ ln,
62
+ `字下げ行に置けるのは door / window / seg / area のみです: ${head}`,
63
+ );
64
+ }
65
+ continue;
66
+ }
67
+
68
+ current = [];
69
+ currentSpaces = [];
70
+ switch (head) {
71
+ case "koyu": {
72
+ model.version = rest[0] ?? "0.1";
73
+ break;
74
+ }
75
+ case "name": {
76
+ model.name = rest.join(" ");
77
+ break;
78
+ }
79
+ case "unit": {
80
+ if (rest[0] !== "mm") throw new SourceError(ln, `v0の単位はmmのみです: ${rest[0]}`);
81
+ break;
82
+ }
83
+ case "grid": {
84
+ const axis = rest[0];
85
+ if (axis !== "X" && axis !== "Y") {
86
+ throw new SourceError(ln, `grid の軸は X か Y です: ${axis}`);
87
+ }
88
+ const coords = rest.slice(1).map((t) => toNumber(t, ln, "gridの座標"));
89
+ if (coords.length < 2) throw new SourceError(ln, "grid には座標を2つ以上書きます");
90
+ for (let k = 1; k < coords.length; k++) {
91
+ if (coords[k]! <= coords[k - 1]!) {
92
+ throw new SourceError(ln, "grid の座標は昇順で書きます");
93
+ }
94
+ }
95
+ model.grid[axis] = {
96
+ names: coords.map((_, k) => `${axis}${k + 1}`),
97
+ coords,
98
+ };
99
+ break;
100
+ }
101
+ case "level": {
102
+ const name = rest[0];
103
+ if (!name) throw new SourceError(ln, "level には名前が要ります");
104
+ const z = toNumber(rest[1] ?? "", ln, "levelの高さ(z)");
105
+ 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");
109
+
110
+ // 範囲宣言: level L3..L9 6700 pitch:2900 — 基準階のレベルを一度に宣言する
111
+ const range = /^([A-Za-z]+)(\d+)\.\.([A-Za-z]+)(\d+)$/.exec(name);
112
+ if (range) {
113
+ const [, p1, n1, p2, n2] = range;
114
+ if (p1 !== p2 || Number(n1) >= Number(n2)) {
115
+ throw new SourceError(ln, `レベル範囲が読めません: ${name}`);
116
+ }
117
+ if (pitch === undefined || pitch <= 0) {
118
+ throw new SourceError(ln, `レベル範囲には pitch:(階高mm) が要ります: ${name}`);
119
+ }
120
+ for (let k = Number(n1); k <= Number(n2); k++) {
121
+ const nm = `${p1}${k}`;
122
+ if (model.levels[nm]) throw new SourceError(ln, `レベルが重複しています: ${nm}`);
123
+ model.levels[nm] = {
124
+ name: nm,
125
+ z: z + pitch * (k - Number(n1)),
126
+ ...(h !== undefined ? { h } : {}),
127
+ ...(slab !== undefined ? { slab } : {}),
128
+ };
129
+ }
130
+ break;
131
+ }
132
+ if (pitch !== undefined) {
133
+ throw new SourceError(ln, "pitch はレベル範囲 (L?..L?) の宣言でのみ使えます");
134
+ }
135
+ if (model.levels[name]) throw new SourceError(ln, `レベルが重複しています: ${name}`);
136
+ model.levels[name] = {
137
+ name,
138
+ z,
139
+ ...(h !== undefined ? { h } : {}),
140
+ ...(slab !== undefined ? { slab } : {}),
141
+ };
142
+ break;
143
+ }
144
+ case "space": {
145
+ const path = rest[0];
146
+ if (!path) throw new SourceError(ln, "space にはパスが要ります");
147
+ for (const [p] of expandSpan(model, [path], ln)) {
148
+ const space = parseSpace([p!, ...rest.slice(1)], ln, model);
149
+ if (model.spaces.has(space.path)) {
150
+ throw new SourceError(ln, `空間パスが重複しています: ${space.path}`);
151
+ }
152
+ model.spaces.set(space.path, space);
153
+ currentSpaces.push(space);
154
+ }
155
+ break;
156
+ }
157
+ case "boundary": {
158
+ const pa = rest[0];
159
+ const pb = rest[1];
160
+ if (!pa || !pb) {
161
+ throw new SourceError(ln, "boundary は boundary /パスA /パスB [属性...] の形で書きます");
162
+ }
163
+ for (const [ea, eb] of expandSpan(model, [pa, pb], ln)) {
164
+ const b = parseBoundary([ea!, eb!, ...rest.slice(2)], ln);
165
+ model.boundaries.push(b);
166
+ current.push(b);
167
+ }
168
+ break;
169
+ }
170
+ case "zone": {
171
+ // 数える集約 — 住戸・部門など。幾何は持たず、パス接頭辞で空間を束ねる
172
+ const zpath = rest[0];
173
+ if (!zpath || !zpath.startsWith("/")) {
174
+ throw new SourceError(ln, "zone は zone /パス [属性...] の形で書きます");
175
+ }
176
+ for (const [p] of expandSpan(model, [zpath], ln)) {
177
+ if (model.zones.has(p!)) {
178
+ throw new SourceError(ln, `ゾーンパスが重複しています: ${p}`);
179
+ }
180
+ model.zones.set(p!, { path: p!, attrs: parseAttrs(rest.slice(1), ln), line: ln });
181
+ }
182
+ break;
183
+ }
184
+ case "stack": {
185
+ // 垂直に連続する空間列: stack ev L1..L10 type:shaft
186
+ const leaf = rest[0];
187
+ const span = rest[1];
188
+ if (!leaf || leaf.startsWith("/") || !span) {
189
+ throw new SourceError(ln, "stack は stack <名前> <L?..L?> type:stair|shaft の形で書きます");
190
+ }
191
+ const levels = resolveSpanLevels(model, span, ln);
192
+ const attrs = parseAttrs(rest.slice(2), ln);
193
+ const kind = takeString(attrs, "type");
194
+ if (kind !== "stair" && kind !== "shaft" && kind !== "void") {
195
+ throw new SourceError(ln, `stack の type は stair / shaft / void です: ${kind}`);
196
+ }
197
+ for (let i = 0; i + 1 < levels.length; i++) {
198
+ const b: Boundary = {
199
+ a: `/${levels[i]!}/${leaf}`,
200
+ b: `/${levels[i + 1]!}/${leaf}`,
201
+ kind,
202
+ attrs: { ...attrs },
203
+ openings: [],
204
+ segs: [],
205
+ line: ln,
206
+ };
207
+ model.boundaries.push(b);
208
+ current.push(b);
209
+ }
210
+ break;
211
+ }
212
+ default:
213
+ throw new SourceError(ln, `未知のキーワードです: ${head}`);
214
+ }
215
+ }
216
+ return model;
217
+ }
218
+
219
+ // ---- 各要素 ----
220
+
221
+ function parseSpace(rest: string[], ln: number, model: Model): Space {
222
+ const path = rest[0];
223
+ if (!path || !path.startsWith("/")) {
224
+ throw new SourceError(ln, "space は space /パス 型 [X?..X? Y?..Y? [+ ...]] の形で書きます");
225
+ }
226
+ const type = rest[1];
227
+ if (!type) throw new SourceError(ln, `space ${path} に型(語彙)が要ります`);
228
+
229
+ // 領域は「+」区切りで複数書ける (L字などの合併)
230
+ const groups: string[][] = [[]];
231
+ const attrTokens: string[] = [];
232
+ for (const t of rest.slice(2)) {
233
+ if (t === "+") {
234
+ groups.push([]);
235
+ } else if (t.includes("..")) {
236
+ groups[groups.length - 1]!.push(t);
237
+ } else {
238
+ attrTokens.push(t);
239
+ }
240
+ }
241
+ const attrs = parseAttrs(attrTokens, ln);
242
+
243
+ // レベルは既定でパス先頭から読む。階を跨ぐくくり (メゾネット等) は level: で明示する
244
+ const explicit = takeString(attrs, "level");
245
+ if (explicit !== undefined && !model.levels[explicit]) {
246
+ throw new SourceError(ln, `未宣言のレベルです: level:${explicit}`);
247
+ }
248
+ const seg = path.split("/")[1];
249
+ const level = explicit ?? (seg && model.levels[seg] ? seg : undefined);
250
+
251
+ const space: Space = { path, type, level, grids: [], rects: [], areas: [], attrs, line: ln };
252
+ for (const g of groups) {
253
+ if (g.length === 0) continue;
254
+ const r = parseRegion(g, ln, model);
255
+ space.grids.push(r.grid);
256
+ space.rects.push(r.rect);
257
+ }
258
+ return space;
259
+ }
260
+
261
+ /** 領域指定 (X?..X? Y?..Y?) をグリッド参照とmm矩形に解決する */
262
+ function parseRegion(
263
+ regionTokens: string[],
264
+ ln: number,
265
+ model: Model,
266
+ ): { grid: { xa: string; xb: string; ya: string; yb: string }; rect: Rect } {
267
+ if (regionTokens.length !== 2) {
268
+ throw new SourceError(ln, "領域は X?..X? と Y?..Y? の2つで指定します");
269
+ }
270
+ let xr: [number, number] | undefined;
271
+ let yr: [number, number] | undefined;
272
+ let xg: [string, string] | undefined;
273
+ let yg: [string, string] | undefined;
274
+ for (const t of regionTokens) {
275
+ const [p, q] = t.split("..");
276
+ if (!p || !q) throw new SourceError(ln, `領域指定が読めません: ${t}`);
277
+ const rp = resolveRef(model, p, ln);
278
+ const rq = resolveRef(model, q, ln);
279
+ if (rp.axis !== rq.axis) {
280
+ throw new SourceError(ln, `領域の両端は同じ軸の通りで指定します: ${t}`);
281
+ }
282
+ if (rp.axis === "X") {
283
+ xr = [rp.coord, rq.coord];
284
+ xg = [p, q];
285
+ } else {
286
+ yr = [rp.coord, rq.coord];
287
+ yg = [p, q];
288
+ }
289
+ }
290
+ if (!xr || !yr || !xg || !yg) {
291
+ throw new SourceError(ln, "領域には X系とY系の通りを1組ずつ使います");
292
+ }
293
+ if (xr[0] === xr[1] || yr[0] === yr[1]) throw new SourceError(ln, "領域の幅がゼロです");
294
+ return {
295
+ grid: { xa: xg[0], xb: xg[1], ya: yg[0], yb: yg[1] },
296
+ rect: {
297
+ x1: Math.min(xr[0], xr[1]),
298
+ x2: Math.max(xr[0], xr[1]),
299
+ y1: Math.min(yr[0], yr[1]),
300
+ y2: Math.max(yr[0], yr[1]),
301
+ },
302
+ };
303
+ }
304
+
305
+ /** 数えない分節: 室内の領域 (床材の切替など) */
306
+ function parseArea(rest: string[], ln: number, model: Model): Area {
307
+ const regionTokens = rest.filter((t) => t.includes(".."));
308
+ const attrTokens = rest.filter((t) => !t.includes(".."));
309
+ const r = parseRegion(regionTokens, ln, model);
310
+ return { grid: r.grid, rect: r.rect, attrs: parseAttrs(attrTokens, ln), line: ln };
311
+ }
312
+
313
+ /** 数えない分節: 境界上の区間 (壁材の途中変更など) */
314
+ function parseSeg(rest: string[], ln: number): Seg {
315
+ const attrs = parseAttrs(rest, ln);
316
+ const w = takeNumber(attrs, "w");
317
+ if (w === undefined || w <= 0) {
318
+ throw new SourceError(ln, "seg には幅 w:(mm) が要ります");
319
+ }
320
+ const at = takeNumber(attrs, "at") ?? 0.5;
321
+ if (at < 0 || at > 1) throw new SourceError(ln, "at は 0..1 で指定します");
322
+ const edge = takeEdge(attrs, ln);
323
+ return { w, at, ...(edge ? { edge } : {}), attrs, line: ln };
324
+ }
325
+
326
+ /** レベルのスパン (L2..L9) を、宣言済みレベルのz順の並びに解決する */
327
+ function resolveSpanLevels(model: Model, token: string, ln: number): string[] {
328
+ const m = /^([A-Za-z]+\d+)\.\.([A-Za-z]+\d+)$/.exec(token);
329
+ if (!m) throw new SourceError(ln, `レベル範囲が読めません: ${token}`);
330
+ const from = model.levels[m[1]!];
331
+ const to = model.levels[m[2]!];
332
+ if (!from || !to) {
333
+ throw new SourceError(ln, `未宣言のレベルを含む範囲です (levelを先に書きます): ${token}`);
334
+ }
335
+ if (from.z >= to.z) throw new SourceError(ln, `範囲の向きが逆です: ${token}`);
336
+ return Object.values(model.levels)
337
+ .filter((l) => l.z >= from.z && l.z <= to.z)
338
+ .sort((a, b) => a.z - b.z)
339
+ .map((l) => l.name);
340
+ }
341
+
342
+ /**
343
+ * パス中のレベルスパン (/L2..L9/A) を展開する。
344
+ * 一行の中の複数パスは同じスパンを指す必要があり、同じレベルに揃って展開される (基準階の書き味)
345
+ */
346
+ function expandSpan(model: Model, paths: string[], ln: number): string[][] {
347
+ const spans = new Set<string>();
348
+ for (const p of paths) {
349
+ const seg = p.split("/")[1];
350
+ if (seg && /^[A-Za-z]+\d+\.\.[A-Za-z]+\d+$/.test(seg)) spans.add(seg);
351
+ }
352
+ if (spans.size === 0) return [paths];
353
+ if (spans.size > 1) {
354
+ throw new SourceError(ln, `一行の中のレベル範囲は揃えます: ${[...spans].join(", ")}`);
355
+ }
356
+ const span = [...spans][0]!;
357
+ const levels = resolveSpanLevels(model, span, ln);
358
+ return levels.map((lv) =>
359
+ paths.map((p) => {
360
+ const segs = p.split("/");
361
+ if (segs[1] === span) segs[1] = lv;
362
+ return segs.join("/");
363
+ }),
364
+ );
365
+ }
366
+
367
+ /** 通り参照 (X2, X2+600, Y3-150 など) を軸と座標mmに解決する */
368
+ function resolveRef(model: Model, token: string, ln: number): { axis: "X" | "Y"; coord: number } {
369
+ const m = /^([XY]\d+)([+-]\d+)?$/.exec(token);
370
+ if (!m) throw new SourceError(ln, `未定義の通り名です: ${token}`);
371
+ const name = m[1]!;
372
+ const offset = m[2] ? Number(m[2]) : 0;
373
+ for (const axis of ["X", "Y"] as const) {
374
+ const g = model.grid[axis];
375
+ const i = g.names.indexOf(name);
376
+ if (i >= 0) return { axis, coord: g.coords[i]! + offset };
377
+ }
378
+ throw new SourceError(ln, `未定義の通り名です: ${token}`);
379
+ }
380
+
381
+ function parseBoundary(rest: string[], ln: number): Boundary {
382
+ const a = rest[0];
383
+ const b = rest[1];
384
+ if (!a?.startsWith("/") || !b?.startsWith("/")) {
385
+ throw new SourceError(ln, "boundary は boundary /パスA /パスB [属性...] の形で書きます");
386
+ }
387
+ const attrs = parseAttrs(rest.slice(2), ln);
388
+ const t = takeNumber(attrs, "t");
389
+ const kindRaw = takeString(attrs, "type") ?? "wall";
390
+ if (!["wall", "open", "stair", "shaft", "void"].includes(kindRaw)) {
391
+ throw new SourceError(
392
+ ln,
393
+ `boundary の type は wall / open / stair / shaft / void です: ${kindRaw}`,
394
+ );
395
+ }
396
+ const air = takeNumber(attrs, "air");
397
+ if (air !== undefined && air !== 0 && air !== 1) {
398
+ throw new SourceError(ln, "air は 0 / 1 で指定します (1=遮蔽しない: 手すり・柵など)");
399
+ }
400
+ const edge = takeEdge(attrs, ln);
401
+ return {
402
+ a,
403
+ b,
404
+ kind: kindRaw as Boundary["kind"],
405
+ ...(t !== undefined ? { t } : {}),
406
+ ...(air === 1 ? { air: true } : {}),
407
+ ...(edge ? { edge } : {}),
408
+ attrs,
409
+ openings: [],
410
+ segs: [],
411
+ line: ln,
412
+ };
413
+ }
414
+
415
+ function parseOpening(kind: "door" | "window", rest: string[], ln: number): Opening {
416
+ const attrs = parseAttrs(rest, ln);
417
+ const w = takeNumber(attrs, "w");
418
+ if (w === undefined || w <= 0) {
419
+ throw new SourceError(ln, `${kind} には幅 w:(mm) が要ります`);
420
+ }
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 で指定します");
424
+ const edge = takeEdge(attrs, ln);
425
+ const hingeRaw = takeString(attrs, "hinge");
426
+ if (hingeRaw !== undefined && !EDGES.has(hingeRaw)) {
427
+ throw new SourceError(ln, `hinge は N/E/S/W で指定します: ${hingeRaw}`);
428
+ }
429
+ const swingRaw = takeString(attrs, "swing");
430
+ if (swingRaw !== undefined && swingRaw !== "a" && swingRaw !== "b") {
431
+ throw new SourceError(ln, `swing は a / b (境界のどちら側へ開くか) です: ${swingRaw}`);
432
+ }
433
+ return {
434
+ kind,
435
+ w,
436
+ ...(h !== undefined ? { h } : {}),
437
+ at,
438
+ ...(edge ? { edge } : {}),
439
+ ...(hingeRaw ? { hinge: hingeRaw as Edge } : {}),
440
+ ...(swingRaw ? { swing: swingRaw as "a" | "b" } : {}),
441
+ attrs,
442
+ line: ln,
443
+ };
444
+ }
445
+
446
+ // ---- 低レベル ----
447
+
448
+ /** 空白区切り。"..."内の空白は保持し、引用符外の # 以降はコメント */
449
+ export function tokenize(line: string, ln: number): string[] {
450
+ const tokens: string[] = [];
451
+ let cur = "";
452
+ let inQuote = false;
453
+ for (const ch of line) {
454
+ if (ch === '"') {
455
+ inQuote = !inQuote;
456
+ continue;
457
+ }
458
+ if (!inQuote && ch === "#") break;
459
+ if (!inQuote && /\s/.test(ch)) {
460
+ if (cur) tokens.push(cur);
461
+ cur = "";
462
+ continue;
463
+ }
464
+ cur += ch;
465
+ }
466
+ if (inQuote) throw new SourceError(ln, "引用符が閉じていません");
467
+ if (cur) tokens.push(cur);
468
+ return tokens;
469
+ }
470
+
471
+ function parseAttrs(tokens: string[], ln: number): Attrs {
472
+ const attrs: Attrs = {};
473
+ for (const t of tokens) {
474
+ const idx = t.indexOf(":");
475
+ if (idx <= 0) throw new SourceError(ln, `属性は key:value で書きます: ${t}`);
476
+ const key = t.slice(0, idx);
477
+ const rawVal = t.slice(idx + 1);
478
+ if (rawVal === "") throw new SourceError(ln, `属性 ${key} に値がありません`);
479
+ attrs[key] = maybeNumber(rawVal);
480
+ }
481
+ return attrs;
482
+ }
483
+
484
+ function maybeNumber(v: string): AttrValue {
485
+ return /^-?\d+(\.\d+)?$/.test(v) ? Number(v) : v;
486
+ }
487
+
488
+ function toNumber(v: string, ln: number, what: string): number {
489
+ if (!/^-?\d+(\.\d+)?$/.test(v)) throw new SourceError(ln, `${what}が数値ではありません: ${v}`);
490
+ return Number(v);
491
+ }
492
+
493
+ function takeNumber(attrs: Attrs, key: string): number | undefined {
494
+ const v = attrs[key];
495
+ if (v === undefined) return undefined;
496
+ delete attrs[key];
497
+ return typeof v === "number" ? v : Number.NaN;
498
+ }
499
+
500
+ function takeString(attrs: Attrs, key: string): string | undefined {
501
+ const v = attrs[key];
502
+ if (v === undefined) return undefined;
503
+ delete attrs[key];
504
+ return String(v);
505
+ }
506
+
507
+ function takeEdge(attrs: Attrs, ln: number): Edge | undefined {
508
+ const v = takeString(attrs, "edge");
509
+ if (v === undefined) return undefined;
510
+ if (!EDGES.has(v)) throw new SourceError(ln, `edge は N/E/S/W で指定します: ${v}`);
511
+ return v as Edge;
512
+ }