barmark 0.25.0 → 0.26.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 CHANGED
@@ -9,6 +9,53 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.26.0] - 2026-11-29
13
+
14
+ ### Added
15
+
16
+ - **Each bar line's first written bar number** (Kevin, 2026-11-28; grammar
17
+ A.7 and decisions log): a bar line in `lines`, and a bar line excluded for
18
+ an error, carries `firstWritten`, the written number of its first bar (0 for
19
+ the pickup), as `PlayedBar.written` counts, whether or not any pass plays
20
+ it. Every line of a system that has one has the same. It is absent on a
21
+ line that writes no bar, and on a line of a system whose bars the
22
+ numbering does not count (a one-line system whose line is dropped, a
23
+ system whose reference line writes none, a system with no reference line
24
+ whose counted line is a dropped bar line), and on a line past a part's
25
+ share of the lines in a chart of many parts. So a host can number and
26
+ pair every written bar, played or not, without recounting from the
27
+ lines. `extractPart` gives its lines the numbers its bars are played
28
+ with. No chart changes meaning, and nothing else in the parse changes.
29
+
30
+ ## [0.25.1] - 2026-11-29
31
+
32
+ ### Fixed
33
+
34
+ - **A heading that plays no bars no longer costs a scan of the chart.**
35
+ Each such heading's error (it names the line where a section of that
36
+ name is written later, if one is) searched every section of the chart,
37
+ so the parse took time quadratic in their number: 8,000 empty headings
38
+ took over 2 s, and 8,000 more sections after them some 10 to 13 s. The
39
+ parse now finds that line in constant time: 8,000 empty headings take
40
+ about 15 ms, and about 0.4 s with the 8,000 sections after them.
41
+ - **The sections after an `al Coda`'s Coda no longer cost a scan each.**
42
+ Whether a bar follows the Coda asked every later heading whether it is a
43
+ replay, each from the top of the chart: 32,000 sections after the Coda
44
+ took about 1.5 s, and now take about 0.4 s, growing linearly.
45
+ - **Lines that play nothing are no longer walked on every pass.** Text
46
+ lines, lyric rows, credits and breaks were walked each time their
47
+ section was replayed and each time a repeat around them played a pass,
48
+ so N replays of N such lines, or `|: … :| x N` around them, took time
49
+ quadratic in N (8,000 replays of 8,000 lines: about 1 s; 1 MB of either
50
+ shape: over 30 s). They are now left out of what plays, at the top of a
51
+ section and inside its repeats: 8,000 replays take about 40 ms, and
52
+ 1 MB under a second. Directive lines are still applied every time, so
53
+ repeating a run of them is still quadratic (seconds at 100 kB, a minute
54
+ or more at 1 MB): that is open.
55
+
56
+ No chart reads differently: the same bars, events and sections, and the
57
+ same diagnostics at the same lines in the same order.
58
+
12
59
  ## [0.25.0] - 2026-11-19
13
60
 
14
61
  Grammar changes: more spellings of the major seventh (Kevin, 2026-11-18).
@@ -929,7 +976,9 @@ The first working release: the reference parser and printer for the
929
976
 
930
977
  - **A placeholder package that holds the `barmark` name** while the spec and parser are written.
931
978
 
932
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.25.0...HEAD
979
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.26.0...HEAD
980
+ [0.26.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.25.1...barmark-v0.26.0
981
+ [0.25.1]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.25.0...barmark-v0.25.1
933
982
  [0.25.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.24.0...barmark-v0.25.0
934
983
  [0.24.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.23.1...barmark-v0.24.0
935
984
  [0.23.1]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.23.0...barmark-v0.23.1
package/README.md CHANGED
@@ -191,7 +191,14 @@ an object. It has two kinds of field:
191
191
  - **The chart as written:** `title`, `parts`, `credits` and `lines`, one
192
192
  entry per source line in order (the title, a bar line, a heading, a
193
193
  directive, a `part:` line, a credit, a lyric row, text, a break, or an
194
- invalid bar line kept as written), each with its line number.
194
+ invalid bar line kept as written), each with its line number. A bar
195
+ line, and an invalid one, carries `firstWritten`: the written number of its
196
+ first bar (0 for a pickup), played or not; absent on a line that writes
197
+ no bar, and on a line of a system whose bars the numbering does not
198
+ count (a one-line system whose line is dropped, say), and on a line past
199
+ a part's share of the lines in a very long chart of many parts (the
200
+ grammar's A.7 row lists every case). A pickup's line has
201
+ `firstWritten: 0`, which is falsy: test it with `!== undefined`.
195
202
  - **The chart as played**, worked out from the written lines:
196
203
  - `events` is play order with fermatas and an open repeat kept as
197
204
  events.
@@ -64,8 +64,15 @@ function missingMarks(jump, plan, first) {
64
64
  const writes = (l) => l.kind === "bars" || l.kind === "invalid";
65
65
  /** Whether the section heading at `i` is a replay: no bar line before the next heading. */
66
66
  function isReplay(lines, i) {
67
- const next = lines.findIndex((l, k) => k > i && l.kind === "section");
68
- return !lines.slice(i + 1, next < 0 ? undefined : next).some(writes);
67
+ // It looks ahead from the heading, not from the top: barAfter asks every heading after a Coda.
68
+ for (let k = i + 1; k < lines.length; k++) {
69
+ const l = lines[k];
70
+ if (l.kind === "section")
71
+ return true;
72
+ if (writes(l))
73
+ return false;
74
+ }
75
+ return true;
69
76
  }
70
77
  /** Whether a bar follows `m`: on its line, on a later one, or in a later replay (grammar 5.2). */
71
78
  function barAfter(lines, m) {
@@ -46,4 +46,12 @@ sing?: Sing): {
46
46
  diagnostics: ChartDiagnostic[];
47
47
  /** Whether play stopped at a limit: what follows from that alone goes unreported. */
48
48
  stopped: boolean;
49
+ /**
50
+ * The first written number of each line this expansion reads that writes
51
+ * a bar, by its line number: counting from 1 with no pickup shift, over
52
+ * the lines given (one stream's, in a chart of parts: no system sharing),
53
+ * played or not. None for a line that writes no bar, and for a line past
54
+ * a part's share of lines. `withFirstWritten` makes the bar lines' field.
55
+ */
56
+ firsts: Map<number, number>;
49
57
  };
@@ -79,13 +79,26 @@ function pushesIn(slots, meter) {
79
79
  }
80
80
  const normalize = (name) => name.toLowerCase().replace(/\s+/g, " ").trim();
81
81
  const isWritten = (s) => s.lines.some((l) => l.kind === "bars" || l.kind === "invalid");
82
- /** Grammar §6: an empty section that matches nothing, and how to fix it. */
83
- function unmatchedReplay(heading, sections) {
84
- const name = normalize(heading.name);
85
- const later = sections.find((s) => s.heading &&
86
- s.heading.line > heading.line &&
87
- normalize(s.heading.name) === name &&
88
- isWritten(s))?.heading;
82
+ /**
83
+ * For each section, the first written section after it with its name, when
84
+ * there is one: one pass from the end, so an empty heading's message costs
85
+ * constant time, not a scan of the chart.
86
+ */
87
+ function writtenLater(sections) {
88
+ const next = new Map();
89
+ const later = new Map();
90
+ for (let i = sections.length - 1; i >= 0; i--) {
91
+ const s = sections[i];
92
+ if (!s.heading)
93
+ continue;
94
+ later.set(s, next.get(s.key));
95
+ if (isWritten(s))
96
+ next.set(s.key, s.heading);
97
+ }
98
+ return later;
99
+ }
100
+ /** Grammar §6: an empty section that matches nothing, and how to fix it; `later` is where it is written, if it is. */
101
+ function unmatchedReplay(heading, later) {
89
102
  const fix = later
90
103
  ? `replays use only earlier sections (it is written at line ${later.line})`
91
104
  : "write its bars under it, or remove the heading";
@@ -141,10 +154,10 @@ sing) {
141
154
  reported.add(key);
142
155
  diagnostics.push({ line, severity: "error", message, token });
143
156
  };
144
- const sections = [{ heading: null, lines: [] }];
157
+ const sections = [{ heading: null, key: "", lines: [] }];
145
158
  for (const l of readable) {
146
159
  if (l.kind === "section")
147
- sections.push({ heading: l, lines: [] });
160
+ sections.push({ heading: l, key: normalize(l.name), lines: [] });
148
161
  else if (l.kind !== "title")
149
162
  sections.at(-1)?.lines.push(l);
150
163
  }
@@ -214,22 +227,40 @@ sing) {
214
227
  return markBar(c, line);
215
228
  return c.kind === "simile" ? simile(c, line) : ownBar(c, line);
216
229
  };
217
- for (const l of sections.flatMap((s) => s.lines)) {
218
- if (l.kind === "invalid")
219
- written += l.written;
220
- if (l.kind !== "bars")
221
- continue;
230
+ /** Each bar line's first written number, by its line number; none for a line that writes no bar. */
231
+ const firsts = new Map();
232
+ const resolveLine = (l) => {
222
233
  let bar = 0;
223
- resolved.set(l, l.cells
234
+ return l.cells
224
235
  .map((c) => resolveCell(c, l.line))
225
236
  .filter((p) => p !== null)
226
- .map((p) => (p.kind === "bar" ? { ...p, bar: bar++ } : p)));
227
- }
237
+ .map((p) => (p.kind === "bar" ? { ...p, bar: bar++ } : p));
238
+ };
239
+ /** A line's bars take the numbers after those written before it; an excluded line's too. */
240
+ const readLine = (l) => {
241
+ const first = written + 1;
242
+ if (l.kind === "invalid")
243
+ written += l.written;
244
+ if (l.kind === "bars")
245
+ resolved.set(l, resolveLine(l));
246
+ if (written >= first)
247
+ firsts.set(l.line, first);
248
+ };
249
+ for (const l of sections.flatMap((s) => s.lines))
250
+ readLine(l);
228
251
  const unused = pending.mark;
229
252
  if (unused)
230
253
  error(unused.line, "a rehearsal mark marks the bar after it; none follows", `@${unused.label}`);
231
- // Pair |: and :| within each section, with any endings.
232
- const itemsOf = new Map(sections.map((s) => [s, pairRepeats(s.lines, error)]));
254
+ // Pair |: and :| within each section, with any endings. A line that is neither bars nor a
255
+ // directive plays nothing, so it is left out, at the top and inside a repeat (an ending holds
256
+ // only bar lines): each pass and each replay walks only what it plays.
257
+ const plays = (e) => e.line.kind === "bars" || e.line.kind === "directive";
258
+ const playing = (items) => items.flatMap((it) => {
259
+ if (it.kind === "line")
260
+ return plays(it) ? [it] : [];
261
+ return [{ ...it, entries: it.entries.filter(plays) }];
262
+ });
263
+ const itemsOf = new Map(sections.map((s) => [s, playing(pairRepeats(s.lines, error))]));
233
264
  const plan = planJump(readable, error);
234
265
  const start = () => ({
235
266
  key: null,
@@ -515,6 +546,7 @@ sing) {
515
546
  const events = [];
516
547
  const occurrences = [];
517
548
  const definitions = new Map();
549
+ const later = writtenLater(sections);
518
550
  /** Headings already played, so a written section played again by the jump is a replay. */
519
551
  const heard = new Set();
520
552
  const playSections = () => {
@@ -526,7 +558,7 @@ sing) {
526
558
  };
527
559
  /** A written section's bars into `out`. */
528
560
  const playWritten = (s, h, out) => {
529
- definitions.set(normalize(h.name), s);
561
+ definitions.set(s.key, s);
530
562
  if (h.elided)
531
563
  error(h.line, "-> is only for a replay; leave the bar out instead", "->");
532
564
  times(h.repeat, h.line, out, (into) => playItems(itemsOf.get(s) ?? [], into));
@@ -543,9 +575,9 @@ sing) {
543
575
  // A nameless heading is already an error; it has nothing to replay.
544
576
  if (!h.name)
545
577
  return null;
546
- const source = definitions.get(normalize(h.name));
578
+ const source = definitions.get(s.key);
547
579
  if (!source?.heading) {
548
- error(h.line, unmatchedReplay(h, sections), h.name);
580
+ error(h.line, unmatchedReplay(h, later.get(s)), h.name);
549
581
  return null;
550
582
  }
551
583
  where.replay = h.line;
@@ -614,5 +646,5 @@ sing) {
614
646
  });
615
647
  if (pedal)
616
648
  diagnostics.push(...pedalWarnings(flattenBars(events)));
617
- return { events, sections: sectionsOut, diagnostics, stopped: capped };
649
+ return { events, sections: sectionsOut, diagnostics, stopped: capped, firsts };
618
650
  }
@@ -0,0 +1,10 @@
1
+ import type { Line } from "./types.js";
2
+ /**
3
+ * The lines with each bar line's `firstWritten` (grammar 2.4, A.7), from the
4
+ * numbering the parse's bars were played with: `firsts`, `expand`'s first
5
+ * number of each line it read (counting from 1, by line number), one
6
+ * lower when the chart opens with a pickup. A system's lines take the
7
+ * number of the one line of it that stream read, found among `written`
8
+ * (the chart's lines as written); a line that writes no bar takes none.
9
+ */
10
+ export declare function withFirstWritten(lines: Line[], written: Line[], firsts: Map<number, number>, pickup: boolean): Line[];
@@ -0,0 +1,30 @@
1
+ const isBarLine = (l) => l.kind === "bars" || l.kind === "invalid";
2
+ /** Whether a line writes a bar: a bar or `%` cell, or for an excluded line a bar its pipes write. */
3
+ const writesBar = (l) => l.kind === "invalid"
4
+ ? l.written > 0
5
+ : l.cells.some((c) => c.kind === "slots" || c.kind === "simile");
6
+ /**
7
+ * The lines with each bar line's `firstWritten` (grammar 2.4, A.7), from the
8
+ * numbering the parse's bars were played with: `firsts`, `expand`'s first
9
+ * number of each line it read (counting from 1, by line number), one
10
+ * lower when the chart opens with a pickup. A system's lines take the
11
+ * number of the one line of it that stream read, found among `written`
12
+ * (the chart's lines as written); a line that writes no bar takes none.
13
+ */
14
+ export function withFirstWritten(lines, written, firsts, pickup) {
15
+ const bySystem = new Map();
16
+ for (const l of written) {
17
+ if (!isBarLine(l) || l.system === undefined)
18
+ continue;
19
+ const first = firsts.get(l.line);
20
+ if (first !== undefined)
21
+ bySystem.set(l.system, first);
22
+ }
23
+ const shift = pickup ? 1 : 0;
24
+ return lines.map((l) => {
25
+ if (!isBarLine(l) || !writesBar(l))
26
+ return l;
27
+ const first = l.system === undefined ? firsts.get(l.line) : bySystem.get(l.system);
28
+ return first === undefined ? l : { ...l, firstWritten: first - shift };
29
+ });
30
+ }
@@ -16,7 +16,8 @@ export declare function parseChart(text: string): ChartParse;
16
16
  * line: the part's first line also plays its bare and tutti lines, and a
17
17
  * line rests where it isn't written. A part that writes its bare line
18
18
  * beside its named lines makes it a line of its own (`lineName` `""`), and
19
- * the named lines no longer play it. Null when the chart declares no part
19
+ * the named lines no longer play it. Its bar lines' `firstWritten` are the
20
+ * numbers its bars are played with. Null when the chart declares no part
20
21
  * with that label, or the part no line of that name. Never throws.
21
22
  */
22
23
  export declare function extractPart(text: string, label: string, lineName?: string): ChartParse | null;
@@ -1,5 +1,6 @@
1
1
  import { readDocument } from "./document.js";
2
2
  import { expand, flattenBars, workFor } from "./expand.js";
3
+ import { withFirstWritten } from "./firstWritten.js";
3
4
  import { projectPart, projectPartLines } from "./parts.js";
4
5
  import { pedalWarnings } from "./pedal.js";
5
6
  import { numberFromPickup } from "./pickup.js";
@@ -213,7 +214,15 @@ function playStreams(lines, parts, shown, voices) {
213
214
  const zipped = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d), stopped);
214
215
  const events = foldEvents(zipped, byPart(streams, shown), shown);
215
216
  diagnostics.push(...linedPedalWarnings(events, shown));
216
- return { events, sections: played[0]?.sections ?? [], diagnostics, stopped };
217
+ // The bars carry the first stream's numbers (`zipBar`), so its lines' numbers are the chart's.
218
+ const lead = played[0];
219
+ return {
220
+ events,
221
+ sections: lead?.sections ?? [],
222
+ diagnostics,
223
+ stopped,
224
+ firsts: lead?.firsts ?? new Map(),
225
+ };
217
226
  }
218
227
  const lined = (parts) => parts.some((p) => p.lines?.length);
219
228
  /** What the voices found as they sang (grammar 7.7), once playing is done; past a stop, not what it left unsung. */
@@ -238,7 +247,7 @@ export function parseChart(text) {
238
247
  part,
239
248
  parts: withSings(doc.parts, voice.all),
240
249
  credits: doc.credits,
241
- lines: doc.lines,
250
+ lines: withFirstWritten(doc.lines, doc.lines, played.firsts, pickup),
242
251
  events: played.events,
243
252
  bars,
244
253
  pickup,
@@ -277,7 +286,8 @@ function extractOf(doc, part, voices, lineName) {
277
286
  * line: the part's first line also plays its bare and tutti lines, and a
278
287
  * line rests where it isn't written. A part that writes its bare line
279
288
  * beside its named lines makes it a line of its own (`lineName` `""`), and
280
- * the named lines no longer play it. Null when the chart declares no part
289
+ * the named lines no longer play it. Its bar lines' `firstWritten` are the
290
+ * numbers its bars are played with. Null when the chart declares no part
281
291
  * with that label, or the part no line of that name. Never throws.
282
292
  */
283
293
  export function extractPart(text, label, lineName) {
@@ -293,7 +303,8 @@ export function extractPart(text, label, lineName) {
293
303
  const pickup = numberFromPickup(bars);
294
304
  // The lyric rows are their owner's, or the chart's when no part sings (grammar 7.7).
295
305
  const rows = voices.some((v) => v.singer === voice.owner);
296
- const lines = rows ? extract.lines : extract.lines.filter((l) => l.kind !== "lyrics");
306
+ const numbered = withFirstWritten(extract.lines, doc.lines, played.firsts, pickup);
307
+ const lines = rows ? numbered : numbered.filter((l) => l.kind !== "lyrics");
297
308
  const kept = new Set(lines.map((l) => l.line));
298
309
  // A stand-in may sit on another part's line; that part's problems are not this one's.
299
310
  for (const l of doc.lines)
@@ -218,6 +218,27 @@ export type Line = {
218
218
  system?: number;
219
219
  /** Set when the line's bars or structure differ from its system's (an error): its part is tacet there. */
220
220
  dropped?: true;
221
+ /**
222
+ * The written number of the line's first bar (grammar 2.4, A.7): what
223
+ * `PlayedBar.written` is for that bar when a pass plays it, and the number
224
+ * it would have when none does. 0 when that bar is the chart's pickup.
225
+ * Every line of a system that writes a bar has the system's, a `dropped`
226
+ * line included.
227
+ * Absent when the line writes no bar (no bar or `%` cell), and on a line
228
+ * of a system whose bars the numbering does not count. The numbering is
229
+ * the first part's (its first line, when it writes lines): the chart's
230
+ * first declared part, or in an extract the extracted part. A system
231
+ * counts what its reference line (its first bar line not `dropped`)
232
+ * writes, whatever that part's own line there is: none when it writes no
233
+ * bar (a reference of only `...`, say). With no reference, it counts
234
+ * that part's own line there, else the system's first line: an excluded
235
+ * line's `written`, and none for a `dropped` bar line (a one-line system
236
+ * whose line is dropped, say). A line past a part's share of the lines
237
+ * (`expand`'s limit for a chart of several parts) has none either. A
238
+ * played bar's `line` may be a line with none (a tacet bar on a dropped
239
+ * line that writes no bar): `PlayedBar.written` is the bar's number.
240
+ */
241
+ firstWritten?: number;
221
242
  }
222
243
  /** A text line; `escaped` when written with a leading `\` (grammar 1), which `text` leaves off. */
223
244
  | {
@@ -264,6 +285,13 @@ export type Line = {
264
285
  part?: string;
265
286
  lineName?: string;
266
287
  system?: number;
288
+ /**
289
+ * The written number its first bar would have (grammar 2.4, A.7), as on
290
+ * a bar line: in a system, the system's. Absent when `written` is 0, and
291
+ * when the parse's numbering counts no bars for it, by the rule on a bar
292
+ * line's `firstWritten`.
293
+ */
294
+ firstWritten?: number;
267
295
  };
268
296
  export type PlayedBar = {
269
297
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "Markdown-inspired plain-text notation for chord charts: a parser, a canonical printer and Harte-label conversion.",
5
5
  "type": "module",
6
6
  "exports": {