barmark 0.13.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -1
- package/README.md +33 -1
- package/dist/chart/barLine.d.ts +1 -0
- package/dist/chart/document.d.ts +1 -0
- package/dist/chart/document.js +127 -10
- package/dist/chart/expand.d.ts +5 -1
- package/dist/chart/expand.js +29 -11
- package/dist/chart/index.d.ts +3 -2
- package/dist/chart/index.js +3 -2
- package/dist/chart/parse.d.ts +13 -1
- package/dist/chart/parse.js +119 -4
- package/dist/chart/parts.d.ts +7 -0
- package/dist/chart/parts.js +77 -0
- package/dist/chart/print.d.ts +6 -0
- package/dist/chart/print.js +113 -23
- package/dist/chart/systems.d.ts +16 -0
- package/dist/chart/systems.js +93 -0
- package/dist/chart/types.d.ts +31 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,53 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
|
|
|
9
9
|
|
|
10
10
|
## [Unreleased]
|
|
11
11
|
|
|
12
|
+
## [0.14.1] - 2026-11-03
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- In a system, the line that differs from the one most lines agree with
|
|
17
|
+
is the error, wherever it sits; before, an odd first line made every
|
|
18
|
+
other line of its system the error (grammar 7.4).
|
|
19
|
+
- Parts that play one bar in different meters (a `%` copying a one-bar
|
|
20
|
+
meter) are reported on every line that plays it, naming the meters.
|
|
21
|
+
- `printPart` leaves out a `part:` line set aside for sharing a label,
|
|
22
|
+
which would otherwise declare a part again in the extract.
|
|
23
|
+
- If parts ever fell out of step, the first place they part is now an
|
|
24
|
+
error instead of a silent fallback.
|
|
25
|
+
|
|
26
|
+
## [0.14.0] - 2026-11-03
|
|
27
|
+
|
|
28
|
+
### Added
|
|
29
|
+
|
|
30
|
+
- **Several parts in one chart** (grammar 7.4): one `part: Label = Name`
|
|
31
|
+
line per part, bar lines labelled with their part (`Gtr | G | C |`),
|
|
32
|
+
and **systems**: consecutive labelled lines that share bars, one per
|
|
33
|
+
part. A part missing from a system is tacet; an unlabelled line is
|
|
34
|
+
tutti. Every line of a system writes the same bars and structure
|
|
35
|
+
(repeats, endings, marks, directions, fermatas, one-bar meters), and
|
|
36
|
+
the reader checks it. `%` and a leading `/` resolve per part, in
|
|
37
|
+
written order; after the part was tacet they are errors.
|
|
38
|
+
- The parse gains `parts` (score order); in a multi-part chart each
|
|
39
|
+
played bar gains `parts` (`{ line, slots, words? }` per part, null
|
|
40
|
+
where tacet), and `tacet: true` where the first part rests (its slots
|
|
41
|
+
then read `N.C.`). Lines gain `part` and `system`.
|
|
42
|
+
- `extractPart(text, label)`, `printPart(text, label)` and
|
|
43
|
+
`projectPart(lines, parts, label)`: one part as a single-part chart.
|
|
44
|
+
- `printChart` prints a system in score order with its labels padded and
|
|
45
|
+
its pipes lined up.
|
|
46
|
+
- A bar line after a word that is not a declared part (`Bass | E | A |`)
|
|
47
|
+
is text with a warning, in any chart; one whose first word is a chord
|
|
48
|
+
(`G | C | D |`) warns that a bar line starts with `|` and suggests
|
|
49
|
+
`| G | C | D |`.
|
|
50
|
+
|
|
51
|
+
### Changed
|
|
52
|
+
|
|
53
|
+
- A second `part:` line is no longer an error. `ChartParse.part` is the
|
|
54
|
+
one part of a single-part chart (null for several) and is deprecated
|
|
55
|
+
in favour of `parts`.
|
|
56
|
+
- Several parts share the expansion budget: each part expands at most
|
|
57
|
+
`MAX_EXPANDED_BARS / parts` bars.
|
|
58
|
+
|
|
12
59
|
## [0.13.0] - 2026-11-03
|
|
13
60
|
|
|
14
61
|
### Added
|
|
@@ -317,7 +364,9 @@ The first working release: the reference parser and printer for the
|
|
|
317
364
|
|
|
318
365
|
- **A placeholder package that holds the `barmark` name** while the spec and parser are written.
|
|
319
366
|
|
|
320
|
-
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.
|
|
367
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.14.1...HEAD
|
|
368
|
+
[0.14.1]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.14.0...barmark-v0.14.1
|
|
369
|
+
[0.14.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.13.0...barmark-v0.14.0
|
|
321
370
|
[0.13.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.12.0...barmark-v0.13.0
|
|
322
371
|
[0.12.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.11.0...barmark-v0.12.0
|
|
323
372
|
[0.11.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.10.0...barmark-v0.11.0
|
package/README.md
CHANGED
|
@@ -37,17 +37,20 @@ declarations included and no runtime dependencies.
|
|
|
37
37
|
```ts
|
|
38
38
|
import {
|
|
39
39
|
degreeOf,
|
|
40
|
+
extractPart,
|
|
40
41
|
harteToBarmark,
|
|
41
42
|
parseChart,
|
|
42
43
|
parseChord,
|
|
43
44
|
printChart,
|
|
44
45
|
printParsed,
|
|
46
|
+
printPart,
|
|
47
|
+
projectPart,
|
|
45
48
|
transposeChart,
|
|
46
49
|
} from "barmark";
|
|
47
50
|
|
|
48
51
|
const chart = parseChart(text);
|
|
49
52
|
chart.title; // "Example Song"
|
|
50
|
-
chart.
|
|
53
|
+
chart.parts; // [{ label: "Gtr", name: "Electric Guitar 1" }] from part: Gtr = Electric Guitar 1, in score order
|
|
51
54
|
chart.bars; // every bar in play order: its number as written, slots, section, key, meter, tempo, feel (and swingPercent)
|
|
52
55
|
chart.sections; // each section as played, with its first and last bar
|
|
53
56
|
chart.diagnostics; // errors and warnings, each with a line number
|
|
@@ -67,6 +70,35 @@ if (chord.ok) chord.value.harte; // "B:hdim7"
|
|
|
67
70
|
harteToBarmark("A:sus4(b7,9)"); // "A9sus4"
|
|
68
71
|
```
|
|
69
72
|
|
|
73
|
+
### Several parts
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
part: Gtr = Electric Guitar 1
|
|
77
|
+
part: Pno = Piano
|
|
78
|
+
|
|
79
|
+
Gtr | G | C | G | D |
|
|
80
|
+
Pno | G/B | C | Em | D |
|
|
81
|
+
| C | G | D | G |
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
A chart with several `part:` lines writes **systems** (grammar 7.4):
|
|
85
|
+
labelled lines that share bars, one per part. A part missing from a
|
|
86
|
+
system is tacet there; an unlabelled line is tutti, played by every part.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const chart = parseChart(text);
|
|
90
|
+
chart.bars[0].parts; // [{ line, slots, words? }, …] per part, in score order; null where tacet
|
|
91
|
+
chart.bars[0].slots; // the first part's slots (N.C., with tacet: true, where it is tacet)
|
|
92
|
+
|
|
93
|
+
extractPart(text, "Pno"); // one part as a single-part ChartParse, or null
|
|
94
|
+
printPart(text, "Pno"); // the same as text: tacet bars written N.C.
|
|
95
|
+
projectPart(chart.lines, chart.parts, "Pno"); // one part's lines
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`printChart` sorts a system into score order and lines up its pipes.
|
|
99
|
+
`chart.part` still gives the one part of a single-part chart, and is
|
|
100
|
+
deprecated in favour of `parts`.
|
|
101
|
+
|
|
70
102
|
- **`parseChart`** never throws. A line with an error is left out of the
|
|
71
103
|
play order and reported, and the rest of the chart still reads.
|
|
72
104
|
- **`printChart`** changes spelling and layout only: lines with errors and
|
package/dist/chart/barLine.d.ts
CHANGED
package/dist/chart/document.d.ts
CHANGED
package/dist/chart/document.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { parseChord } from "../chords/parse.js";
|
|
2
2
|
import { misplacedRepeat, parseBarLine, readRepeat, repeatTail } from "./barLine.js";
|
|
3
3
|
import { parseDirectiveLine } from "./directives.js";
|
|
4
|
+
import { groupSystems } from "./systems.js";
|
|
4
5
|
const NEAR_DIRECTIVES = {
|
|
5
6
|
meter: "time",
|
|
6
7
|
signature: "time",
|
|
@@ -24,8 +25,46 @@ function readPart(text) {
|
|
|
24
25
|
const [, label, name] = labelled;
|
|
25
26
|
if (!label)
|
|
26
27
|
return "part: needs a label before =";
|
|
28
|
+
if (/^[|#@0-9]/.test(label))
|
|
29
|
+
return "a part label can't start with |, #, @ or a digit";
|
|
27
30
|
return name ? { label, name } : "part: needs a name after the label";
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Several parts each need a label, and no two may share one in any case
|
|
34
|
+
* (grammar 7.4). A part that breaks either rule is text, with the error.
|
|
35
|
+
*/
|
|
36
|
+
function checkParts(lines, diagnostics) {
|
|
37
|
+
const declared = lines.filter((l) => l.kind === "part");
|
|
38
|
+
if (declared.length < 2)
|
|
39
|
+
return declared.map(partOf);
|
|
40
|
+
const seen = new Map();
|
|
41
|
+
const parts = [];
|
|
42
|
+
for (const l of declared) {
|
|
43
|
+
const problem = partProblem(l, seen);
|
|
44
|
+
if (!problem) {
|
|
45
|
+
parts.push(partOf(l));
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const text = l.label ? `part: ${l.label} = ${l.name}` : `part: ${l.name}`;
|
|
49
|
+
diagnostics.push({ line: l.line, severity: "error", token: text, ...problem });
|
|
50
|
+
lines[lines.indexOf(l)] = { kind: "text", line: l.line, text };
|
|
51
|
+
}
|
|
52
|
+
return parts;
|
|
53
|
+
}
|
|
54
|
+
const partOf = (l) => (l.label ? { label: l.label, name: l.name } : { name: l.name });
|
|
55
|
+
function partProblem(l, seen) {
|
|
56
|
+
if (!l.label)
|
|
57
|
+
return {
|
|
58
|
+
message: "a chart with several parts labels each one",
|
|
59
|
+
suggestion: `part: Label = ${l.name}`,
|
|
60
|
+
};
|
|
61
|
+
const key = l.label.toLowerCase();
|
|
62
|
+
const first = seen.get(key);
|
|
63
|
+
if (first)
|
|
64
|
+
return { message: `two parts share the label ${first}` };
|
|
65
|
+
seen.set(key, l.label);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
29
68
|
function readHeading(text, line, diagnostics) {
|
|
30
69
|
let rest = text.trim();
|
|
31
70
|
const elided = /(->|→)$/.test(rest);
|
|
@@ -63,6 +102,41 @@ function readHeading(text, line, diagnostics) {
|
|
|
63
102
|
});
|
|
64
103
|
return { kind: "section", line, name: rest, repeat, elided };
|
|
65
104
|
}
|
|
105
|
+
/** A label and the bar line after it: `Gtr | G |` (grammar 7.4). */
|
|
106
|
+
const LABELLED = /^([^\s|]+)(\s*)(\|.*)$/;
|
|
107
|
+
const readsCleanly = (barLine) => {
|
|
108
|
+
const read = parseBarLine(barLine, 0);
|
|
109
|
+
return Boolean(read.bars) && read.diagnostics.length === 0;
|
|
110
|
+
};
|
|
111
|
+
/** `G | C | D |`: a bar line with its leading pipe left off. */
|
|
112
|
+
function missingPipe(text, line) {
|
|
113
|
+
const fixed = `| ${text}`;
|
|
114
|
+
if (!readsCleanly(fixed))
|
|
115
|
+
return null;
|
|
116
|
+
return {
|
|
117
|
+
line,
|
|
118
|
+
severity: "warning",
|
|
119
|
+
message: "a bar line starts with |",
|
|
120
|
+
token: text,
|
|
121
|
+
suggestion: fixed,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/** A warning when a word before a clean bar line is not a declared part (grammar 7.4). */
|
|
125
|
+
function undeclaredLabel(text, line) {
|
|
126
|
+
const m = LABELLED.exec(text);
|
|
127
|
+
if (!m?.[1] || !m[3])
|
|
128
|
+
return null;
|
|
129
|
+
if (parseChord(m[1]).ok)
|
|
130
|
+
return missingPipe(text, line);
|
|
131
|
+
if (!readsCleanly(m[3]))
|
|
132
|
+
return null;
|
|
133
|
+
return {
|
|
134
|
+
line,
|
|
135
|
+
severity: "warning",
|
|
136
|
+
message: `${m[1]} is not a part of this chart; declare it with part: ${m[1]} = …`,
|
|
137
|
+
token: m[1],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
66
140
|
/** A warning when a text line looks like an older chart format. */
|
|
67
141
|
function oldFormat(text, line) {
|
|
68
142
|
const warn = (message, suggestion) => ({
|
|
@@ -79,6 +153,9 @@ function oldFormat(text, line) {
|
|
|
79
153
|
const name = near?.[1] ? NEAR_DIRECTIVES[near[1].toLowerCase()] : undefined;
|
|
80
154
|
if (near && name)
|
|
81
155
|
return warn(`not a directive; did you mean ${name}:?`, `${name}: ${near[2]}`);
|
|
156
|
+
const undeclared = undeclaredLabel(text, line);
|
|
157
|
+
if (undeclared)
|
|
158
|
+
return undeclared;
|
|
82
159
|
const unescaped = escapedBarLine(text);
|
|
83
160
|
if (unescaped)
|
|
84
161
|
return warn("escaped pipes; a bar line uses a plain |", unescaped);
|
|
@@ -118,7 +195,6 @@ export function readDocument(text) {
|
|
|
118
195
|
const lines = [];
|
|
119
196
|
const diagnostics = [];
|
|
120
197
|
let title = null;
|
|
121
|
-
let part = null;
|
|
122
198
|
/** The line kept as text, with why it isn't what it looks like. */
|
|
123
199
|
const asText = (t, line, severity, message) => {
|
|
124
200
|
diagnostics.push({ line, severity, message, token: t });
|
|
@@ -136,9 +212,6 @@ export function readDocument(text) {
|
|
|
136
212
|
return asText(t, line, "error", read);
|
|
137
213
|
if (lines.some((l) => l.kind === "bars" || l.kind === "invalid"))
|
|
138
214
|
return asText(t, line, "error", "part: names the whole chart; put it before the first bar");
|
|
139
|
-
if (part)
|
|
140
|
-
return asText(t, line, "error", "a chart names one part; several parts are not supported yet");
|
|
141
|
-
part = read;
|
|
142
215
|
return { kind: "part", line, ...read };
|
|
143
216
|
};
|
|
144
217
|
const otherLine = (t, line) => {
|
|
@@ -155,12 +228,47 @@ export function readDocument(text) {
|
|
|
155
228
|
diagnostics.push(old);
|
|
156
229
|
return { kind: "text", line, text: t };
|
|
157
230
|
};
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
231
|
+
const barLine = (t, line) => {
|
|
232
|
+
const r = parseBarLine(t, line);
|
|
233
|
+
diagnostics.push(...r.diagnostics);
|
|
234
|
+
return r.bars ?? { kind: "invalid", line, text: t, written: r.written };
|
|
235
|
+
};
|
|
236
|
+
/** A declared part's bar line; labelled only when the chart has several parts. */
|
|
237
|
+
const labelledLine = (t, line) => {
|
|
238
|
+
const m = LABELLED.exec(t);
|
|
239
|
+
const declared = lines.filter((l) => l.kind === "part");
|
|
240
|
+
const found = declared.find((p) => p.label?.toLowerCase() === m?.[1]?.toLowerCase());
|
|
241
|
+
if (!m || !found?.label)
|
|
242
|
+
return null;
|
|
243
|
+
const [, word = "", space, rest = ""] = m;
|
|
244
|
+
if (!space) {
|
|
245
|
+
diagnostics.push({
|
|
246
|
+
line,
|
|
247
|
+
severity: "error",
|
|
248
|
+
message: "a space goes between the label and |",
|
|
249
|
+
token: t,
|
|
250
|
+
suggestion: `${word} ${rest}`,
|
|
251
|
+
});
|
|
252
|
+
const written = parseBarLine(rest, line).written;
|
|
253
|
+
return {
|
|
254
|
+
kind: "invalid",
|
|
255
|
+
line,
|
|
256
|
+
text: t,
|
|
257
|
+
written,
|
|
258
|
+
...(declared.length > 1 && { part: found.label }),
|
|
259
|
+
};
|
|
163
260
|
}
|
|
261
|
+
const read = barLine(rest, line);
|
|
262
|
+
if (declared.length < 2 || (read.kind !== "bars" && read.kind !== "invalid"))
|
|
263
|
+
return read;
|
|
264
|
+
return { ...read, part: found.label };
|
|
265
|
+
};
|
|
266
|
+
const classify = (t, line) => {
|
|
267
|
+
if (t.startsWith("|"))
|
|
268
|
+
return barLine(t, line);
|
|
269
|
+
const labelled = labelledLine(t, line);
|
|
270
|
+
if (labelled)
|
|
271
|
+
return labelled;
|
|
164
272
|
if (t.startsWith("# "))
|
|
165
273
|
return titleLine(t, line);
|
|
166
274
|
if (t === "##" || t.startsWith("## "))
|
|
@@ -178,5 +286,14 @@ export function readDocument(text) {
|
|
|
178
286
|
const empty = noBarLines(lines);
|
|
179
287
|
if (empty)
|
|
180
288
|
diagnostics.push(empty);
|
|
181
|
-
|
|
289
|
+
const parts = checkParts(lines, diagnostics);
|
|
290
|
+
const grouped = parts.length > 1 ? groupSystems(lines) : { lines, diagnostics: [] };
|
|
291
|
+
diagnostics.push(...grouped.diagnostics);
|
|
292
|
+
return {
|
|
293
|
+
title,
|
|
294
|
+
part: parts.length === 1 ? (parts[0] ?? null) : null,
|
|
295
|
+
parts,
|
|
296
|
+
lines: grouped.lines,
|
|
297
|
+
diagnostics,
|
|
298
|
+
};
|
|
182
299
|
}
|
package/dist/chart/expand.d.ts
CHANGED
|
@@ -7,7 +7,11 @@ export declare function flattenBars(events: ChartEvent[]): PlayedBar[];
|
|
|
7
7
|
* Turn the written chart into the bars in play order: sections and their
|
|
8
8
|
* replays, repeats, directive changes, `%` and elision.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* `label` names the part a projected chart is (grammar 7.4), for its
|
|
12
|
+
* messages; `share` is how many parts split the expansion budget.
|
|
13
|
+
*/
|
|
14
|
+
export declare function expand(lines: Line[], label?: string, share?: number): {
|
|
11
15
|
events: ChartEvent[];
|
|
12
16
|
sections: SectionOccurrence[];
|
|
13
17
|
diagnostics: ChartDiagnostic[];
|
package/dist/chart/expand.js
CHANGED
|
@@ -3,8 +3,6 @@ import { beatsOf } from "./directives.js";
|
|
|
3
3
|
import { pairRepeats } from "./repeats.js";
|
|
4
4
|
/** Nested repeats multiply; expansion stops here rather than run away. */
|
|
5
5
|
export const MAX_EXPANDED_BARS = 10_000;
|
|
6
|
-
/** Repeats of bodies that play no bars (fermatas only) are bounded too. */
|
|
7
|
-
const MAX_WORK = MAX_EXPANDED_BARS * 10;
|
|
8
6
|
/**
|
|
9
7
|
* How each mark moves play along. A mark the jump does not use is an error
|
|
10
8
|
* that drops the jump, so with a jump planned every mark here applies.
|
|
@@ -79,8 +77,15 @@ function removeLastBar(events) {
|
|
|
79
77
|
* Turn the written chart into the bars in play order: sections and their
|
|
80
78
|
* replays, repeats, directive changes, `%` and elision.
|
|
81
79
|
*/
|
|
82
|
-
|
|
80
|
+
/**
|
|
81
|
+
* `label` names the part a projected chart is (grammar 7.4), for its
|
|
82
|
+
* messages; `share` is how many parts split the expansion budget.
|
|
83
|
+
*/
|
|
84
|
+
export function expand(lines, label, share = 1) {
|
|
83
85
|
const diagnostics = [];
|
|
86
|
+
const maxBars = Math.max(1, Math.floor(MAX_EXPANDED_BARS / share));
|
|
87
|
+
// Repeats of bodies that play no bars (fermatas only) are bounded too.
|
|
88
|
+
const maxWork = maxBars * 10;
|
|
84
89
|
// The return after a jump plays sections again: it reports only what the first pass did not.
|
|
85
90
|
const reported = new Set();
|
|
86
91
|
let again = false;
|
|
@@ -101,6 +106,7 @@ export function expand(lines) {
|
|
|
101
106
|
// % copies the bar written before it, one-bar meter included, so a section plays the same wherever it is replayed.
|
|
102
107
|
const resolved = new Map();
|
|
103
108
|
let previous = null;
|
|
109
|
+
const tacetBefore = (line, what) => error(line, `${label ?? "this part"} was tacet in the bar before; ${what}`, what.slice(0, 1));
|
|
104
110
|
let written = 0;
|
|
105
111
|
/** A rehearsal mark waiting for the bar after it (grammar 2.5). */
|
|
106
112
|
const pending = { mark: null };
|
|
@@ -123,22 +129,33 @@ export function expand(lines) {
|
|
|
123
129
|
const simile = (c, line) => {
|
|
124
130
|
if (!previous)
|
|
125
131
|
error(line, "% has no bar before it to repeat", "%");
|
|
132
|
+
else if (previous.tacet)
|
|
133
|
+
tacetBefore(line, "% has nothing to copy");
|
|
126
134
|
const meter = previous?.meter ?? null;
|
|
127
135
|
const slots = previous?.slots ?? [{ kind: "hold" }];
|
|
128
136
|
return marked({ kind: "bar", meter, slots, own: false, written: ++written }, c.words);
|
|
129
137
|
};
|
|
130
|
-
|
|
138
|
+
/** A bar that opens with `/` holds what was sounding before it. */
|
|
139
|
+
const checkHold = (c, line) => {
|
|
131
140
|
const first = c.slots[0];
|
|
132
141
|
const head = first?.kind === "group" ? first.members[0] : first;
|
|
133
|
-
if (
|
|
142
|
+
if (head?.kind !== "hold")
|
|
143
|
+
return;
|
|
144
|
+
if (!previous)
|
|
134
145
|
error(line, "/ has nothing before it to hold", "/");
|
|
135
|
-
|
|
146
|
+
else if (previous.tacet)
|
|
147
|
+
tacetBefore(line, "/ has nothing to hold");
|
|
148
|
+
};
|
|
149
|
+
const ownBar = (c, line) => {
|
|
150
|
+
checkHold(c, line);
|
|
151
|
+
previous = { meter: c.meter, slots: c.slots, ...(c.tacet && { tacet: c.tacet }) };
|
|
136
152
|
const bar = {
|
|
137
153
|
kind: "bar",
|
|
138
154
|
meter: c.meter,
|
|
139
155
|
slots: c.slots,
|
|
140
156
|
own: true,
|
|
141
157
|
written: ++written,
|
|
158
|
+
...(c.tacet && { tacet: c.tacet }),
|
|
142
159
|
};
|
|
143
160
|
return marked(bar, c.words);
|
|
144
161
|
};
|
|
@@ -218,7 +235,7 @@ export function expand(lines) {
|
|
|
218
235
|
const checked = new Set();
|
|
219
236
|
const stop = (line) => {
|
|
220
237
|
capped = true;
|
|
221
|
-
error(line, `expansion stopped at ${
|
|
238
|
+
error(line, `expansion stopped at ${maxBars} bars`, "x");
|
|
222
239
|
};
|
|
223
240
|
let sectionName = null;
|
|
224
241
|
let phase = "main";
|
|
@@ -254,6 +271,7 @@ export function expand(lines) {
|
|
|
254
271
|
slots: p.slots,
|
|
255
272
|
...(p.rehearsal === undefined ? {} : { rehearsal: p.rehearsal }),
|
|
256
273
|
...(p.words === undefined ? {} : { words: p.words }),
|
|
274
|
+
...(p.tacet && { tacet: p.tacet }),
|
|
257
275
|
};
|
|
258
276
|
};
|
|
259
277
|
/** How long the slot played just before is, in whole notes; null after a fermata or at the start. */
|
|
@@ -278,13 +296,13 @@ export function expand(lines) {
|
|
|
278
296
|
return direct(p.direction);
|
|
279
297
|
if (seeking())
|
|
280
298
|
return;
|
|
281
|
-
if (++work >
|
|
299
|
+
if (++work > maxWork)
|
|
282
300
|
return stop(line);
|
|
283
301
|
if (p.kind === "fermata") {
|
|
284
302
|
before.length = null;
|
|
285
303
|
return void out.push({ kind: "fermata", line });
|
|
286
304
|
}
|
|
287
|
-
if (count >=
|
|
305
|
+
if (count >= maxBars)
|
|
288
306
|
return stop(line);
|
|
289
307
|
count++;
|
|
290
308
|
const bar = barOf(p, line);
|
|
@@ -311,7 +329,7 @@ export function expand(lines) {
|
|
|
311
329
|
// After the jump every repeat plays once.
|
|
312
330
|
const passes = phase === "main" ? (repeat ?? 1) : 1;
|
|
313
331
|
for (let i = 0; i < passes && !stopped(); i++) {
|
|
314
|
-
if (++work >
|
|
332
|
+
if (++work > maxWork)
|
|
315
333
|
return stop(line);
|
|
316
334
|
body(out);
|
|
317
335
|
}
|
|
@@ -346,7 +364,7 @@ export function expand(lines) {
|
|
|
346
364
|
const highest = typeof it.repeat === "number" ? it.repeat : 1;
|
|
347
365
|
const first = phase === "main" ? 1 : highest;
|
|
348
366
|
for (let pass = first; pass <= highest && !stopped(); pass++) {
|
|
349
|
-
if (++work >
|
|
367
|
+
if (++work > maxWork)
|
|
350
368
|
return stop(it.line);
|
|
351
369
|
for (const e of it.entries)
|
|
352
370
|
playLine(e, out);
|
package/dist/chart/index.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ export { printDirection } from "./direction.js";
|
|
|
4
4
|
export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
|
|
5
5
|
export { readDocument } from "./document.js";
|
|
6
6
|
export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
|
|
7
|
-
export { parseChart } from "./parse.js";
|
|
8
|
-
export {
|
|
7
|
+
export { extractPart, parseChart } from "./parse.js";
|
|
8
|
+
export { projectPart } from "./parts.js";
|
|
9
|
+
export { printCell, printChart, printLine, printParsed, printPart, printPasses, printSlot, reduceSlots, } from "./print.js";
|
|
9
10
|
export { type TransposeBy, transposeChart } from "./transpose.js";
|
|
10
11
|
export type * from "./types.js";
|
package/dist/chart/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { printDirection } from "./direction.js";
|
|
|
4
4
|
export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
|
|
5
5
|
export { readDocument } from "./document.js";
|
|
6
6
|
export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
|
|
7
|
-
export { parseChart } from "./parse.js";
|
|
8
|
-
export {
|
|
7
|
+
export { extractPart, parseChart } from "./parse.js";
|
|
8
|
+
export { projectPart } from "./parts.js";
|
|
9
|
+
export { printCell, printChart, printLine, printParsed, printPart, printPasses, printSlot, reduceSlots, } from "./print.js";
|
|
9
10
|
export { transposeChart } from "./transpose.js";
|
package/dist/chart/parse.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
import type { ChartParse } from "./types.js";
|
|
1
|
+
import type { ChartDiagnostic, ChartEvent, ChartParse } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Each part's events, played in lockstep: the shapes match because every
|
|
4
|
+
* line of a system shares its structure. Where they don't, the lead's
|
|
5
|
+
* shape is kept and the first place they part is an error.
|
|
6
|
+
*/
|
|
7
|
+
export declare function zipEvents(each: ChartEvent[][], report: (d: ChartDiagnostic) => void): ChartEvent[];
|
|
2
8
|
/** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
|
|
3
9
|
export declare function parseChart(text: string): ChartParse;
|
|
10
|
+
/**
|
|
11
|
+
* One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
|
|
12
|
+
* its own bars, and `N.C.` marked `tacet` where it rests. Null when the
|
|
13
|
+
* chart declares no part with that label. Never throws.
|
|
14
|
+
*/
|
|
15
|
+
export declare function extractPart(text: string, label: string): ChartParse | null;
|
package/dist/chart/parse.js
CHANGED
|
@@ -1,18 +1,133 @@
|
|
|
1
1
|
import { readDocument } from "./document.js";
|
|
2
2
|
import { expand, flattenBars } from "./expand.js";
|
|
3
|
+
import { projectPart } from "./parts.js";
|
|
4
|
+
const RANK = { error: 0, warning: 1 };
|
|
5
|
+
/** Sorted by line, errors before warnings. */
|
|
6
|
+
const sortDiagnostics = (all) => all.sort((a, b) => a.line - b.line || RANK[a.severity] - RANK[b.severity]);
|
|
7
|
+
const keyOf = (d) => `${d.line}\u0000${d.severity}\u0000${d.message}`;
|
|
8
|
+
/** What every part reported, a problem the parts share once: each kept as often as one part reports it. */
|
|
9
|
+
function mergeReports(each) {
|
|
10
|
+
const most = new Map();
|
|
11
|
+
for (const list of each) {
|
|
12
|
+
const mine = new Map();
|
|
13
|
+
for (const d of list)
|
|
14
|
+
mine.set(keyOf(d), [...(mine.get(keyOf(d)) ?? []), d]);
|
|
15
|
+
for (const [k, ds] of mine)
|
|
16
|
+
if (ds.length > (most.get(k)?.length ?? 0))
|
|
17
|
+
most.set(k, ds);
|
|
18
|
+
}
|
|
19
|
+
return [...most.values()].flat();
|
|
20
|
+
}
|
|
21
|
+
const partBar = (b) => b.tacet ? null : { line: b.line, slots: b.slots, ...(b.words && { words: b.words }) };
|
|
22
|
+
const meterText = (b) => `${b.meter.n}/${b.meter.d}`;
|
|
23
|
+
/** One bar of every part: shared fields from the first, the meter from the first that plays. */
|
|
24
|
+
function zipBar(bars, report) {
|
|
25
|
+
const [first, ...rest] = bars;
|
|
26
|
+
const playing = bars.filter((b) => !b.tacet);
|
|
27
|
+
const meter = playing[0]?.meter ?? first.meter;
|
|
28
|
+
const meters = [...new Set(playing.map(meterText))];
|
|
29
|
+
// A `%` copies its bar's one-bar meter, so parts can disagree: say so on every line that plays the bar.
|
|
30
|
+
if (meters.length > 1)
|
|
31
|
+
for (const line of new Set(playing.map((b) => b.line)))
|
|
32
|
+
report({
|
|
33
|
+
line,
|
|
34
|
+
severity: "error",
|
|
35
|
+
message: `the parts disagree on this bar's meter (${meters.join(", ")})`,
|
|
36
|
+
token: "|",
|
|
37
|
+
});
|
|
38
|
+
return { ...first, meter, parts: [first, ...rest].map(partBar) };
|
|
39
|
+
}
|
|
40
|
+
const lineOf = (e) => (e.kind === "bar" ? e.bar.line : e.line);
|
|
41
|
+
/** The first event at which some part's timeline differs in shape from the lead's, or -1. */
|
|
42
|
+
function stepAt(each) {
|
|
43
|
+
const [lead = []] = each;
|
|
44
|
+
const longest = Math.max(...each.map((events) => events.length));
|
|
45
|
+
for (let i = 0; i < longest; i++)
|
|
46
|
+
if (each.some((events) => events[i]?.kind !== lead[i]?.kind))
|
|
47
|
+
return i;
|
|
48
|
+
return -1;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Each part's events, played in lockstep: the shapes match because every
|
|
52
|
+
* line of a system shares its structure. Where they don't, the lead's
|
|
53
|
+
* shape is kept and the first place they part is an error.
|
|
54
|
+
*/
|
|
55
|
+
export function zipEvents(each, report) {
|
|
56
|
+
const [lead = []] = each;
|
|
57
|
+
const apart = stepAt(each);
|
|
58
|
+
const at = lead[apart] ?? lead[lead.length - 1];
|
|
59
|
+
if (apart >= 0 && at)
|
|
60
|
+
report({
|
|
61
|
+
line: lineOf(at),
|
|
62
|
+
severity: "error",
|
|
63
|
+
message: "the parts fall out of step here; check that every line of this system writes the same marks",
|
|
64
|
+
token: "|",
|
|
65
|
+
});
|
|
66
|
+
return lead.map((e, i) => {
|
|
67
|
+
const peers = each.map((events) => events[i] ?? e);
|
|
68
|
+
if (e.kind === "bar")
|
|
69
|
+
return {
|
|
70
|
+
kind: "bar",
|
|
71
|
+
bar: zipBar(peers.map((p) => (p.kind === "bar" ? p.bar : e.bar)), report),
|
|
72
|
+
};
|
|
73
|
+
if (e.kind === "open")
|
|
74
|
+
return {
|
|
75
|
+
...e,
|
|
76
|
+
events: zipEvents(peers.map((p) => (p.kind === "open" ? p.events : e.events)), report),
|
|
77
|
+
};
|
|
78
|
+
return e;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** Several parts: each projected and played, then zipped into one timeline (grammar 7.4). */
|
|
82
|
+
function playParts(lines, parts) {
|
|
83
|
+
const played = parts.map((p) => expand(projectPart(lines, parts, p.label ?? ""), p.label, parts.length));
|
|
84
|
+
const diagnostics = mergeReports(played.map((p) => p.diagnostics));
|
|
85
|
+
const events = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
|
|
86
|
+
return { events, sections: played[0]?.sections ?? [], diagnostics };
|
|
87
|
+
}
|
|
3
88
|
/** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
|
|
4
89
|
export function parseChart(text) {
|
|
5
90
|
const doc = readDocument(text);
|
|
6
|
-
const played = expand(doc.lines);
|
|
7
|
-
const rank = { error: 0, warning: 1 };
|
|
8
|
-
const diagnostics = [...doc.diagnostics, ...played.diagnostics].sort((a, b) => a.line - b.line || rank[a.severity] - rank[b.severity]);
|
|
91
|
+
const played = doc.parts.length > 1 ? playParts(doc.lines, doc.parts) : expand(doc.lines);
|
|
9
92
|
return {
|
|
10
93
|
title: doc.title,
|
|
11
94
|
part: doc.part,
|
|
95
|
+
parts: doc.parts,
|
|
12
96
|
lines: doc.lines,
|
|
13
97
|
events: played.events,
|
|
14
98
|
bars: flattenBars(played.events),
|
|
15
99
|
sections: played.sections,
|
|
16
|
-
diagnostics,
|
|
100
|
+
diagnostics: sortDiagnostics([...doc.diagnostics, ...played.diagnostics]),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
|
|
105
|
+
* its own bars, and `N.C.` marked `tacet` where it rests. Null when the
|
|
106
|
+
* chart declares no part with that label. Never throws.
|
|
107
|
+
*/
|
|
108
|
+
export function extractPart(text, label) {
|
|
109
|
+
const doc = readDocument(text);
|
|
110
|
+
const part = doc.parts.find((p) => p.label?.toLowerCase() === label.toLowerCase());
|
|
111
|
+
if (!part)
|
|
112
|
+
return null;
|
|
113
|
+
const lines = doc.parts.length > 1 ? projectPart(doc.lines, doc.parts, label) : doc.lines;
|
|
114
|
+
const played = expand(lines, part.label);
|
|
115
|
+
const kept = new Set(lines.map((l) => l.line));
|
|
116
|
+
// A stand-in may sit on another part's line; that part's problems are not this one's.
|
|
117
|
+
for (const l of doc.lines)
|
|
118
|
+
if ((l.kind === "bars" || l.kind === "invalid") && l.part && l.part !== part.label)
|
|
119
|
+
kept.delete(l.line);
|
|
120
|
+
return {
|
|
121
|
+
title: doc.title,
|
|
122
|
+
part,
|
|
123
|
+
parts: [part],
|
|
124
|
+
lines,
|
|
125
|
+
events: played.events,
|
|
126
|
+
bars: flattenBars(played.events),
|
|
127
|
+
sections: played.sections,
|
|
128
|
+
diagnostics: sortDiagnostics([
|
|
129
|
+
...doc.diagnostics.filter((d) => kept.has(d.line)),
|
|
130
|
+
...played.diagnostics,
|
|
131
|
+
]),
|
|
17
132
|
};
|
|
18
133
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Line, Part } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* One part of a multi-part chart as a single-part chart's lines (grammar
|
|
4
|
+
* 7.4): the tutti lines, the part's own lines, and a bar of `N.C.` marked
|
|
5
|
+
* `tacet` for each bar of a system it doesn't play. Never throws.
|
|
6
|
+
*/
|
|
7
|
+
export declare function projectPart(lines: Line[], parts: Part[], label: string): Line[];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** A bar of `N.C.` where the part rests; the system's marks stay (grammar 7.4). */
|
|
2
|
+
const standInCell = (c) => c.kind === "slots" || c.kind === "simile"
|
|
3
|
+
? {
|
|
4
|
+
kind: "slots",
|
|
5
|
+
meter: c.kind === "slots" ? c.meter : null,
|
|
6
|
+
slots: [{ kind: "nc" }],
|
|
7
|
+
tacet: true,
|
|
8
|
+
}
|
|
9
|
+
: c;
|
|
10
|
+
/** A system no line of which reads: kept as an error line, so its bars still count. */
|
|
11
|
+
function unread(first) {
|
|
12
|
+
const invalid = first?.kind === "invalid" ? first : null;
|
|
13
|
+
return {
|
|
14
|
+
kind: "invalid",
|
|
15
|
+
line: first?.line ?? 0,
|
|
16
|
+
text: invalid?.text ?? "",
|
|
17
|
+
written: invalid?.written ?? 0,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** The line a system's structure is read from: its first line that reads and agrees. */
|
|
21
|
+
const referenceOf = (system) => system.find((l) => l.kind === "bars" && !l.dropped);
|
|
22
|
+
/**
|
|
23
|
+
* The system's structure with every bar a tacet bar, numbered `at`: the
|
|
24
|
+
* part's own line when it has one that errs, so its error stays its own,
|
|
25
|
+
* or else the reference line, which has none.
|
|
26
|
+
*/
|
|
27
|
+
function standIn(system, at) {
|
|
28
|
+
const ref = referenceOf(system);
|
|
29
|
+
if (!ref)
|
|
30
|
+
return unread(at);
|
|
31
|
+
const { part: _p, system: _s, dropped: _d, ...rest } = ref;
|
|
32
|
+
return { ...rest, line: at.line, cells: ref.cells.map(standInCell) };
|
|
33
|
+
}
|
|
34
|
+
/** A part's own line, as a single-part chart writes it. */
|
|
35
|
+
function unlabelled(l) {
|
|
36
|
+
if (l.kind === "invalid") {
|
|
37
|
+
const { part: _p, system: _s, ...rest } = l;
|
|
38
|
+
return rest;
|
|
39
|
+
}
|
|
40
|
+
const { part: _p, system: _s, dropped: _d, ...rest } = l;
|
|
41
|
+
return rest;
|
|
42
|
+
}
|
|
43
|
+
const plays = (l, key) => l.part?.toLowerCase() === key && l.kind === "bars" && !l.dropped;
|
|
44
|
+
const inSystem = (l) => (l.kind === "bars" || l.kind === "invalid") && l.system !== undefined;
|
|
45
|
+
function systemsOf(lines) {
|
|
46
|
+
const systems = new Map();
|
|
47
|
+
for (const l of lines)
|
|
48
|
+
if (inSystem(l))
|
|
49
|
+
systems.set(l.system, [...(systems.get(l.system) ?? []), l]);
|
|
50
|
+
return systems;
|
|
51
|
+
}
|
|
52
|
+
/** What one line becomes in the part's chart: itself, its unlabelled self, a stand-in, or nothing. */
|
|
53
|
+
function projectLine(l, key, systems) {
|
|
54
|
+
if (l.kind === "part")
|
|
55
|
+
return l.label?.toLowerCase() === key ? l : null;
|
|
56
|
+
return inSystem(l) ? projectSystemLine(l, key, systems.get(l.system) ?? []) : l;
|
|
57
|
+
}
|
|
58
|
+
/** A system's line in one part's chart: the part's own line, a stand-in where it rests, or nothing. */
|
|
59
|
+
function projectSystemLine(l, key, system) {
|
|
60
|
+
const own = system.find((m) => m.part?.toLowerCase() === key);
|
|
61
|
+
if (own === l && plays(l, key))
|
|
62
|
+
return unlabelled(l);
|
|
63
|
+
const at = own ?? referenceOf(system) ?? system[0];
|
|
64
|
+
return at === l ? standIn(system, l) : null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* One part of a multi-part chart as a single-part chart's lines (grammar
|
|
68
|
+
* 7.4): the tutti lines, the part's own lines, and a bar of `N.C.` marked
|
|
69
|
+
* `tacet` for each bar of a system it doesn't play. Never throws.
|
|
70
|
+
*/
|
|
71
|
+
export function projectPart(lines, parts, label) {
|
|
72
|
+
if (!parts.length)
|
|
73
|
+
return lines;
|
|
74
|
+
const key = label.toLowerCase();
|
|
75
|
+
const systems = systemsOf(lines);
|
|
76
|
+
return lines.map((l) => projectLine(l, key, systems)).filter((l) => l !== null);
|
|
77
|
+
}
|
package/dist/chart/print.d.ts
CHANGED
|
@@ -30,3 +30,9 @@ export declare function printChart(text: string): string;
|
|
|
30
30
|
* text isn't read twice. `edit`, if given, changes each line before it prints.
|
|
31
31
|
*/
|
|
32
32
|
export declare function printParsed(text: string, parsed: ChartParse, edit?: (l: Line) => Line): string;
|
|
33
|
+
/**
|
|
34
|
+
* One part of a chart in canonical form, as a single-part chart (grammar
|
|
35
|
+
* 7.4): its `part:` line, the tutti lines, its own lines, and `N.C.` bars
|
|
36
|
+
* where it is tacet. Null when no part has that label. Never throws.
|
|
37
|
+
*/
|
|
38
|
+
export declare function printPart(text: string, label: string): string | null;
|
package/dist/chart/print.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { isBar } from "./barLine.js";
|
|
2
2
|
import { printDirection } from "./direction.js";
|
|
3
3
|
import { beatsOf } from "./directives.js";
|
|
4
|
-
import {
|
|
4
|
+
import { readDocument } from "./document.js";
|
|
5
|
+
import { extractPart, parseChart } from "./parse.js";
|
|
5
6
|
/** A slot with no attack of its own continues whatever sounds before it. A repeated chord name is a re-attack. */
|
|
6
7
|
function continues(sounding, s) {
|
|
7
8
|
if (s.kind === "hold")
|
|
@@ -124,6 +125,15 @@ function printDirectives(d) {
|
|
|
124
125
|
parts.push(`feel: ${d.feel}${d.swingPercent === undefined ? "" : ` ${d.swingPercent}%`}`);
|
|
125
126
|
return parts.join(", ");
|
|
126
127
|
}
|
|
128
|
+
/** A bar line's opening, cells and closing, each printed. */
|
|
129
|
+
function barPieces(l, beats) {
|
|
130
|
+
return {
|
|
131
|
+
open: l.opens ? "|:" : l.ending ? `|${printPasses(l.ending)}` : "|",
|
|
132
|
+
cells: l.cells.map((c, i) => printCell(c, beats[i])),
|
|
133
|
+
close: `${l.closes ? ":|" : "|"}${printRepeat(l.repeat)}`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const joinPieces = (p) => `${p.open} ${p.cells.join(" | ")} ${p.close}`;
|
|
127
137
|
/**
|
|
128
138
|
* One parsed line in canonical form. Text and invalid lines print as stored.
|
|
129
139
|
* `beats` gives each cell's beat count where known, so reduction adds no warning.
|
|
@@ -139,7 +149,7 @@ export function printLine(l, beats = []) {
|
|
|
139
149
|
case "part":
|
|
140
150
|
return `part: ${l.label ? `${l.label} = ` : ""}${l.name}`;
|
|
141
151
|
case "bars":
|
|
142
|
-
return
|
|
152
|
+
return joinPieces(barPieces(l, beats));
|
|
143
153
|
default:
|
|
144
154
|
return l.text;
|
|
145
155
|
}
|
|
@@ -152,50 +162,130 @@ export function printLine(l, beats = []) {
|
|
|
152
162
|
export function printChart(text) {
|
|
153
163
|
return printParsed(text, parseChart(text));
|
|
154
164
|
}
|
|
155
|
-
/**
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
165
|
+
/** A system's lines in score order, labels padded and pipes lined up (section 8, grammar 7.4). */
|
|
166
|
+
function alignSystem(system, parsed, beatsFor, edit) {
|
|
167
|
+
const score = (l) => parsed.parts.findIndex((p) => p.label === l.part);
|
|
168
|
+
const width = Math.max(...parsed.parts.map((p) => p.label?.length ?? 0));
|
|
169
|
+
const pieces = [...system]
|
|
170
|
+
.sort((a, b) => score(a) - score(b))
|
|
171
|
+
.map((l) => ({ label: l.part ?? "", ...barPieces(edit(l), beatsFor(l)) }));
|
|
172
|
+
const columns = pieces[0]?.cells.map((_, i) => Math.max(...pieces.map((p) => p.cells[i]?.length ?? 0)));
|
|
173
|
+
return pieces.map((p) => `${p.label.padEnd(width)} ${joinPieces({ ...p, cells: p.cells.map((c, i) => c.padEnd(columns?.[i] ?? 0)) })}`.trimEnd());
|
|
174
|
+
}
|
|
175
|
+
function systemsOf(lines) {
|
|
176
|
+
const systems = new Map();
|
|
177
|
+
for (const l of lines)
|
|
178
|
+
if ((l.kind === "bars" || l.kind === "invalid") && l.system !== undefined)
|
|
179
|
+
systems.set(l.system, [...(systems.get(l.system) ?? []), l]);
|
|
180
|
+
return [...systems.values()];
|
|
181
|
+
}
|
|
182
|
+
/** Canonical text for the lines of a multi-part chart's systems, by line number. */
|
|
183
|
+
function systemTexts(parsed, errored, beatsFor, edit) {
|
|
184
|
+
const texts = new Map();
|
|
185
|
+
for (const lines of systemsOf(parsed.lines)) {
|
|
186
|
+
const valid = lines.filter((l) => l.kind === "bars" && !errored.has(l.line));
|
|
187
|
+
// A system with an error is neither sorted nor padded, so the error's line stays where it was written.
|
|
188
|
+
const printed = valid.length === lines.length
|
|
189
|
+
? alignSystem(valid, parsed, beatsFor, edit)
|
|
190
|
+
: valid.map((l) => `${l.part} ${printLine(edit(l), beatsFor(l))}`);
|
|
191
|
+
for (const [i, l] of valid.entries())
|
|
192
|
+
texts.set(l.line, printed[i] ?? "");
|
|
193
|
+
}
|
|
194
|
+
return texts;
|
|
195
|
+
}
|
|
196
|
+
/** Each line's beat counts on its first play, every part's lines included. */
|
|
197
|
+
function beatsOfLines(parsed) {
|
|
160
198
|
const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
|
|
161
|
-
const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
|
|
162
|
-
// Each cell's beat count on its first play, which is when its slot count is checked.
|
|
163
199
|
const firstPlay = new Map();
|
|
164
200
|
for (const b of parsed.bars) {
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
201
|
+
const lines = new Set([b.line, ...(b.parts ?? []).flatMap((p) => (p ? [p.line] : []))]);
|
|
202
|
+
for (const line of lines) {
|
|
203
|
+
const l = byLine.get(line);
|
|
204
|
+
const meters = firstPlay.get(line) ?? [];
|
|
205
|
+
const cells = l?.kind === "bars" ? l.cells.filter(isBar).length : 0;
|
|
206
|
+
if (meters.length < cells)
|
|
207
|
+
firstPlay.set(line, [...meters, beatsOf(b.meter)]);
|
|
208
|
+
}
|
|
170
209
|
}
|
|
171
|
-
|
|
210
|
+
return (l) => {
|
|
172
211
|
if (l.kind !== "bars")
|
|
173
212
|
return [];
|
|
174
213
|
const meters = firstPlay.get(l.line) ?? [];
|
|
175
214
|
let next = 0;
|
|
176
215
|
return l.cells.map((c) => (isBar(c) ? meters[next++] : undefined));
|
|
177
216
|
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* `printChart` over a parse you already have (`parseChart(text)`), so the
|
|
220
|
+
* text isn't read twice. `edit`, if given, changes each line before it prints.
|
|
221
|
+
*/
|
|
222
|
+
export function printParsed(text, parsed, edit = (l) => l) {
|
|
223
|
+
return printLines(text, parsed, edit, new Set());
|
|
224
|
+
}
|
|
225
|
+
function linePrinter(parsed, edit) {
|
|
226
|
+
const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
|
|
227
|
+
const beatsFor = beatsOfLines(parsed);
|
|
228
|
+
const systems = systemTexts(parsed, errored, beatsFor, edit);
|
|
229
|
+
return (raw, l) => {
|
|
230
|
+
if (!l)
|
|
231
|
+
return raw.trim().startsWith("```") ? raw.trim() : raw.trimEnd();
|
|
232
|
+
if (l.kind === "text" || errored.has(l.line))
|
|
233
|
+
return raw.trimEnd();
|
|
234
|
+
return systems.get(l.line) ?? printLine(edit(l), beatsFor(l));
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/** Blank lines: runs collapse to one, and one goes before a heading unless the line before is one. */
|
|
238
|
+
function spaceBefore(out, l, afterHeading) {
|
|
239
|
+
if (out.length === 0 || out.at(-1) === "")
|
|
240
|
+
return;
|
|
241
|
+
if (!l || (l.kind === "section" && !afterHeading))
|
|
242
|
+
out.push("");
|
|
243
|
+
}
|
|
244
|
+
/** The printer, leaving out the physical lines in `skip`. */
|
|
245
|
+
function printLines(text, parsed, edit, skip) {
|
|
246
|
+
const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
|
|
247
|
+
const print = linePrinter(parsed, edit);
|
|
178
248
|
const out = [];
|
|
179
249
|
let lastWasHeading = false;
|
|
180
250
|
const physical = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
181
251
|
for (const [i, raw] of physical.entries()) {
|
|
252
|
+
if (skip.has(i + 1))
|
|
253
|
+
continue;
|
|
182
254
|
const t = raw.trim();
|
|
183
255
|
if (!t) {
|
|
184
|
-
|
|
185
|
-
out.push("");
|
|
256
|
+
spaceBefore(out, undefined, false);
|
|
186
257
|
continue;
|
|
187
258
|
}
|
|
188
259
|
const l = t.startsWith("```") ? undefined : byLine.get(i + 1);
|
|
189
260
|
// Consecutive headings (a run of replays) stay together, as the spec's examples write them.
|
|
190
|
-
if (l?.kind === "section"
|
|
191
|
-
out
|
|
261
|
+
if (l?.kind === "section")
|
|
262
|
+
spaceBefore(out, l, lastWasHeading);
|
|
192
263
|
lastWasHeading = l?.kind === "section";
|
|
193
|
-
|
|
194
|
-
out.push(t.startsWith("```") ? t : raw.trimEnd());
|
|
195
|
-
else
|
|
196
|
-
out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(edit(l), beatsFor(l)));
|
|
264
|
+
out.push(print(raw, l));
|
|
197
265
|
}
|
|
266
|
+
return finish(out);
|
|
267
|
+
}
|
|
268
|
+
/** The lines as text, without trailing blank lines. */
|
|
269
|
+
function finish(out) {
|
|
198
270
|
while (out.at(-1) === "")
|
|
199
271
|
out.pop();
|
|
200
272
|
return out.length ? `${out.join("\n")}\n` : "";
|
|
201
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* One part of a chart in canonical form, as a single-part chart (grammar
|
|
276
|
+
* 7.4): its `part:` line, the tutti lines, its own lines, and `N.C.` bars
|
|
277
|
+
* where it is tacet. Null when no part has that label. Never throws.
|
|
278
|
+
*/
|
|
279
|
+
export function printPart(text, label) {
|
|
280
|
+
const parsed = extractPart(text, label);
|
|
281
|
+
if (!parsed)
|
|
282
|
+
return null;
|
|
283
|
+
const kept = new Set(parsed.lines.map((l) => l.line));
|
|
284
|
+
const skip = new Set(readDocument(text)
|
|
285
|
+
.lines.filter((l) =>
|
|
286
|
+
// A `part:` demoted to text (a shared label) would declare a part again in the extract.
|
|
287
|
+
(l.kind === "text" && /^part\s*:/i.test(l.text)) ||
|
|
288
|
+
((l.kind === "part" || l.kind === "bars" || l.kind === "invalid") && !kept.has(l.line)))
|
|
289
|
+
.map((l) => l.line));
|
|
290
|
+
return printLines(text, parsed, (l) => l, skip);
|
|
291
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ChartDiagnostic, Line } from "./types.js";
|
|
2
|
+
type BarsLine = Extract<Line, {
|
|
3
|
+
kind: "bars";
|
|
4
|
+
}>;
|
|
5
|
+
/** The marks of a bar line that shape the timeline, which every line of a system shares (grammar 7.4). */
|
|
6
|
+
export declare function structureOf(l: BarsLine): string;
|
|
7
|
+
/**
|
|
8
|
+
* Group a multi-part chart's labelled bar lines into systems and check that
|
|
9
|
+
* each system's lines share their bars and structure. A line that differs is
|
|
10
|
+
* an error and is marked `dropped`. Never throws.
|
|
11
|
+
*/
|
|
12
|
+
export declare function groupSystems(lines: Line[]): {
|
|
13
|
+
lines: Line[];
|
|
14
|
+
diagnostics: ChartDiagnostic[];
|
|
15
|
+
};
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { printDirection } from "./direction.js";
|
|
2
|
+
const repeatOf = (r) => (r === null ? "" : r === "open" ? " x ..." : ` x${r}`);
|
|
3
|
+
function cellShape(c) {
|
|
4
|
+
switch (c.kind) {
|
|
5
|
+
case "slots":
|
|
6
|
+
return c.meter ? `B[${c.meter.n}/${c.meter.d}]` : "B";
|
|
7
|
+
case "simile":
|
|
8
|
+
return "B";
|
|
9
|
+
case "fermata":
|
|
10
|
+
return "...";
|
|
11
|
+
case "rehearsal":
|
|
12
|
+
return `@${c.label}`;
|
|
13
|
+
default:
|
|
14
|
+
return printDirection(c.direction);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const opening = (l) => (l.opens ? "|:" : l.ending ? `|${l.ending.join(",")}` : "|");
|
|
18
|
+
const closing = (l) => `${l.closes ? ":|" : "|"}${repeatOf(l.repeat)}`;
|
|
19
|
+
/** The marks of a bar line that shape the timeline, which every line of a system shares (grammar 7.4). */
|
|
20
|
+
export function structureOf(l) {
|
|
21
|
+
return `${opening(l)} ${l.cells.map(cellShape).join(" ")} ${closing(l)}`;
|
|
22
|
+
}
|
|
23
|
+
const barCount = (l) => l.cells.filter((c) => c.kind === "slots" || c.kind === "simile").length;
|
|
24
|
+
/** Why a system's line differs from its first, or null. */
|
|
25
|
+
function differs(l, ref) {
|
|
26
|
+
const n = barCount(ref);
|
|
27
|
+
if (barCount(l) !== n)
|
|
28
|
+
return `this system's lines have ${n} bars (line ${ref.line}); this one has ${barCount(l)}`;
|
|
29
|
+
if (opening(l) !== opening(ref) || closing(l) !== closing(ref))
|
|
30
|
+
return `every line of a system writes the same repeat marks (line ${ref.line})`;
|
|
31
|
+
if (structureOf(l) !== structureOf(ref))
|
|
32
|
+
return `every line of a system writes the same marks in the same places (line ${ref.line})`;
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const isLabelled = (l) => (l.kind === "bars" || l.kind === "invalid") && l.part !== undefined;
|
|
36
|
+
/** Number each run of labelled lines with no label twice (grammar 7.4). */
|
|
37
|
+
function numberSystems(lines) {
|
|
38
|
+
let system = -1;
|
|
39
|
+
let seen = null;
|
|
40
|
+
return lines.map((l) => {
|
|
41
|
+
if (!isLabelled(l)) {
|
|
42
|
+
seen = null;
|
|
43
|
+
return l;
|
|
44
|
+
}
|
|
45
|
+
const label = l.part ?? "";
|
|
46
|
+
if (!seen || seen.has(label)) {
|
|
47
|
+
system++;
|
|
48
|
+
seen = new Set();
|
|
49
|
+
}
|
|
50
|
+
seen.add(label);
|
|
51
|
+
return { ...l, system };
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Each system's reference line: the first whose structure most of the
|
|
56
|
+
* system's lines share, so one odd line is the error wherever it sits.
|
|
57
|
+
*/
|
|
58
|
+
function referenceLines(lines) {
|
|
59
|
+
const bySystem = new Map();
|
|
60
|
+
for (const l of lines)
|
|
61
|
+
if (l.kind === "bars" && l.system !== undefined)
|
|
62
|
+
bySystem.set(l.system, [...(bySystem.get(l.system) ?? []), l]);
|
|
63
|
+
const refs = new Map();
|
|
64
|
+
for (const [system, members] of bySystem) {
|
|
65
|
+
const votes = (l) => members.filter((m) => structureOf(m) === structureOf(l)).length;
|
|
66
|
+
const best = members.reduce((a, b) => (votes(b) > votes(a) ? b : a));
|
|
67
|
+
refs.set(system, best);
|
|
68
|
+
}
|
|
69
|
+
return refs;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Group a multi-part chart's labelled bar lines into systems and check that
|
|
73
|
+
* each system's lines share their bars and structure. A line that differs is
|
|
74
|
+
* an error and is marked `dropped`. Never throws.
|
|
75
|
+
*/
|
|
76
|
+
export function groupSystems(lines) {
|
|
77
|
+
const diagnostics = [];
|
|
78
|
+
const numbered = numberSystems(lines);
|
|
79
|
+
const refs = referenceLines(numbered);
|
|
80
|
+
const out = numbered.map((l) => {
|
|
81
|
+
if (l.kind !== "bars" || l.system === undefined)
|
|
82
|
+
return l;
|
|
83
|
+
const ref = refs.get(l.system);
|
|
84
|
+
if (!ref || ref === l)
|
|
85
|
+
return l;
|
|
86
|
+
const problem = differs(l, ref);
|
|
87
|
+
if (!problem)
|
|
88
|
+
return l;
|
|
89
|
+
diagnostics.push({ line: l.line, severity: "error", message: problem, token: l.part ?? "" });
|
|
90
|
+
return { ...l, dropped: true };
|
|
91
|
+
});
|
|
92
|
+
return { lines: out, diagnostics };
|
|
93
|
+
}
|
package/dist/chart/types.d.ts
CHANGED
|
@@ -117,6 +117,8 @@ export type Cell = {
|
|
|
117
117
|
meter: Meter | null;
|
|
118
118
|
slots: Slot[];
|
|
119
119
|
words?: Words[];
|
|
120
|
+
/** A stand-in bar of `N.C.` for a part that is tacet there (grammar 7.4); only a projected part has one. */
|
|
121
|
+
tacet?: true;
|
|
120
122
|
} | {
|
|
121
123
|
kind: "simile";
|
|
122
124
|
words?: Words[];
|
|
@@ -158,6 +160,12 @@ export type Line = {
|
|
|
158
160
|
repeat: Repeat;
|
|
159
161
|
/** The passes an ending plays on (grammar 5.1), in order; null for any other bar line. */
|
|
160
162
|
ending: number[] | null;
|
|
163
|
+
/** The declared label of the part whose line this is (grammar 7.4); absent on a tutti line and in a single-part chart. */
|
|
164
|
+
part?: string;
|
|
165
|
+
/** Which system a labelled line is in, from 0 in chart order. */
|
|
166
|
+
system?: number;
|
|
167
|
+
/** Set when the line's bars or structure differ from its system's (an error): its part is tacet there. */
|
|
168
|
+
dropped?: true;
|
|
161
169
|
} | {
|
|
162
170
|
kind: "text";
|
|
163
171
|
line: number;
|
|
@@ -172,6 +180,8 @@ export type Line = {
|
|
|
172
180
|
line: number;
|
|
173
181
|
text: string;
|
|
174
182
|
written: number;
|
|
183
|
+
part?: string;
|
|
184
|
+
system?: number;
|
|
175
185
|
};
|
|
176
186
|
export type PlayedBar = {
|
|
177
187
|
/** 1-based measure number in play order; fermatas are not counted. */
|
|
@@ -204,7 +214,21 @@ export type PlayedBar = {
|
|
|
204
214
|
rehearsal?: string;
|
|
205
215
|
/** Words written on this bar; absent when none. `%` copies chords, not words. */
|
|
206
216
|
words?: Words[];
|
|
217
|
+
/** Set when the part is tacet in this bar (grammar 7.4): its slots then read `N.C.`. */
|
|
218
|
+
tacet?: true;
|
|
219
|
+
/**
|
|
220
|
+
* In a chart of several parts, each part's bar, aligned with
|
|
221
|
+
* `ChartParse.parts`; null where that part is tacet. The top-level
|
|
222
|
+
* `slots` and `words` are the first part's.
|
|
223
|
+
*/
|
|
224
|
+
parts?: PartBar[];
|
|
207
225
|
};
|
|
226
|
+
/** One part's share of a played bar (grammar 7.4): the line it is written on, its slots and words. */
|
|
227
|
+
export type PartBar = {
|
|
228
|
+
line: number;
|
|
229
|
+
slots: Slot[];
|
|
230
|
+
words?: Words[];
|
|
231
|
+
} | null;
|
|
208
232
|
/** An open repeat shows one pass; the number of passes is unknown. */
|
|
209
233
|
export type ChartEvent = {
|
|
210
234
|
kind: "bar";
|
|
@@ -233,8 +257,14 @@ export type SectionOccurrence = {
|
|
|
233
257
|
};
|
|
234
258
|
export type ChartParse = {
|
|
235
259
|
title: string | null;
|
|
236
|
-
/**
|
|
260
|
+
/**
|
|
261
|
+
* The one part a single-part chart names (grammar 7.3); null when it names
|
|
262
|
+
* none or several.
|
|
263
|
+
* @deprecated use `parts`.
|
|
264
|
+
*/
|
|
237
265
|
part: Part | null;
|
|
266
|
+
/** Every part the chart declares, in score order (grammar 7.4); empty when none. */
|
|
267
|
+
parts: Part[];
|
|
238
268
|
lines: Line[];
|
|
239
269
|
events: ChartEvent[];
|
|
240
270
|
/** `events` flattened, with an open repeat's pass included once. */
|