divide atom parser into sub-parsers with context
This commit is contained in:
parent
900b8b198e
commit
9c910b8076
1 changed files with 160 additions and 45 deletions
|
@ -16,8 +16,6 @@ use nom::{
|
|||
Err, IResult, Parser,
|
||||
};
|
||||
|
||||
use crate::bng::score::{modifier, quick_modifier, slope_modifier};
|
||||
|
||||
use super::{
|
||||
super::super::Expression as Instruction, Atom, FlatAtom, Modifier, QuickModifier, SlopeModifier,
|
||||
};
|
||||
|
@ -45,21 +43,61 @@ where
|
|||
))(i)
|
||||
}
|
||||
|
||||
fn atom<'a, E>(notes: &'a str) -> impl Parser<&'a str, FlatAtom, E>
|
||||
fn note<'a, E>(notes: &'a str) -> impl Parser<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str>
|
||||
+ ContextError<&'a str>
|
||||
+ FromExternalError<&'a str, TryFromIntError>
|
||||
+ FromExternalError<&'a str, E>,
|
||||
E: ParseError<&'a str> + ContextError<&'a str> + FromExternalError<&'a str, TryFromIntError>,
|
||||
{
|
||||
context(
|
||||
"atom",
|
||||
alt((
|
||||
"note",
|
||||
map_res(map_opt(one_of(notes), |c| notes.find(c)), u8::try_from).map(FlatAtom::Note),
|
||||
value(FlatAtom::Rest, char(Atom::REST)),
|
||||
)
|
||||
}
|
||||
|
||||
fn rest<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context("rest", value(FlatAtom::Rest, char(Atom::REST)))(i)
|
||||
}
|
||||
|
||||
fn start_here<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"start_here",
|
||||
value(FlatAtom::StartHere, char(Atom::START_HERE)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn modifier<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
use super::super::modifier;
|
||||
context(
|
||||
"modifier",
|
||||
preceded(char(Atom::MODIFIER), modifier).map(FlatAtom::Modifier),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn quick_modifier<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
use super::super::quick_modifier;
|
||||
context(
|
||||
"quick_modifier",
|
||||
quick_modifier.map(FlatAtom::QuickModifier),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn loop_starts<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"loop_starts",
|
||||
preceded(
|
||||
char(Atom::LOOP.0),
|
||||
map_opt(opt(u8), |n| {
|
||||
|
@ -71,9 +109,43 @@ where
|
|||
}),
|
||||
)
|
||||
.map(FlatAtom::LoopStarts),
|
||||
value(FlatAtom::LoopEnds, char(Atom::LOOP.1)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn loop_ends<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context("loop_ends", value(FlatAtom::LoopEnds, char(Atom::LOOP.1)))(i)
|
||||
}
|
||||
|
||||
fn tuple_starts<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"tuple_starts",
|
||||
value(FlatAtom::TupleStarts, char(Atom::TUPLE.0)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn tuple_ends<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"tuple_ends",
|
||||
value(FlatAtom::TupleEnds, char(Atom::TUPLE.1)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn slope_starts<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str> + FromExternalError<&'a str, E>,
|
||||
{
|
||||
use super::super::slope_modifier;
|
||||
context(
|
||||
"slope_starts",
|
||||
terminated(
|
||||
preceded(
|
||||
char(Atom::SLOPE.0),
|
||||
|
@ -89,7 +161,25 @@ where
|
|||
char(','),
|
||||
)
|
||||
.map(|(sm, i)| FlatAtom::SlopeStarts(sm, i)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn slope_ends<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"slope_ends",
|
||||
value(FlatAtom::SlopeEnds, char(Atom::SLOPE.1)),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn comment<'a, E>(i: &'a str) -> IResult<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str> + ContextError<&'a str>,
|
||||
{
|
||||
context(
|
||||
"comment",
|
||||
value(
|
||||
FlatAtom::Comment,
|
||||
delimited(
|
||||
|
@ -98,6 +188,31 @@ where
|
|||
char(Atom::COMMENT.1),
|
||||
),
|
||||
),
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn atom<'a, E>(notes: &'a str) -> impl Parser<&'a str, FlatAtom, E>
|
||||
where
|
||||
E: ParseError<&'a str>
|
||||
+ ContextError<&'a str>
|
||||
+ FromExternalError<&'a str, TryFromIntError>
|
||||
+ FromExternalError<&'a str, E>,
|
||||
{
|
||||
context(
|
||||
"atom",
|
||||
alt((
|
||||
note(notes),
|
||||
rest,
|
||||
start_here,
|
||||
modifier,
|
||||
quick_modifier,
|
||||
loop_starts,
|
||||
loop_ends,
|
||||
tuple_starts,
|
||||
tuple_ends,
|
||||
slope_starts,
|
||||
slope_ends,
|
||||
comment,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue