Skip to content

Commit

Permalink
Use simple grammar for chord parsing (#1538)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Jan 5, 2025
1 parent b585e27 commit 442226f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/parser/chord/simple_suffix_grammar.pegjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ChordSuffix
= [a-zA-Z0-9\(\)#\+\-o♭♯Δ]*
36 changes: 26 additions & 10 deletions test/chord/parse_suffixes.test.ts
Original file line number Diff line number Diff line change
@@ -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<string> = new Set<string>();
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);
});
});
});
});
});
});
2 changes: 1 addition & 1 deletion unibuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 442226f

Please sign in to comment.