From 442226f5b785f2c65776f480e474f4755ac9a862 Mon Sep 17 00:00:00 2001 From: Martijn Versluis Date: Sun, 5 Jan 2025 14:15:01 +0100 Subject: [PATCH] Use simple grammar for chord parsing (#1538) --- package.json | 1 + src/parser/chord/simple_suffix_grammar.pegjs | 2 ++ test/chord/parse_suffixes.test.ts | 36 ++++++++++++++------ unibuild.ts | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 src/parser/chord/simple_suffix_grammar.pegjs diff --git a/package.json b/package.json index b7af1daa5..68e748012 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "build": "yarn unibuild", "build:release": "yarn unibuild --force --release", "ci": "yarn install && yarn unibuild ci", + "debug:chord": "yarn build && tsx script/debug_parser.ts chord", "debug:chordpro": "yarn build && tsx script/debug_parser.ts chord_pro", "debug:chords-over-words": "yarn build && tsx script/debug_parser.ts chords_over_words --include-chord-grammar", "eslint": "node_modules/.bin/eslint", diff --git a/src/parser/chord/simple_suffix_grammar.pegjs b/src/parser/chord/simple_suffix_grammar.pegjs new file mode 100644 index 000000000..7ae9577b5 --- /dev/null +++ b/src/parser/chord/simple_suffix_grammar.pegjs @@ -0,0 +1,2 @@ +ChordSuffix + = [a-zA-Z0-9\(\)#\+\-o♭♯Δ]* diff --git a/test/chord/parse_suffixes.test.ts b/test/chord/parse_suffixes.test.ts index 635051d73..8d421245b 100644 --- a/test/chord/parse_suffixes.test.ts +++ b/test/chord/parse_suffixes.test.ts @@ -1,19 +1,35 @@ import { Chord } from '../../src'; import SUFFIX_MAPPING from '../../src/normalize_mappings/suffix-normalize-mapping'; +import Key from '../../src/key'; + +const keys: Set = new Set(); +const baseKey = Key.parse('A')!; + +for (let i = 0; i < 12; i += 1) { + keys.add(baseKey.transpose(i).toString()); + keys.add(baseKey.transpose(i).useModifier('#').toString()); + keys.add(baseKey.transpose(i).useModifier('b').toString()); + keys.add(baseKey.transpose(i).toNumeralString(baseKey)); + keys.add(baseKey.transpose(i).useModifier('#').toNumeralString(baseKey)); + keys.add(baseKey.transpose(i).useModifier('b').toNumeralString(baseKey)); + keys.add(baseKey.transpose(i).toNumericString(baseKey)); + keys.add(baseKey.transpose(i).useModifier('#').toNumericString(baseKey)); + keys.add(baseKey.transpose(i).useModifier('b').toNumericString(baseKey)); +} describe('Chord', () => { describe('#parse', () => { - const base = 'Eb'; - - Object - .keys(SUFFIX_MAPPING) - .filter((suffix) => suffix !== '[blank]') - .forEach((suffix) => { - const chord = `${base}${suffix}`; + keys.forEach((base) => { + Object + .keys(SUFFIX_MAPPING) + .filter((suffix) => suffix !== '[blank]') + .forEach((suffix) => { + const chord = `${base}${suffix}`; - it(`parses ${chord}`, () => { - expect(Chord.parse(chord)?.toString()).toEqual(chord); + it(`parses ${chord}`, () => { + expect(Chord.parseOrFail(chord).toString()).toEqual(chord); + }); }); - }); + }); }); }); diff --git a/unibuild.ts b/unibuild.ts index 05dfad8d4..1917d48a7 100755 --- a/unibuild.ts +++ b/unibuild.ts @@ -52,7 +52,7 @@ unibuild((u: Builder) => { }); const chordParser = u.asset('chordParser', { - input: ['src/parser/chord/base_grammar.pegjs', chordSuffixGrammar], + input: ['src/parser/chord/base_grammar.pegjs', 'src/parser/chord/simple_suffix_grammar.pegjs'], outfile: 'src/parser/chord/peg_parser.ts', build: ({ release }: BuildOptions, baseGrammar: string, suffixGrammar: string) => ( peggyGenerate(`${baseGrammar}\n\n${suffixGrammar}`, release)