Last active
July 17, 2022 05:51
-
-
Save ThinaticSystem/385a2eb5540baee0426a7ef54d8852b6 to your computer and use it in GitHub Desktop.
terrario(0.2.0)習作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as P from 'terrario'; | |
const _ = P.option(P.alt([P.str(' '), P.str(' ')]).many(1)); | |
const degreeNatural = P.alt([ | |
P.str('Ⅰ'), | |
P.str('Ⅱ'), | |
P.str('Ⅲ'), | |
P.str('Ⅳ'), | |
P.str('Ⅴ'), | |
P.str('Ⅵ'), | |
P.str('Ⅶ'), | |
]); | |
const toneNatural = P.alt([ | |
P.str('C'), | |
P.str('D'), | |
P.str('E'), | |
P.str('F'), | |
P.str('G'), | |
P.str('A'), | |
P.str('B'), | |
]); | |
const accidental = P.alt([ | |
P.str('♯'), | |
P.str('♭'), | |
P.str('𝄪'), | |
P.str('𝄫'), | |
]); | |
const note = P.alt([ | |
P.seq([toneNatural, P.option(accidental)]).map(v => (v[1]) ? v[0] + v[1] : v[0]), | |
P.seq([P.option(accidental), degreeNatural]).map(v => (v[0]) ? v[0] + v[1] : v[1]), | |
]); | |
const triadNotSus4 = P.alt([ | |
P.str('m'), | |
P.str('dim'), | |
P.str('aug'), | |
]); | |
const triadSus4 = P.str('sus4'); | |
const tetrad = P.alt([ | |
P.str('7').map(() => 'min7'), | |
P.str('Maj7'), | |
P.str('6'), | |
]); | |
const chordBasic = P.seq([note, P.option(triadNotSus4), P.option(tetrad), P.option(triadSus4)]).map(v => { | |
return { root: v[0], triad: (v[3] ?? v[1]) ?? 'Maj', tetrad: v[2] }; | |
}); | |
const tension = P.alt([ | |
P.str('9'), | |
P.str('♭9'), | |
P.str('♯9'), | |
P.str('11'), | |
P.str('♯11'), | |
P.str('13'), | |
P.str('♭13'), | |
P.str('Maj7'), | |
]); | |
const bass = P.seq([P.str('/'), note], 1); | |
const chord = P.seq([ | |
_, | |
chordBasic, | |
P.option( | |
P.alt([ | |
P.seq([ | |
_, P.str('('), tension.sep1(P.seq([P.str(','), _])), P.str(')') | |
], 2), | |
P.seq([ | |
P.str('add'), tension | |
], 1), | |
]) | |
), | |
_, | |
P.option(bass), | |
_, | |
]).map(v => {v[1].tensions = v[2]; v[1].bass = v[4]; return v[1]}); | |
[ | |
'C', ' Eaug ', 'D♭m7 ', 'GmMaj7 ', | |
'Ⅰ', ' Ⅲaug ', ' ♭Ⅱm7', ' ⅤmMaj7 ', | |
'Ⅴadd♯11', 'Ⅰ(9)', 'Ⅲaug(9,11)', '♭Ⅱm7(♭9,♯11,♭13)', 'ⅤmMaj7(♭13)', | |
'Ⅰ7sus4(9, 13)/♭Ⅶ', 'Ⅲaug(♯11, 13) /♯Ⅳ', 'C♯m7/D', | |
].forEach(input => { | |
const result = chord.handler(input, 0, {}); | |
if (!result.success) { | |
throw new Error("couldn't parse"); | |
} | |
console.log(input, '=>', result.value, '\n'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment