Skip to content

Commit

Permalink
Correctly parse multiple whitespace characters (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Dec 1, 2024
1 parent 5c1a631 commit fad247b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions script/debug_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ const grammarFile = `${parserFolder}/grammar.pegjs`;
const helpersFile = `${parserFolder}/helpers.ts`;
const chordGrammarFile = './src/parser/chord/base_grammar.pegjs';
const chordSuffixGrammarFile = './src/parser/chord/suffix_grammar.pegjs';
const whitespaceGrammarFile = './src/parser/whitespace_grammar.pegjs';
const sectionsGrammarFile = './src/parser/chord_pro/sections_grammar.pegjs';
const chordDefinitionGrammarFile = './src/parser/chord_definition/grammar.pegjs';

const parserGrammar = fs.readFileSync(grammarFile, 'utf8');
const chordGrammar = skipChordGrammar ? '' : fs.readFileSync(chordGrammarFile);
const chordSuffixGrammar = fs.readFileSync(chordSuffixGrammarFile);
const whitespaceGrammar = fs.readFileSync(whitespaceGrammarFile);
const sectionsGrammar = fs.readFileSync(sectionsGrammarFile);
const chordDefinitionGrammar = fs.readFileSync(chordDefinitionGrammarFile);

const result = esbuild.buildSync({
bundle: true,
Expand All @@ -33,6 +39,9 @@ const parserSource = [
parserGrammar,
chordGrammar,
chordSuffixGrammar,
chordDefinitionGrammar,
sectionsGrammar,
whitespaceGrammar,
].join('\n\n');

async function run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WhitespaceCharacter
= [ \t\n\r]

Space "space"
= [ \t]+
= $([ \t]+)

NewLine
= CarriageReturn / LineFeed / CarriageReturnLineFeed
Expand Down
9 changes: 9 additions & 0 deletions test/parser/chord_pro_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ describe('ChordProParser', () => {
expect(song.lines[0].items[0]).toBeTag('comment', 'Some {comment}');
});

it('correctly parses multiple whitespace characters', () => {
const chordSheet = '[C]Let it be ';
const song = new ChordProParser().parse(chordSheet);
const { items } = song.lines[0];

expect(items[0]).toBeChordLyricsPair('C', 'Let ');
expect(items[1]).toBeChordLyricsPair('', 'it be ');
});

it('parses directive with empty value', () => {
const song = new ChordProParser().parse('{c: }');
expect(song.lines[0].items[0]).toBeTag('comment', '');
Expand Down
4 changes: 2 additions & 2 deletions unibuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ unibuild((u: Builder) => {
'src/parser/chord_pro/grammar.pegjs',
'src/parser/chord_definition/grammar.pegjs',
sectionsGrammar,
'src/parser/whitespace.grammar',
'src/parser/whitespace_grammar.pegjs',
],
outfile: 'src/parser/chord_pro/peg_parser.ts',
build: ({ release }: BuildOptions, ...grammars: string[]) => {
Expand All @@ -82,7 +82,7 @@ unibuild((u: Builder) => {
const chordDefinitionParser = u.asset('chordDefinitionParser', {
input: [
'src/parser/chord_definition/grammar.pegjs',
'src/parser/whitespace.grammar',
'src/parser/whitespace_grammar.pegjs',
],
outfile: 'src/parser/chord_definition/peg_parser.ts',
build: ({ release }: BuildOptions, ...grammars: string[]) => (
Expand Down

0 comments on commit fad247b

Please sign in to comment.