move sheet space parsing to whole string parsing, add parse until eof

This commit is contained in:
Breval Ferrari 2024-10-31 13:31:28 -04:00
parent c5a1540e0f
commit 2038ba2893
No known key found for this signature in database
GPG key ID: 6FED68D87C479A59
2 changed files with 65 additions and 57 deletions

View file

@ -6,7 +6,13 @@ use bng_macros::{QuickModifierParser, SlopeModifierParser};
use derive_new::new; use derive_new::new;
use derived_deref::Deref; use derived_deref::Deref;
use lex::lexer::flat_atom_parser; use lex::lexer::flat_atom_parser;
use nom::multi::many0; use nom::{
branch::alt,
character::complete::char,
combinator::eof,
multi::many0,
sequence::{preceded, terminated},
};
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
use serde::Serialize; use serde::Serialize;
use serde::{ use serde::{
@ -98,7 +104,13 @@ impl<'de> Deserialize<'de> for Atoms {
const FIELDS: &[&str] = &["notes", "sheet"]; const FIELDS: &[&str] = &["notes", "sheet"];
let NotesSheet { notes, sheet } = let NotesSheet { notes, sheet } =
deserializer.deserialize_struct("NotesSheet", FIELDS, NotesSheetVisitor)?; deserializer.deserialize_struct("NotesSheet", FIELDS, NotesSheetVisitor)?;
let x = many0(flat_atom_parser(&notes))(&sheet) let x = terminated(
many0(preceded(
many0(alt((char('\t'), char(' '), char('\n'), char('\r')))),
flat_atom_parser(&notes),
)),
eof,
)(&sheet)
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map_err(AtomsSerializeError::Parsing) .map_err(AtomsSerializeError::Parsing)
.map_err(de::Error::custom) .map_err(de::Error::custom)

View file

@ -39,8 +39,6 @@ impl Parse for Modifier {
} }
pub fn flat_atom_parser(notes: &str) -> impl Parser<&str, FlatAtom, nom::error::Error<&str>> { pub fn flat_atom_parser(notes: &str) -> impl Parser<&str, FlatAtom, nom::error::Error<&str>> {
preceded(
many0(alt((char('\t'), char(' '), char('\n'), char('\r')))),
alt(( 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)), value(FlatAtom::Rest, char(Atom::REST)),
@ -68,9 +66,8 @@ pub fn flat_atom_parser(notes: &str) -> impl Parser<&str, FlatAtom, nom::error::
SlopeModifier::parse, SlopeModifier::parse,
char(' '), char(' '),
map_res(take_till1(|c| c == ','), |s: &str| { map_res(take_till1(|c| c == ','), |s: &str| {
s.parse().map_err(|_| { s.parse()
nom::error::Error::new(s, nom::error::ErrorKind::Verify) .map_err(|_| nom::error::Error::new(s, nom::error::ErrorKind::Verify))
})
}), }),
), ),
), ),
@ -86,6 +83,5 @@ pub fn flat_atom_parser(notes: &str) -> impl Parser<&str, FlatAtom, nom::error::
char(Atom::COMMENT.1), char(Atom::COMMENT.1),
), ),
), ),
)), ))
)
} }