use nom::branch::alt; use nom::bytes::complete::is_not; use nom::bytes::complete::tag; use nom::bytes::complete::tag_no_case; use nom::character::complete::anychar; use nom::character::complete::line_ending; use nom::character::complete::space0; use nom::character::complete::space1; use nom::combinator::consumed; use nom::combinator::eof; use nom::combinator::map; use nom::combinator::opt; use nom::combinator::peek; use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; use nom::multi::separated_list0; use nom::multi::separated_list1; use nom::sequence::tuple; use super::org_source::OrgSource; use crate::context::parser_with_context; use crate::context::ContextElement; use crate::context::ContextMatcher; use crate::context::ExitClass; use crate::context::ExitMatcherNode; use crate::context::RefContext; use crate::error::Res; use crate::parser::object_parser::standard_set_object; use crate::parser::util::blank_line; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::start_of_line; use crate::parser::util::text_until_exit; use crate::types::CharOffsetInLine; use crate::types::CommentBlock; use crate::types::ExampleBlock; use crate::types::ExportBlock; use crate::types::LineNumber; use crate::types::Object; use crate::types::PlainText; use crate::types::RetainLabels; use crate::types::SrcBlock; use crate::types::SwitchNumberLines; use crate::types::VerseBlock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn verse_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, VerseBlock<'s>> { let (remaining, name) = lesser_block_begin("verse")(context, input)?; let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?; let lesser_block_end_specialized = lesser_block_end("verse"); let contexts = [ ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, }), ]; let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, }; let object_matcher = parser_with_context!(standard_set_object)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); // Check for a completely empty block let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) { Ok((remaining, (whitespace, (_children, _exit_contents)))) => ( remaining, vec![Object::PlainText(PlainText { source: whitespace.into(), })], ), Err(_) => { let (remaining, (children, _exit_contents)) = many_till(object_matcher, exit_matcher)(remaining)?; (remaining, children) } }; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, VerseBlock { source: source.into(), name: name.into(), data: parameters.map(|parameters| Into::<&str>::into(parameters)), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn comment_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, CommentBlock<'s>> { let (remaining, _name) = lesser_block_begin("comment")(context, input)?; let (remaining, _parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?; let lesser_block_end_specialized = lesser_block_end("comment"); let contexts = [ ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, }), ]; let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); let (remaining, contents) = parser_with_context!(text_until_exit)(&parser_context)(remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, CommentBlock { source: source.into(), contents: contents.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn example_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, ExampleBlock<'s>> { let (remaining, _name) = lesser_block_begin("example")(context, input)?; let (remaining, parameters) = opt(tuple((space1, example_switches)))(remaining)?; let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?; let lesser_block_end_specialized = lesser_block_end("example"); let contexts = [ ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, }), ]; let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); let parameters = parameters.map(|(_, parameters)| parameters); let (remaining, contents) = content(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); let (switches, number_lines, retain_labels, use_labels, label_format) = { if let Some(parameters) = parameters { ( if parameters.source.len() == 0 { None } else { Some(parameters.source) }, parameters.number_lines, parameters.retain_labels, parameters.use_labels, parameters.label_format, ) } else { (None, None, RetainLabels::Yes, true, None) } }; Ok(( remaining, ExampleBlock { source: source.into(), switches, number_lines, retain_labels, use_labels, label_format, contents, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn export_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, ExportBlock<'s>> { let (remaining, name) = lesser_block_begin("export")(context, input)?; // https://orgmode.org/worg/org-syntax.html#Blocks claims that export blocks must have a single word for data but testing shows no data and multi-word data still parses as an export block. let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?; let lesser_block_end_specialized = lesser_block_end("export"); let contexts = [ ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, }), ]; let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, }; let (remaining, contents) = parser_with_context!(text_until_exit)(&parser_context)(remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, ExportBlock { source: source.into(), name: name.into(), data: parameters.map(|parameters| Into::<&str>::into(parameters)), contents: contents.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn src_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, SrcBlock<'s>> { let (remaining, _name) = lesser_block_begin("src")(context, input)?; // https://orgmode.org/worg/org-syntax.html#Blocks claims that data is mandatory and must follow the LANGUAGE SWITCHES ARGUMENTS pattern but testing has shown that no data and incorrect data here will still parse to a src block. let (remaining, parameters) = opt(tuple((space1, src_switches)))(remaining)?; let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?; let lesser_block_end_specialized = lesser_block_end("src"); let contexts = [ ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, }), ]; let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, }; let (remaining, contents) = content(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); let (switches, language, number_lines, retain_labels, use_labels, label_format) = { if let Some(parameters) = parameters { ( if parameters.source.len() == 0 { None } else { Some(parameters.source) }, parameters.language, parameters.number_lines, parameters.retain_labels, parameters.use_labels, parameters.label_format, ) } else { (None, None, None, RetainLabels::Yes, true, None) } }; Ok(( remaining, SrcBlock { source: source.into(), switches, language, number_lines, retain_labels, use_labels, label_format, contents, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not(" \t\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn data<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } fn lesser_block_end<'c>(current_name: &'c str) -> impl ContextMatcher + 'c { // Since the lesser block names are statically defined in code, we can simply assert that the name is lowercase instead of causing an allocation by converting to lowercase. debug_assert!(current_name == current_name.to_lowercase()); move |context, input: OrgSource<'_>| _lesser_block_end(context, input, current_name) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _lesser_block_end<'b, 'g, 'r, 's, 'c>( _context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, current_name_lower: &'c str, ) -> Res, OrgSource<'s>> { start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _ws, _ending)) = tuple(( tag_no_case("#+end_"), tag_no_case(current_name_lower), space0, alt((eof, line_ending)), ))(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, source)) } /// Parser for the beginning of a lesser block /// /// current_name MUST be lowercase. We do not do the conversion ourselves because it is not allowed in a const fn. const fn lesser_block_begin<'c>(current_name: &'c str) -> impl ContextMatcher + 'c { // TODO: Since this is a const fn, is there ANY way to "generate" functions at compile time? move |context, input: OrgSource<'_>| _lesser_block_begin(context, input, current_name) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>( _context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, current_name_lower: &'c str, ) -> Res, OrgSource<'s>> { start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( tag_no_case("#+begin_"), verify(name, |name: &OrgSource<'_>| { Into::<&str>::into(name).to_lowercase().as_str() == current_name_lower }), ))(remaining)?; Ok((remaining, name)) } #[derive(Debug)] struct ExampleSrcSwitches<'s> { source: &'s str, language: Option<&'s str>, number_lines: Option, retain_labels: RetainLabels, use_labels: bool, label_format: Option<&'s str>, } #[derive(Debug)] enum SwitchState { Normal, NewLineNumber, ContinuedLineNumber, LabelFormat, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn src_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSrcSwitches<'s>> { example_src_switches(true)(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSrcSwitches<'s>> { let (remaining, switches) = example_src_switches(false)(input)?; debug_assert!(switches.language.is_none()); Ok((remaining, switches)) } fn example_src_switches( grab_language: bool, ) -> impl for<'s> Fn(OrgSource<'s>) -> Res, ExampleSrcSwitches<'s>> { move |input| _example_src_switches(input, grab_language) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _example_src_switches<'s>( input: OrgSource<'s>, grab_language: bool, ) -> Res, ExampleSrcSwitches<'s>> { let mut number_lines = None; let mut retain_labels = RetainLabels::Yes; let mut use_labels = true; let mut label_format = None; let mut saw_r = false; let mut language = None; let remaining = if grab_language { let (remain, first_word) = opt(map(tuple((switch_word, space0)), |(word, _)| word))(input)?; language = first_word.map(Into::<&str>::into); remain } else { input }; let (remaining, (source, (words, _))) = if language.is_none() { consumed(tuple((separated_list1(space1, switch_word), space0)))(remaining)? } else { consumed(tuple((separated_list0(space1, switch_word), space0)))(remaining)? }; let mut state = SwitchState::Normal; for word in words { let normalized_word = Into::<&str>::into(word); loop { match (&state, normalized_word) { (SwitchState::Normal, "-n") => { state = SwitchState::NewLineNumber; } (SwitchState::Normal, "+n") => { state = SwitchState::ContinuedLineNumber; } (SwitchState::Normal, "-r") => { saw_r = true; use_labels = false; match retain_labels { RetainLabels::Yes => { retain_labels = RetainLabels::No; } _ => {} } } (SwitchState::Normal, "-l") => { state = SwitchState::LabelFormat; } (SwitchState::Normal, "-k") => { use_labels = false; let text_until_flag = input.get_until(word); let character_offset = Into::<&str>::into(text_until_flag).chars().count(); let character_offset = CharOffsetInLine::try_from(character_offset) .expect("Character offset should fit in CharOffsetInLine"); retain_labels = RetainLabels::Keep(character_offset); } (SwitchState::NewLineNumber, _) => { let val = normalized_word.parse::(); if let Ok(val) = val { if val < 0 { number_lines = Some(SwitchNumberLines::New(0)); } else { // Note that this can result in a negative 1 if the val is originally 0. number_lines = Some(SwitchNumberLines::New(val - 1)); } state = SwitchState::Normal; } else { number_lines = Some(SwitchNumberLines::New(0)); state = SwitchState::Normal; continue; // Re-processes the word } } (SwitchState::ContinuedLineNumber, _) => { let val = normalized_word.parse::(); if let Ok(val) = val { if val < 0 { number_lines = Some(SwitchNumberLines::Continued(0)); } else { // Note that this can result in a negative 1 if the val is originally 0. number_lines = Some(SwitchNumberLines::Continued(val - 1)); } state = SwitchState::Normal; } else { number_lines = Some(SwitchNumberLines::Continued(0)); state = SwitchState::Normal; continue; // Re-processes the word } } (SwitchState::LabelFormat, _) => { label_format = Some(normalized_word); state = SwitchState::Normal; } (SwitchState::Normal, _) => {} }; break; } } let retain_labels = match retain_labels { RetainLabels::Keep(_) if !saw_r => RetainLabels::Yes, _ => retain_labels, }; Ok(( remaining, ExampleSrcSwitches { source: Into::<&str>::into(source), language, number_lines, retain_labels, use_labels, label_format, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn switch_word<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { alt(( map( tuple((tag(r#"""#), is_not("\"\r\n"), tag(r#"""#))), |(_, contents, _)| contents, ), is_not(" \t\r\n"), ))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn content<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, String> { let mut ret = String::new(); let mut remaining = input; let exit_matcher_parser = parser_with_context!(exit_matcher_parser)(context); loop { if exit_matcher_parser(remaining).is_ok() { break; } let (remain, (pre_escape_whitespace, line)) = content_line(remaining)?; pre_escape_whitespace.map(|val| ret.push_str(Into::<&str>::into(val))); ret.push_str(line.into()); remaining = remain; } Ok((remaining, ret)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn content_line<'s>( input: OrgSource<'s>, ) -> Res, (Option>, OrgSource<'s>)> { let (remaining, pre_escape_whitespace) = opt(map( tuple(( recognize(tuple(( space0, many_till( tag(","), peek(tuple((tag(","), alt((tag("#+"), tag("*")))))), ), ))), tag(","), )), |(pre_comma, _)| pre_comma, ))(input)?; let (remaining, line_post_escape) = recognize(many_till(anychar, line_ending))(remaining)?; Ok((remaining, (pre_escape_whitespace, line_post_escape))) }