From 5ab37d8bbd15c115b393bec6d38c0d8ff69172d2 Mon Sep 17 00:00:00 2001 From: 35V LG84 <35vlg84-x4e6b92@e257.fi> Date: Sun, 19 Jan 2025 12:08:35 +0200 Subject: [PATCH] parser: allow totally empty comments This is important for txn body comments as some editors removes spaces at the end of line Signed-off-by: 35V LG84 <35vlg84-x4e6b92@e257.fi> --- tackler-core/src/parser/parts/comment.rs | 22 ++++++++++--- tackler-core/src/parser/parts/txn_comment.rs | 34 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/tackler-core/src/parser/parts/comment.rs b/tackler-core/src/parser/parts/comment.rs index c8465a2..024ef47 100644 --- a/tackler-core/src/parser/parts/comment.rs +++ b/tackler-core/src/parser/parts/comment.rs @@ -5,7 +5,8 @@ */ use crate::parser::Stream; -use winnow::ascii::till_line_ending; +use winnow::ascii::{line_ending, till_line_ending}; +use winnow::combinator::{alt, peek}; use winnow::stream::AsChar; use winnow::token::one_of; use winnow::{seq, PResult, Parser}; @@ -13,10 +14,21 @@ use winnow::{seq, PResult, Parser}; pub(crate) fn p_comment<'s>(is: &mut Stream<'s>) -> PResult<&'s str> { let m = seq!( _: ';', - // this can not be space1 as we must preserve space for equity and identity reports - _: one_of(AsChar::is_space), - till_line_ending, + alt(( + seq!( + // this can not be space1 as we must preserve space for equity and identity reports + _: one_of(AsChar::is_space), + till_line_ending + ), + // allow totally empty comment ";\n" - this is important for + // txn body comments as some editors removes spaces at the end of line + peek(line_ending).map(|_| {("",)}) + )).map(|x| x.0), ) + .map(|x| x.0) .parse_next(is)?; - Ok(m.0) + Ok(m) } + +// The line_end handling must work with outer contex, +// so for testing, see txn_comment.rs diff --git a/tackler-core/src/parser/parts/txn_comment.rs b/tackler-core/src/parser/parts/txn_comment.rs index 1c4c201..0e4c361 100644 --- a/tackler-core/src/parser/parts/txn_comment.rs +++ b/tackler-core/src/parser/parts/txn_comment.rs @@ -18,3 +18,37 @@ pub(crate) fn parse_txn_comment<'s>(is: &mut Stream<'s>) -> PResult<&'s str> { .parse_next(is)?; Ok(m.0) } +#[cfg(test)] +mod tests { + use super::*; + use crate::kernel::Settings; + + #[test] + fn test_txn_comment() { + let comments = vec![ + (" ; comment\n", "comment"), + ("\t;\tcomment\n", "comment"), + (" \t; \tcomment\n", "\tcomment"), + ("\t ;\t comment\n", " comment"), + (" ; comment \n", "comment "), + ("\t;\tcomment\t\n", "comment\t"), + ("\t ;\n", ""), + (" ; \n", " "), + (" ;\t\t\n", "\t"), + (" ; \t \n", "\t "), + ]; + let mut settings = Settings::default(); + + for c in comments { + let mut is = Stream { + input: c.0, + state: &mut settings, + }; + + let res = parse_txn_comment(&mut is); + + assert!(res.is_ok()); + assert_eq!(c.1, res.unwrap(/*:test:*/)); + } + } +}