@meltemi/diagrams-model 1.11.16 → 1.11.18

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 (60) hide show
  1. package/dist/c4.d.ts.map +1 -1
  2. package/dist/c4.js +2 -0
  3. package/dist/c4.js.map +1 -1
  4. package/dist/calendar.d.ts +107 -0
  5. package/dist/calendar.d.ts.map +1 -0
  6. package/dist/calendar.js +224 -0
  7. package/dist/calendar.js.map +1 -0
  8. package/dist/compile.d.ts.map +1 -1
  9. package/dist/compile.js +5 -2
  10. package/dist/compile.js.map +1 -1
  11. package/dist/diagram-types.d.ts.map +1 -1
  12. package/dist/diagram-types.js +4 -0
  13. package/dist/diagram-types.js.map +1 -1
  14. package/dist/diff.d.ts +26 -1
  15. package/dist/diff.d.ts.map +1 -1
  16. package/dist/diff.js +299 -4
  17. package/dist/diff.js.map +1 -1
  18. package/dist/flow.d.ts.map +1 -1
  19. package/dist/flow.js +26 -3
  20. package/dist/flow.js.map +1 -1
  21. package/dist/gantt.d.ts +13 -0
  22. package/dist/gantt.d.ts.map +1 -0
  23. package/dist/gantt.js +1257 -0
  24. package/dist/gantt.js.map +1 -0
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +2 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/json.d.ts +51 -1
  30. package/dist/json.d.ts.map +1 -1
  31. package/dist/json.js +45 -0
  32. package/dist/json.js.map +1 -1
  33. package/dist/messages/changes.d.ts +11 -0
  34. package/dist/messages/changes.d.ts.map +1 -1
  35. package/dist/messages/changes.js +41 -3
  36. package/dist/messages/changes.js.map +1 -1
  37. package/dist/messages/en.d.ts +101 -0
  38. package/dist/messages/en.d.ts.map +1 -1
  39. package/dist/messages/en.js +140 -0
  40. package/dist/messages/en.js.map +1 -1
  41. package/dist/messages/es.d.ts.map +1 -1
  42. package/dist/messages/es.js +135 -0
  43. package/dist/messages/es.js.map +1 -1
  44. package/dist/messages/index.d.ts +76 -0
  45. package/dist/messages/index.d.ts.map +1 -1
  46. package/dist/schema.d.ts +37 -5
  47. package/dist/schema.d.ts.map +1 -1
  48. package/dist/schema.js +134 -4
  49. package/dist/schema.js.map +1 -1
  50. package/dist/story.d.ts +14 -5
  51. package/dist/story.d.ts.map +1 -1
  52. package/dist/story.js +80 -12
  53. package/dist/story.js.map +1 -1
  54. package/dist/style-values.d.ts +6 -0
  55. package/dist/style-values.d.ts.map +1 -1
  56. package/dist/style-values.js +24 -2
  57. package/dist/style-values.js.map +1 -1
  58. package/dist/types.d.ts +103 -3
  59. package/dist/types.d.ts.map +1 -1
  60. package/package.json +5 -5
package/dist/gantt.js ADDED
@@ -0,0 +1,1257 @@
1
+ import {} from "@meltemi/diagrams-language";
2
+ import { SECONDS_PER_DAY, addWorking, dayNumber, dayOfDate, endOfDay, formatDate, formatDay, nextWorking, parseDate, unitSeconds, weekdayOfDay, workingBetween, workingCalendar, } from "./calendar.js";
3
+ import { FlowAnalyzer } from "./flow.js";
4
+ import { messages } from "./messages/index.js";
5
+ import { AXIS_DIRECTIVES, DURATION_UNITS, GANTT_AXIS_PLACES, GANTT_CHART_STATEMENTS, GANTT_CLAUSES, GANTT_DAY_WIDTH_RANGE, GANTT_DEPENDENCIES, GANTT_DISPLAYS, GANTT_ELEMENT_KINDS, GANTT_LABEL_PLACES, GANTT_PROFILE, GANTT_RESERVED_IDS, GANTT_STATUS_WORDS, TICK_UNITS, WEEKDAYS, } from "./schema.js";
6
+ import { describeArg, headerHasError, singleIdent } from "./style-values.js";
7
+ /** Analyzes one `diagram <id>: gantt { }` block. */
8
+ export function analyzeGanttDiagram(node, id, lines, bag) {
9
+ const analyzer = new GanttAnalyzer(node, id, lines, bag, GANTT_PROFILE);
10
+ return { diagram: analyzer.run(), errors: analyzer.errors };
11
+ }
12
+ const KINDS = new Set(GANTT_ELEMENT_KINDS);
13
+ const CHART_STATEMENTS = new Set(GANTT_CHART_STATEMENTS);
14
+ const STATUS_WORDS = GANTT_STATUS_WORDS;
15
+ /** The forms of the statements, as messages write them. */
16
+ const FORMS = {
17
+ section: '`section <id> ["Label"] { ... }`',
18
+ task: '`task <id> ["Label"] [from <date>] [after <ids>] [for <duration> | to <date> | until <ids>] [done | active] [critical]`',
19
+ milestone: '`milestone <id> ["Label"] [at <date>] [after <ids>] [done | active] [critical]`',
20
+ marker: '`marker <id> ["Label"] (at <date> | after <ids>)`',
21
+ };
22
+ const EXAMPLES = {
23
+ section: 'section build "Build" { }',
24
+ task: 'task design "Design" from 2026-06-01 for 5d',
25
+ milestone: 'milestone launch "Launch" after build',
26
+ marker: 'marker freeze "Code freeze" at 2026-07-01',
27
+ };
28
+ /** The statements of a chart, as the hints of an unknown statement list them. */
29
+ const CHART_WORDS = [
30
+ ...GANTT_ELEMENT_KINDS,
31
+ ...GANTT_CHART_STATEMENTS,
32
+ "title",
33
+ "description",
34
+ "theme",
35
+ "preset",
36
+ "canvas",
37
+ "style",
38
+ "animate",
39
+ "class",
40
+ "rule",
41
+ ];
42
+ /** The longest a duration may be: a century of days, which no chart needs. */
43
+ const MAX_DURATION_SECONDS = 36_600 * SECONDS_PER_DAY;
44
+ const blockOf = (stmt) => stmt.block;
45
+ class GanttAnalyzer extends FlowAnalyzer {
46
+ items = [];
47
+ chartStatements = [];
48
+ nodes = [];
49
+ calendar = workingCalendar([], [], []);
50
+ chart = {
51
+ calendar: { excludedWeekdays: [], excludedDates: [], includedDates: [], weekStart: 0 },
52
+ axisFormat: null,
53
+ ticks: null,
54
+ axis: "bottom",
55
+ labels: "bar",
56
+ display: "normal",
57
+ dependencies: "shown",
58
+ dayWidth: null,
59
+ today: null,
60
+ span: null,
61
+ };
62
+ run() {
63
+ const diagram = super.run();
64
+ return { ...diagram, gantt: this.chart };
65
+ }
66
+ // ---------------------------------------------------------------- classification
67
+ classifyEvent(stmt, _context, _out) {
68
+ if (stmt.kind === "RelationStatement") {
69
+ this.relationStatement(stmt);
70
+ return true;
71
+ }
72
+ if (stmt.kind === "ShorthandDeclaration") {
73
+ this.shorthand(stmt);
74
+ return true;
75
+ }
76
+ if (stmt.kind !== "KeywordStatement")
77
+ return false;
78
+ const keyword = stmt.keyword.text;
79
+ if (KINDS.has(keyword)) {
80
+ this.collect(stmt, null);
81
+ return true;
82
+ }
83
+ if (CHART_STATEMENTS.has(keyword)) {
84
+ this.chartStatements.push(stmt);
85
+ return true;
86
+ }
87
+ return false;
88
+ }
89
+ /** `a -> b` in a chart: dependencies are written with `after`. */
90
+ relationStatement(stmt) {
91
+ const m = messages();
92
+ const target = stmt.target.kind === "QualifiedRef" ? stmt.target.text : null;
93
+ this.error("gantt.relation", m.diagnostics.gantt.relation, stmt, {
94
+ hints: [m.hints.gantt.relation(stmt.source.text, target)],
95
+ data: { ids: target === null ? [stmt.source.text] : [stmt.source.text, target] },
96
+ });
97
+ }
98
+ /** `a["Label"]` in a chart: a chart declares tasks, milestones, markers and sections. */
99
+ shorthand(stmt) {
100
+ const m = messages();
101
+ this.error("prop.unknown", m.diagnostics.gantt.shorthand(stmt.id.text), stmt.id, {
102
+ hints: [m.hints.gantt.statements],
103
+ data: { candidates: GANTT_ELEMENT_KINDS },
104
+ });
105
+ }
106
+ /** Collects a section, task, milestone or marker, and what a section's block holds. */
107
+ collect(stmt, section) {
108
+ const kind = stmt.keyword.text;
109
+ this.items.push({ stmt, kind, section });
110
+ if (kind !== "section" || !stmt.block)
111
+ return;
112
+ for (const inner of stmt.block.statements) {
113
+ if (inner.kind === "Error" || headerHasError(inner))
114
+ continue;
115
+ this.collectInSection(inner, stmt);
116
+ }
117
+ }
118
+ collectInSection(inner, section) {
119
+ const m = messages();
120
+ if (inner.kind === "RelationStatement") {
121
+ this.relationStatement(inner);
122
+ return;
123
+ }
124
+ if (inner.kind === "ShorthandDeclaration") {
125
+ this.error("prop.unknown", m.diagnostics.gantt.shorthand(inner.id.text), inner.id, {
126
+ hints: [m.hints.gantt.sectionContents],
127
+ data: { candidates: ["task", "milestone", "marker"] },
128
+ });
129
+ return;
130
+ }
131
+ if (inner.kind !== "KeywordStatement")
132
+ return;
133
+ const keyword = inner.keyword.text;
134
+ if (keyword === "section") {
135
+ this.error("prop.unknown", m.diagnostics.gantt.sectionInSection, inner.keyword, {
136
+ hints: [m.hints.gantt.sectionContents],
137
+ data: { candidates: ["task", "milestone", "marker"] },
138
+ });
139
+ return;
140
+ }
141
+ if (keyword === "task" || keyword === "milestone" || keyword === "marker") {
142
+ this.items.push({ stmt: inner, kind: keyword, section });
143
+ return;
144
+ }
145
+ // `description`, `url`, `class` and `link` are read with the section's own block.
146
+ if (keyword === "description" ||
147
+ keyword === "url" ||
148
+ keyword === "link" ||
149
+ (keyword === "class" && !inner.block)) {
150
+ return;
151
+ }
152
+ const chartLevel = CHART_WORDS.includes(keyword);
153
+ this.error("prop.unknown", m.diagnostics.gantt.unknownInSection(keyword), inner.keyword, {
154
+ hints: [chartLevel ? m.hints.flow.diagramLevel(keyword) : m.hints.gantt.sectionContents],
155
+ data: { candidates: chartLevel ? [] : ["task", "milestone", "marker"] },
156
+ });
157
+ }
158
+ otherProfileKind() {
159
+ return undefined;
160
+ }
161
+ declarationWords() {
162
+ return GANTT_ELEMENT_KINDS;
163
+ }
164
+ statementCandidates() {
165
+ return CHART_WORDS;
166
+ }
167
+ statementHints() {
168
+ return [messages().hints.gantt.statements];
169
+ }
170
+ // ---------------------------------------------------------------- the chart
171
+ afterRelations(_classified) {
172
+ this.readChart();
173
+ this.readItems();
174
+ this.checkDependencies();
175
+ this.schedule();
176
+ this.buildRelations();
177
+ this.measure();
178
+ }
179
+ /** What the chart statements write: the calendar first, which durations are counted in. */
180
+ readChart() {
181
+ const m = messages();
182
+ const seen = new Map();
183
+ const excludedWeekdays = new Set();
184
+ const excludedDates = new Set();
185
+ const included = [];
186
+ let weekStart = 0;
187
+ let axisFormat = null;
188
+ let ticks = null;
189
+ let axis = "bottom";
190
+ let labels = "bar";
191
+ let display = "normal";
192
+ let dependencies = "shown";
193
+ let dayWidth = null;
194
+ let today = null;
195
+ for (const stmt of this.chartStatements) {
196
+ const name = stmt.keyword.text;
197
+ if (name === "exclude" || name === "include") {
198
+ const items = this.dayList(stmt, name);
199
+ if (name === "exclude") {
200
+ const before = new Set(excludedWeekdays);
201
+ for (const item of items) {
202
+ if ("weekday" in item)
203
+ excludedWeekdays.add(item.weekday);
204
+ else
205
+ excludedDates.add(item.date);
206
+ }
207
+ if (excludedWeekdays.size === 7) {
208
+ this.invalid(m.diagnostics.gantt.everyDayExcluded, stmt, [
209
+ m.hints.gantt.everyDayExcluded,
210
+ ]);
211
+ excludedWeekdays.clear();
212
+ for (const weekday of before)
213
+ excludedWeekdays.add(weekday);
214
+ }
215
+ }
216
+ else {
217
+ for (const item of items)
218
+ if ("date" in item)
219
+ included.push(item);
220
+ }
221
+ continue;
222
+ }
223
+ const first = seen.get(name);
224
+ if (first) {
225
+ this.error("prop.duplicate", m.diagnostics.flow.setMoreThanOnceInDiagram(name), stmt.keyword, {
226
+ related: [{ message: m.related.firstSet, at: first }],
227
+ });
228
+ continue;
229
+ }
230
+ seen.set(name, stmt.keyword);
231
+ switch (name) {
232
+ case "weekStart": {
233
+ const value = this.readValue(stmt, { kind: "enum", values: WEEKDAYS }, name);
234
+ if (typeof value === "string")
235
+ weekStart = WEEKDAYS.indexOf(value);
236
+ break;
237
+ }
238
+ case "axisFormat": {
239
+ const value = this.readValue(stmt, { kind: "string" }, name);
240
+ if (typeof value !== "string")
241
+ break;
242
+ const bad = badDirective(value);
243
+ if (bad !== null) {
244
+ this.invalid(m.diagnostics.gantt.badDirective(bad, AXIS_DIRECTIVES.map((d) => `%${d}`)), stmt.args[0], [m.hints.gantt.axisFormat]);
245
+ break;
246
+ }
247
+ axisFormat = value;
248
+ break;
249
+ }
250
+ case "ticks":
251
+ ticks = this.readTicks(stmt);
252
+ break;
253
+ case "axis": {
254
+ const value = this.readValue(stmt, { kind: "enum", values: GANTT_AXIS_PLACES }, name);
255
+ if (typeof value === "string")
256
+ axis = value;
257
+ break;
258
+ }
259
+ case "labels": {
260
+ const value = this.readValue(stmt, { kind: "enum", values: GANTT_LABEL_PLACES }, name);
261
+ if (typeof value === "string")
262
+ labels = value;
263
+ break;
264
+ }
265
+ case "display": {
266
+ const value = this.readValue(stmt, { kind: "enum", values: GANTT_DISPLAYS }, name);
267
+ if (typeof value === "string")
268
+ display = value;
269
+ break;
270
+ }
271
+ case "dependencies": {
272
+ const value = this.readValue(stmt, { kind: "enum", values: GANTT_DEPENDENCIES }, name);
273
+ if (typeof value === "string")
274
+ dependencies = value;
275
+ break;
276
+ }
277
+ case "dayWidth": {
278
+ const value = this.readValue(stmt, { kind: "number", ...GANTT_DAY_WIDTH_RANGE }, name);
279
+ if (typeof value === "number")
280
+ dayWidth = value;
281
+ break;
282
+ }
283
+ case "today":
284
+ today = this.readToday(stmt);
285
+ break;
286
+ }
287
+ }
288
+ // A date in `include` that nothing excludes is a working day already.
289
+ const excluded = workingCalendar([...excludedWeekdays], [...excludedDates], []);
290
+ const includedDates = new Set();
291
+ for (const { date, arg } of included) {
292
+ const day = dayOfDate(date);
293
+ const covered = excluded.excludedDays.has(day) || excluded.weekdayExcluded[weekdayOfDay(day)];
294
+ if (!covered) {
295
+ this.invalid(m.diagnostics.gantt.alreadyWorking(date), arg, [m.hints.gantt.alreadyWorking]);
296
+ continue;
297
+ }
298
+ includedDates.add(date);
299
+ }
300
+ const calendar = {
301
+ excludedWeekdays: [...excludedWeekdays].sort((a, b) => a - b),
302
+ excludedDates: [...excludedDates].sort(),
303
+ includedDates: [...includedDates].sort(),
304
+ weekStart,
305
+ };
306
+ this.calendar = workingCalendar(calendar.excludedWeekdays, calendar.excludedDates, calendar.includedDates);
307
+ this.chart = {
308
+ calendar,
309
+ axisFormat,
310
+ ticks,
311
+ axis,
312
+ labels,
313
+ display,
314
+ dependencies,
315
+ dayWidth,
316
+ today,
317
+ span: null,
318
+ };
319
+ }
320
+ /** The items of `exclude` and `include`: weekdays (`weekends` is two of them) and dates. */
321
+ dayList(stmt, name) {
322
+ const m = messages();
323
+ const out = [];
324
+ if (stmt.block || stmt.chain) {
325
+ this.invalid(m.diagnostics.common.doesNotTakeBlock(name), stmt.block ?? stmt.chain);
326
+ return out;
327
+ }
328
+ if (stmt.args.length === 0) {
329
+ this.invalid(m.diagnostics.gantt.needsDays(name), stmt.keyword, [
330
+ m.hints.example(name === "exclude" ? "exclude weekends, 2026-12-25" : "include 2026-06-06"),
331
+ ]);
332
+ return out;
333
+ }
334
+ for (const arg of stmt.args) {
335
+ const word = singleIdent(arg);
336
+ if (name === "exclude" && word?.text === "weekends") {
337
+ out.push({ weekday: 5 }, { weekday: 6 });
338
+ continue;
339
+ }
340
+ if (name === "exclude" && word && WEEKDAYS.includes(word.text)) {
341
+ out.push({ weekday: WEEKDAYS.indexOf(word.text) });
342
+ continue;
343
+ }
344
+ if (arg.kind === "DateLiteral") {
345
+ const read = this.checkDate(arg);
346
+ if (!read)
347
+ continue;
348
+ if (read.hasTime) {
349
+ this.invalid(m.diagnostics.gantt.dayNotInstant(name, arg.text), arg, [
350
+ m.hints.gantt.dateForm,
351
+ ]);
352
+ continue;
353
+ }
354
+ out.push({ date: read.text, arg });
355
+ continue;
356
+ }
357
+ this.invalid(m.diagnostics.gantt.notADay(describeArg(arg), name), arg, [
358
+ name === "exclude" ? m.hints.gantt.excludeItems(WEEKDAYS) : m.hints.gantt.includeItems,
359
+ ]);
360
+ }
361
+ return out;
362
+ }
363
+ /** `ticks <n><unit>`: a whole number with a duration unit, `mo` or `y`. */
364
+ readTicks(stmt) {
365
+ const m = messages();
366
+ if (stmt.block || stmt.chain) {
367
+ this.invalid(m.diagnostics.common.doesNotTakeBlock("ticks"), stmt.block ?? stmt.chain);
368
+ return null;
369
+ }
370
+ const [arg, extra] = stmt.args;
371
+ if (!arg) {
372
+ this.invalid(m.diagnostics.gantt.needsTicks, stmt.keyword, [m.hints.gantt.ticks(TICK_UNITS)]);
373
+ return null;
374
+ }
375
+ if (extra) {
376
+ this.invalid(m.diagnostics.values.oneValue("ticks"), extra);
377
+ return null;
378
+ }
379
+ const ok = arg.kind === "NumberLiteral" &&
380
+ arg.unit !== null &&
381
+ TICK_UNITS.includes(arg.unit) &&
382
+ Number.isInteger(arg.value) &&
383
+ arg.value > 0;
384
+ if (!ok) {
385
+ this.invalid(m.diagnostics.gantt.badTicks(describeArg(arg)), arg, [
386
+ m.hints.gantt.ticks(TICK_UNITS),
387
+ ]);
388
+ return null;
389
+ }
390
+ return arg.token.text;
391
+ }
392
+ /** `today` or `today <date>`. */
393
+ readToday(stmt) {
394
+ const m = messages();
395
+ const range = this.range(stmt);
396
+ if (stmt.block || stmt.chain) {
397
+ this.invalid(m.diagnostics.common.doesNotTakeBlock("today"), stmt.block ?? stmt.chain);
398
+ return null;
399
+ }
400
+ const [arg, extra] = stmt.args;
401
+ if (!arg)
402
+ return { date: null, range };
403
+ if (extra) {
404
+ this.invalid(m.diagnostics.values.oneValue("today"), extra);
405
+ return null;
406
+ }
407
+ if (arg.kind !== "DateLiteral") {
408
+ this.invalid(m.diagnostics.gantt.notDate("today", describeArg(arg)), arg, [
409
+ m.hints.gantt.dateForm,
410
+ ]);
411
+ return null;
412
+ }
413
+ const read = this.checkDate(arg);
414
+ if (!read)
415
+ return null;
416
+ if (read.hasTime) {
417
+ this.invalid(m.diagnostics.gantt.dayNotInstant("today", arg.text), arg, [
418
+ m.hints.gantt.dateForm,
419
+ ]);
420
+ return null;
421
+ }
422
+ return { date: read.text, range };
423
+ }
424
+ // ---------------------------------------------------------------- dates and durations
425
+ /** A date literal checked against the calendar: it names a day that exists. */
426
+ checkDate(arg) {
427
+ const parsed = parseDate(arg.text);
428
+ if (parsed.ok)
429
+ return { arg, text: arg.text, time: parsed.time, hasTime: parsed.hasTime };
430
+ const m = messages();
431
+ this.invalid(m.diagnostics.gantt.dateProblem(arg.text, describeProblem(parsed.problem)), arg, [
432
+ m.hints.gantt.dateForm,
433
+ ]);
434
+ return undefined;
435
+ }
436
+ /** The date after a `from`, `at` or `to`: a literal that exists. */
437
+ dateAfter(word, value) {
438
+ const m = messages();
439
+ if (!value || this.endsList(value)) {
440
+ this.invalid(m.diagnostics.gantt.needsDate(word.text), word, [m.hints.gantt.dateForm]);
441
+ return undefined;
442
+ }
443
+ if (value.kind === "StringLiteral") {
444
+ this.invalid(m.diagnostics.gantt.quotedDate(word.text, value.value), value, [
445
+ m.hints.gantt.unquotedDate(value.value),
446
+ ]);
447
+ return undefined;
448
+ }
449
+ if (value.kind !== "DateLiteral") {
450
+ this.invalid(m.diagnostics.gantt.notDate(word.text, describeArg(value)), value, [
451
+ m.hints.gantt.dateForm,
452
+ ]);
453
+ return undefined;
454
+ }
455
+ return this.checkDate(value);
456
+ }
457
+ /** Whether an argument is a word that ends a value: a clause word or a status word. */
458
+ endsList(arg) {
459
+ const word = singleIdent(arg);
460
+ return word !== undefined && GANTT_RESERVED_IDS.includes(word.text);
461
+ }
462
+ /** The duration after a `for`: a number above 0 with a unit. */
463
+ durationAfter(word, value) {
464
+ const m = messages();
465
+ const units = DURATION_UNITS;
466
+ if (!value || this.endsList(value)) {
467
+ this.invalid(m.diagnostics.gantt.needsDuration(word.text), word, [
468
+ m.hints.gantt.durationForm(DURATION_UNITS),
469
+ ]);
470
+ return undefined;
471
+ }
472
+ if (value.kind !== "NumberLiteral") {
473
+ this.invalid(m.diagnostics.gantt.notDuration(describeArg(value)), value, [
474
+ m.hints.gantt.durationForm(DURATION_UNITS),
475
+ ]);
476
+ return undefined;
477
+ }
478
+ const text = value.token.text;
479
+ if (value.unit === null || !units.includes(value.unit)) {
480
+ this.invalid(m.diagnostics.gantt.durationUnit(text, value.unit, DURATION_UNITS), value, [
481
+ m.hints.gantt.durationForm(DURATION_UNITS),
482
+ ]);
483
+ return undefined;
484
+ }
485
+ if (value.value <= 0) {
486
+ this.invalid(m.diagnostics.gantt.durationAboveZero(text), value, [
487
+ value.value === 0 ? m.hints.gantt.zeroDuration : m.hints.gantt.durationForm(DURATION_UNITS),
488
+ ]);
489
+ return undefined;
490
+ }
491
+ const seconds = value.value * unitSeconds(value.unit, this.calendar);
492
+ if (seconds > MAX_DURATION_SECONDS) {
493
+ this.invalid(m.diagnostics.gantt.durationTooLong(text), value);
494
+ return undefined;
495
+ }
496
+ return { amount: value.value, unit: value.unit, arg: value };
497
+ }
498
+ // ---------------------------------------------------------------- sections, tasks, markers
499
+ readItems() {
500
+ const nodes = [];
501
+ for (const item of this.items) {
502
+ if (item.kind === "section")
503
+ this.readSection(item);
504
+ else {
505
+ const node = this.readBar(item, item.kind);
506
+ if (node)
507
+ nodes.push(node);
508
+ }
509
+ }
510
+ this.nodes = nodes;
511
+ }
512
+ readSection(item) {
513
+ const stmt = item.stmt;
514
+ const m = messages();
515
+ const form = FORMS.section;
516
+ if (stmt.chain) {
517
+ this.invalid(m.diagnostics.common.singleBlock("section"), stmt.chain.clauses[1].keyword);
518
+ return;
519
+ }
520
+ const [idArg, labelArg, ...extra] = stmt.args;
521
+ const idToken = singleIdent(idArg);
522
+ if (!idToken) {
523
+ this.invalid(m.diagnostics.common.expected(form), idArg ?? stmt.keyword, [
524
+ m.hints.example(EXAMPLES.section),
525
+ ]);
526
+ return;
527
+ }
528
+ this.checkReservedId(idToken);
529
+ let label = idToken.text;
530
+ if (labelArg) {
531
+ if (labelArg.kind !== "StringLiteral") {
532
+ this.invalid(m.diagnostics.flow.quotedSource("label", idToken.text), labelArg);
533
+ return;
534
+ }
535
+ label = labelArg.value;
536
+ }
537
+ if (extra.length > 0) {
538
+ this.invalid(m.diagnostics.flow.valueAfter("label", form), extra[0]);
539
+ return;
540
+ }
541
+ const block = this.groupBlock(blockOf(stmt), idToken);
542
+ this.addElement(this.element({
543
+ id: idToken.text,
544
+ kind: "section",
545
+ label,
546
+ labelWritten: labelArg !== undefined,
547
+ description: block.description,
548
+ url: block.url,
549
+ classes: block.classes,
550
+ link: block.link,
551
+ task: null,
552
+ groupPath: [],
553
+ stmt,
554
+ idToken,
555
+ }));
556
+ }
557
+ /** A task, milestone or marker: the label, the clauses, the status and the block. */
558
+ readBar(item, kind) {
559
+ const stmt = item.stmt;
560
+ const m = messages();
561
+ const form = FORMS[kind];
562
+ if (stmt.chain) {
563
+ this.invalid(m.diagnostics.common.singleBlock(kind), stmt.chain.clauses[1].keyword);
564
+ return undefined;
565
+ }
566
+ const [idArg, ...rest] = stmt.args;
567
+ const idToken = singleIdent(idArg);
568
+ if (!idToken) {
569
+ this.invalid(m.diagnostics.common.expected(form), idArg ?? stmt.keyword, [
570
+ m.hints.example(EXAMPLES[kind]),
571
+ ]);
572
+ return undefined;
573
+ }
574
+ this.checkReservedId(idToken);
575
+ let label = idToken.text;
576
+ let labelWritten = false;
577
+ if (rest[0]?.kind === "StringLiteral") {
578
+ label = rest[0].value;
579
+ labelWritten = true;
580
+ rest.shift();
581
+ }
582
+ const clauses = GANTT_CLAUSES[kind];
583
+ const statuses = kind === "marker" ? [] : STATUS_WORDS;
584
+ const seen = new Map();
585
+ /** Records a word, or reports it as written twice and returns true. */
586
+ const claimed = (word) => {
587
+ const first = seen.get(word.text);
588
+ if (first) {
589
+ this.error("prop.duplicate", m.diagnostics.gantt.clauseTwice(word.text), word, {
590
+ related: [{ message: m.related.firstWritten, at: first }],
591
+ });
592
+ return true;
593
+ }
594
+ seen.set(word.text, word);
595
+ return false;
596
+ };
597
+ let from = null;
598
+ let fromClause = null;
599
+ let to = null;
600
+ let forClause = null;
601
+ let end = null;
602
+ const after = [];
603
+ let afterRange = null;
604
+ const until = [];
605
+ let status = null;
606
+ let critical = false;
607
+ let i = 0;
608
+ while (i < rest.length) {
609
+ const arg = rest[i];
610
+ const word = singleIdent(arg);
611
+ if (!word) {
612
+ this.invalid(m.diagnostics.gantt.strayValue(describeArg(arg), form), arg, [
613
+ m.hints.gantt.clauses(clauses, statuses),
614
+ ]);
615
+ i++;
616
+ continue;
617
+ }
618
+ const text = word.text;
619
+ if (clauses.includes(text)) {
620
+ // One end clause: a second names the first, however it is written.
621
+ const other = text === "for" || text === "to" || text === "until"
622
+ ? ["for", "to", "until"].find((name) => name !== text && seen.has(name))
623
+ : undefined;
624
+ if (other) {
625
+ this.error("prop.duplicate", m.diagnostics.gantt.endTwice(text, other), word, {
626
+ related: [{ message: m.related.firstWritten, at: seen.get(other) }],
627
+ });
628
+ i = this.skipClause(rest, i + 1);
629
+ continue;
630
+ }
631
+ if (claimed(word)) {
632
+ i = this.skipClause(rest, i + 1);
633
+ continue;
634
+ }
635
+ if (text === "from" || text === "at" || text === "to") {
636
+ const value = this.dateAfter(word, rest[i + 1]);
637
+ const valueArg = value ? rest[i + 1] : undefined;
638
+ const consumed = rest[i + 1] && !this.endsList(rest[i + 1]) ? 2 : 1;
639
+ if (value && valueArg) {
640
+ const range = this.lines.range({ start: word.start, end: valueArg.end });
641
+ const valueRange = this.range(valueArg);
642
+ if (text === "to") {
643
+ to = value;
644
+ end = { kind: "to", date: value.text, range, valueRange };
645
+ }
646
+ else {
647
+ from = value;
648
+ fromClause = { range, valueRange };
649
+ }
650
+ }
651
+ i += consumed;
652
+ continue;
653
+ }
654
+ if (text === "for") {
655
+ const duration = this.durationAfter(word, rest[i + 1]);
656
+ const consumed = rest[i + 1] && !this.endsList(rest[i + 1]) ? 2 : 1;
657
+ if (duration) {
658
+ forClause = duration;
659
+ end = {
660
+ kind: "for",
661
+ amount: duration.amount,
662
+ unit: duration.unit,
663
+ range: this.lines.range({ start: word.start, end: duration.arg.end }),
664
+ valueRange: this.range(duration.arg),
665
+ };
666
+ }
667
+ i += consumed;
668
+ continue;
669
+ }
670
+ // `after` and `until`: ids up to the next clause word or status word.
671
+ const ids = [];
672
+ let j = i + 1;
673
+ while (j < rest.length) {
674
+ const next = rest[j];
675
+ if (this.endsList(next))
676
+ break;
677
+ const token = singleIdent(next);
678
+ if (!token) {
679
+ this.invalid(m.diagnostics.gantt.notAnId(text, describeArg(next)), next, [
680
+ m.hints.gantt.idList(text),
681
+ ]);
682
+ }
683
+ else if (ids.some((entry) => entry.id === token.text)) {
684
+ this.error("prop.duplicate", m.diagnostics.gantt.idTwice(token.text, text), token, {
685
+ related: [
686
+ {
687
+ message: m.related.firstWritten,
688
+ at: ids.find((e) => e.id === token.text).token,
689
+ },
690
+ ],
691
+ });
692
+ }
693
+ else {
694
+ ids.push({ id: token.text, token });
695
+ }
696
+ j++;
697
+ }
698
+ if (ids.length === 0 && j === i + 1) {
699
+ this.invalid(m.diagnostics.gantt.needsIds(text), word, [m.hints.gantt.idList(text)]);
700
+ }
701
+ const last = rest[j - 1];
702
+ const clauseRange = this.lines.range({ start: word.start, end: (last ?? word).end });
703
+ if (text === "after") {
704
+ after.push(...ids);
705
+ afterRange = clauseRange;
706
+ }
707
+ else {
708
+ until.push(...ids);
709
+ end = {
710
+ kind: "until",
711
+ ids: ids.map((e) => ({ id: e.id, range: this.range(e.token) })),
712
+ range: clauseRange,
713
+ };
714
+ }
715
+ i = j;
716
+ continue;
717
+ }
718
+ if (statuses.includes(text)) {
719
+ if (text === "critical") {
720
+ if (!claimed(word))
721
+ critical = true;
722
+ }
723
+ else {
724
+ const other = text === "done" ? "active" : "done";
725
+ if (seen.has(other)) {
726
+ this.error("prop.duplicate", m.diagnostics.gantt.statusTwice(text, other), word, {
727
+ related: [{ message: m.related.firstWritten, at: seen.get(other) }],
728
+ });
729
+ }
730
+ else if (!claimed(word)) {
731
+ status = text;
732
+ }
733
+ }
734
+ i++;
735
+ continue;
736
+ }
737
+ // Not a word of this statement: the clauses it has, and what the other statements use.
738
+ const elsewhere = ["from", "for", "to", "until"].includes(text)
739
+ ? "task"
740
+ : text === "at"
741
+ ? "milestone"
742
+ : null;
743
+ this.error("prop.unknown", m.diagnostics.gantt.unknownWord(text, kind), word, {
744
+ hints: [
745
+ m.hints.gantt.clauses(clauses, statuses),
746
+ ...(elsewhere ? [m.hints.gantt.otherKind(text, elsewhere)] : []),
747
+ ],
748
+ data: { candidates: [...clauses, ...statuses] },
749
+ });
750
+ i = this.skipClause(rest, i + 1);
751
+ }
752
+ if (kind === "marker" && !from && after.length === 0 && !seen.has("at") && !seen.has("after")) {
753
+ this.invalid(m.diagnostics.gantt.markerNeedsPlace(idToken.text), idToken, [
754
+ m.hints.example(EXAMPLES.marker),
755
+ ]);
756
+ }
757
+ if (kind === "marker" && seen.has("at") && seen.has("after")) {
758
+ this.error("prop.duplicate", m.diagnostics.gantt.markerOnePlace(idToken.text), seen.get("after"), {
759
+ related: [{ message: m.related.firstWritten, at: seen.get("at") }],
760
+ });
761
+ }
762
+ const block = this.elementBlock(blockOf(stmt), this.profile.elementProperties, "Element", idToken);
763
+ const task = {
764
+ from: from && fromClause ? { date: from.text, ...fromClause } : null,
765
+ after: [],
766
+ afterRange,
767
+ end,
768
+ status,
769
+ critical,
770
+ start: null,
771
+ finish: null,
772
+ working: null,
773
+ };
774
+ const element = this.element({
775
+ id: idToken.text,
776
+ kind,
777
+ label,
778
+ labelWritten,
779
+ description: block.description,
780
+ url: block.url,
781
+ classes: block.classes,
782
+ link: block.link,
783
+ task,
784
+ groupPath: this.sectionPath(item.section),
785
+ stmt,
786
+ idToken,
787
+ });
788
+ if (!this.addElement(element))
789
+ return undefined;
790
+ // A written date on a day that does not work moves to the next one that does.
791
+ if (from && kind !== "marker")
792
+ this.warnExcludedStart(from);
793
+ if (to)
794
+ this.warnExcludedEnd(to);
795
+ return {
796
+ kind,
797
+ stmt,
798
+ element,
799
+ task,
800
+ from,
801
+ to,
802
+ forSeconds: forClause
803
+ ? Math.max(1, Math.round(forClause.amount * unitSeconds(forClause.unit, this.calendar)))
804
+ : null,
805
+ after,
806
+ until,
807
+ startWritten: seen.has("from") || seen.has("at") || seen.has("after"),
808
+ edges: [],
809
+ };
810
+ }
811
+ /** The arguments to skip after an unknown word: the values written for it. */
812
+ skipClause(rest, from) {
813
+ let i = from;
814
+ while (i < rest.length && singleIdent(rest[i]) === undefined)
815
+ i++;
816
+ return i;
817
+ }
818
+ checkReservedId(token) {
819
+ if (!GANTT_RESERVED_IDS.includes(token.text))
820
+ return;
821
+ const m = messages();
822
+ this.invalid(m.diagnostics.gantt.reservedId(token.text), token, [m.hints.gantt.reservedId]);
823
+ }
824
+ sectionPath(section) {
825
+ const id = section ? singleIdent(section.args[0]) : undefined;
826
+ return id ? [id.text] : [];
827
+ }
828
+ /** An element of the chart: the fields the other profiles leave empty. */
829
+ element(fields) {
830
+ return {
831
+ id: fields.id,
832
+ kind: fields.kind,
833
+ label: fields.label,
834
+ implicit: false,
835
+ description: fields.description,
836
+ classes: fields.classes,
837
+ tags: [],
838
+ ports: [],
839
+ layer: null,
840
+ layerRange: null,
841
+ link: fields.link,
842
+ url: fields.url,
843
+ source: null,
844
+ sourceRange: null,
845
+ labelWritten: fields.labelWritten,
846
+ entity: null,
847
+ technology: null,
848
+ external: false,
849
+ shape: null,
850
+ line: null,
851
+ pen: null,
852
+ list: null,
853
+ task: fields.task,
854
+ attachedTo: null,
855
+ attachedToRange: null,
856
+ typeKind: null,
857
+ data: {},
858
+ groupPath: fields.groupPath,
859
+ range: this.range(fields.stmt),
860
+ idRange: this.range(fields.idToken),
861
+ idToken: fields.idToken,
862
+ node: fields.stmt,
863
+ };
864
+ }
865
+ /** `gantt.excluded-day` for a start written on a day that does not work. */
866
+ warnExcludedStart(written) {
867
+ const moved = nextWorking(written.time, this.calendar);
868
+ if (moved === null || moved === written.time)
869
+ return;
870
+ this.warnExcluded(written, moved, "starts");
871
+ }
872
+ /** `gantt.excluded-day` for an end written on a day that does not work. */
873
+ warnExcludedEnd(written) {
874
+ const day = dayNumber(written.time);
875
+ if (nextWorking(day * SECONDS_PER_DAY, this.calendar) === day * SECONDS_PER_DAY)
876
+ return;
877
+ this.warnExcluded(written, this.lastWorkingBefore(written.time), "ends");
878
+ }
879
+ warnExcluded(written, to, what) {
880
+ const m = messages();
881
+ const day = dayNumber(written.time);
882
+ const target = to === null ? null : dayNumber(what === "ends" ? to - 1 : to);
883
+ this.bag.add({
884
+ code: "gantt.excluded-day",
885
+ severity: "warning",
886
+ message: m.diagnostics.gantt.excludedDay(written.text, weekdayOfDay(day), target === null ? null : { date: formatDay(target), weekday: weekdayOfDay(target) }, what),
887
+ span: { start: written.arg.start, end: written.arg.end },
888
+ hints: [m.hints.gantt.excludedDay],
889
+ });
890
+ }
891
+ /** The end of the last working day on or before the day of an instant; null when none. */
892
+ lastWorkingBefore(time) {
893
+ let day = dayNumber(time);
894
+ for (let i = 0; i < 400; i++, day--) {
895
+ if (nextWorking(day * SECONDS_PER_DAY, this.calendar) === day * SECONDS_PER_DAY) {
896
+ return (day + 1) * SECONDS_PER_DAY;
897
+ }
898
+ }
899
+ return null;
900
+ }
901
+ // ---------------------------------------------------------------- dependencies
902
+ /** The ids `after` and `until` may name, for the candidates of an unknown one. */
903
+ dependable() {
904
+ return this.nodes.filter((n) => n.kind !== "marker").map((n) => n.element.id);
905
+ }
906
+ checkDependencies() {
907
+ const m = messages();
908
+ const index = new Map();
909
+ this.nodes.forEach((node, i) => index.set(node.element.id, i));
910
+ // The previous task or milestone in source order, which a task without a start follows.
911
+ let previous = null;
912
+ const previousOf = [];
913
+ this.nodes.forEach((node, i) => {
914
+ previousOf.push(previous);
915
+ if (node.kind !== "marker")
916
+ previous = i;
917
+ });
918
+ this.nodes.forEach((node, i) => {
919
+ const edges = [];
920
+ const resolved = [];
921
+ for (const [via, list] of [
922
+ ["after", node.after],
923
+ ["until", node.until],
924
+ ]) {
925
+ for (const written of list) {
926
+ const target = this.elementsById.get(written.id);
927
+ if (!target) {
928
+ this.error("ref.unknown-element", m.diagnostics.common.unknownElement(written.id), written.token, {
929
+ hints: [m.hints.gantt.unknownTask(written.id)],
930
+ data: { ids: [written.id], candidates: this.dependable() },
931
+ });
932
+ continue;
933
+ }
934
+ if (target.kind === "section" || target.kind === "marker") {
935
+ this.invalid(m.diagnostics.gantt.dependencyKind(via, written.id, target.kind), written.token, [m.hints.gantt.idList(via)]);
936
+ continue;
937
+ }
938
+ const to = index.get(written.id);
939
+ if (to === undefined)
940
+ continue;
941
+ this.reference(written.token, written.id, "element", "dependency", node.stmt);
942
+ edges.push({ to, via, token: written.token });
943
+ if (via === "after")
944
+ resolved.push({ id: written.id, range: this.range(written.token) });
945
+ }
946
+ }
947
+ node.task.after = resolved;
948
+ const follows = !node.startWritten;
949
+ const before = previousOf[i];
950
+ if (node.kind !== "marker" && follows && before !== null && before !== undefined) {
951
+ edges.push({ to: before, via: "previous", token: null });
952
+ }
953
+ node.edges = edges;
954
+ });
955
+ }
956
+ // ---------------------------------------------------------------- the schedule
957
+ /** Resolves every start and end in dependency order, and reports what cannot be resolved. */
958
+ schedule() {
959
+ const m = messages();
960
+ const count = this.nodes.length;
961
+ const dependents = Array.from({ length: count }, () => []);
962
+ const pending = new Array(count).fill(0);
963
+ this.nodes.forEach((node, i) => {
964
+ for (const to of new Set(node.edges.map((e) => e.to))) {
965
+ dependents[to].push(i);
966
+ pending[i]++;
967
+ }
968
+ });
969
+ const done = new Array(count).fill(false);
970
+ const failed = new Array(count).fill(false);
971
+ const queue = [];
972
+ for (let i = 0; i < count; i++)
973
+ if (pending[i] === 0)
974
+ queue.push(i);
975
+ for (let head = 0; head < queue.length; head++) {
976
+ const i = queue[head];
977
+ done[i] = true;
978
+ if (!this.resolveNode(i, failed))
979
+ failed[i] = true;
980
+ for (const next of dependents[i]) {
981
+ pending[next]--;
982
+ if (pending[next] === 0)
983
+ queue.push(next);
984
+ }
985
+ }
986
+ // What is left waits on a loop, or on something that does.
987
+ const leftover = done.map((d) => !d);
988
+ if (!leftover.some(Boolean))
989
+ return;
990
+ const components = stronglyConnected(count, (n) => this.nodes[n].edges.map((e) => e.to), leftover);
991
+ for (const component of components) {
992
+ const members = new Set(component);
993
+ const selfLoop = component.length === 1 &&
994
+ this.nodes[component[0]].edges.some((e) => e.to === component[0]);
995
+ if (component.length < 2 && !selfLoop)
996
+ continue;
997
+ const ids = [...members].sort((a, b) => a - b).map((n) => this.nodes[n].element.id);
998
+ const references = component
999
+ .flatMap((n) => this.nodes[n].edges.filter((e) => e.token !== null && members.has(e.to)).map((e) => e.token))
1000
+ .sort((a, b) => a.start - b.start);
1001
+ for (const token of references) {
1002
+ this.error("gantt.cycle", m.diagnostics.gantt.cycle(ids), token, {
1003
+ related: references
1004
+ .filter((other) => other !== token)
1005
+ .map((other) => ({ message: m.related.inTheLoop, at: other })),
1006
+ hints: [m.hints.gantt.cycle],
1007
+ data: { ids },
1008
+ });
1009
+ }
1010
+ }
1011
+ }
1012
+ /** The start and the end of one node, its dependencies being resolved; false when it has none. */
1013
+ resolveNode(i, failed) {
1014
+ const m = messages();
1015
+ const node = this.nodes[i];
1016
+ if (node.edges.some((e) => failed[e.to]))
1017
+ return false;
1018
+ const finishOf = (n) => this.nodes[n].task.finish;
1019
+ const startOf = (n) => this.nodes[n].task.start;
1020
+ // The start: the later of the written date and the ends of what it waits for.
1021
+ const starts = [];
1022
+ if (node.from)
1023
+ starts.push(node.from.time);
1024
+ for (const e of node.edges)
1025
+ if (e.via !== "until")
1026
+ starts.push(finishOf(e.to));
1027
+ if (starts.length === 0) {
1028
+ // A start that was written wrong, and a marker without a place, are reported already.
1029
+ if (!node.startWritten && node.kind !== "marker") {
1030
+ this.error("gantt.unscheduled", m.diagnostics.gantt.unscheduled(node.element.id), node.element.idToken, { hints: [m.hints.gantt.unscheduled] });
1031
+ }
1032
+ return false;
1033
+ }
1034
+ let start = Math.max(...starts);
1035
+ // A marker stays where it is put; a task or milestone moves to the next working time.
1036
+ if (node.kind !== "marker") {
1037
+ const moved = nextWorking(start, this.calendar);
1038
+ if (moved === null)
1039
+ return this.noWorkingTime(node);
1040
+ start = moved;
1041
+ }
1042
+ let finish = start;
1043
+ if (node.kind === "task") {
1044
+ const end = node.task.end;
1045
+ if (node.forSeconds !== null) {
1046
+ const added = addWorking(start, node.forSeconds, this.calendar);
1047
+ if (added === null)
1048
+ return this.noWorkingTime(node);
1049
+ finish = added;
1050
+ }
1051
+ else if (node.to) {
1052
+ finish = node.to.hasTime ? node.to.time : endOfDay(node.to.time);
1053
+ if (!node.to.hasTime) {
1054
+ const before = this.lastWorkingBefore(node.to.time);
1055
+ if (before !== null && before < finish)
1056
+ finish = before;
1057
+ }
1058
+ }
1059
+ else {
1060
+ // `until` ends at the earliest start of what it names; with none that resolved (they are
1061
+ // reported), and without an end clause, a task lasts a day.
1062
+ const targets = end?.kind === "until"
1063
+ ? node.edges.filter((e) => e.via === "until").map((e) => startOf(e.to))
1064
+ : [];
1065
+ if (targets.length > 0) {
1066
+ finish = Math.min(...targets);
1067
+ }
1068
+ else {
1069
+ const added = addWorking(start, SECONDS_PER_DAY, this.calendar);
1070
+ if (added === null)
1071
+ return this.noWorkingTime(node);
1072
+ finish = added;
1073
+ }
1074
+ }
1075
+ if (finish <= start) {
1076
+ this.error("gantt.schedule", m.diagnostics.gantt.endsBeforeStart(node.element.id, formatDate(start), formatDate(finish), finish === start), this.endToken(node) ?? node.element.idToken, { hints: [m.hints.gantt.endsBeforeStart] });
1077
+ return false;
1078
+ }
1079
+ }
1080
+ node.task.start = start;
1081
+ node.task.finish = finish;
1082
+ node.task.working = workingBetween(start, finish, this.calendar);
1083
+ return true;
1084
+ }
1085
+ /** The token to report an end that is not after the start on: the end clause's word. */
1086
+ endToken(node) {
1087
+ const words = node.stmt.args
1088
+ .map((arg) => singleIdent(arg))
1089
+ .filter((t) => t !== undefined);
1090
+ return words.find((t) => t.text === "to" || t.text === "until" || t.text === "for") ?? null;
1091
+ }
1092
+ noWorkingTime(node) {
1093
+ const m = messages();
1094
+ this.error("gantt.schedule", m.diagnostics.gantt.noWorkingTime(node.element.id), node.element.idToken, {
1095
+ hints: [m.hints.gantt.noWorkingTime],
1096
+ });
1097
+ return false;
1098
+ }
1099
+ // ---------------------------------------------------------------- relations and span
1100
+ /** Each `after` id is an arrow from that task to the one that waits, derived from the clause. */
1101
+ buildRelations() {
1102
+ const counts = new Map();
1103
+ for (const node of this.nodes) {
1104
+ for (const reference of node.task.after) {
1105
+ const pair = `${reference.id}->${node.element.id}`;
1106
+ const n = (counts.get(pair) ?? 0) + 1;
1107
+ counts.set(pair, n);
1108
+ const relation = {
1109
+ key: `${pair}#${n}`,
1110
+ id: null,
1111
+ from: reference.id,
1112
+ to: node.element.id,
1113
+ line: "solid",
1114
+ heads: "end",
1115
+ twoWay: false,
1116
+ async: false,
1117
+ returns: null,
1118
+ technology: null,
1119
+ sources: [],
1120
+ derived: true,
1121
+ label: null,
1122
+ description: null,
1123
+ assumption: null,
1124
+ data: {},
1125
+ classes: [],
1126
+ tags: [],
1127
+ layer: null,
1128
+ layerRange: null,
1129
+ groupPath: [],
1130
+ range: reference.range,
1131
+ idRange: null,
1132
+ node: node.stmt,
1133
+ };
1134
+ this.relations.push(relation);
1135
+ this.relationsByKey.set(relation.key, relation);
1136
+ }
1137
+ }
1138
+ }
1139
+ /** The chart's span: the earliest start to the latest end of what is scheduled. */
1140
+ measure() {
1141
+ let start = Infinity;
1142
+ let end = -Infinity;
1143
+ for (const node of this.nodes) {
1144
+ const { start: s, finish: f } = node.task;
1145
+ if (s === null || f === null)
1146
+ continue;
1147
+ start = Math.min(start, s);
1148
+ end = Math.max(end, f);
1149
+ }
1150
+ this.chart = {
1151
+ ...this.chart,
1152
+ span: start <= end ? { start, end } : null,
1153
+ };
1154
+ }
1155
+ }
1156
+ // -------------------------------------------------------------------------- helpers
1157
+ /** What is wrong with a date that has the form but no day: the pieces a message names. */
1158
+ function describeProblem(problem) {
1159
+ switch (problem.kind) {
1160
+ case "month":
1161
+ return { kind: "month", month: problem.month };
1162
+ case "day":
1163
+ return {
1164
+ kind: "day",
1165
+ year: problem.year,
1166
+ month: problem.month,
1167
+ day: problem.day,
1168
+ daysInMonth: problem.daysInMonth,
1169
+ };
1170
+ case "hour":
1171
+ return { kind: "hour", value: problem.hour };
1172
+ case "minute":
1173
+ return { kind: "minute", value: problem.minute };
1174
+ case "second":
1175
+ return { kind: "second", value: problem.second };
1176
+ case "format":
1177
+ return { kind: "format" };
1178
+ }
1179
+ }
1180
+ /**
1181
+ * The first directive of an `axisFormat` that is not one of `AXIS_DIRECTIVES`, as written (`%Q`),
1182
+ * or a lone `%` at the end; null when every directive is known.
1183
+ */
1184
+ export function badDirective(format) {
1185
+ for (let i = 0; i < format.length; i++) {
1186
+ if (format[i] !== "%")
1187
+ continue;
1188
+ let j = i + 1;
1189
+ const flag = format[j] === "-";
1190
+ if (flag)
1191
+ j++;
1192
+ const directive = format[j];
1193
+ if (directive === undefined)
1194
+ return format.slice(i);
1195
+ if (!AXIS_DIRECTIVES.includes(directive) ||
1196
+ (flag && directive === "%")) {
1197
+ return format.slice(i, j + 1);
1198
+ }
1199
+ i = j;
1200
+ }
1201
+ return null;
1202
+ }
1203
+ /**
1204
+ * The strongly connected components of the nodes `among`, by Tarjan's algorithm, with the nodes
1205
+ * a component's members depend on as its edges.
1206
+ */
1207
+ function stronglyConnected(count, edges, among) {
1208
+ const index = new Array(count).fill(-1);
1209
+ const low = new Array(count).fill(0);
1210
+ const onStack = new Array(count).fill(false);
1211
+ const stack = [];
1212
+ const components = [];
1213
+ let counter = 0;
1214
+ for (let root = 0; root < count; root++) {
1215
+ if (!among[root] || index[root] !== -1)
1216
+ continue;
1217
+ const work = [{ node: root, next: 0 }];
1218
+ index[root] = low[root] = counter++;
1219
+ stack.push(root);
1220
+ onStack[root] = true;
1221
+ while (work.length > 0) {
1222
+ const frame = work[work.length - 1];
1223
+ const targets = edges(frame.node);
1224
+ if (frame.next < targets.length) {
1225
+ const to = targets[frame.next++];
1226
+ if (!among[to])
1227
+ continue;
1228
+ if (index[to] === -1) {
1229
+ index[to] = low[to] = counter++;
1230
+ stack.push(to);
1231
+ onStack[to] = true;
1232
+ work.push({ node: to, next: 0 });
1233
+ }
1234
+ else if (onStack[to]) {
1235
+ low[frame.node] = Math.min(low[frame.node], index[to]);
1236
+ }
1237
+ continue;
1238
+ }
1239
+ work.pop();
1240
+ const parent = work[work.length - 1];
1241
+ if (parent)
1242
+ low[parent.node] = Math.min(low[parent.node], low[frame.node]);
1243
+ if (low[frame.node] === index[frame.node]) {
1244
+ const component = [];
1245
+ let member;
1246
+ do {
1247
+ member = stack.pop();
1248
+ onStack[member] = false;
1249
+ component.push(member);
1250
+ } while (member !== frame.node);
1251
+ components.push(component);
1252
+ }
1253
+ }
1254
+ }
1255
+ return components;
1256
+ }
1257
+ //# sourceMappingURL=gantt.js.map