@cardenelabs/dragon 0.42.2 → 0.44.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 (43) hide show
  1. package/README.md +34 -1
  2. package/dist/cli.cjs +4630 -1690
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +4630 -1690
  5. package/dist/cli.js.map +1 -1
  6. package/dist/export.cjs +4267 -1555
  7. package/dist/export.cjs.map +1 -1
  8. package/dist/export.d.cts +1 -1
  9. package/dist/export.d.ts +1 -1
  10. package/dist/export.js +4267 -1555
  11. package/dist/export.js.map +1 -1
  12. package/dist/index.cjs +4348 -1381
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.cts +118 -3
  15. package/dist/index.d.ts +118 -3
  16. package/dist/index.js +4348 -1381
  17. package/dist/index.js.map +1 -1
  18. package/dist/{ink-B8EZL7JV.d.cts → ink-DeDtzaTL.d.cts} +47 -3
  19. package/dist/{ink-B8EZL7JV.d.ts → ink-DeDtzaTL.d.ts} +47 -3
  20. package/dist/node.cjs +3504 -1625
  21. package/dist/node.cjs.map +1 -1
  22. package/dist/node.js +3504 -1625
  23. package/dist/node.js.map +1 -1
  24. package/dist/player.global.js +3 -2
  25. package/package.json +1 -1
  26. package/src/field-messages.ts +6 -0
  27. package/src/index.ts +8 -1
  28. package/src/layout/lineup.ts +219 -0
  29. package/src/layout/tree.ts +458 -0
  30. package/src/look/values.ts +73 -2
  31. package/src/messages.ts +88 -2
  32. package/src/parser.ts +212 -2
  33. package/src/printer.ts +36 -2
  34. package/src/render/icons.ts +2 -0
  35. package/src/render/lineup.ts +184 -0
  36. package/src/render/parts.ts +16 -7
  37. package/src/render/svg.ts +72 -6
  38. package/src/render/tree.ts +165 -0
  39. package/src/schema.ts +76 -4
  40. package/src/schemas/diagram.schema.json +207 -19
  41. package/src/schemas/validate-diagram.js +1 -1
  42. package/src/timeline.ts +16 -2
  43. package/src/words.ts +25 -2
package/src/messages.ts CHANGED
@@ -88,6 +88,42 @@ export interface Messages {
88
88
  badHash: string;
89
89
  /** 時間軸の図だけの行か属性 (`word`) を、別の種類の図に書いた (#3165) */
90
90
  timeOnly: (word: string) => string;
91
+ /** 木の図だけの行 (`leaf` と `node`) を、別の種類の図に書いた (#3233) */
92
+ treeOnly: (word: string) => string;
93
+ treeNoGroup: string;
94
+ noLeaves: string;
95
+ leafShape: string;
96
+ nodeShape: string;
97
+ /** 親の子に、まだ書いていない名札を書いた */
98
+ noTreeChild: (id: string) => string;
99
+ /** 親の子に、木の箱でない箱 (`box`) を書いた */
100
+ notTreeBox: (id: string) => string;
101
+ /** もう親を持つ箱を、別の親の子にした */
102
+ twoParents: (id: string) => string;
103
+ /** 最後の段を終えて、親を持たない箱が根の外に残った */
104
+ manyRoots: (id: string, root: string) => string;
105
+ /** 列の図だけの行 (`item`、`line`、`seal`、`open`、`pick`、`stamp`) を、別の種類の図に書いた (#3239) */
106
+ lineupOnly: (word: string) => string;
107
+ lineupNoGroup: string;
108
+ lineupNoEdge: string;
109
+ lineupNoAct: string;
110
+ noItems: string;
111
+ itemShape: string;
112
+ lineShape: string;
113
+ /** `seal`、`open`、`pick` の行の形 */
114
+ laneShape: (word: string) => string;
115
+ stampShape: string;
116
+ /** チップの字が上限の字数を超えた */
117
+ labelTooLong: (limit: number) => string;
118
+ /** `line` の行に、宣言していないチップを書いた */
119
+ noSuchItem: (id: string) => string;
120
+ /** 同じ `line` の行に、同じチップを 2 度書いた */
121
+ itemTwice: (id: string) => string;
122
+ /** `line` の行のチップが、1 列の上限を超えた */
123
+ laneFull: (lane: string, limit: number) => string;
124
+ lineOnSealed: (lane: string) => string;
125
+ sealTwice: (lane: string) => string;
126
+ openUnsealed: (lane: string) => string;
91
127
  /** 時間軸の図の宣言の行を 2 つ書いた */
92
128
  timeTwice: (word: string) => string;
93
129
  noScale: string;
@@ -162,7 +198,7 @@ const ja = (): Messages => {
162
198
  emptyCode: "code の後ろに命令を書く",
163
199
  sourceShape: "source は source <名前> [url: <URL>] の形で書く",
164
200
  declarationInStep: "group と box と strip と branch と、scale と period と row と point と zoom は最初の step より前に書く",
165
- stepLineBeforeStep: "線と say と code と source と act と note と set と commit と枝を切る行と、now と mark と span の行は step の下に書く",
201
+ stepLineBeforeStep: "線と say と code と source と act と note と set と commit と枝を切る行と、now と mark と span と、leaf と node の行は step の下に書く",
166
202
  nestedGroup: "group の中に group は書けない。先に end で閉じる",
167
203
  strayEnd: "end の前に、閉じる group が無い",
168
204
  endTakesNothing: "end の後ろには何も書かない",
@@ -210,6 +246,31 @@ const ja = (): Messages => {
210
246
  headReserved: (name) => `${name} は枝分かれの図で今いる場所の札の名前で、名札に使えない`,
211
247
  badHash: "hash は 0 から 9 と a から f の字で 4 から 40 字にする",
212
248
  timeOnly: (word) => `${word} は time (時間軸の図) でだけ書ける`,
249
+ treeOnly: (word) => `${word} の行は tree (木の図) でだけ書ける`,
250
+ treeNoGroup: "tree (木の図) には group を書けない。木の箱は leaf と node の行で書く",
251
+ noLeaves: "leaf の行で葉を 1 つ以上書く",
252
+ leafShape: "leaf は leaf <名札> [hash: <hash>] の形で書く",
253
+ nodeShape: "node は node <名札> of <子>, <子> [hash: <hash>] の形で書く",
254
+ noTreeChild: (id) => `${id} という葉か親はまだ書いていない。子は前に leaf か node で書いた名札にする`,
255
+ notTreeBox: (id) => `${id} は box の行で置いた箱で、木の箱でない。子は leaf か node で書いた名札にする`,
256
+ twoParents: (id) => `${id} はもう別の親の子になっている。木の箱の親は 1 つだけ`,
257
+ manyRoots: (id, root) => `${id} が親を持たないまま残る。木の根は 1 つだけで、${id} も ${root} も最後に 1 つの根へ組む`,
258
+ lineupOnly: (word) => `${word} の行は lineup (列の図) でだけ書ける`,
259
+ lineupNoGroup: "lineup (列の図) には group を書けない。列は box の行で宣言する",
260
+ lineupNoEdge: "lineup (列の図) には線の行を書けない。チップは line の行で列の並びを書くと動く",
261
+ lineupNoAct: "lineup (列の図) には act の行を書けない。列の右上の字は note の行で書き換える",
262
+ noItems: "item の行でチップを 1 つ以上宣言する",
263
+ itemShape: "item は item <名札>, <名札> [color: <色>, label: <字>] の形で書く",
264
+ lineShape: "line は line <列>: <チップ>, <チップ> の形で書く",
265
+ laneShape: (word) => `${word} は ${word} <列> の形で書く`,
266
+ stampShape: "stamp は stamp <列>: <字> の形で書く",
267
+ labelTooLong: (limit) => `label は ${limit} 字までにする`,
268
+ noSuchItem: (id) => `${id} というチップは宣言していない`,
269
+ itemTwice: (id) => `${id} はこの行に 2 度書いている。同じ列に同じチップは 1 枚だけ`,
270
+ laneFull: (lane, limit) => `${lane} の列に並べられるチップは ${limit} 枚まで`,
271
+ lineOnSealed: (lane) => `${lane} の列は封をしているので並びを書けない。先に open で開ける`,
272
+ sealTwice: (lane) => `${lane} の列にはもう封をしている`,
273
+ openUnsealed: (lane) => `${lane} の列には封をしていない`,
213
274
  timeTwice: (word) => `${word} は 1 つだけ宣言する`,
214
275
  noScale: "scale の行で目盛りを宣言する",
215
276
  noRows: "row の行で印の列を 1 つ以上宣言する",
@@ -276,7 +337,7 @@ const en = (): Messages => {
276
337
  emptyCode: "Write the command after code",
277
338
  sourceShape: "Write a source as source <name> [url: <URL>]",
278
339
  declarationInStep: "Write group, box, strip, branch, scale, period, row, point and zoom before the first step",
279
- stepLineBeforeStep: "Write lines, say, code, source, act, note, set, commit, branch cuts, now, mark and span under a step",
340
+ stepLineBeforeStep: "Write lines, say, code, source, act, note, set, commit, branch cuts, now, mark, span, leaf and node under a step",
280
341
  nestedGroup: "A group cannot hold another group. Close it with end first",
281
342
  strayEnd: "There is no group for this end to close",
282
343
  endTakesNothing: "Write nothing after end",
@@ -324,6 +385,31 @@ const en = (): Messages => {
324
385
  headReserved: (name) => `${name} names where you are in a git diagram and cannot be an id`,
325
386
  badHash: "Write a hash as 4 to 40 characters of 0-9 and a-f",
326
387
  timeOnly: (word) => `${word} can be written only in time diagrams`,
388
+ treeOnly: (word) => `${word} lines can be written only in tree diagrams`,
389
+ treeNoGroup: "A tree diagram takes no group. Write the tree boxes with leaf and node lines",
390
+ noLeaves: "Write at least one leaf with a leaf line",
391
+ leafShape: "Write a leaf as leaf <id> [hash: <hash>]",
392
+ nodeShape: "Write a node as node <id> of <child>, <child> [hash: <hash>]",
393
+ noTreeChild: (id) => `No leaf or node named ${id} has been written yet. A child must be an id written earlier with leaf or node`,
394
+ notTreeBox: (id) => `${id} is a box from a box line, not a tree box. A child must be an id written with leaf or node`,
395
+ twoParents: (id) => `${id} is already a child of another node. A tree box has only one parent`,
396
+ manyRoots: (id, root) => `${id} is left without a parent. A tree has one root, so join ${id} and ${root} under one root by the end`,
397
+ lineupOnly: (word) => `${word} lines can be written only in lineup diagrams`,
398
+ lineupNoGroup: "A lineup diagram takes no group. Declare the lanes with box lines",
399
+ lineupNoEdge: "A lineup diagram takes no edge lines. Chips move when a line line rewrites a lane",
400
+ lineupNoAct: "A lineup diagram takes no act lines. Rewrite the text at the top right of a lane with a note line",
401
+ noItems: "Declare at least one chip with an item line",
402
+ itemShape: "Write an item as item <id>, <id> [color: <color>, label: <text>]",
403
+ lineShape: "Write a line as line <lane>: <chip>, <chip>",
404
+ laneShape: (word) => `Write ${word} as ${word} <lane>`,
405
+ stampShape: "Write a stamp as stamp <lane>: <text>",
406
+ labelTooLong: (limit) => `A label is at most ${limit} characters`,
407
+ noSuchItem: (id) => `The chip ${id} is not declared`,
408
+ itemTwice: (id) => `${id} is written twice on this line. A lane holds one of each chip`,
409
+ laneFull: (lane, limit) => `The lane ${lane} holds at most ${limit} chips`,
410
+ lineOnSealed: (lane) => `The lane ${lane} is sealed, so its order cannot be written. Open it first`,
411
+ sealTwice: (lane) => `The lane ${lane} is already sealed`,
412
+ openUnsealed: (lane) => `The lane ${lane} is not sealed`,
327
413
  timeTwice: (word) => `Declare only one ${word}`,
328
414
  noScale: "Declare the scale with a scale line",
329
415
  noRows: "Declare at least one row with a row line",
package/src/parser.ts CHANGED
@@ -51,6 +51,15 @@
51
51
  * 中身の帯の欄の名前の後ろに `[meter: <割合>]` を書くと物差しになり、`set` の値を割合 (`97%` か `2/3`) で書く。
52
52
  * 印の列と箱は同じ名札を持てる (空いた目盛りの列でその箱を空きの枠にするため)。時間軸の図では `now` を名札に使えない。
53
53
  *
54
+ * 木の図 (`tree`、#3233) は、段の中に `leaf <名札> [hash: <hash>]` で葉を出し、`node <名札> of <子>, <子> [hash: <hash>]` で
55
+ * 前に書いた葉か親を子にして親を組む。箱は書いた段で現れ、親は 1 つだけ持てる。最後の段を終えた時に親を持たない木の箱は根の 1 つだけ。
56
+ * 線の端には木の箱も `box` の箱も書ける。木の図に区画 (`group`) は書けず、`leaf` と `node` の行は木の図でだけ書ける。
57
+ *
58
+ * 列の図 (`lineup`、#3239) は、`box` の行が列になり、最初の `step` より前に `item <名札>, <名札> [color: <色>, label: <字>]` でチップを宣言する。
59
+ * 段の中で `line <列>: <チップ>, <チップ>` と書くと列の並びが替わり、`seal <列>` で封をし、`open <列>` で開け、`pick <列>` で列を選び、
60
+ * `stamp <列>: <字>` で札を押す。列の図に区画 (`group`) と線と `act` の行は書けない。封をした列の並びは、開けるまで書けない。
61
+ * `item`、`line`、`seal`、`open`、`pick`、`stamp` の行は列の図でだけ書ける。
62
+ *
54
63
  * 行の頭の語と同じ綴りの名札 (`end` や `step`) も書ける。名札の次に `->` が来る行は線と読む。
55
64
  * 誤りは最初の 1 つで止め、行と列を示す。誤りの文の言葉は呼ぶ側が渡す。
56
65
  */
@@ -65,6 +74,7 @@ import {
65
74
  type GitBranch,
66
75
  type GitCommit,
67
76
  type LabelSide,
77
+ type LineupItem,
68
78
  type Region,
69
79
  type StatePath,
70
80
  type Step,
@@ -88,9 +98,14 @@ import {
88
98
  HEAD,
89
99
  HEADS,
90
100
  KINDS,
101
+ LABEL_MOST,
102
+ LANE_ATTRS,
103
+ LANE_MOST,
104
+ LINEUP_HEADS,
91
105
  MERGE,
92
106
  type NotationLang,
93
107
  NOW,
108
+ OF,
94
109
  OLD_HEADS,
95
110
  ON,
96
111
  SIDES,
@@ -98,6 +113,7 @@ import {
98
113
  STATE_ENDS,
99
114
  TIME_EDGE_ATTRS,
100
115
  TIME_HEADS,
116
+ TREE_HEADS,
101
117
  VIEWS,
102
118
  } from "./words";
103
119
 
@@ -135,6 +151,13 @@ const EDGE = /^([^\s:[\]]+?)\s*->\s*([^\s:[\]]+)/du;
135
151
  const COMMIT = new RegExp(`^commit\\s+([^\\s[]+)(?:\\s+${ON}\\s+([^\\s[]+))?(?:\\s+${COPY}\\s+([^\\s[]+))?(?:\\s+${MERGE}\\s+([^\\s[]+))?`, "du");
136
152
  /** 枝分かれの図の、枝を切る行。枝と `from` と切る commit */
137
153
  const CUT = new RegExp(`^([^\\s[]+)\\s+${FROM}(?:\\s+([^\\s[]+))?\\s*$`, "du");
154
+ /** 木の図の `leaf` の行の名札と、`node` の行の名札と `of` */
155
+ const LEAF = /^leaf\s+([^\s[]+)/du;
156
+ const NODE = new RegExp(`^node\\s+([^\\s[]+)\\s+${OF}\\s+`, "du");
157
+ /** 列の図の、`line <列>:` と `stamp <列>:` の行の列と、`seal`、`open`、`pick` の行の列 */
158
+ const ORDER = /^line\s+([^\s:[]+)\s*:/du;
159
+ const STAMP = /^stamp\s+([^\s:[]+)\s*:/du;
160
+ const ON_LANE = /^(?:seal|open|pick)\s+(\S+)\s*$/du;
138
161
  /** commit の hash。原本の JSON の hash の形と同じ */
139
162
  const HASH = /^[0-9a-f]{4,40}$/u;
140
163
  /** 箱の飛び先と段の出典の URL。原本の JSON の URL の形と同じ */
@@ -356,6 +379,14 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
356
379
  const isState = kind === "state";
357
380
  const isGit = kind === "git";
358
381
  const isTime = kind === "time";
382
+ const isTree = kind === "tree";
383
+ const isLineup = kind === "lineup";
384
+ /** 列の図で宣言したチップと、今封をしている列 (#3239) */
385
+ const items: LineupItem[] = [];
386
+ const sealed = new Set<string>();
387
+ /** 木の図で書いた木の箱と、書いた行と名札の位置。子の箱から親の名札 */
388
+ const trees = new Map<string, { source: SourceLine; at: number }>();
389
+ const parents = new Map<string, string>();
359
390
  /** 時間軸の図の目盛りと区切りと印の列と節目と拡大の範囲 (#3165) */
360
391
  let scale: TimeScale | undefined;
361
392
  let period: TimePeriod | undefined;
@@ -414,6 +445,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
414
445
  if (!boxes.some((box) => box.id === id) && !points.some((point) => point.id === id)) throw fail(source, at, m.undeclaredNote(id));
415
446
  return id;
416
447
  }
448
+ if (isTree && trees.has(id)) return id;
417
449
  if (!isState || !isOneOf(STATE_ENDS, id)) return boxAt(source, id, at);
418
450
  if ((id === "start") !== (side === "from")) throw fail(source, at, id === "start" ? m.intoStart : m.outOfFinish);
419
451
  return id;
@@ -458,6 +490,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
458
490
  // 名札の次に `->` が来る行は、名札が行の頭の語と同じ綴りでも線と読む
459
491
  const edge = EDGE.exec(text);
460
492
  if (edge) {
493
+ if (isLineup) throw fail(source, 0, m.lineupNoEdge);
461
494
  // 状態遷移図の最初の step より前の線は、移れる道の地図
462
495
  if (isState && !building) paths.push(readPath(source, edge));
463
496
  else inStep(source).events.push(readEdge(source, edge));
@@ -472,6 +505,8 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
472
505
  }
473
506
  if (!isGit && (word === "branch" || word === "commit")) throw fail(source, 0, m.gitOnly(word));
474
507
  if (!isTime && isOneOf(TIME_HEADS, word)) throw fail(source, 0, m.timeOnly(word));
508
+ if (!isTree && isOneOf(TREE_HEADS, word)) throw fail(source, 0, m.treeOnly(word));
509
+ if (!isLineup && isOneOf(LINEUP_HEADS, word)) throw fail(source, 0, m.lineupOnly(word));
475
510
  if (!isOneOf(HEADS, word)) {
476
511
  const old = oldForm(text, building !== undefined);
477
512
  throw fail(source, 0, old ? m.oldForm(old.word, old.shape) : m.unknownHead(word));
@@ -515,6 +550,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
515
550
  inStep(source).sources.push(readSource(source));
516
551
  break;
517
552
  case "act": {
553
+ if (isLineup) throw fail(source, 0, m.lineupNoAct);
518
554
  const step = inStep(source);
519
555
  const on = readOnBox(source, word, m.actShape);
520
556
  step.events.push({ type: "action", box: on.box, label: on.text });
@@ -562,6 +598,36 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
562
598
  case "span":
563
599
  inStep(source).events.push(readSpan(source));
564
600
  break;
601
+ case "leaf": {
602
+ const step = inStep(source);
603
+ step.events.push(readLeaf(source));
604
+ break;
605
+ }
606
+ case "node": {
607
+ const step = inStep(source);
608
+ step.events.push(readNode(source));
609
+ break;
610
+ }
611
+ case "item":
612
+ readItem(source);
613
+ break;
614
+ case "line": {
615
+ const step = inStep(source);
616
+ step.events.push(readOrder(source));
617
+ break;
618
+ }
619
+ case "seal":
620
+ case "open":
621
+ case "pick": {
622
+ const step = inStep(source);
623
+ step.events.push(readOnLane(source, word));
624
+ break;
625
+ }
626
+ case "stamp": {
627
+ const step = inStep(source);
628
+ step.events.push(readStamp(source));
629
+ break;
630
+ }
565
631
  }
566
632
  }
567
633
  if (group) throw fail(group.source, 0, m.unclosedGroup(group.id));
@@ -569,8 +635,15 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
569
635
  finish();
570
636
 
571
637
  if (isGit && branches.length === 0) throw fail(head, 0, m.noBranches);
572
- if (boxes.length === 0 && !isGit && !isTime) throw fail(head, 0, m.noBoxes);
638
+ if (isTree && trees.size === 0) throw fail(head, 0, m.noLeaves);
639
+ if (isLineup && items.length === 0) throw fail(head, 0, m.noItems);
640
+ if (boxes.length === 0 && !isGit && !isTime && !isTree) throw fail(head, 0, m.noBoxes);
573
641
  if (steps.length === 0) throw fail(head, 0, m.noSteps);
642
+ // 木の形は全ての行を読んでから確かめる。親を持たない木の箱が 2 つ以上なら、書いた順で 2 つ目を示す
643
+ const roots = [...trees.keys()].filter((id) => !parents.has(id));
644
+ const [root, stray] = roots;
645
+ const strayAt = stray === undefined ? undefined : trees.get(stray);
646
+ if (root !== undefined && stray !== undefined && strayAt) throw fail(strayAt.source, strayAt.at, m.manyRoots(stray, root));
574
647
 
575
648
  return {
576
649
  version: FORMAT_VERSION,
@@ -584,6 +657,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
584
657
  ...(kind === "sequence" ? { sequence: { lifelines: boxes.map((box) => box.id) } } : {}),
585
658
  ...(paths.length > 0 ? { state: { paths } } : {}),
586
659
  ...(isGit ? { git: { branches } } : {}),
660
+ ...(isLineup ? { lineup: { items } } : {}),
587
661
  ...(isTime && scale
588
662
  ? {
589
663
  time: {
@@ -786,6 +860,8 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
786
860
  function readGroup(source: SourceLine): void {
787
861
  beforeSteps(source);
788
862
  if (isGit) throw fail(source, 0, m.gitNoGroup);
863
+ if (isTree) throw fail(source, 0, m.treeNoGroup);
864
+ if (isLineup) throw fail(source, 0, m.lineupNoGroup);
789
865
  if (group) throw fail(source, 0, m.nestedGroup);
790
866
  const declared = readDeclared(source, "group", ATTRS.group, m);
791
867
  if (regions.some((region) => region.id === declared.id)) {
@@ -801,7 +877,7 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
801
877
  /** `box` の行。開いている `group` の中なら、その区画に置く */
802
878
  function readBox(source: SourceLine): void {
803
879
  beforeSteps(source);
804
- const declared = readDeclared(source, "box", ATTRS.box, m);
880
+ const declared = readDeclared(source, "box", isLineup ? LANE_ATTRS : ATTRS.box, m);
805
881
  notReserved(source, declared.id, declared.idAt);
806
882
  declare(source, declared.id, declared.idAt);
807
883
  firstBox ??= source;
@@ -898,6 +974,140 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
898
974
  return { type: "branch", branch, from };
899
975
  }
900
976
 
977
+ /** 木の図の `leaf <名札> [hash: <hash>]` の行 */
978
+ function readLeaf(source: SourceLine): DiagramEvent {
979
+ const text = source.text;
980
+ const found = LEAF.exec(text);
981
+ const id = found?.[1];
982
+ if (!found || id === undefined) throw fail(source, 0, m.leafShape);
983
+ const idAt = found.indices?.[1]?.[0] ?? 0;
984
+ if (!ID.test(id)) throw fail(source, idAt, m.badId(id));
985
+ declare(source, id, idAt);
986
+ trees.set(id, { source, at: idAt });
987
+ const rest = skipSpaces(text, found[0].length);
988
+ if (rest < text.length && text[rest] !== "[") throw fail(source, rest, m.leafShape);
989
+ const hash = treeHash(source, rest, ATTRS.leaf);
990
+ return { type: "leaf", id, ...(hash !== undefined ? { hash } : {}) };
991
+ }
992
+
993
+ /**
994
+ * 木の図の `node <名札> of <子>, <子> [hash: <hash>]` の行。子は前に書いた葉か親で、まだ親を持たない箱に限る。
995
+ * 子は `,` で区切り、`[` か行の終わりで終わる
996
+ */
997
+ function readNode(source: SourceLine): DiagramEvent {
998
+ const text = source.text;
999
+ const found = NODE.exec(text);
1000
+ const id = found?.[1];
1001
+ if (!found || id === undefined) throw fail(source, 0, m.nodeShape);
1002
+ const idAt = found.indices?.[1]?.[0] ?? 0;
1003
+ if (!ID.test(id)) throw fail(source, idAt, m.badId(id));
1004
+ declare(source, id, idAt);
1005
+ const children: string[] = [];
1006
+ let end = found[0].length;
1007
+ for (;;) {
1008
+ const start = skipSpaces(text, end);
1009
+ end = start;
1010
+ while (end < text.length && text[end] !== "," && text[end] !== "[") end++;
1011
+ const child = text.slice(start, end).trim();
1012
+ if (child === "" || !ID.test(child)) throw fail(source, start, m.nodeShape);
1013
+ if (!trees.has(child)) throw fail(source, start, ids.has(child) && child !== id ? m.notTreeBox(child) : m.noTreeChild(child));
1014
+ if (parents.has(child)) throw fail(source, start, m.twoParents(child));
1015
+ parents.set(child, id);
1016
+ children.push(child);
1017
+ if (text[end] !== ",") break;
1018
+ end++;
1019
+ }
1020
+ const hash = treeHash(source, end, ATTRS.node);
1021
+ trees.set(id, { source, at: idAt });
1022
+ return { type: "node", id, children, ...(hash !== undefined ? { hash } : {}) };
1023
+ }
1024
+
1025
+ /** 葉と親の行の名札の後ろの `[hash: <hash>]`。`at` は `[` か行の終わり */
1026
+ function treeHash(source: SourceLine, at: number, keys: readonly string[]): string | undefined {
1027
+ if (at >= source.text.length) return undefined;
1028
+ const hash = readAttrs(source, at, keys, m).get("hash");
1029
+ if (hash && !HASH.test(hash.value)) throw fail(source, hash.at, m.badHash);
1030
+ return hash?.value;
1031
+ }
1032
+
1033
+ /** `from` から `to` の前までの、`,` で区切った名札と、それぞれの位置 */
1034
+ function namesAt(source: SourceLine, from: number, to: number, shape: string): { value: string; at: number }[] {
1035
+ const text = source.text;
1036
+ const names: { value: string; at: number }[] = [];
1037
+ for (let i = from; ; ) {
1038
+ const start = skipSpaces(text, i);
1039
+ const comma = text.indexOf(",", start);
1040
+ const end = comma < 0 || comma > to ? to : comma;
1041
+ const value = text.slice(start, end).trim();
1042
+ if (value === "") throw fail(source, start, shape);
1043
+ if (!ID.test(value)) throw fail(source, start, m.badId(value));
1044
+ names.push({ value, at: start });
1045
+ if (end >= to) return names;
1046
+ i = end + 1;
1047
+ }
1048
+ }
1049
+
1050
+ /** 列の図の `item <名札>, <名札> [color: <色>, label: <字>]` の行。並べた名札ごとに、同じ色と字のチップを宣言する */
1051
+ function readItem(source: SourceLine): void {
1052
+ beforeSteps(source);
1053
+ const open = source.text.indexOf("[");
1054
+ const end = open < 0 ? source.text.length : open;
1055
+ const attrs = open < 0 ? new Map<string, Attr>() : readAttrs(source, open, ATTRS.item, m);
1056
+ const color = attrs.get("color");
1057
+ if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
1058
+ const label = attrs.get("label");
1059
+ if (label && [...label.value].length > LABEL_MOST) throw fail(source, label.at, m.labelTooLong(LABEL_MOST));
1060
+ for (const { value, at } of namesAt(source, "item".length, end, m.itemShape)) {
1061
+ declare(source, value, at);
1062
+ items.push({ id: value, ...(color ? { color: color.value as Color } : {}), ...(label ? { label: label.value } : {}) });
1063
+ }
1064
+ }
1065
+
1066
+ /** 列の図の `line <列>: <チップ>, <チップ>` の行。チップは宣言したチップで、列に 1 枚ずつ、上限まで。封をした列には書けない */
1067
+ function readOrder(source: SourceLine): DiagramEvent {
1068
+ const found = ORDER.exec(source.text);
1069
+ const lane = found?.[1];
1070
+ if (!found || lane === undefined) throw fail(source, 0, m.lineShape);
1071
+ const at = found.indices?.[1]?.[0] ?? 0;
1072
+ const box = boxAt(source, lane, at);
1073
+ if (sealed.has(box)) throw fail(source, at, m.lineOnSealed(box));
1074
+ const chips = namesAt(source, found[0].length, source.text.length, m.lineShape);
1075
+ const seen = new Set<string>();
1076
+ chips.forEach(({ value, at: chipAt }, k) => {
1077
+ if (!items.some((item) => item.id === value)) throw fail(source, chipAt, m.noSuchItem(value));
1078
+ if (seen.has(value)) throw fail(source, chipAt, m.itemTwice(value));
1079
+ if (k >= LANE_MOST) throw fail(source, chipAt, m.laneFull(box, LANE_MOST));
1080
+ seen.add(value);
1081
+ });
1082
+ return { type: "line", box, items: chips.map((chip) => chip.value) };
1083
+ }
1084
+
1085
+ /** 列の図の `seal <列>`、`open <列>`、`pick <列>` の行。封は 1 列に 1 つで、封をした列だけを開けられる */
1086
+ function readOnLane(source: SourceLine, word: "seal" | "open" | "pick"): DiagramEvent {
1087
+ const found = ON_LANE.exec(source.text);
1088
+ const lane = found?.[1];
1089
+ if (!found || lane === undefined) throw fail(source, 0, m.laneShape(word));
1090
+ const at = found.indices?.[1]?.[0] ?? 0;
1091
+ const box = boxAt(source, lane, at);
1092
+ if (word === "seal") {
1093
+ if (sealed.has(box)) throw fail(source, at, m.sealTwice(box));
1094
+ sealed.add(box);
1095
+ }
1096
+ if (word === "open" && !sealed.delete(box)) throw fail(source, at, m.openUnsealed(box));
1097
+ return { type: word, box };
1098
+ }
1099
+
1100
+ /** 列の図の `stamp <列>: <字>` の行 */
1101
+ function readStamp(source: SourceLine): DiagramEvent {
1102
+ const found = STAMP.exec(source.text);
1103
+ const lane = found?.[1];
1104
+ if (!found || lane === undefined) throw fail(source, 0, m.stampShape);
1105
+ const box = boxAt(source, lane, found.indices?.[1]?.[0] ?? 0);
1106
+ const named = readName(source, found[0].length, m);
1107
+ if (named.value === "" || named.rest < source.text.length) throw fail(source, 0, m.stampShape);
1108
+ return { type: "stamp", box, text: named.value };
1109
+ }
1110
+
901
1111
  /** `strip <題>: <欄>, <欄>` の行 */
902
1112
  function readStrip(source: SourceLine): void {
903
1113
  beforeSteps(source);
package/src/printer.ts CHANGED
@@ -13,10 +13,12 @@
13
13
  * 枝分かれの図 (#3164) は、枝の `branch` の行を箱より前に書き、札を動かす線の運ぶ物が送る側の名前の札なら書かない。
14
14
  * 時間軸の図 (#3165) は、目盛りと区切りと印の列と節目と拡大の範囲の行を箱より前に書き、物差しの欄の後ろに `[meter: <割合>]` を書く。
15
15
  * 時間軸の図の `note` は属性を持てるので、`[` を含む字を `"` で囲む。
16
+ * 木の図 (#3233) の葉と親は、段の中に `leaf` と `node … of …` の行で書く。
17
+ * 列の図 (#3239) のチップは箱の後ろに `item` の行で書き、色が同じで字を持たないチップが続く所は 1 行に並べる。
16
18
  * 段の出典 (#3199) は `say` と `code` の後ろ、段の中で起きることより前に、JSON の並びのまま 1 つずつ `source` の行に書く。
17
19
  */
18
- import type { Box, Diagram, DiagramEvent, Region, TimeSpec } from "./schema";
19
- import { AT, COPY, FROM, MERGE, NOW, ON } from "./words";
20
+ import type { Box, Diagram, DiagramEvent, LineupItem, Region, TimeSpec } from "./schema";
21
+ import { AT, COPY, FROM, MERGE, NOW, OF, ON } from "./words";
20
22
 
21
23
  const quote = (text: string): string => `"${text.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
22
24
  /** 行の終わりか `[` の前に置く名前と文 */
@@ -89,9 +91,40 @@ function event(e: DiagramEvent, kind: Diagram["kind"]): string {
89
91
  ])}`;
90
92
  case "branch":
91
93
  return `${e.branch} ${FROM} ${e.from}`;
94
+ case "leaf":
95
+ return `leaf ${e.id}${attrs([["hash", e.hash]])}`;
96
+ case "node":
97
+ return `node ${e.id} ${OF} ${e.children.join(", ")}${attrs([["hash", e.hash]])}`;
98
+ case "line":
99
+ return `line ${e.box}: ${e.items.join(", ")}`;
100
+ case "seal":
101
+ case "open":
102
+ case "pick":
103
+ return `${e.type} ${e.box}`;
104
+ case "stamp":
105
+ return `stamp ${e.box}: ${name(e.text)}`;
92
106
  }
93
107
  }
94
108
 
109
+ /** 列の図のチップの宣言の行。色が同じで字を持たないチップが続く所は 1 行に並べる */
110
+ function itemLines(items: LineupItem[]): string {
111
+ const rows: LineupItem[][] = [];
112
+ for (const item of items) {
113
+ const row = rows.at(-1);
114
+ if (row && item.label === undefined && row[0]?.label === undefined && row[0]?.color === item.color) row.push(item);
115
+ else rows.push([item]);
116
+ }
117
+ return rows
118
+ .map(
119
+ (row) =>
120
+ `item ${row.map((item) => item.id).join(", ")}${attrs([
121
+ ["color", row[0]?.color],
122
+ ["label", row[0]?.label],
123
+ ])}`,
124
+ )
125
+ .join("\n");
126
+ }
127
+
95
128
  /** 時間軸の図の目盛りと区切りと印の列と節目と拡大の範囲の宣言の塊 */
96
129
  function timeBlocks(time: TimeSpec): string[] {
97
130
  const head = [
@@ -150,6 +183,7 @@ export function printNotation(diagram: Diagram): string {
150
183
  ...(branches.length > 0 ? [branches.join("\n")] : []),
151
184
  ...(diagram.time ? timeBlocks(diagram.time) : []),
152
185
  ...declarations(diagram),
186
+ ...(diagram.lineup && diagram.lineup.items.length > 0 ? [itemLines(diagram.lineup.items)] : []),
153
187
  ];
154
188
  if (diagram.strip) {
155
189
  const meters = new Map((diagram.strip.meters ?? []).map((m) => [m.field, m.line]));
@@ -5,6 +5,7 @@
5
5
  * `undo` (戻る矢印) と `yen` (円の印) は、注文の試作 (`docs/design/base/scenes/order.js`) が返品と返金に足した形 (#3171)。
6
6
  * package の入口からも出し、見本帳の部品「絵」の頁がこの表から使える絵柄の一覧を作る。1 つずつの絵は `iconSvg` が描く (#3172)。
7
7
  * `flag` (旗) は、時間軸の図の節目の丸に描く形 (#3165)。ブロックの確定の試作 (`docs/design/base/scenes/finality.js`) が持っていた。
8
+ * `tree` (親子に分かれる木) は、木の図の根の箱に描く形 (#3233)。step 2 で本人が選んだ試作が持っていた。
8
9
  */
9
10
  export const ICONS: Readonly<Record<string, string>> = {
10
11
  app: "M -9 -7 h18 a2 2 0 0 1 2 2 v10 a2 2 0 0 1 -2 2 h-18 a2 2 0 0 1 -2 -2 v-10 a2 2 0 0 1 2 -2 z M -11 -2 h22",
@@ -43,5 +44,6 @@ export const ICONS: Readonly<Record<string, string>> = {
43
44
  layers: "M 0 -8 l9 4.5 l-9 4.5 l-9 -4.5 z M -9 2.5 l9 4.5 l9 -4.5",
44
45
  code: "M -4 -6 l-5 6 l5 6 M 4 -6 l5 6 l-5 6",
45
46
  paint: "M -8 8 c 0 -3 2 -5 4.5 -5 l 9 -9 l 3.5 3.5 l -9 9 c 0 2.5 -2 4.5 -5 4.5 z",
47
+ tree: "M 0 -5 v4 M -6 -1 h12 M -6 -1 v3 M 6 -1 v3 M -9 2 h6 v6 h-6 z M 3 2 h6 v6 h-6 z M -3 -10 h6 v5 h-6 z",
46
48
  block: "M -9 -7 h18 a2 2 0 0 1 2 2 v10 a2 2 0 0 1 -2 2 h-18 a2 2 0 0 1 -2 -2 v-10 a2 2 0 0 1 2 -2 z M -11 -2.5 h22 M -6 2.5 h8",
47
49
  };