From 0d07a6aad3133fb37538d73a7b86b6d0ab7d6ed2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Apr 2023 13:30:15 -0400 Subject: [PATCH] Initial structure for dynamic blocks. --- src/compare/diff.rs | 3 ++- src/parser/dynamic_block.rs | 22 ++++++++++++++++++++++ src/parser/element.rs | 12 ++++++++++++ src/parser/greater_block.rs | 2 -- src/parser/greater_element.rs | 8 ++++++++ src/parser/mod.rs | 2 ++ 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/parser/dynamic_block.rs diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 0a0dd08..8f0b903 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -3,6 +3,7 @@ use crate::compare::util::get_offsets; use crate::parser::Comment; use crate::parser::Document; use crate::parser::DocumentElement; +use crate::parser::Drawer; use crate::parser::Element; use crate::parser::FootnoteDefinition; use crate::parser::GreaterBlock; @@ -11,7 +12,6 @@ use crate::parser::Paragraph; use crate::parser::PlainList; use crate::parser::PlainListItem; use crate::parser::Section; -use crate::parser::Drawer; #[derive(Debug)] pub struct DiffResult { @@ -217,6 +217,7 @@ fn compare_element<'s>( Element::Paragraph(obj) => compare_paragraph(source, emacs, obj), Element::PlainList(obj) => compare_plain_list(source, emacs, obj), Element::GreaterBlock(obj) => compare_greater_block(source, emacs, obj), + Element::DynamicBlock(obj) => todo!(), Element::FootnoteDefinition(obj) => compare_footnote_definition(source, emacs, obj), Element::Comment(obj) => compare_comment(source, emacs, obj), Element::Drawer(obj) => compare_drawer(source, emacs, obj), diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs new file mode 100644 index 0000000..6d601ba --- /dev/null +++ b/src/parser/dynamic_block.rs @@ -0,0 +1,22 @@ +use super::error::Res; +use super::Context; +use crate::parser::error::CustomError; +use crate::parser::error::MyError; +use crate::parser::greater_element::DynamicBlock; +use crate::parser::util::immediate_in_section; +use crate::parser::util::start_of_line; + +#[tracing::instrument(ret, level = "debug")] +pub fn dynamic_block<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, DynamicBlock<'s>> { + // TODO: Do I need to differentiate between different dynamic block types. + if immediate_in_section(context, "dynamic block") { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Cannot nest objects of the same element", + )))); + } + start_of_line(context, input)?; + todo!() +} diff --git a/src/parser/element.rs b/src/parser/element.rs index 189d62d..ecf9a80 100644 --- a/src/parser/element.rs +++ b/src/parser/element.rs @@ -1,8 +1,10 @@ use super::comment::comment; use super::drawer::drawer; +use super::dynamic_block::dynamic_block; use super::error::Res; use super::footnote_definition::footnote_definition; use super::greater_block::greater_block; +use super::greater_element::DynamicBlock; use super::greater_element::FootnoteDefinition; use super::greater_element::GreaterBlock; use super::greater_element::PlainList; @@ -23,6 +25,7 @@ pub enum Element<'s> { Paragraph(Paragraph<'s>), PlainList(PlainList<'s>), GreaterBlock(GreaterBlock<'s>), + DynamicBlock(DynamicBlock<'s>), FootnoteDefinition(FootnoteDefinition<'s>), Comment(Comment<'s>), Drawer(Drawer<'s>), @@ -34,6 +37,7 @@ impl<'s> Source<'s> for Element<'s> { Element::Paragraph(obj) => obj.source, Element::PlainList(obj) => obj.source, Element::GreaterBlock(obj) => obj.source, + Element::DynamicBlock(obj) => obj.source, Element::FootnoteDefinition(obj) => obj.source, Element::Comment(obj) => obj.source, Element::Drawer(obj) => obj.source, @@ -65,6 +69,12 @@ impl<'s> Source<'s> for GreaterBlock<'s> { } } +impl<'s> Source<'s> for DynamicBlock<'s> { + fn get_source(&'s self) -> &'s str { + self.source + } +} + impl<'s> Source<'s> for FootnoteDefinition<'s> { fn get_source(&'s self) -> &'s str { self.source @@ -100,12 +110,14 @@ pub fn non_paragraph_element<'r, 's>( ) -> Res<&'s str, Element<'s>> { let plain_list_matcher = parser_with_context!(plain_list)(context); let greater_block_matcher = parser_with_context!(greater_block)(context); + let dynamic_block_matcher = parser_with_context!(dynamic_block)(context); let footnote_definition_matcher = parser_with_context!(footnote_definition)(context); let comment_matcher = parser_with_context!(comment)(context); let drawer_matcher = parser_with_context!(drawer)(context); alt(( map(plain_list_matcher, Element::PlainList), map(greater_block_matcher, Element::GreaterBlock), + map(dynamic_block_matcher, Element::DynamicBlock), map(footnote_definition_matcher, Element::FootnoteDefinition), map(comment_matcher, Element::Comment), map(drawer_matcher, Element::Drawer), diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 2061e48..05d358f 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -1,12 +1,10 @@ use super::error::Res; -use super::object::TextMarkup; use super::Context; use crate::parser::element::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; use crate::parser::greater_element::GreaterBlock; -use crate::parser::object::Object; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; diff --git a/src/parser/greater_element.rs b/src/parser/greater_element.rs index c9e5975..9018726 100644 --- a/src/parser/greater_element.rs +++ b/src/parser/greater_element.rs @@ -22,6 +22,14 @@ pub struct GreaterBlock<'s> { pub children: Vec>, } +#[derive(Debug)] +pub struct DynamicBlock<'s> { + pub source: &'s str, + pub name: &'s str, + pub parameters: Option<&'s str>, + pub children: Vec>, +} + #[derive(Debug)] pub struct FootnoteDefinition<'s> { pub source: &'s str, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8f63930..3a2eb61 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,6 +1,7 @@ mod comment; mod document; mod drawer; +mod dynamic_block; mod element; mod error; mod exiting; @@ -24,6 +25,7 @@ pub use document::Heading; pub use document::Section; pub use element::Element; pub use greater_element::Drawer; +pub use greater_element::DynamicBlock; pub use greater_element::FootnoteDefinition; pub use greater_element::GreaterBlock; pub use greater_element::PlainList;