Skip to content

Commit

Permalink
parser: allow totally empty comments
Browse files Browse the repository at this point in the history
This is important for txn body comments
as some editors removes spaces at the end of line

Signed-off-by: 35V LG84 <[email protected]>
  • Loading branch information
35VLG84 committed Jan 19, 2025
1 parent 1692219 commit 5ab37d8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tackler-core/src/parser/parts/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@
*/

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};

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
34 changes: 34 additions & 0 deletions tackler-core/src/parser/parts/txn_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:*/));
}
}
}

0 comments on commit 5ab37d8

Please sign in to comment.