From 7df899e3a78c1c9d97dd4f23740c74776788d887 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 21 Apr 2023 23:36:38 -0400 Subject: [PATCH] Read affiliated keywords. --- src/parser/element_parser.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index e2ccc9b8..04b74661 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -16,24 +16,32 @@ use super::lesser_block::src_block; use super::lesser_block::verse_block; use super::paragraph::paragraph; use super::plain_list::plain_list; +use super::util::start_of_line; use super::Context; use crate::error::Res; use crate::parser::parser_with_context::parser_with_context; use crate::parser::table::org_mode_table; use nom::branch::alt; use nom::combinator::map; +use nom::multi::many0; #[tracing::instrument(ret, level = "debug")] pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context); - let paragraph_matcher = parser_with_context!(paragraph)(context); + let paragraph_matcher = parser_with_context!(paragraph_element)(context); - alt(( - non_paragraph_matcher, - map(paragraph_matcher, Element::Paragraph), - ))(input) + alt((non_paragraph_matcher, paragraph_matcher))(input) } +pub fn paragraph_element<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Element<'s>> { + let paragraph_matcher = parser_with_context!(paragraph)(context); + let keyword_matcher = parser_with_context!(keyword)(context); + let (remaining, affiliated_keywords) = many0(keyword_matcher)(input)?; + map(paragraph_matcher, Element::Paragraph)(remaining) +} pub fn non_paragraph_element<'r, 's>( context: Context<'r, 's>, input: &'s str, @@ -55,7 +63,8 @@ pub fn non_paragraph_element<'r, 's>( let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context); let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context); let keyword_matcher = parser_with_context!(keyword)(context); - alt(( + let (remaining, mut affiliated_keywords) = many0(keyword_matcher)(input)?; + let (remaining, element) = match alt(( map(plain_list_matcher, Element::PlainList), map(greater_block_matcher, Element::GreaterBlock), map(dynamic_block_matcher, Element::DynamicBlock), @@ -72,6 +81,13 @@ pub fn non_paragraph_element<'r, 's>( map(diary_sexp_matcher, Element::DiarySexp), map(fixed_width_area_matcher, Element::FixedWidthArea), map(horizontal_rule_matcher, Element::HorizontalRule), - map(keyword_matcher, Element::Keyword), - ))(input) + ))(remaining) + { + the_ok @ Ok(_) => the_ok, + Err(_) => { + affiliated_keywords.clear(); + map(keyword_matcher, Element::Keyword)(input) + } + }?; + Ok((remaining, element)) }