|
| 1 | +const CHORD_PATTERN = /^[A-G](?:#|b)?(?:m|min|maj|dim|aug|sus2|sus4|7|maj7|m7|min7|dim7|add9)?$/; |
| 2 | +const DIRECTIVES = new Set(["key", "style", "tempo"]); |
| 3 | + |
| 4 | +export class SongSheetParser { |
| 5 | + parse(sourceText) { |
| 6 | + const text = String(sourceText || "").trim(); |
| 7 | + if (!text) { |
| 8 | + return { ok: false, message: "Song Sheet is empty. Add tempo, key, style, and at least one section." }; |
| 9 | + } |
| 10 | + const lines = String(sourceText || "").split(/\r?\n/); |
| 11 | + const metadata = { key: "", style: "", tempo: 120 }; |
| 12 | + const sections = []; |
| 13 | + const warnings = []; |
| 14 | + let activeSection = null; |
| 15 | + for (let index = 0; index < lines.length; index += 1) { |
| 16 | + const lineNumber = index + 1; |
| 17 | + const line = lines[index].trim(); |
| 18 | + if (!line) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + if (line.startsWith("[") || line.endsWith("]")) { |
| 22 | + if (!line.startsWith("[") || !line.endsWith("]")) { |
| 23 | + return { ok: false, message: `Malformed section header on line ${lineNumber}: ${line}` }; |
| 24 | + } |
| 25 | + const label = line.slice(1, -1).trim(); |
| 26 | + if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(label)) { |
| 27 | + return { ok: false, message: `Malformed section label on line ${lineNumber}: ${label || "(empty)"}` }; |
| 28 | + } |
| 29 | + activeSection = { |
| 30 | + bars: 0, |
| 31 | + chords: [], |
| 32 | + label, |
| 33 | + lineNumber, |
| 34 | + loop: label.toLowerCase() === "loop", |
| 35 | + timeline: [] |
| 36 | + }; |
| 37 | + sections.push(activeSection); |
| 38 | + continue; |
| 39 | + } |
| 40 | + if (line.includes("=")) { |
| 41 | + if (activeSection) { |
| 42 | + return { ok: false, message: `Unsupported directive inside section ${activeSection.label} on line ${lineNumber}: ${line}` }; |
| 43 | + } |
| 44 | + const parsedDirective = this.parseDirective(line, lineNumber); |
| 45 | + if (!parsedDirective.ok) { |
| 46 | + return parsedDirective; |
| 47 | + } |
| 48 | + metadata[parsedDirective.key] = parsedDirective.value; |
| 49 | + continue; |
| 50 | + } |
| 51 | + if (!activeSection && line.includes(":")) { |
| 52 | + return { ok: false, message: `Unsupported Song Sheet syntax on line ${lineNumber}: ${line}` }; |
| 53 | + } |
| 54 | + if (!activeSection) { |
| 55 | + return { ok: false, message: `Chord progression on line ${lineNumber} must appear inside a [section].` }; |
| 56 | + } |
| 57 | + const chords = line.split(/\s+/).filter(Boolean); |
| 58 | + chords.forEach((chord) => { |
| 59 | + if (!CHORD_PATTERN.test(chord)) { |
| 60 | + warnings.push(`Invalid chord "${chord}" in section ${activeSection.label} on line ${lineNumber}; chord was skipped.`); |
| 61 | + return; |
| 62 | + } |
| 63 | + activeSection.timeline.push({ |
| 64 | + bar: activeSection.timeline.length + 1, |
| 65 | + chord, |
| 66 | + durationBars: 1, |
| 67 | + section: activeSection.label |
| 68 | + }); |
| 69 | + activeSection.chords.push(chord); |
| 70 | + }); |
| 71 | + } |
| 72 | + if (!sections.length) { |
| 73 | + return { ok: false, message: "Song Sheet must include at least one [section]." }; |
| 74 | + } |
| 75 | + sections.forEach((section) => { |
| 76 | + section.bars = section.chords.length; |
| 77 | + if (!section.chords.length) { |
| 78 | + warnings.push(`Section ${section.label} is empty.`); |
| 79 | + } |
| 80 | + }); |
| 81 | + const bars = sections.reduce((total, section) => total + section.bars, 0); |
| 82 | + const chordCount = sections.reduce((total, section) => total + section.chords.length, 0); |
| 83 | + const estimatedDurationSeconds = Number(((bars * 4 * 60) / metadata.tempo).toFixed(3)); |
| 84 | + return { |
| 85 | + bars, |
| 86 | + chordCount, |
| 87 | + estimatedDurationSeconds, |
| 88 | + key: metadata.key || "not declared", |
| 89 | + ok: true, |
| 90 | + sections, |
| 91 | + sectionSummary: sections.map((section) => `${section.label}: ${section.bars} bars, ${section.chords.length} chords${section.loop ? ", loop" : ""}`).join("; "), |
| 92 | + style: metadata.style || "not declared", |
| 93 | + tempo: metadata.tempo, |
| 94 | + timeline: sections.flatMap((section) => section.timeline), |
| 95 | + warnings, |
| 96 | + warningSummary: warnings.length ? warnings.join("; ") : "none" |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + parseDirective(line, lineNumber) { |
| 101 | + const match = line.match(/^([A-Za-z]+)\s*=\s*(.+)$/); |
| 102 | + if (!match) { |
| 103 | + return { ok: false, message: `Unsupported directive syntax on line ${lineNumber}: ${line}` }; |
| 104 | + } |
| 105 | + const key = match[1].toLowerCase(); |
| 106 | + const value = match[2].trim(); |
| 107 | + if (!DIRECTIVES.has(key)) { |
| 108 | + return { ok: false, message: `Unsupported Song Sheet directive on line ${lineNumber}: ${key}` }; |
| 109 | + } |
| 110 | + if (key === "tempo") { |
| 111 | + const tempo = Number(value); |
| 112 | + if (!Number.isFinite(tempo) || tempo <= 0) { |
| 113 | + return { ok: false, message: `Invalid tempo on line ${lineNumber}: ${value}` }; |
| 114 | + } |
| 115 | + return { key, ok: true, value: tempo }; |
| 116 | + } |
| 117 | + return { key, ok: true, value }; |
| 118 | + } |
| 119 | +} |
0 commit comments