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;
|
2023-04-15 17:08:22 -04:00
|
|
|
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;
|
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-04-15 16:53:58 -04:00
|
|
|
use super::util::get_consumed;
|
2023-04-22 21:45:18 -04:00
|
|
|
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;
|
2023-04-18 22:10:44 -04:00
|
|
|
use crate::parser::util::immediate_in_section;
|
2023-04-15 16:34:33 -04:00
|
|
|
use crate::parser::util::start_of_line;
|
|
|
|
use crate::parser::Comment;
|
2023-04-15 16:31:38 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
pub fn comment<'r, 's>(
|
2023-09-02 19:16:44 -04:00
|
|
|
context: RefContext<'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Comment<'s>> {
|
2023-04-18 22:10:44 -04:00
|
|
|
if immediate_in_section(context, "comment") {
|
|
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
2023-08-23 00:30:26 -04:00
|
|
|
"Cannot nest objects of the same element".into(),
|
2023-04-18 22:10:44 -04:00
|
|
|
))));
|
|
|
|
}
|
|
|
|
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);
|
2023-04-21 18:42:31 -04:00
|
|
|
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);
|
2023-08-23 00:30:26 -04:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Comment {
|
|
|
|
source: source.into(),
|
|
|
|
},
|
|
|
|
))
|
2023-04-15 16:34:33 -04:00
|
|
|
}
|
2023-04-15 16:37:25 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 00:30:26 -04:00
|
|
|
fn comment_line<'r, 's>(
|
2023-09-02 19:16:44 -04:00
|
|
|
_context: RefContext<'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-08-24 19:29:00 -04:00
|
|
|
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 16:37:25 -04:00
|
|
|
}
|
2023-04-15 17:04:47 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-04-22 21:45:18 -04:00
|
|
|
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
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|