replace Token trait with consts
This commit is contained in:
parent
040bb4ecb3
commit
9e0ead7605
3 changed files with 60 additions and 60 deletions
|
@ -1,11 +1,11 @@
|
|||
use super::{Atom, Modifier, QuickModifier, SlopeModifier};
|
||||
use super::{Atom, FlatAtom, Modifier, QuickModifier, SlopeModifier};
|
||||
|
||||
pub(super) mod lexer;
|
||||
|
||||
const MORE: bool = true;
|
||||
const LESS: bool = false;
|
||||
const ON: bool = true;
|
||||
const OFF: bool = false;
|
||||
pub(super) const MORE: bool = true;
|
||||
pub(super) const LESS: bool = false;
|
||||
pub(super) const ON: bool = true;
|
||||
pub(super) const OFF: bool = false;
|
||||
|
||||
struct WrappingTokens;
|
||||
impl WrappingTokens {
|
||||
|
@ -19,51 +19,34 @@ impl WrappingTokens {
|
|||
const QUOTE_DEG: (char, char) = ('\'', '°');
|
||||
}
|
||||
|
||||
pub(super) trait Token<T> {
|
||||
fn token(self) -> T;
|
||||
impl QuickModifier {
|
||||
pub(super) const VOLUME: (char, char) = WrappingTokens::PLUS_MINUS;
|
||||
pub(super) const OCTAVE: (char, char) = WrappingTokens::RIGHT_LEFT;
|
||||
pub(super) const LENGTH: (char, char) = WrappingTokens::SLASH_BACKSLASH;
|
||||
pub(super) const PIZZ: (char, char) = WrappingTokens::QUOTE_DEG;
|
||||
}
|
||||
|
||||
impl Token<(char, char)> for QuickModifier {
|
||||
fn token(self) -> (char, char) {
|
||||
match self {
|
||||
Self::Volume(_) => WrappingTokens::PLUS_MINUS,
|
||||
Self::Octave(_) => WrappingTokens::RIGHT_LEFT,
|
||||
Self::Length(_) => WrappingTokens::SLASH_BACKSLASH,
|
||||
Self::Pizz(_) => WrappingTokens::QUOTE_DEG,
|
||||
}
|
||||
}
|
||||
impl Modifier {
|
||||
pub(super) const VOLUME: char = 'v';
|
||||
pub(super) const OCTAVE: char = 'o';
|
||||
pub(super) const LENGTH: char = 'l';
|
||||
pub(super) const TEMPO: char = 't';
|
||||
}
|
||||
|
||||
impl Token<char> for Modifier {
|
||||
fn token(self) -> char {
|
||||
match self {
|
||||
Self::Volume(_) => 'v',
|
||||
Self::Octave(_) => 'o',
|
||||
Self::Length(_) => 'l',
|
||||
Self::Tempo(_) => 't',
|
||||
}
|
||||
}
|
||||
impl SlopeModifier {
|
||||
pub(super) const NOTE: char = 'n';
|
||||
pub(super) const VOLUME: char = 'v';
|
||||
pub(super) const OCTAVE: char = 'o';
|
||||
pub(super) const LENGTH: char = 'l';
|
||||
pub(super) const TEMPO: char = 't';
|
||||
}
|
||||
|
||||
impl Token<char> for SlopeModifier {
|
||||
fn token(self) -> char {
|
||||
match self {
|
||||
Self::Note => 'n',
|
||||
Self::Volume => 'v',
|
||||
Self::Octave => 'o',
|
||||
Self::Length => 'l',
|
||||
Self::Tempo => 't',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Token<char> for Atom {
|
||||
fn token(self) -> char {
|
||||
match self {
|
||||
Atom::Rest => '.',
|
||||
Atom::StartHere => ':',
|
||||
Atom::Modifier(_) => '!',
|
||||
_ => unimplemented!("not a singleton"),
|
||||
}
|
||||
}
|
||||
impl Atom {
|
||||
pub(super) const REST: char = '.';
|
||||
pub(super) const START_HERE: char = ':';
|
||||
pub(super) const MODIFIER: char = '!';
|
||||
pub(super) const LOOP: (char, char) = WrappingTokens::PARENTHESES;
|
||||
pub(super) const TUPLE: (char, char) = WrappingTokens::SQUARE_BRACKETS;
|
||||
pub(super) const SLOPE: (char, char) = WrappingTokens::BRACKETS;
|
||||
pub(super) const COMMENT: (char, char) = WrappingTokens::SEMICOLON_COMMA;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,25 @@
|
|||
use nom::{
|
||||
branch::alt, character::complete::char, combinator::value, sequence::delimited, IResult, Parser,
|
||||
branch::alt,
|
||||
character::complete::{char, one_of, u8},
|
||||
combinator::value,
|
||||
multi::many0,
|
||||
sequence::{delimited, pair, preceded},
|
||||
Err, IResult, Parser,
|
||||
};
|
||||
|
||||
use crate::bng::score::QuickModifier;
|
||||
|
||||
use super::Token;
|
||||
use crate::bng::score::{Atom, FlatAtom, Modifier, QuickModifier};
|
||||
|
||||
pub(crate) trait Parse: Sized {
|
||||
fn parse(input: &str) -> IResult<&str, Self>;
|
||||
}
|
||||
|
||||
fn atom_parser<'a>(notes: &'a str) -> impl Parser<&str, FlatAtom, nom::error::Error<&str>> {
|
||||
alt((
|
||||
one_of(notes).map(FlatAtom::Note),
|
||||
value(FlatAtom::Rest, char(Atom::REST)),
|
||||
value(FlatAtom::StartHere, char(Atom::START_HERE)),
|
||||
preceded(char(Atom::MODIFIER), Modifier::parse).map(FlatAtom::Modifier),
|
||||
QuickModifier::parse.map(FlatAtom::QuickModifier),
|
||||
preceded(char(Atom::LOOP.0), u8).map(FlatAtom::LoopStarts),
|
||||
))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue