Skip to content

Commit 5f67f64

Browse files
Add support for consecutive chord lines in UltimateGuitarParser (#1977)
When parsing chord lines, the parser now correctly detects if the following line is also a chord line and treats both as separate chord-only lines instead of merging them as chords + lyrics. This enables proper parsing of instrumental sections with multiple chord lines like: [Intro] D A Bm G D A Bm G Addresses point 4 of #1734
1 parent 8c3cf3d commit 5f67f64

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/parser/ultimate_guitar_parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class UltimateGuitarParser extends ChordSheetParser {
108108
private parseChordLineWithNextLine(chordsLine: string): void {
109109
const nextLine = this.readLine();
110110

111-
if (this.isDirectiveLine(nextLine)) {
111+
if (this.isNonLyricsLine(nextLine)) {
112112
this.parseChordsOnly(chordsLine);
113113
this.unreadLine();
114114
} else {
@@ -120,12 +120,13 @@ class UltimateGuitarParser extends ChordSheetParser {
120120
this.currentLine -= 1;
121121
}
122122

123-
private isDirectiveLine(line: string): boolean {
123+
private isNonLyricsLine(line: string): boolean {
124124
return VERSE_LINE_REGEX.test(line) ||
125125
CHORUS_LINE_REGEX.test(line) ||
126126
BRIDGE_LINE_REGEX.test(line) ||
127127
PART_LINE_REGEX.test(line) ||
128128
OTHER_METADATA_LINE_REGEX.test(line) ||
129+
CHORD_LINE_REGEX.test(line) ||
129130
line.trim().length === 0;
130131
}
131132

test/parser/ultimate_guitar_parser.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,35 @@ describe('UltimateGuitarParser', () => {
446446
expect(line0Items[2]).toBeChordLyricsPair('Am', '');
447447
expect(line0Items[3]).toBeChordLyricsPair('F', 'x3');
448448
});
449+
450+
it('parses consecutive chord lines without lyrics', () => {
451+
const chordSheet = heredoc`
452+
[Intro]
453+
D A Bm G
454+
D A Bm G`;
455+
456+
const parser = new UltimateGuitarParser({ preserveWhitespace: false });
457+
const song = parser.parse(chordSheet);
458+
const { lines } = song;
459+
460+
expect(lines.length).toEqual(4);
461+
462+
const line0Items = lines[0].items;
463+
expect(line0Items[0]).toBeTag('start_of_part', 'Intro');
464+
465+
const line1Items = lines[1].items;
466+
expect(line1Items[0]).toBeChordLyricsPair('D', '');
467+
expect(line1Items[1]).toBeChordLyricsPair('A', '');
468+
expect(line1Items[2]).toBeChordLyricsPair('Bm', '');
469+
expect(line1Items[3]).toBeChordLyricsPair('G', '');
470+
471+
const line2Items = lines[2].items;
472+
expect(line2Items[0]).toBeChordLyricsPair('D', '');
473+
expect(line2Items[1]).toBeChordLyricsPair('A', '');
474+
expect(line2Items[2]).toBeChordLyricsPair('Bm', '');
475+
expect(line2Items[3]).toBeChordLyricsPair('G', '');
476+
477+
const line3Items = lines[3].items;
478+
expect(line3Items[0]).toBeTag('end_of_part', '');
479+
});
449480
});

0 commit comments

Comments
 (0)