diff --git a/src/parser.rs b/src/parser.rs index 139db70..0700e20 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,12 +8,15 @@ use fasteval::Evaler; use nom::{ AsBytes, AsChar, Compare, FindToken, Finish, IResult, Input, Offset, Parser as NomParser, branch::alt, - bytes::complete::{tag, take}, - character::{complete::char, streaming::one_of}, + bytes::complete::{tag, take, take_till}, + character::{ + complete::{char, space1}, + streaming::one_of, + }, combinator::value, error::{Error, ErrorKind}, multi::many0, - sequence::preceded, + sequence::{delimited, preceded}, }; use nom_locate::LocatedSpan; @@ -41,12 +44,22 @@ where pub fn parse_all( &self, ) -> Result>, Error>> { - many0(alt(( - Silence::parser().map(into_box), - Marker::parser().map(into_box), - Note::parser(self.notes).map(into_box), - VariableChange::parser::, C>(self.variables).map(into_box), - ))) + let space_or_comment = || { + value( + (), + many0(value((), char('#').and(take_till(|c| c == '\n'))).or(value((), space1))), + ) + }; + many0(delimited( + space_or_comment(), + alt(( + Silence::parser().map(into_box), + Marker::parser().map(into_box), + Note::parser(self.notes).map(into_box), + VariableChange::parser::, C>(self.variables).map(into_box), + )), + space_or_comment(), + )) .parse_complete(LocatedSpan::new(self.input)) .finish() .map(|(_, o)| o)