From 2e7db0f8bda42e38f5620fce83e67d6125ac342a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 11 Dec 2023 15:59:56 -0500 Subject: [PATCH] Implement the new fields for lesser block. --- src/parser/lesser_block.rs | 48 +++++++++++++++++++++++-------------- src/types/lesser_element.rs | 46 +++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index a09ce0d..9f06497 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -80,22 +80,28 @@ where 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, contents, children) = + match consumed(many_till(blank_line, exit_matcher))(remaining) { + Ok((remaining, (whitespace, (_children, _exit_contents)))) => ( + remaining, + whitespace, + if whitespace.len() > 0 { + vec![Object::PlainText(PlainText { + source: whitespace.into(), + })] + } else { + Vec::new() + }, + ), + Err(_) => { + let (remaining, (contents, (children, _exit_contents))) = + consumed(many_till(object_matcher, exit_matcher))(remaining)?; + (remaining, contents, children) + } + }; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok(( @@ -108,6 +114,8 @@ where ), data: parameters.map(Into::<&str>::into), children, + contents: Into::<&str>::into(contents), + post_blank: post_blank.map(Into::<&str>::into), }, )) } @@ -144,7 +152,7 @@ where let (remaining, contents) = parser_with_context!(text_until_exit)(&parser_context)(remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok(( @@ -156,6 +164,7 @@ where affiliated_keywords, ), contents: contents.into(), + post_blank: post_blank.map(Into::<&str>::into), }, )) } @@ -205,7 +214,7 @@ where let (remaining, contents) = text_until_exit(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = { @@ -237,6 +246,7 @@ where use_labels, label_format, value: Into::<&str>::into(contents), + post_blank: post_blank.map(Into::<&str>::into), }, )) } @@ -279,7 +289,7 @@ where let (remaining, contents) = text_until_exit(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok(( @@ -293,6 +303,7 @@ where export_type: export_type.map(Into::<&str>::into), data: parameters.map(Into::<&str>::into), value: Into::<&str>::into(contents), + post_blank: post_blank.map(Into::<&str>::into), }, )) } @@ -334,7 +345,7 @@ where let (remaining, contents) = text_until_exit(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; - let (remaining, _trailing_ws) = + let (remaining, post_blank) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = { @@ -372,6 +383,7 @@ where use_labels, label_format, value: Into::<&str>::into(contents), + post_blank: post_blank.map(Into::<&str>::into), }, )) } diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index e4eab1e..596d056 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -50,6 +50,8 @@ pub struct VerseBlock<'s> { pub affiliated_keywords: AffiliatedKeywords<'s>, pub data: Option<&'s str>, pub children: Vec>, + pub contents: &'s str, + pub post_blank: Option<&'s str>, } #[derive(Debug)] @@ -57,6 +59,7 @@ pub struct CommentBlock<'s> { pub source: &'s str, pub affiliated_keywords: AffiliatedKeywords<'s>, pub contents: &'s str, + pub post_blank: Option<&'s str>, } pub type CharOffsetInLine = u16; @@ -80,6 +83,7 @@ pub struct ExampleBlock<'s> { pub use_labels: bool, pub label_format: Option<&'s str>, pub value: &'s str, + pub post_blank: Option<&'s str>, } #[derive(Debug)] @@ -89,6 +93,7 @@ pub struct ExportBlock<'s> { pub export_type: Option<&'s str>, pub data: Option<&'s str>, pub value: &'s str, + pub post_blank: Option<&'s str>, } #[derive(Debug)] @@ -104,6 +109,7 @@ pub struct SrcBlock<'s> { pub use_labels: bool, pub label_format: Option<&'s str>, pub value: &'s str, + pub post_blank: Option<&'s str>, } #[derive(Debug)] @@ -258,11 +264,15 @@ impl<'s> StandardProperties<'s> for VerseBlock<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + Some(self.contents) } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } } impl<'s> StandardProperties<'s> for CommentBlock<'s> { @@ -271,11 +281,15 @@ impl<'s> StandardProperties<'s> for CommentBlock<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + None } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } } impl<'s> StandardProperties<'s> for ExampleBlock<'s> { @@ -284,11 +298,15 @@ impl<'s> StandardProperties<'s> for ExampleBlock<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + None } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } } impl<'s> StandardProperties<'s> for ExportBlock<'s> { @@ -297,11 +315,15 @@ impl<'s> StandardProperties<'s> for ExportBlock<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + None } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } } impl<'s> StandardProperties<'s> for SrcBlock<'s> { @@ -310,11 +332,15 @@ impl<'s> StandardProperties<'s> for SrcBlock<'s> { } fn get_contents<'b>(&'b self) -> Option<&'s str> { - todo!() + None } fn get_post_blank(&self) -> PostBlank { - todo!() + self.post_blank + .map(|text| text.lines().count()) + .unwrap_or(0) + .try_into() + .expect("Too much post-blank to fit into a PostBlank.") } }