Compare commits
No commits in common. "9c910b80764e9450c026de14455a0fc926d8c7b4" and "315238f14d914babefbd731524c26fe8d1e78b8b" have entirely different histories.
9c910b8076
...
315238f14d
4 changed files with 71 additions and 195 deletions
|
@ -21,7 +21,6 @@ bng_macros = { path = "bng_macros" }
|
||||||
const_format = "0.2.33"
|
const_format = "0.2.33"
|
||||||
thiserror = "1.0.64"
|
thiserror = "1.0.64"
|
||||||
derive-new = "0.7.0"
|
derive-new = "0.7.0"
|
||||||
naan = "0.1.32"
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["play", "save"]
|
default = ["play", "save"]
|
||||||
|
|
|
@ -8,6 +8,7 @@ use anyhow::Context;
|
||||||
use bng_macros::{ModifierParser, QuickModifierParser, SlopeModifierParser};
|
use bng_macros::{ModifierParser, QuickModifierParser, SlopeModifierParser};
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use derived_deref::Deref;
|
use derived_deref::Deref;
|
||||||
|
use lex::lexer::atom;
|
||||||
use nom::{
|
use nom::{
|
||||||
branch::alt,
|
branch::alt,
|
||||||
character::{complete::char, streaming::one_of},
|
character::{complete::char, streaming::one_of},
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
num::TryFromIntError,
|
num::TryFromIntError,
|
||||||
ops::Deref,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use naan::fun::{F1Once, F2Once};
|
|
||||||
use nom::{
|
use nom::{
|
||||||
character::complete::one_of,
|
character::complete::one_of,
|
||||||
combinator::all_consuming,
|
combinator::all_consuming,
|
||||||
|
@ -20,7 +18,7 @@ use serde::{
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::bng::score::lex::lexer::root;
|
use crate::bng::score::lex::lexer::atom;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
utils::{inflate, InflateError},
|
utils::{inflate, InflateError},
|
||||||
|
@ -56,7 +54,11 @@ impl<'de> Deserialize<'de> for Atoms {
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
} else {
|
} else {
|
||||||
root(&sheet, ¬es)
|
root(&sheet, ¬es)
|
||||||
.map_err(|e| pretty_verbose_err.curry().call1(sheet.as_str()).call1(e))
|
.map_err(|e: nom::Err<VerboseError<&str>>| match e {
|
||||||
|
nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(),
|
||||||
|
nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n),
|
||||||
|
nom::Err::Error(e) | nom::Err::Failure(e) => convert_error(sheet.as_str(), e),
|
||||||
|
})
|
||||||
.map_err(AtomsSerializeError::Parsing)
|
.map_err(AtomsSerializeError::Parsing)
|
||||||
.map_err(de::Error::custom)
|
.map_err(de::Error::custom)
|
||||||
.and_then(|(_, v)| {
|
.and_then(|(_, v)| {
|
||||||
|
@ -69,13 +71,22 @@ impl<'de> Deserialize<'de> for Atoms {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pretty_verbose_err<I>(input: I, e: nom::Err<VerboseError<I>>) -> String
|
fn maybe_yml_str_space<'a, E>() -> impl Parser<&'a str, Vec<char>, E>
|
||||||
where
|
where
|
||||||
I: Deref<Target = str>,
|
E: nom::error::ParseError<&'a str> + ContextError<&'a str>,
|
||||||
{
|
{
|
||||||
match e {
|
context("yml white space", many0(one_of(" \t\r\n")))
|
||||||
nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(),
|
|
||||||
nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n),
|
|
||||||
nom::Err::Error(e) | nom::Err::Failure(e) => convert_error(input, e),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn root<'a, E>(i: &'a str, notes: &'a str) -> IResult<&'a str, Vec<FlatAtom>, E>
|
||||||
|
where
|
||||||
|
E: ParseError<&'a str>
|
||||||
|
+ ContextError<&'a str>
|
||||||
|
+ FromExternalError<&'a str, TryFromIntError>
|
||||||
|
+ FromExternalError<&'a str, E>,
|
||||||
|
{
|
||||||
|
all_consuming(terminated(
|
||||||
|
many1(preceded(maybe_yml_str_space(), atom(notes))),
|
||||||
|
maybe_yml_str_space(),
|
||||||
|
))(i)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,15 @@ use nom::{
|
||||||
branch::alt,
|
branch::alt,
|
||||||
bytes::complete::{take_till, take_till1},
|
bytes::complete::{take_till, take_till1},
|
||||||
character::complete::{anychar, char, one_of, space1, u16, u8},
|
character::complete::{anychar, char, one_of, space1, u16, u8},
|
||||||
combinator::{all_consuming, map_opt, map_res, opt, value, verify},
|
combinator::{map_opt, map_res, opt, value, verify},
|
||||||
error::{context, ContextError, ErrorKind, FromExternalError, ParseError},
|
error::{context, ContextError, ErrorKind, FromExternalError, ParseError},
|
||||||
multi::{many0, many1},
|
multi::many0,
|
||||||
sequence::{delimited, pair, preceded, separated_pair, terminated},
|
sequence::{delimited, pair, preceded, separated_pair, terminated},
|
||||||
Err, IResult, Parser,
|
Err, IResult, Parser,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::bng::score::{modifier, quick_modifier, slope_modifier};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
super::super::Expression as Instruction, Atom, FlatAtom, Modifier, QuickModifier, SlopeModifier,
|
super::super::Expression as Instruction, Atom, FlatAtom, Modifier, QuickModifier, SlopeModifier,
|
||||||
};
|
};
|
||||||
|
@ -23,81 +25,21 @@ use super::{
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
fn maybe_yml_str_space<'a, E>() -> impl Parser<&'a str, Vec<char>, E>
|
pub fn atom<'a, E>(notes: &'a str) -> impl Parser<&'a str, FlatAtom, E>
|
||||||
where
|
|
||||||
E: nom::error::ParseError<&'a str> + ContextError<&'a str>,
|
|
||||||
{
|
|
||||||
context("yml white space", many0(one_of(" \t\r\n")))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root<'a, E>(i: &'a str, notes: &'a str) -> IResult<&'a str, Vec<FlatAtom>, E>
|
|
||||||
where
|
where
|
||||||
E: ParseError<&'a str>
|
E: ParseError<&'a str>
|
||||||
+ ContextError<&'a str>
|
+ ContextError<&'a str>
|
||||||
+ FromExternalError<&'a str, TryFromIntError>
|
+ FromExternalError<&'a str, TryFromIntError>
|
||||||
+ FromExternalError<&'a str, E>,
|
+ FromExternalError<&'a str, E>,
|
||||||
{
|
|
||||||
all_consuming(terminated(
|
|
||||||
many1(preceded(maybe_yml_str_space(), atom(notes))),
|
|
||||||
maybe_yml_str_space(),
|
|
||||||
))(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
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>,
|
|
||||||
{
|
{
|
||||||
context(
|
context(
|
||||||
"note",
|
"atom",
|
||||||
|
alt((
|
||||||
map_res(map_opt(one_of(notes), |c| notes.find(c)), u8::try_from).map(FlatAtom::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)),
|
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),
|
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),
|
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(
|
preceded(
|
||||||
char(Atom::LOOP.0),
|
char(Atom::LOOP.0),
|
||||||
map_opt(opt(u8), |n| {
|
map_opt(opt(u8), |n| {
|
||||||
|
@ -109,43 +51,9 @@ where
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.map(FlatAtom::LoopStarts),
|
.map(FlatAtom::LoopStarts),
|
||||||
)(i)
|
value(FlatAtom::LoopEnds, char(Atom::LOOP.1)),
|
||||||
}
|
|
||||||
|
|
||||||
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)),
|
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)),
|
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(
|
terminated(
|
||||||
preceded(
|
preceded(
|
||||||
char(Atom::SLOPE.0),
|
char(Atom::SLOPE.0),
|
||||||
|
@ -161,25 +69,7 @@ where
|
||||||
char(','),
|
char(','),
|
||||||
)
|
)
|
||||||
.map(|(sm, i)| FlatAtom::SlopeStarts(sm, i)),
|
.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)),
|
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(
|
value(
|
||||||
FlatAtom::Comment,
|
FlatAtom::Comment,
|
||||||
delimited(
|
delimited(
|
||||||
|
@ -188,31 +78,6 @@ where
|
||||||
char(Atom::COMMENT.1),
|
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…
Add table
Add a link
Reference in a new issue