Skip to content

Commit 442226f

Browse files
Use simple grammar for chord parsing (#1538)
1 parent b585e27 commit 442226f

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"build": "yarn unibuild",
6565
"build:release": "yarn unibuild --force --release",
6666
"ci": "yarn install && yarn unibuild ci",
67+
"debug:chord": "yarn build && tsx script/debug_parser.ts chord",
6768
"debug:chordpro": "yarn build && tsx script/debug_parser.ts chord_pro",
6869
"debug:chords-over-words": "yarn build && tsx script/debug_parser.ts chords_over_words --include-chord-grammar",
6970
"eslint": "node_modules/.bin/eslint",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ChordSuffix
2+
= [a-zA-Z0-9\(\)#\+\-o♭♯Δ]*

test/chord/parse_suffixes.test.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
import { Chord } from '../../src';
22
import SUFFIX_MAPPING from '../../src/normalize_mappings/suffix-normalize-mapping';
3+
import Key from '../../src/key';
4+
5+
const keys: Set<string> = new Set<string>();
6+
const baseKey = Key.parse('A')!;
7+
8+
for (let i = 0; i < 12; i += 1) {
9+
keys.add(baseKey.transpose(i).toString());
10+
keys.add(baseKey.transpose(i).useModifier('#').toString());
11+
keys.add(baseKey.transpose(i).useModifier('b').toString());
12+
keys.add(baseKey.transpose(i).toNumeralString(baseKey));
13+
keys.add(baseKey.transpose(i).useModifier('#').toNumeralString(baseKey));
14+
keys.add(baseKey.transpose(i).useModifier('b').toNumeralString(baseKey));
15+
keys.add(baseKey.transpose(i).toNumericString(baseKey));
16+
keys.add(baseKey.transpose(i).useModifier('#').toNumericString(baseKey));
17+
keys.add(baseKey.transpose(i).useModifier('b').toNumericString(baseKey));
18+
}
319

420
describe('Chord', () => {
521
describe('#parse', () => {
6-
const base = 'Eb';
7-
8-
Object
9-
.keys(SUFFIX_MAPPING)
10-
.filter((suffix) => suffix !== '[blank]')
11-
.forEach((suffix) => {
12-
const chord = `${base}${suffix}`;
22+
keys.forEach((base) => {
23+
Object
24+
.keys(SUFFIX_MAPPING)
25+
.filter((suffix) => suffix !== '[blank]')
26+
.forEach((suffix) => {
27+
const chord = `${base}${suffix}`;
1328

14-
it(`parses ${chord}`, () => {
15-
expect(Chord.parse(chord)?.toString()).toEqual(chord);
29+
it(`parses ${chord}`, () => {
30+
expect(Chord.parseOrFail(chord).toString()).toEqual(chord);
31+
});
1632
});
17-
});
33+
});
1834
});
1935
});

unibuild.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unibuild((u: Builder) => {
5252
});
5353

5454
const chordParser = u.asset('chordParser', {
55-
input: ['src/parser/chord/base_grammar.pegjs', chordSuffixGrammar],
55+
input: ['src/parser/chord/base_grammar.pegjs', 'src/parser/chord/simple_suffix_grammar.pegjs'],
5656
outfile: 'src/parser/chord/peg_parser.ts',
5757
build: ({ release }: BuildOptions, baseGrammar: string, suffixGrammar: string) => (
5858
peggyGenerate(`${baseGrammar}\n\n${suffixGrammar}`, release)

0 commit comments

Comments
 (0)