barmark 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -1
- package/README.md +33 -0
- package/dist/chart/barLine.d.ts +1 -0
- package/dist/chart/directives.js +4 -0
- package/dist/chart/document.d.ts +3 -1
- package/dist/chart/document.js +186 -35
- 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 +6 -0
- package/dist/chart/parse.js +93 -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 +112 -23
- package/dist/chart/systems.d.ts +16 -0
- package/dist/chart/systems.js +78 -0
- package/dist/chart/types.d.ts +44 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,52 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
|
|
|
9
9
|
|
|
10
10
|
## [Unreleased]
|
|
11
11
|
|
|
12
|
+
## [0.14.0] - 2026-11-03
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- **Several parts in one chart** (grammar 7.4): one `part: Label = Name`
|
|
17
|
+
line per part, bar lines labelled with their part (`Gtr | G | C |`),
|
|
18
|
+
and **systems**: consecutive labelled lines that share bars, one per
|
|
19
|
+
part. A part missing from a system is tacet; an unlabelled line is
|
|
20
|
+
tutti. Every line of a system writes the same bars and structure
|
|
21
|
+
(repeats, endings, marks, directions, fermatas, one-bar meters), and
|
|
22
|
+
the reader checks it. `%` and a leading `/` resolve per part, in
|
|
23
|
+
written order; after the part was tacet they are errors.
|
|
24
|
+
- The parse gains `parts` (score order); in a multi-part chart each
|
|
25
|
+
played bar gains `parts` (`{ line, slots, words? }` per part, null
|
|
26
|
+
where tacet), and `tacet: true` where the first part rests (its slots
|
|
27
|
+
then read `N.C.`). Lines gain `part` and `system`.
|
|
28
|
+
- `extractPart(text, label)`, `printPart(text, label)` and
|
|
29
|
+
`projectPart(lines, parts, label)`: one part as a single-part chart.
|
|
30
|
+
- `printChart` prints a system in score order with its labels padded and
|
|
31
|
+
its pipes lined up.
|
|
32
|
+
- A bar line after a word that is not a declared part (`Bass | E | A |`)
|
|
33
|
+
is text with a warning, in any chart; one whose first word is a chord
|
|
34
|
+
(`G | C | D |`) warns that a bar line starts with `|` and suggests
|
|
35
|
+
`| G | C | D |`.
|
|
36
|
+
|
|
37
|
+
### Changed
|
|
38
|
+
|
|
39
|
+
- A second `part:` line is no longer an error. `ChartParse.part` is the
|
|
40
|
+
one part of a single-part chart (null for several) and is deprecated
|
|
41
|
+
in favour of `parts`.
|
|
42
|
+
- Several parts share the expansion budget: each part expands at most
|
|
43
|
+
`MAX_EXPANDED_BARS / parts` bars.
|
|
44
|
+
|
|
45
|
+
## [0.13.0] - 2026-11-03
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
|
|
49
|
+
- **`part:` names the chart's part** (grammar 7.3, issue #70): `part:
|
|
50
|
+
Electric Guitar 1`, or with a short label, `part: Gtr = Electric
|
|
51
|
+
Guitar 1`. It goes on a line of its own, once, before the first bar,
|
|
52
|
+
and the name may hold commas. The parse gains `part` (`{ name, label?
|
|
53
|
+
}`, or null) and a `part` line kind; the canonical form is `part:
|
|
54
|
+
Label = Name`. Errors for a missing name or label, a `part:` after the
|
|
55
|
+
first bar or a second one, and one sharing its line with a directive;
|
|
56
|
+
`instrument:` and `parts:` are near misses.
|
|
57
|
+
|
|
12
58
|
## [0.12.0] - 2026-11-03
|
|
13
59
|
|
|
14
60
|
Rehearsal marks and words on a bar, issue #70 (grammar 2.5 and 2.6);
|
|
@@ -304,7 +350,9 @@ The first working release: the reference parser and printer for the
|
|
|
304
350
|
|
|
305
351
|
- **A placeholder package that holds the `barmark` name** while the spec and parser are written.
|
|
306
352
|
|
|
307
|
-
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.
|
|
353
|
+
[Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.14.0...HEAD
|
|
354
|
+
[0.14.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.13.0...barmark-v0.14.0
|
|
355
|
+
[0.13.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.12.0...barmark-v0.13.0
|
|
308
356
|
[0.12.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.11.0...barmark-v0.12.0
|
|
309
357
|
[0.11.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.10.0...barmark-v0.11.0
|
|
310
358
|
[0.10.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.9.0...barmark-v0.10.0
|
package/README.md
CHANGED
|
@@ -37,16 +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"
|
|
53
|
+
chart.parts; // [{ label: "Gtr", name: "Electric Guitar 1" }] from part: Gtr = Electric Guitar 1, in score order
|
|
50
54
|
chart.bars; // every bar in play order: its number as written, slots, section, key, meter, tempo, feel (and swingPercent)
|
|
51
55
|
chart.sections; // each section as played, with its first and last bar
|
|
52
56
|
chart.diagnostics; // errors and warnings, each with a line number
|
|
@@ -66,6 +70,35 @@ if (chord.ok) chord.value.harte; // "B:hdim7"
|
|
|
66
70
|
harteToBarmark("A:sus4(b7,9)"); // "A9sus4"
|
|
67
71
|
```
|
|
68
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
|
+
|
|
69
102
|
- **`parseChart`** never throws. A line with an error is left out of the
|
|
70
103
|
play order and reported, and the rest of the chart still reads.
|
|
71
104
|
- **`printChart`** changes spelling and layout only: lines with errors and
|
package/dist/chart/barLine.d.ts
CHANGED
package/dist/chart/directives.js
CHANGED
|
@@ -109,6 +109,10 @@ export function parseDirectiveLine(text, line) {
|
|
|
109
109
|
const m = /^(\w+)\s*:\s*(.*)$/.exec(part);
|
|
110
110
|
const name = m?.[1]?.toLowerCase();
|
|
111
111
|
const value = m?.[2]?.trim() ?? "";
|
|
112
|
+
if (name === "part") {
|
|
113
|
+
error("part: goes on a line of its own; the name runs to the end of the line", part);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
112
116
|
if (!m || !name || !DIRECTIVE_NAMES.includes(name)) {
|
|
113
117
|
error("unknown directive (key, time, tempo, feel)", part);
|
|
114
118
|
continue;
|
package/dist/chart/document.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { ChartDiagnostic, Line } from "./types.js";
|
|
1
|
+
import type { ChartDiagnostic, Line, Part } from "./types.js";
|
|
2
2
|
/** Classify every line of a chart: title, sections, directives, bar lines and text. */
|
|
3
3
|
export declare function readDocument(text: string): {
|
|
4
4
|
title: string | null;
|
|
5
|
+
part: Part | null;
|
|
6
|
+
parts: Part[];
|
|
5
7
|
lines: Line[];
|
|
6
8
|
diagnostics: ChartDiagnostic[];
|
|
7
9
|
};
|
package/dist/chart/document.js
CHANGED
|
@@ -1,12 +1,70 @@
|
|
|
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",
|
|
7
8
|
bpm: "tempo",
|
|
8
9
|
groove: "feel",
|
|
10
|
+
instrument: "part",
|
|
11
|
+
parts: "part",
|
|
9
12
|
};
|
|
13
|
+
const DIRECTIVE_AFTER = /,\s*(key|time|tempo|feel)\s*:/i;
|
|
14
|
+
/** A `part:` line's label and name, or why it has none (grammar 7.3). */
|
|
15
|
+
function readPart(text) {
|
|
16
|
+
const m = /^part\s*:(.*)$/i.exec(text);
|
|
17
|
+
if (!m)
|
|
18
|
+
return null;
|
|
19
|
+
const value = (m[1] ?? "").trim().replace(/\s+/g, " ");
|
|
20
|
+
if (DIRECTIVE_AFTER.test(value))
|
|
21
|
+
return "part: goes on a line of its own; the name runs to the end of the line";
|
|
22
|
+
const labelled = /^([^\s=]*)\s*=\s*(.*)$/.exec(value);
|
|
23
|
+
if (!labelled)
|
|
24
|
+
return value ? { name: value } : "part: needs a name";
|
|
25
|
+
const [, label, name] = labelled;
|
|
26
|
+
if (!label)
|
|
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";
|
|
30
|
+
return name ? { label, name } : "part: needs a name after the label";
|
|
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
|
+
}
|
|
10
68
|
function readHeading(text, line, diagnostics) {
|
|
11
69
|
let rest = text.trim();
|
|
12
70
|
const elided = /(->|→)$/.test(rest);
|
|
@@ -44,6 +102,41 @@ function readHeading(text, line, diagnostics) {
|
|
|
44
102
|
});
|
|
45
103
|
return { kind: "section", line, name: rest, repeat, elided };
|
|
46
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
|
+
}
|
|
47
140
|
/** A warning when a text line looks like an older chart format. */
|
|
48
141
|
function oldFormat(text, line) {
|
|
49
142
|
const warn = (message, suggestion) => ({
|
|
@@ -60,6 +153,9 @@ function oldFormat(text, line) {
|
|
|
60
153
|
const name = near?.[1] ? NEAR_DIRECTIVES[near[1].toLowerCase()] : undefined;
|
|
61
154
|
if (near && name)
|
|
62
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;
|
|
63
159
|
const unescaped = escapedBarLine(text);
|
|
64
160
|
if (unescaped)
|
|
65
161
|
return warn("escaped pipes; a bar line uses a plain |", unescaped);
|
|
@@ -99,50 +195,105 @@ export function readDocument(text) {
|
|
|
99
195
|
const lines = [];
|
|
100
196
|
const diagnostics = [];
|
|
101
197
|
let title = null;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
});
|
|
126
|
-
lines.push({ kind: "text", line, text: t });
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
if (t === "##" || t.startsWith("## ")) {
|
|
130
|
-
lines.push(readHeading(t.slice(2), line, diagnostics));
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
198
|
+
/** The line kept as text, with why it isn't what it looks like. */
|
|
199
|
+
const asText = (t, line, severity, message) => {
|
|
200
|
+
diagnostics.push({ line, severity, message, token: t });
|
|
201
|
+
return { kind: "text", line, text: t };
|
|
202
|
+
};
|
|
203
|
+
const titleLine = (t, line) => {
|
|
204
|
+
if (title !== null)
|
|
205
|
+
return asText(t, line, "warning", "a chart has one title; this is text");
|
|
206
|
+
title = t.slice(2).trim();
|
|
207
|
+
return { kind: "title", line, text: title };
|
|
208
|
+
};
|
|
209
|
+
/** A `part:` line names the chart once, before its first bar (grammar 7.3). */
|
|
210
|
+
const partLine = (read, t, line) => {
|
|
211
|
+
if (typeof read === "string")
|
|
212
|
+
return asText(t, line, "error", read);
|
|
213
|
+
if (lines.some((l) => l.kind === "bars" || l.kind === "invalid"))
|
|
214
|
+
return asText(t, line, "error", "part: names the whole chart; put it before the first bar");
|
|
215
|
+
return { kind: "part", line, ...read };
|
|
216
|
+
};
|
|
217
|
+
const otherLine = (t, line) => {
|
|
218
|
+
const read = readPart(t);
|
|
219
|
+
if (read)
|
|
220
|
+
return partLine(read, t, line);
|
|
133
221
|
const directive = parseDirectiveLine(t, line);
|
|
134
222
|
if (directive) {
|
|
135
223
|
diagnostics.push(...directive.diagnostics);
|
|
136
|
-
|
|
137
|
-
return;
|
|
224
|
+
return { kind: "directive", line, set: directive.set };
|
|
138
225
|
}
|
|
139
226
|
const old = oldFormat(t, line);
|
|
140
227
|
if (old)
|
|
141
228
|
diagnostics.push(old);
|
|
142
|
-
|
|
229
|
+
return { kind: "text", line, text: t };
|
|
230
|
+
};
|
|
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
|
+
};
|
|
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;
|
|
272
|
+
if (t.startsWith("# "))
|
|
273
|
+
return titleLine(t, line);
|
|
274
|
+
if (t === "##" || t.startsWith("## "))
|
|
275
|
+
return readHeading(t.slice(2), line, diagnostics);
|
|
276
|
+
return otherLine(t, line);
|
|
277
|
+
};
|
|
278
|
+
text
|
|
279
|
+
.replace(/^\uFEFF/, "")
|
|
280
|
+
.split(/\r?\n/)
|
|
281
|
+
.forEach((raw, i) => {
|
|
282
|
+
const t = raw.trim();
|
|
283
|
+
if (t && !t.startsWith("```"))
|
|
284
|
+
lines.push(classify(t, i + 1));
|
|
143
285
|
});
|
|
144
286
|
const empty = noBarLines(lines);
|
|
145
287
|
if (empty)
|
|
146
288
|
diagnostics.push(empty);
|
|
147
|
-
|
|
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
|
+
};
|
|
148
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,9 @@
|
|
|
1
1
|
import type { ChartParse } from "./types.js";
|
|
2
2
|
/** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
|
|
3
3
|
export declare function parseChart(text: string): ChartParse;
|
|
4
|
+
/**
|
|
5
|
+
* One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
|
|
6
|
+
* its own bars, and `N.C.` marked `tacet` where it rests. Null when the
|
|
7
|
+
* chart declares no part with that label. Never throws.
|
|
8
|
+
*/
|
|
9
|
+
export declare function extractPart(text: string, label: string): ChartParse | null;
|
package/dist/chart/parse.js
CHANGED
|
@@ -1,17 +1,106 @@
|
|
|
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
|
+
/** One bar of every part: shared fields from the first, the meter from the first that plays. */
|
|
23
|
+
function zipBar(bars, report) {
|
|
24
|
+
const [first, ...rest] = bars;
|
|
25
|
+
const playing = bars.filter((b) => !b.tacet);
|
|
26
|
+
const meter = playing[0]?.meter ?? first.meter;
|
|
27
|
+
if (playing.some((b) => b.meter.n !== meter.n || b.meter.d !== meter.d))
|
|
28
|
+
report({
|
|
29
|
+
line: playing[1]?.line ?? first.line,
|
|
30
|
+
severity: "error",
|
|
31
|
+
message: "the parts disagree on this bar's meter",
|
|
32
|
+
token: "|",
|
|
33
|
+
});
|
|
34
|
+
return { ...first, meter, parts: [first, ...rest].map(partBar) };
|
|
35
|
+
}
|
|
36
|
+
/** Each part's events, played in lockstep: the shapes match because every line of a system shares its structure. */
|
|
37
|
+
function zipEvents(each, report) {
|
|
38
|
+
const [lead = []] = each;
|
|
39
|
+
return lead.map((e, i) => {
|
|
40
|
+
const peers = each.map((events) => events[i] ?? e);
|
|
41
|
+
if (e.kind === "bar")
|
|
42
|
+
return {
|
|
43
|
+
kind: "bar",
|
|
44
|
+
bar: zipBar(peers.map((p) => (p.kind === "bar" ? p.bar : e.bar)), report),
|
|
45
|
+
};
|
|
46
|
+
if (e.kind === "open")
|
|
47
|
+
return {
|
|
48
|
+
...e,
|
|
49
|
+
events: zipEvents(peers.map((p) => (p.kind === "open" ? p.events : e.events)), report),
|
|
50
|
+
};
|
|
51
|
+
return e;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/** Several parts: each projected and played, then zipped into one timeline (grammar 7.4). */
|
|
55
|
+
function playParts(lines, parts) {
|
|
56
|
+
const played = parts.map((p) => expand(projectPart(lines, parts, p.label ?? ""), p.label, parts.length));
|
|
57
|
+
const diagnostics = mergeReports(played.map((p) => p.diagnostics));
|
|
58
|
+
const events = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
|
|
59
|
+
return { events, sections: played[0]?.sections ?? [], diagnostics };
|
|
60
|
+
}
|
|
3
61
|
/** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
|
|
4
62
|
export function parseChart(text) {
|
|
5
63
|
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]);
|
|
64
|
+
const played = doc.parts.length > 1 ? playParts(doc.lines, doc.parts) : expand(doc.lines);
|
|
9
65
|
return {
|
|
10
66
|
title: doc.title,
|
|
67
|
+
part: doc.part,
|
|
68
|
+
parts: doc.parts,
|
|
11
69
|
lines: doc.lines,
|
|
12
70
|
events: played.events,
|
|
13
71
|
bars: flattenBars(played.events),
|
|
14
72
|
sections: played.sections,
|
|
15
|
-
diagnostics,
|
|
73
|
+
diagnostics: sortDiagnostics([...doc.diagnostics, ...played.diagnostics]),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
|
|
78
|
+
* its own bars, and `N.C.` marked `tacet` where it rests. Null when the
|
|
79
|
+
* chart declares no part with that label. Never throws.
|
|
80
|
+
*/
|
|
81
|
+
export function extractPart(text, label) {
|
|
82
|
+
const doc = readDocument(text);
|
|
83
|
+
const part = doc.parts.find((p) => p.label?.toLowerCase() === label.toLowerCase());
|
|
84
|
+
if (!part)
|
|
85
|
+
return null;
|
|
86
|
+
const lines = doc.parts.length > 1 ? projectPart(doc.lines, doc.parts, label) : doc.lines;
|
|
87
|
+
const played = expand(lines, part.label);
|
|
88
|
+
const kept = new Set(lines.map((l) => l.line));
|
|
89
|
+
// A stand-in may sit on another part's line; that part's problems are not this one's.
|
|
90
|
+
for (const l of doc.lines)
|
|
91
|
+
if ((l.kind === "bars" || l.kind === "invalid") && l.part && l.part !== part.label)
|
|
92
|
+
kept.delete(l.line);
|
|
93
|
+
return {
|
|
94
|
+
title: doc.title,
|
|
95
|
+
part,
|
|
96
|
+
parts: [part],
|
|
97
|
+
lines,
|
|
98
|
+
events: played.events,
|
|
99
|
+
bars: flattenBars(played.events),
|
|
100
|
+
sections: played.sections,
|
|
101
|
+
diagnostics: sortDiagnostics([
|
|
102
|
+
...doc.diagnostics.filter((d) => kept.has(d.line)),
|
|
103
|
+
...played.diagnostics,
|
|
104
|
+
]),
|
|
16
105
|
};
|
|
17
106
|
}
|
|
@@ -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.
|
|
@@ -136,8 +146,10 @@ export function printLine(l, beats = []) {
|
|
|
136
146
|
return `## ${l.name.replace(/\s+/g, " ")}${printRepeat(l.repeat)}${l.elided ? " ->" : ""}`;
|
|
137
147
|
case "directive":
|
|
138
148
|
return printDirectives(l.set);
|
|
149
|
+
case "part":
|
|
150
|
+
return `part: ${l.label ? `${l.label} = ` : ""}${l.name}`;
|
|
139
151
|
case "bars":
|
|
140
|
-
return
|
|
152
|
+
return joinPieces(barPieces(l, beats));
|
|
141
153
|
default:
|
|
142
154
|
return l.text;
|
|
143
155
|
}
|
|
@@ -150,50 +162,127 @@ export function printLine(l, beats = []) {
|
|
|
150
162
|
export function printChart(text) {
|
|
151
163
|
return printParsed(text, parseChart(text));
|
|
152
164
|
}
|
|
153
|
-
/**
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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) {
|
|
158
198
|
const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
|
|
159
|
-
const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
|
|
160
|
-
// Each cell's beat count on its first play, which is when its slot count is checked.
|
|
161
199
|
const firstPlay = new Map();
|
|
162
200
|
for (const b of parsed.bars) {
|
|
163
|
-
const
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
+
}
|
|
168
209
|
}
|
|
169
|
-
|
|
210
|
+
return (l) => {
|
|
170
211
|
if (l.kind !== "bars")
|
|
171
212
|
return [];
|
|
172
213
|
const meters = firstPlay.get(l.line) ?? [];
|
|
173
214
|
let next = 0;
|
|
174
215
|
return l.cells.map((c) => (isBar(c) ? meters[next++] : undefined));
|
|
175
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);
|
|
176
248
|
const out = [];
|
|
177
249
|
let lastWasHeading = false;
|
|
178
250
|
const physical = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
179
251
|
for (const [i, raw] of physical.entries()) {
|
|
252
|
+
if (skip.has(i + 1))
|
|
253
|
+
continue;
|
|
180
254
|
const t = raw.trim();
|
|
181
255
|
if (!t) {
|
|
182
|
-
|
|
183
|
-
out.push("");
|
|
256
|
+
spaceBefore(out, undefined, false);
|
|
184
257
|
continue;
|
|
185
258
|
}
|
|
186
259
|
const l = t.startsWith("```") ? undefined : byLine.get(i + 1);
|
|
187
260
|
// Consecutive headings (a run of replays) stay together, as the spec's examples write them.
|
|
188
|
-
if (l?.kind === "section"
|
|
189
|
-
out
|
|
261
|
+
if (l?.kind === "section")
|
|
262
|
+
spaceBefore(out, l, lastWasHeading);
|
|
190
263
|
lastWasHeading = l?.kind === "section";
|
|
191
|
-
|
|
192
|
-
out.push(t.startsWith("```") ? t : raw.trimEnd());
|
|
193
|
-
else
|
|
194
|
-
out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(edit(l), beatsFor(l)));
|
|
264
|
+
out.push(print(raw, l));
|
|
195
265
|
}
|
|
266
|
+
return finish(out);
|
|
267
|
+
}
|
|
268
|
+
/** The lines as text, without trailing blank lines. */
|
|
269
|
+
function finish(out) {
|
|
196
270
|
while (out.at(-1) === "")
|
|
197
271
|
out.pop();
|
|
198
272
|
return out.length ? `${out.join("\n")}\n` : "";
|
|
199
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) => (l.kind === "part" || l.kind === "bars" || l.kind === "invalid") && !kept.has(l.line))
|
|
286
|
+
.map((l) => l.line));
|
|
287
|
+
return printLines(text, parsed, (l) => l, skip);
|
|
288
|
+
}
|
|
@@ -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,78 @@
|
|
|
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
|
+
* Group a multi-part chart's labelled bar lines into systems and check that
|
|
56
|
+
* each system's lines share their bars and structure. A line that differs is
|
|
57
|
+
* an error and is marked `dropped`. Never throws.
|
|
58
|
+
*/
|
|
59
|
+
export function groupSystems(lines) {
|
|
60
|
+
const diagnostics = [];
|
|
61
|
+
const numbered = numberSystems(lines);
|
|
62
|
+
const first = new Map();
|
|
63
|
+
const out = numbered.map((l) => {
|
|
64
|
+
if (l.kind !== "bars" || l.system === undefined)
|
|
65
|
+
return l;
|
|
66
|
+
const ref = first.get(l.system);
|
|
67
|
+
if (!ref) {
|
|
68
|
+
first.set(l.system, l);
|
|
69
|
+
return l;
|
|
70
|
+
}
|
|
71
|
+
const problem = differs(l, ref);
|
|
72
|
+
if (!problem)
|
|
73
|
+
return l;
|
|
74
|
+
diagnostics.push({ line: l.line, severity: "error", message: problem, token: l.part ?? "" });
|
|
75
|
+
return { ...l, dropped: true };
|
|
76
|
+
});
|
|
77
|
+
return { lines: out, diagnostics };
|
|
78
|
+
}
|
package/dist/chart/types.d.ts
CHANGED
|
@@ -26,6 +26,14 @@ export type TempoChange = {
|
|
|
26
26
|
kind: "a tempo";
|
|
27
27
|
};
|
|
28
28
|
/** `tempo:` gives either `tempo` (a steady tempo) or `tempoChange`, never both. */
|
|
29
|
+
/**
|
|
30
|
+
* The part a chart is written for (grammar 7.3): `part: Electric Guitar 1`,
|
|
31
|
+
* or with a short label, `part: Gtr = Electric Guitar 1`.
|
|
32
|
+
*/
|
|
33
|
+
export type Part = {
|
|
34
|
+
name: string;
|
|
35
|
+
label?: string;
|
|
36
|
+
};
|
|
29
37
|
export type Directives = {
|
|
30
38
|
key?: Key;
|
|
31
39
|
time?: Meter;
|
|
@@ -109,6 +117,8 @@ export type Cell = {
|
|
|
109
117
|
meter: Meter | null;
|
|
110
118
|
slots: Slot[];
|
|
111
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;
|
|
112
122
|
} | {
|
|
113
123
|
kind: "simile";
|
|
114
124
|
words?: Words[];
|
|
@@ -138,7 +148,10 @@ export type Line = {
|
|
|
138
148
|
kind: "directive";
|
|
139
149
|
line: number;
|
|
140
150
|
set: Directives;
|
|
141
|
-
} | {
|
|
151
|
+
} | ({
|
|
152
|
+
kind: "part";
|
|
153
|
+
line: number;
|
|
154
|
+
} & Part) | {
|
|
142
155
|
kind: "bars";
|
|
143
156
|
line: number;
|
|
144
157
|
opens: boolean;
|
|
@@ -147,6 +160,12 @@ export type Line = {
|
|
|
147
160
|
repeat: Repeat;
|
|
148
161
|
/** The passes an ending plays on (grammar 5.1), in order; null for any other bar line. */
|
|
149
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;
|
|
150
169
|
} | {
|
|
151
170
|
kind: "text";
|
|
152
171
|
line: number;
|
|
@@ -161,6 +180,8 @@ export type Line = {
|
|
|
161
180
|
line: number;
|
|
162
181
|
text: string;
|
|
163
182
|
written: number;
|
|
183
|
+
part?: string;
|
|
184
|
+
system?: number;
|
|
164
185
|
};
|
|
165
186
|
export type PlayedBar = {
|
|
166
187
|
/** 1-based measure number in play order; fermatas are not counted. */
|
|
@@ -193,7 +214,21 @@ export type PlayedBar = {
|
|
|
193
214
|
rehearsal?: string;
|
|
194
215
|
/** Words written on this bar; absent when none. `%` copies chords, not words. */
|
|
195
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[];
|
|
196
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;
|
|
197
232
|
/** An open repeat shows one pass; the number of passes is unknown. */
|
|
198
233
|
export type ChartEvent = {
|
|
199
234
|
kind: "bar";
|
|
@@ -222,6 +257,14 @@ export type SectionOccurrence = {
|
|
|
222
257
|
};
|
|
223
258
|
export type ChartParse = {
|
|
224
259
|
title: string | null;
|
|
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
|
+
*/
|
|
265
|
+
part: Part | null;
|
|
266
|
+
/** Every part the chart declares, in score order (grammar 7.4); empty when none. */
|
|
267
|
+
parts: Part[];
|
|
225
268
|
lines: Line[];
|
|
226
269
|
events: ChartEvent[];
|
|
227
270
|
/** `events` flattened, with an open repeat's pass included once. */
|