organic/src/parser/comment.rs

94 lines
2.9 KiB
Rust
Raw Normal View History

2023-04-15 16:59:30 -04:00
use nom::branch::alt;
2023-04-15 16:53:58 -04:00
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
2023-04-15 16:59:30 -04:00
use nom::character::complete::line_ending;
2023-04-15 16:53:58 -04:00
use nom::character::complete::space0;
use nom::character::complete::space1;
2023-04-15 16:59:30 -04:00
use nom::combinator::eof;
2023-04-15 16:53:58 -04:00
use nom::combinator::not;
2023-04-15 16:59:30 -04:00
use nom::combinator::opt;
2023-04-15 16:53:58 -04:00
use nom::multi::many0;
use nom::sequence::preceded;
use nom::sequence::tuple;
use super::org_source::OrgSource;
2023-04-15 16:53:58 -04:00
use super::util::get_consumed;
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
2023-04-15 16:53:58 -04:00
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::immediate_in_section;
use crate::parser::util::start_of_line;
use crate::parser::Comment;
2023-08-10 20:04:59 -04:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn comment<'r, 's>(
2023-09-02 19:16:44 -04:00
context: RefContext<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Comment<'s>> {
if immediate_in_section(context, "comment") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
))));
}
let parser_context = context.with_additional_node(ContextElement::Context("comment"));
let comment_line_matcher = parser_with_context!(comment_line)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, _first_line) = comment_line_matcher(input)?;
let (remaining, _remaining_lines) =
2023-04-15 16:53:58 -04:00
many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?;
2023-04-15 16:59:30 -04:00
2023-04-15 16:53:58 -04:00
let source = get_consumed(input, remaining);
Ok((
remaining,
Comment {
source: source.into(),
},
))
}
2023-08-10 20:04:59 -04:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn comment_line<'r, 's>(
2023-09-02 19:16:44 -04:00
_context: RefContext<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
2023-04-15 16:53:58 -04:00
let (remaining, _indent) = space0(input)?;
2023-04-21 14:35:34 -04:00
let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple((
tag("#"),
opt(tuple((space1, is_not("\r\n")))),
alt((line_ending, eof)),
))(remaining)?;
2023-04-15 16:53:58 -04:00
let source = get_consumed(input, remaining);
Ok((remaining, source))
}
2023-04-15 17:04:47 -04:00
#[cfg(test)]
mod tests {
use super::*;
2023-04-15 17:04:47 -04:00
use crate::parser::parser_context::ContextTree;
use crate::parser::parser_with_context::parser_with_context;
#[test]
fn require_space_after_hash() {
2023-08-24 17:15:24 -04:00
let input = OrgSource::new(
"# Comment line
2023-04-15 17:04:47 -04:00
#not a comment
2023-08-24 17:15:24 -04:00
# Comment again",
);
2023-04-15 17:04:47 -04:00
let initial_context: ContextTree<'_, '_> = ContextTree::new();
2023-08-24 17:15:24 -04:00
let comment_matcher = parser_with_context!(comment)(&initial_context);
2023-04-21 14:35:34 -04:00
let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment");
assert_eq!(
2023-08-24 17:15:24 -04:00
Into::<&str>::into(remaining),
2023-04-21 14:35:34 -04:00
r#"#not a comment
# Comment again"#
);
2023-04-15 17:04:47 -04:00
assert_eq!(
first_comment.source,
"# Comment line
"
);
}
}