diff --git a/org_mode_samples/target/simple.org b/org_mode_samples/target/simple.org new file mode 100644 index 00000000..695ff63e --- /dev/null +++ b/org_mode_samples/target/simple.org @@ -0,0 +1,3 @@ +foo <> baz + +lorem << ipsum >> dolar diff --git a/src/compare/diff.rs b/src/compare/diff.rs index e669d5d8..f16111d0 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -50,6 +50,7 @@ use crate::parser::StrikeThrough; use crate::parser::Table; use crate::parser::TableCell; use crate::parser::TableRow; +use crate::parser::Target; use crate::parser::Underline; use crate::parser::Verbatim; use crate::parser::VerseBlock; @@ -172,6 +173,7 @@ fn compare_object<'s>( Object::InlineBabelCall(obj) => compare_inline_babel_call(source, emacs, obj), Object::InlineSourceBlock(obj) => compare_inline_source_block(source, emacs, obj), Object::LineBreak(obj) => compare_line_break(source, emacs, obj), + Object::Target(obj) => compare_target(source, emacs, obj), } } @@ -1463,3 +1465,26 @@ fn compare_line_break<'s>( children: Vec::new(), }) } + +fn compare_target<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Target<'s>, +) -> Result> { + let mut this_status = DiffStatus::Good; + let emacs_name = "target"; + if assert_name(emacs, emacs_name).is_err() { + this_status = DiffStatus::Bad; + } + + if assert_bounds(source, emacs, rust).is_err() { + this_status = DiffStatus::Bad; + } + + Ok(DiffResult { + status: this_status, + name: emacs_name.to_owned(), + message: None, + children: Vec::new(), + }) +} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 95e27296..16866063 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -43,6 +43,7 @@ mod regular_link; pub mod sexp; mod source; mod table; +mod target; mod text_markup; mod token; mod util; @@ -97,6 +98,7 @@ pub use object::RadioLink; pub use object::RadioTarget; pub use object::RegularLink; pub use object::StrikeThrough; +pub use object::Target; pub use object::Underline; pub use object::Verbatim; pub use source::Source; diff --git a/src/parser/object.rs b/src/parser/object.rs index 725845a0..ea705a4d 100644 --- a/src/parser/object.rs +++ b/src/parser/object.rs @@ -24,6 +24,7 @@ pub enum Object<'s> { InlineBabelCall(InlineBabelCall<'s>), InlineSourceBlock(InlineSourceBlock<'s>), LineBreak(LineBreak<'s>), + Target(Target<'s>), } #[derive(Debug, PartialEq)] @@ -155,6 +156,11 @@ pub struct LineBreak<'s> { pub source: &'s str, } +#[derive(Debug, PartialEq)] +pub struct Target<'s> { + pub source: &'s str, +} + impl<'s> Source<'s> for Object<'s> { fn get_source(&'s self) -> &'s str { match self { @@ -180,6 +186,7 @@ impl<'s> Source<'s> for Object<'s> { Object::InlineBabelCall(obj) => obj.source, Object::InlineSourceBlock(obj) => obj.source, Object::LineBreak(obj) => obj.source, + Object::Target(obj) => obj.source, } } } @@ -309,3 +316,9 @@ impl<'s> Source<'s> for LineBreak<'s> { self.source } } + +impl<'s> Source<'s> for Target<'s> { + fn get_source(&'s self) -> &'s str { + self.source + } +} diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index d0c70311..ce8c569a 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -21,6 +21,7 @@ use crate::parser::org_macro::org_macro; use crate::parser::plain_link::plain_link; use crate::parser::radio_link::radio_link; use crate::parser::radio_link::radio_target; +use crate::parser::target::target; use crate::parser::text_markup::text_markup; #[tracing::instrument(ret, level = "debug")] @@ -32,6 +33,7 @@ pub fn standard_set_object<'r, 's>( not(|i| context.check_exit_matcher(i))(input)?; alt(( + map(parser_with_context!(target)(context), Object::Target), map(parser_with_context!(line_break)(context), Object::LineBreak), map( parser_with_context!(inline_source_block)(context), @@ -98,6 +100,7 @@ pub fn any_object_except_plain_text<'r, 's>( ) -> Res<&'s str, Object<'s>> { // Used for exit matchers so this does not check exit matcher condition. alt(( + map(parser_with_context!(target)(context), Object::Target), map(parser_with_context!(line_break)(context), Object::LineBreak), map( parser_with_context!(inline_source_block)(context), diff --git a/src/parser/target.rs b/src/parser/target.rs new file mode 100644 index 00000000..8b9e029c --- /dev/null +++ b/src/parser/target.rs @@ -0,0 +1,60 @@ +use nom::bytes::complete::tag; +use nom::character::complete::anychar; +use nom::character::complete::one_of; +use nom::character::complete::space0; +use nom::combinator::peek; +use nom::combinator::recognize; +use nom::combinator::verify; +use nom::multi::many_till; + +use super::Context; +use crate::error::CustomError; +use crate::error::MyError; +use crate::error::Res; +use crate::parser::exiting::ExitClass; +use crate::parser::parser_context::ContextElement; +use crate::parser::parser_context::ExitMatcherNode; +use crate::parser::parser_with_context::parser_with_context; +use crate::parser::util::exit_matcher_parser; +use crate::parser::util::get_consumed; +use crate::parser::util::get_one_before; +use crate::parser::Target; + +#[tracing::instrument(ret, level = "debug")] +pub fn target<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Target<'s>> { + let (remaining, _) = tag("<<")(input)?; + let (remaining, _) = peek(verify(anychar, |c| { + !c.is_whitespace() && !"<>\n".contains(*c) + }))(remaining)?; + + let parser_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &target_end, + })); + let (remaining, body) = recognize(many_till( + anychar, + parser_with_context!(exit_matcher_parser)(&parser_context), + ))(remaining)?; + + let document_root = context.get_document_root().unwrap(); + let preceding_character = get_one_before(document_root, remaining) + .map(|slice| slice.chars().next()) + .flatten() + .expect("We cannot be at the start of the file because we are inside a target."); + if preceding_character.is_whitespace() { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Targets cannot end with whitespace.", + )))); + } + let (remaining, _) = tag(">>")(remaining)?; + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Target { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn target_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + recognize(one_of("<>\n"))(input) +} diff --git a/src/parser/token.rs b/src/parser/token.rs index 8f76b0ef..f3ebe822 100644 --- a/src/parser/token.rs +++ b/src/parser/token.rs @@ -63,6 +63,7 @@ impl<'r, 's> Token<'r, 's> { Object::InlineBabelCall(_) => Box::new(std::iter::empty()), Object::InlineSourceBlock(_) => Box::new(std::iter::empty()), Object::LineBreak(_) => Box::new(std::iter::empty()), + Object::Target(_) => Box::new(std::iter::empty()), }, Token::Element(elem) => match elem { Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),