barmark 0.0.3 → 0.2.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 +79 -0
- package/LICENSE +21 -0
- package/README.md +74 -8
- package/dist/chart/barLine.d.ts +26 -0
- package/dist/chart/barLine.js +242 -0
- package/dist/chart/directives.d.ts +14 -0
- package/dist/chart/directives.js +108 -0
- package/dist/chart/document.d.ts +7 -0
- package/dist/chart/document.js +116 -0
- package/dist/chart/expand.d.ts +14 -0
- package/dist/chart/expand.js +282 -0
- package/dist/chart/index.d.ts +7 -0
- package/dist/chart/index.js +6 -0
- package/dist/chart/parse.d.ts +3 -0
- package/dist/chart/parse.js +17 -0
- package/dist/chart/print.d.ts +25 -0
- package/dist/chart/print.js +144 -0
- package/dist/chart/types.d.ts +135 -0
- package/dist/chart/types.js +1 -0
- package/dist/chords/harte.d.ts +16 -0
- package/dist/chords/harte.js +122 -0
- package/dist/chords/index.d.ts +6 -0
- package/dist/chords/index.js +4 -0
- package/dist/chords/intervals.d.ts +11 -0
- package/dist/chords/intervals.js +60 -0
- package/dist/chords/noteSet.d.ts +3 -0
- package/dist/chords/noteSet.js +74 -0
- package/dist/chords/parse.d.ts +2 -0
- package/dist/chords/parse.js +117 -0
- package/dist/chords/print.d.ts +7 -0
- package/dist/chords/print.js +16 -0
- package/dist/chords/suffixes.d.ts +13 -0
- package/dist/chords/suffixes.js +49 -0
- package/dist/chords/types.d.ts +33 -0
- package/dist/chords/types.js +1 -0
- package/dist/chords/vocabulary.d.ts +40 -0
- package/dist/chords/vocabulary.js +207 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +31 -8
- package/index.js +0 -11
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { parseChord } from "../chords/parse.js";
|
|
2
|
+
import { misplacedRepeat, parseBarLine, readRepeat, repeatTail } from "./barLine.js";
|
|
3
|
+
import { parseDirectiveLine } from "./directives.js";
|
|
4
|
+
const NEAR_DIRECTIVES = {
|
|
5
|
+
meter: "time",
|
|
6
|
+
signature: "time",
|
|
7
|
+
bpm: "tempo",
|
|
8
|
+
groove: "feel",
|
|
9
|
+
};
|
|
10
|
+
function readHeading(text, line, diagnostics) {
|
|
11
|
+
let rest = text.trim();
|
|
12
|
+
const elided = /(->|→)$/.test(rest);
|
|
13
|
+
if (elided)
|
|
14
|
+
rest = rest.replace(/(->|→)$/, "").trim();
|
|
15
|
+
let repeat = null;
|
|
16
|
+
const tail = repeatTail(rest, true);
|
|
17
|
+
if (tail) {
|
|
18
|
+
repeat = readRepeat(tail.count, line, diagnostics);
|
|
19
|
+
rest = tail.rest.trim();
|
|
20
|
+
}
|
|
21
|
+
const typo = /\s(\S+)$/.exec(rest);
|
|
22
|
+
const meant = typo?.[1] ? misplacedRepeat(typo[1]) : null;
|
|
23
|
+
if (typo && meant)
|
|
24
|
+
diagnostics.push({
|
|
25
|
+
line,
|
|
26
|
+
severity: "warning",
|
|
27
|
+
message: "a section repeat is written x N",
|
|
28
|
+
token: typo[1] ?? "",
|
|
29
|
+
suggestion: `## ${rest.slice(0, typo.index)} ${meant}`,
|
|
30
|
+
});
|
|
31
|
+
if (/->|→/.test(rest))
|
|
32
|
+
diagnostics.push({
|
|
33
|
+
line,
|
|
34
|
+
severity: "error",
|
|
35
|
+
message: "-> goes at the end of a replay heading",
|
|
36
|
+
token: "->",
|
|
37
|
+
});
|
|
38
|
+
if (!rest)
|
|
39
|
+
diagnostics.push({
|
|
40
|
+
line,
|
|
41
|
+
severity: "error",
|
|
42
|
+
message: "a section heading needs a name",
|
|
43
|
+
token: text,
|
|
44
|
+
});
|
|
45
|
+
return { kind: "section", line, name: rest, repeat, elided };
|
|
46
|
+
}
|
|
47
|
+
/** A warning when a text line looks like an older chart format. */
|
|
48
|
+
function oldFormat(text, line) {
|
|
49
|
+
const warn = (message, suggestion) => ({
|
|
50
|
+
line,
|
|
51
|
+
severity: "warning",
|
|
52
|
+
message,
|
|
53
|
+
token: text,
|
|
54
|
+
suggestion,
|
|
55
|
+
});
|
|
56
|
+
const label = /^\[([^\]]+)\]$/.exec(text) ?? /^([A-Za-z][\w ]*):$/.exec(text);
|
|
57
|
+
if (label?.[1])
|
|
58
|
+
return warn("a section label; barmark headings start with ##", `## ${label[1].trim()}`);
|
|
59
|
+
const near = /^(\w+)\s*:\s*(.*)$/.exec(text);
|
|
60
|
+
const name = near?.[1] ? NEAR_DIRECTIVES[near[1].toLowerCase()] : undefined;
|
|
61
|
+
if (near && name)
|
|
62
|
+
return warn(`not a directive; did you mean ${name}:?`, `${name}: ${near[2]}`);
|
|
63
|
+
const words = text.split(/\s+/);
|
|
64
|
+
if (words.length >= 2 && words.every((w) => parseChord(w).ok))
|
|
65
|
+
return warn("chords outside a bar line are text", `| ${words.join(" | ")} |`);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/** Classify every line of a chart: title, sections, directives, bar lines and text. */
|
|
69
|
+
export function readDocument(text) {
|
|
70
|
+
const lines = [];
|
|
71
|
+
const diagnostics = [];
|
|
72
|
+
let title = null;
|
|
73
|
+
const physical = text.replace(/^/, "").split(/\r?\n/);
|
|
74
|
+
physical.forEach((raw, i) => {
|
|
75
|
+
const line = i + 1;
|
|
76
|
+
const t = raw.trim();
|
|
77
|
+
if (!t || t.startsWith("```"))
|
|
78
|
+
return;
|
|
79
|
+
if (t.startsWith("|")) {
|
|
80
|
+
const r = parseBarLine(t, line);
|
|
81
|
+
diagnostics.push(...r.diagnostics);
|
|
82
|
+
lines.push(r.bars ?? { kind: "invalid", line, text: t });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (t.startsWith("# ")) {
|
|
86
|
+
if (title === null) {
|
|
87
|
+
title = t.slice(2).trim();
|
|
88
|
+
lines.push({ kind: "title", line, text: title });
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
diagnostics.push({
|
|
92
|
+
line,
|
|
93
|
+
severity: "warning",
|
|
94
|
+
message: "a chart has one title; this is text",
|
|
95
|
+
token: t,
|
|
96
|
+
});
|
|
97
|
+
lines.push({ kind: "text", line, text: t });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (t === "##" || t.startsWith("## ")) {
|
|
101
|
+
lines.push(readHeading(t.slice(2), line, diagnostics));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const directive = parseDirectiveLine(t, line);
|
|
105
|
+
if (directive) {
|
|
106
|
+
diagnostics.push(...directive.diagnostics);
|
|
107
|
+
lines.push({ kind: "directive", line, set: directive.set });
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const old = oldFormat(t, line);
|
|
111
|
+
if (old)
|
|
112
|
+
diagnostics.push(old);
|
|
113
|
+
lines.push({ kind: "text", line, text: t });
|
|
114
|
+
});
|
|
115
|
+
return { title, lines, diagnostics };
|
|
116
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ChartDiagnostic, ChartEvent, Line, PlayedBar, SectionOccurrence } from "./types.js";
|
|
2
|
+
/** Nested repeats multiply; expansion stops here rather than run away. */
|
|
3
|
+
export declare const MAX_EXPANDED_BARS = 10000;
|
|
4
|
+
/** Every played bar in order, with an open repeat's pass included once. */
|
|
5
|
+
export declare function flattenBars(events: ChartEvent[]): PlayedBar[];
|
|
6
|
+
/**
|
|
7
|
+
* Turn the written chart into the bars in play order: sections and their
|
|
8
|
+
* replays, repeats, directive changes, `%` and elision.
|
|
9
|
+
*/
|
|
10
|
+
export declare function expand(lines: Line[]): {
|
|
11
|
+
events: ChartEvent[];
|
|
12
|
+
sections: SectionOccurrence[];
|
|
13
|
+
diagnostics: ChartDiagnostic[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { beatsOf } from "./directives.js";
|
|
2
|
+
/** Nested repeats multiply; expansion stops here rather than run away. */
|
|
3
|
+
export const MAX_EXPANDED_BARS = 10_000;
|
|
4
|
+
/** Repeats of bodies that play no bars (fermatas only) are bounded too. */
|
|
5
|
+
const MAX_WORK = MAX_EXPANDED_BARS * 10;
|
|
6
|
+
const normalize = (name) => name.toLowerCase().replace(/\s+/g, " ").trim();
|
|
7
|
+
/** Every played bar in order, with an open repeat's pass included once. */
|
|
8
|
+
export function flattenBars(events) {
|
|
9
|
+
return events.flatMap((e) => e.kind === "bar" ? [e.bar] : e.kind === "open" ? flattenBars(e.events) : []);
|
|
10
|
+
}
|
|
11
|
+
/** Remove the last bar, looking inside a final open pass and past a final fermata. */
|
|
12
|
+
function removeLastBar(events) {
|
|
13
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
14
|
+
const e = events[i];
|
|
15
|
+
if (e?.kind === "bar") {
|
|
16
|
+
events.splice(i, 1);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (e?.kind === "open" && removeLastBar(e.events))
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Turn the written chart into the bars in play order: sections and their
|
|
26
|
+
* replays, repeats, directive changes, `%` and elision.
|
|
27
|
+
*/
|
|
28
|
+
export function expand(lines) {
|
|
29
|
+
const diagnostics = [];
|
|
30
|
+
const error = (line, message, token) => diagnostics.push({ line, severity: "error", message, token });
|
|
31
|
+
const sections = [{ heading: null, lines: [] }];
|
|
32
|
+
for (const l of lines) {
|
|
33
|
+
if (l.kind === "section")
|
|
34
|
+
sections.push({ heading: l, lines: [] });
|
|
35
|
+
else if (l.kind !== "title")
|
|
36
|
+
sections.at(-1)?.lines.push(l);
|
|
37
|
+
}
|
|
38
|
+
// % copies the bar written before it, one-bar meter included, so a section plays the same wherever it is replayed.
|
|
39
|
+
const resolved = new Map();
|
|
40
|
+
let previous = null;
|
|
41
|
+
for (const l of sections.flatMap((s) => s.lines)) {
|
|
42
|
+
if (l.kind !== "bars")
|
|
43
|
+
continue;
|
|
44
|
+
const cells = [];
|
|
45
|
+
for (const c of l.cells) {
|
|
46
|
+
if (c.kind === "fermata")
|
|
47
|
+
cells.push({ kind: "fermata" });
|
|
48
|
+
else if (c.kind === "simile") {
|
|
49
|
+
if (!previous)
|
|
50
|
+
error(l.line, "% has no bar before it to repeat", "%");
|
|
51
|
+
cells.push({
|
|
52
|
+
kind: "bar",
|
|
53
|
+
meter: previous?.meter ?? null,
|
|
54
|
+
slots: previous?.slots ?? [{ kind: "hold" }],
|
|
55
|
+
written: false,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
const first = c.slots[0];
|
|
60
|
+
const head = first?.kind === "group" ? first.members[0] : first;
|
|
61
|
+
if (!previous && head?.kind === "hold")
|
|
62
|
+
error(l.line, "/ has nothing before it to hold", "/");
|
|
63
|
+
cells.push({ kind: "bar", meter: c.meter, slots: c.slots, written: true });
|
|
64
|
+
previous = { meter: c.meter, slots: c.slots };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
resolved.set(l, cells);
|
|
68
|
+
}
|
|
69
|
+
// Pair |: and :| within each section.
|
|
70
|
+
const itemsOf = new Map();
|
|
71
|
+
for (const s of sections) {
|
|
72
|
+
const items = [];
|
|
73
|
+
let open = null;
|
|
74
|
+
for (const l of s.lines) {
|
|
75
|
+
const bars = l.kind === "bars" ? l : null;
|
|
76
|
+
if (bars?.opens) {
|
|
77
|
+
if (open)
|
|
78
|
+
error(bars.line, "|: inside another |:", "|:");
|
|
79
|
+
else
|
|
80
|
+
open = { line: bars.line, entries: [] };
|
|
81
|
+
}
|
|
82
|
+
if (bars?.closes) {
|
|
83
|
+
if (!open) {
|
|
84
|
+
error(bars.line, ":| has no |: before it in this section", ":|");
|
|
85
|
+
items.push({ kind: "line", line: bars, repeat: null });
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
open.entries.push({ line: bars, repeat: null });
|
|
89
|
+
items.push({
|
|
90
|
+
kind: "bracket",
|
|
91
|
+
line: open.line,
|
|
92
|
+
entries: open.entries,
|
|
93
|
+
repeat: bars.repeat ?? 2,
|
|
94
|
+
});
|
|
95
|
+
open = null;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const entry = { line: l, repeat: bars?.repeat ?? null };
|
|
99
|
+
if (open)
|
|
100
|
+
open.entries.push(entry);
|
|
101
|
+
else
|
|
102
|
+
items.push({ kind: "line", ...entry });
|
|
103
|
+
}
|
|
104
|
+
if (open) {
|
|
105
|
+
error(open.line, "|: is never closed in this section", "|:");
|
|
106
|
+
items.push(...open.entries.map((e) => ({ kind: "line", ...e })));
|
|
107
|
+
}
|
|
108
|
+
itemsOf.set(s, items);
|
|
109
|
+
}
|
|
110
|
+
const state = {
|
|
111
|
+
key: null,
|
|
112
|
+
time: { n: 4, d: 4 },
|
|
113
|
+
tempo: null,
|
|
114
|
+
feel: "straight",
|
|
115
|
+
};
|
|
116
|
+
const apply = (set) => {
|
|
117
|
+
if (set.key)
|
|
118
|
+
state.key = set.key;
|
|
119
|
+
if (set.time)
|
|
120
|
+
state.time = set.time;
|
|
121
|
+
if (set.tempo)
|
|
122
|
+
state.tempo = set.tempo;
|
|
123
|
+
if (set.feel)
|
|
124
|
+
state.feel = set.feel;
|
|
125
|
+
};
|
|
126
|
+
let count = 0;
|
|
127
|
+
let work = 0;
|
|
128
|
+
let capped = false;
|
|
129
|
+
let openDepth = 0;
|
|
130
|
+
const nestedOpen = new Set();
|
|
131
|
+
const checked = new Set();
|
|
132
|
+
const stop = (line) => {
|
|
133
|
+
capped = true;
|
|
134
|
+
error(line, `expansion stopped at ${MAX_EXPANDED_BARS} bars`, "x");
|
|
135
|
+
};
|
|
136
|
+
let sectionName = null;
|
|
137
|
+
const emit = (p, line, out) => {
|
|
138
|
+
if (capped)
|
|
139
|
+
return;
|
|
140
|
+
if (++work > MAX_WORK)
|
|
141
|
+
return stop(line);
|
|
142
|
+
if (p.kind === "fermata") {
|
|
143
|
+
out.push({ kind: "fermata", line });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (count >= MAX_EXPANDED_BARS)
|
|
147
|
+
return stop(line);
|
|
148
|
+
count++;
|
|
149
|
+
const meter = p.meter ?? state.time;
|
|
150
|
+
if (p.written && !checked.has(p)) {
|
|
151
|
+
checked.add(p);
|
|
152
|
+
const k = p.slots.length;
|
|
153
|
+
const b = beatsOf(meter);
|
|
154
|
+
if (b % k !== 0 && k % b !== 0)
|
|
155
|
+
diagnostics.push({
|
|
156
|
+
line,
|
|
157
|
+
severity: "warning",
|
|
158
|
+
message: `${k} slots divide ${b} beats unevenly`,
|
|
159
|
+
token: `${k}`,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const { key, tempo, feel } = state;
|
|
163
|
+
out.push({
|
|
164
|
+
kind: "bar",
|
|
165
|
+
bar: {
|
|
166
|
+
index: 0,
|
|
167
|
+
line,
|
|
168
|
+
section: sectionName,
|
|
169
|
+
meter,
|
|
170
|
+
key,
|
|
171
|
+
tempo,
|
|
172
|
+
feel,
|
|
173
|
+
slots: p.slots,
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
/** Play `body` `repeat` times into `out`; an open repeat plays one pass inside an open event. */
|
|
178
|
+
const times = (repeat, line, out, body) => {
|
|
179
|
+
if (repeat === "open") {
|
|
180
|
+
if (openDepth > 0) {
|
|
181
|
+
if (!nestedOpen.has(line))
|
|
182
|
+
error(line, "an open repeat inside another open repeat", "x ...");
|
|
183
|
+
nestedOpen.add(line);
|
|
184
|
+
body(out);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const inner = [];
|
|
188
|
+
openDepth++;
|
|
189
|
+
body(inner);
|
|
190
|
+
openDepth--;
|
|
191
|
+
out.push({ kind: "open", line, events: inner });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
for (let i = 0; i < (repeat ?? 1) && !capped; i++) {
|
|
195
|
+
if (++work > MAX_WORK)
|
|
196
|
+
return stop(line);
|
|
197
|
+
body(out);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const playLine = ({ line: l, repeat }, out) => {
|
|
201
|
+
if (l.kind === "directive")
|
|
202
|
+
apply(l.set);
|
|
203
|
+
if (l.kind !== "bars")
|
|
204
|
+
return;
|
|
205
|
+
times(repeat, l.line, out, (into) => {
|
|
206
|
+
for (const c of resolved.get(l) ?? [])
|
|
207
|
+
emit(c, l.line, into);
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
const playItems = (items, out) => {
|
|
211
|
+
for (const it of items) {
|
|
212
|
+
if (it.kind === "line")
|
|
213
|
+
playLine(it, out);
|
|
214
|
+
else
|
|
215
|
+
times(it.repeat, it.line, out, (into) => {
|
|
216
|
+
for (const e of it.entries)
|
|
217
|
+
playLine(e, into);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const events = [];
|
|
222
|
+
const occurrences = [];
|
|
223
|
+
const definitions = new Map();
|
|
224
|
+
for (const s of sections) {
|
|
225
|
+
const h = s.heading;
|
|
226
|
+
const items = itemsOf.get(s) ?? [];
|
|
227
|
+
if (!h) {
|
|
228
|
+
sectionName = null;
|
|
229
|
+
playItems(items, events);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
sectionName = h.name;
|
|
233
|
+
const out = [];
|
|
234
|
+
const isDefinition = s.lines.some((l) => l.kind === "bars" || l.kind === "invalid");
|
|
235
|
+
let replayOf = null;
|
|
236
|
+
if (isDefinition) {
|
|
237
|
+
definitions.set(normalize(h.name), s);
|
|
238
|
+
if (h.elided)
|
|
239
|
+
error(h.line, "-> is only for a replay; leave the bar out instead", "->");
|
|
240
|
+
times(h.repeat, h.line, out, (into) => playItems(items, into));
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
// A nameless heading is already an error; it has nothing to replay.
|
|
244
|
+
if (!h.name)
|
|
245
|
+
continue;
|
|
246
|
+
const source = definitions.get(normalize(h.name));
|
|
247
|
+
if (!source?.heading) {
|
|
248
|
+
error(h.line, `no earlier section named "${h.name}" to replay`, h.name);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
replayOf = source.heading.line;
|
|
252
|
+
const body = itemsOf.get(source) ?? [];
|
|
253
|
+
times(h.repeat, h.line, out, (into) => playItems(body, into));
|
|
254
|
+
}
|
|
255
|
+
const elided = !isDefinition && h.elided && removeLastBar(out);
|
|
256
|
+
// A replay heading stands for its bars: its own lines (directives) come after them.
|
|
257
|
+
if (!isDefinition)
|
|
258
|
+
playItems(items, out);
|
|
259
|
+
events.push(...out);
|
|
260
|
+
occurrences.push([
|
|
261
|
+
{ name: h.name, line: h.line, replayOf, firstBar: null, lastBar: null, elided },
|
|
262
|
+
out,
|
|
263
|
+
]);
|
|
264
|
+
}
|
|
265
|
+
flattenBars(events).forEach((b, i) => {
|
|
266
|
+
b.index = i + 1;
|
|
267
|
+
});
|
|
268
|
+
const forms = new Map();
|
|
269
|
+
const sectionsOut = occurrences.map(([occ, out]) => {
|
|
270
|
+
const bars = flattenBars(out);
|
|
271
|
+
const written = occ.replayOf ?? occ.line;
|
|
272
|
+
if (!forms.has(written))
|
|
273
|
+
forms.set(written, forms.size);
|
|
274
|
+
return {
|
|
275
|
+
...occ,
|
|
276
|
+
firstBar: bars[0]?.index ?? null,
|
|
277
|
+
lastBar: bars.at(-1)?.index ?? null,
|
|
278
|
+
form: forms.get(written) ?? 0,
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
return { events, sections: sectionsOut, diagnostics };
|
|
282
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { parseBarLine } from "./barLine.js";
|
|
2
|
+
export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
|
|
3
|
+
export { readDocument } from "./document.js";
|
|
4
|
+
export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
|
|
5
|
+
export { parseChart } from "./parse.js";
|
|
6
|
+
export { printCell, printChart, printLine, printSlot, reduceSlots } from "./print.js";
|
|
7
|
+
export type * from "./types.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { parseBarLine } from "./barLine.js";
|
|
2
|
+
export { beatsOf, DIRECTIVE_NAMES, parseDirectiveLine, parseMeter } from "./directives.js";
|
|
3
|
+
export { readDocument } from "./document.js";
|
|
4
|
+
export { expand, flattenBars, MAX_EXPANDED_BARS } from "./expand.js";
|
|
5
|
+
export { parseChart } from "./parse.js";
|
|
6
|
+
export { printCell, printChart, printLine, printSlot, reduceSlots } from "./print.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { readDocument } from "./document.js";
|
|
2
|
+
import { expand, flattenBars } from "./expand.js";
|
|
3
|
+
/** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
|
|
4
|
+
export function parseChart(text) {
|
|
5
|
+
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]);
|
|
9
|
+
return {
|
|
10
|
+
title: doc.title,
|
|
11
|
+
lines: doc.lines,
|
|
12
|
+
events: played.events,
|
|
13
|
+
bars: flattenBars(played.events),
|
|
14
|
+
sections: played.sections,
|
|
15
|
+
diagnostics,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Cell, Line, Slot } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The fewest slots with the same timeline: `G / D /` is `G D`, `-- --` is `--`.
|
|
4
|
+
* Only a slot with no attack of its own merges (`/`, a repeated `--` or
|
|
5
|
+
* `N.C.`); a repeated chord name never does, and a group never heads a merged
|
|
6
|
+
* block, because stretching it would move its members. Given the bar's beat
|
|
7
|
+
* count, the result is never an uneven slot count unless the input's was the
|
|
8
|
+
* same count, so printing adds no warning.
|
|
9
|
+
*/
|
|
10
|
+
export declare function reduceSlots(slots: Slot[], beats?: number): Slot[];
|
|
11
|
+
/** A slot in canonical spelling; a group's members are joined with `_`. */
|
|
12
|
+
export declare function printSlot(s: Slot): string;
|
|
13
|
+
/** A cell in canonical form: `%`, `...`, or an optional `[n/d]` and the fewest slots. */
|
|
14
|
+
export declare function printCell(c: Cell, beats?: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* One parsed line in canonical form. Text and invalid lines print as stored.
|
|
17
|
+
* `beats` gives each cell's beat count where known, so reduction adds no warning.
|
|
18
|
+
*/
|
|
19
|
+
export declare function printLine(l: Line, beats?: (number | undefined)[]): string;
|
|
20
|
+
/**
|
|
21
|
+
* A chart in canonical form (spec §8). Spelling and layout only: lines with
|
|
22
|
+
* errors and text lines print as written, and the author's blank lines and
|
|
23
|
+
* fences stay. Never throws.
|
|
24
|
+
*/
|
|
25
|
+
export declare function printChart(text: string): string;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { beatsOf } from "./directives.js";
|
|
2
|
+
import { parseChart } from "./parse.js";
|
|
3
|
+
/** A slot with no attack of its own continues whatever sounds before it. A repeated chord name is a re-attack. */
|
|
4
|
+
function continues(sounding, s) {
|
|
5
|
+
if (s.kind === "hold")
|
|
6
|
+
return true;
|
|
7
|
+
if (s.kind === "silence" || s.kind === "nc")
|
|
8
|
+
return sounding?.kind === s.kind;
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The fewest slots with the same timeline: `G / D /` is `G D`, `-- --` is `--`.
|
|
13
|
+
* Only a slot with no attack of its own merges (`/`, a repeated `--` or
|
|
14
|
+
* `N.C.`); a repeated chord name never does, and a group never heads a merged
|
|
15
|
+
* block, because stretching it would move its members. Given the bar's beat
|
|
16
|
+
* count, the result is never an uneven slot count unless the input's was the
|
|
17
|
+
* same count, so printing adds no warning.
|
|
18
|
+
*/
|
|
19
|
+
export function reduceSlots(slots, beats) {
|
|
20
|
+
const k = slots.length;
|
|
21
|
+
const cont = [];
|
|
22
|
+
let sounding = null;
|
|
23
|
+
for (const s of slots) {
|
|
24
|
+
cont.push(continues(sounding, s));
|
|
25
|
+
const last = s.kind === "group" ? s.members.at(-1) : s;
|
|
26
|
+
if (last && last.kind !== "hold")
|
|
27
|
+
sounding = last;
|
|
28
|
+
}
|
|
29
|
+
for (let m = 1; m <= k; m++) {
|
|
30
|
+
if (k % m !== 0)
|
|
31
|
+
continue;
|
|
32
|
+
const size = k / m;
|
|
33
|
+
if (beats !== undefined && m !== k && beats % m !== 0 && m % beats !== 0)
|
|
34
|
+
continue;
|
|
35
|
+
const merges = (c, i) => i % size === 0 ? size === 1 || slots[i]?.kind !== "group" : c;
|
|
36
|
+
if (cont.every(merges))
|
|
37
|
+
return slots.filter((_, i) => i % size === 0);
|
|
38
|
+
}
|
|
39
|
+
return slots;
|
|
40
|
+
}
|
|
41
|
+
const SYMBOLS = { hold: "/", nc: "N.C.", silence: "--", unknown: "?" };
|
|
42
|
+
function printSimple(s) {
|
|
43
|
+
return s.kind === "chord" ? s.chord.canonical : SYMBOLS[s.kind];
|
|
44
|
+
}
|
|
45
|
+
/** A slot in canonical spelling; a group's members are joined with `_`. */
|
|
46
|
+
export function printSlot(s) {
|
|
47
|
+
return s.kind === "group" ? s.members.map(printSimple).join("_") : printSimple(s);
|
|
48
|
+
}
|
|
49
|
+
/** A cell in canonical form: `%`, `...`, or an optional `[n/d]` and the fewest slots. */
|
|
50
|
+
export function printCell(c, beats) {
|
|
51
|
+
if (c.kind === "simile")
|
|
52
|
+
return "%";
|
|
53
|
+
if (c.kind === "fermata")
|
|
54
|
+
return "...";
|
|
55
|
+
const meter = c.meter ? `[${c.meter.n}/${c.meter.d}] ` : "";
|
|
56
|
+
return meter + reduceSlots(c.slots, beats).map(printSlot).join(" ");
|
|
57
|
+
}
|
|
58
|
+
const printRepeat = (r) => (r === null ? "" : r === "open" ? " x ..." : ` x${r}`);
|
|
59
|
+
function printKey(k) {
|
|
60
|
+
if (k.mode === "major")
|
|
61
|
+
return k.tonic;
|
|
62
|
+
if (k.mode === "minor")
|
|
63
|
+
return `${k.tonic}m`;
|
|
64
|
+
return `${k.tonic} ${k.mode}`;
|
|
65
|
+
}
|
|
66
|
+
function printDirectives(d) {
|
|
67
|
+
const parts = [];
|
|
68
|
+
if (d.key)
|
|
69
|
+
parts.push(`key: ${printKey(d.key)}`);
|
|
70
|
+
if (d.time)
|
|
71
|
+
parts.push(`time: ${d.time.n}/${d.time.d}`);
|
|
72
|
+
if (d.tempo !== undefined)
|
|
73
|
+
parts.push(`tempo: ${d.tempo}`);
|
|
74
|
+
if (d.feel)
|
|
75
|
+
parts.push(`feel: ${d.feel}`);
|
|
76
|
+
return parts.join(", ");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* One parsed line in canonical form. Text and invalid lines print as stored.
|
|
80
|
+
* `beats` gives each cell's beat count where known, so reduction adds no warning.
|
|
81
|
+
*/
|
|
82
|
+
export function printLine(l, beats = []) {
|
|
83
|
+
switch (l.kind) {
|
|
84
|
+
case "title":
|
|
85
|
+
return `# ${l.text}`;
|
|
86
|
+
case "section":
|
|
87
|
+
return `## ${l.name.replace(/\s+/g, " ")}${printRepeat(l.repeat)}${l.elided ? " ->" : ""}`;
|
|
88
|
+
case "directive":
|
|
89
|
+
return printDirectives(l.set);
|
|
90
|
+
case "bars":
|
|
91
|
+
return `${l.opens ? "|:" : "|"} ${l.cells.map((c, i) => printCell(c, beats[i])).join(" | ")} ${l.closes ? ":|" : "|"}${printRepeat(l.repeat)}`;
|
|
92
|
+
default:
|
|
93
|
+
return l.text;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A chart in canonical form (spec §8). Spelling and layout only: lines with
|
|
98
|
+
* errors and text lines print as written, and the author's blank lines and
|
|
99
|
+
* fences stay. Never throws.
|
|
100
|
+
*/
|
|
101
|
+
export function printChart(text) {
|
|
102
|
+
const parsed = parseChart(text);
|
|
103
|
+
const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
|
|
104
|
+
const errored = new Set(parsed.diagnostics.filter((d) => d.severity === "error").map((d) => d.line));
|
|
105
|
+
// Each cell's beat count on its first play, which is when its slot count is checked.
|
|
106
|
+
const firstPlay = new Map();
|
|
107
|
+
for (const b of parsed.bars) {
|
|
108
|
+
const l = byLine.get(b.line);
|
|
109
|
+
const meters = firstPlay.get(b.line) ?? [];
|
|
110
|
+
const cells = l?.kind === "bars" ? l.cells.filter((c) => c.kind !== "fermata").length : 0;
|
|
111
|
+
if (meters.length < cells)
|
|
112
|
+
firstPlay.set(b.line, [...meters, beatsOf(b.meter)]);
|
|
113
|
+
}
|
|
114
|
+
const beatsFor = (l) => {
|
|
115
|
+
if (l.kind !== "bars")
|
|
116
|
+
return [];
|
|
117
|
+
const meters = firstPlay.get(l.line) ?? [];
|
|
118
|
+
let next = 0;
|
|
119
|
+
return l.cells.map((c) => (c.kind === "fermata" ? undefined : meters[next++]));
|
|
120
|
+
};
|
|
121
|
+
const out = [];
|
|
122
|
+
let lastWasHeading = false;
|
|
123
|
+
const physical = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
124
|
+
for (const [i, raw] of physical.entries()) {
|
|
125
|
+
const t = raw.trim();
|
|
126
|
+
if (!t) {
|
|
127
|
+
if (out.length > 0 && out.at(-1) !== "")
|
|
128
|
+
out.push("");
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const l = t.startsWith("```") ? undefined : byLine.get(i + 1);
|
|
132
|
+
// Consecutive headings (a run of replays) stay together, as the spec's examples write them.
|
|
133
|
+
if (l?.kind === "section" && !lastWasHeading && out.length > 0 && out.at(-1) !== "")
|
|
134
|
+
out.push("");
|
|
135
|
+
lastWasHeading = l?.kind === "section";
|
|
136
|
+
if (!l)
|
|
137
|
+
out.push(t.startsWith("```") ? t : raw.trimEnd());
|
|
138
|
+
else
|
|
139
|
+
out.push(l.kind === "text" || errored.has(l.line) ? raw.trimEnd() : printLine(l, beatsFor(l)));
|
|
140
|
+
}
|
|
141
|
+
while (out.at(-1) === "")
|
|
142
|
+
out.pop();
|
|
143
|
+
return out.length ? `${out.join("\n")}\n` : "";
|
|
144
|
+
}
|