diff --git a/go-sgf/src/lib.rs b/go-sgf/src/lib.rs index e40dd6e..bde52ac 100644 --- a/go-sgf/src/lib.rs +++ b/go-sgf/src/lib.rs @@ -69,8 +69,8 @@ use nom::{ branch::alt, - bytes::complete::{escaped_transform, is_not, tag}, - character::complete::{alpha1, digit1, multispace0, multispace1}, + bytes::complete::{escaped, escaped_transform, is_not, tag}, + character::complete::{alpha1, digit1, multispace0, multispace1, none_of, one_of}, combinator::{opt, value}, multi::{many0, many1, separated_list1}, sequence::delimited, @@ -395,9 +395,13 @@ fn parse_propval_text<'a, E: nom::error::ParseError<&'a str>>( input: &'a str, ) -> IResult<&'a str, Option, E> { let (input, value) = opt(escaped_transform( - is_not(r"\]"), + none_of("\\]"), '\\', - value("]", tag(r"\]")), // alt((value("]", tag(r"\]")), value("", tag("\\\n")))), + alt(( + value("]", tag("]")), + value("\\", tag("\\")), + value("", tag("\n")), + )), ))(input)?; Ok((input, value.map(|v| v.to_owned()))) } @@ -635,29 +639,42 @@ mod tests { #[test] fn it_parses_propvals_with_hard_linebreaks() { - let (_, propval) = parse_propval::>( - "[There are hard linebreaks & soft linebreaks. -Soft linebreaks...]", + let (_, propval) = parse_propval_text::>( + "There are hard linebreaks & soft linebreaks. +Soft linebreaks...", ) .unwrap(); assert_eq!( propval, - "There are hard linebreaks & soft linebreaks. + Some( + "There are hard linebreaks & soft linebreaks. Soft linebreaks..." - .to_owned() + .to_owned() + ) + ); + } + + #[test] + fn it_parses_propvals_with_escaped_closing_brackets() { + let (_, propval) = + parse_propval_text::>(r"escaped closing \] bracket") + .unwrap(); + assert_eq!( + propval, + Some(r"escaped closing ] bracket".to_owned()).to_owned() ); } #[test] fn it_parses_propvals_with_soft_linebreaks() { - let (_, propval) = parse_propval::>( - r"[Soft linebreaks are linebreaks preceeded by '\\' like this one >o\ -k<. Hard line breaks are all other linebreaks.]", + let (_, propval) = parse_propval_text::>( + r"Soft linebreaks are linebreaks preceeded by '\\' like this one >o\ +k<. Hard line breaks are all other linebreaks.", ) .unwrap(); assert_eq!( propval, - r"Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks." + Some("Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks.".to_owned()) .to_owned() ); }