Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly parse multiple whitespace characters #1469

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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