diff --git a/org_mode_samples/timestamp/simple.org b/org_mode_samples/timestamp/simple.org new file mode 100644 index 0000000..c84b6c7 --- /dev/null +++ b/org_mode_samples/timestamp/simple.org @@ -0,0 +1,14 @@ +# diary +<%%(foo bar baz)> +# active +<1970-01-01 Thu 8:15rest +1w -1d> +# inactive +[1970-01-01 Thu 8:15rest +1w -1d] +# active date range +<1970-01-01 Thu 8:15rest +1w -1d>--<1970-01-01 Thu 8:15rest +1w -1d> +# active time range +<1970-01-01 Thu 8:15rest-13:15otherrest +1w -1d> +# inactive date range +[1970-01-01 Thu 8:15rest +1w -1d]--[1970-01-01 Thu 8:15rest +1w -1d] +# inactive time range +[1970-01-01 Thu 8:15rest-13:15otherrest +1w -1d] diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 829fec0..cde5e2d 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -54,6 +54,7 @@ use crate::parser::Table; use crate::parser::TableCell; use crate::parser::TableRow; use crate::parser::Target; +use crate::parser::Timestamp; use crate::parser::Underline; use crate::parser::Verbatim; use crate::parser::VerseBlock; @@ -180,6 +181,7 @@ fn compare_object<'s>( Object::StatisticsCookie(obj) => compare_statistics_cookie(source, emacs, obj), Object::Subscript(obj) => compare_subscript(source, emacs, obj), Object::Superscript(obj) => compare_superscript(source, emacs, obj), + Object::Timestamp(obj) => compare_timestamp(source, emacs, obj), } } @@ -1563,3 +1565,26 @@ fn compare_superscript<'s>( children: Vec::new(), }) } + +fn compare_timestamp<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Timestamp<'s>, +) -> Result> { + let mut this_status = DiffStatus::Good; + let emacs_name = "timestamp"; + 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 a37107c..62dd249 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -47,6 +47,7 @@ mod subscript_and_superscript; mod table; mod target; mod text_markup; +mod timestamp; mod token; mod util; pub use document::document; @@ -104,6 +105,7 @@ pub use object::StrikeThrough; pub use object::Subscript; pub use object::Superscript; pub use object::Target; +pub use object::Timestamp; 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 d9acdd3..8ad6b8c 100644 --- a/src/parser/object.rs +++ b/src/parser/object.rs @@ -28,6 +28,7 @@ pub enum Object<'s> { StatisticsCookie(StatisticsCookie<'s>), Subscript(Subscript<'s>), Superscript(Superscript<'s>), + Timestamp(Timestamp<'s>), } #[derive(Debug, PartialEq)] @@ -179,6 +180,11 @@ pub struct Superscript<'s> { pub source: &'s str, } +#[derive(Debug, PartialEq)] +pub struct Timestamp<'s> { + pub source: &'s str, +} + impl<'s> Source<'s> for Object<'s> { fn get_source(&'s self) -> &'s str { match self { @@ -205,6 +211,7 @@ impl<'s> Source<'s> for Object<'s> { Object::InlineSourceBlock(obj) => obj.source, Object::LineBreak(obj) => obj.source, Object::Target(obj) => obj.source, + Object::Timestamp(obj) => obj.source, Object::StatisticsCookie(obj) => obj.source, Object::Subscript(obj) => obj.source, Object::Superscript(obj) => obj.source, @@ -361,3 +368,9 @@ impl<'s> Source<'s> for Superscript<'s> { self.source } } + +impl<'s> Source<'s> for Timestamp<'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 b15819e..ad77955 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -26,16 +26,17 @@ use crate::parser::subscript_and_superscript::subscript; use crate::parser::subscript_and_superscript::superscript; use crate::parser::target::target; use crate::parser::text_markup::text_markup; +use crate::parser::timestamp::timestamp; #[tracing::instrument(ret, level = "debug")] pub fn standard_set_object<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Object<'s>> { - // TODO: subscript and superscript, timestamps. not(|i| context.check_exit_matcher(i))(input)?; alt(( + map(parser_with_context!(timestamp)(context), Object::Timestamp), map(parser_with_context!(subscript)(context), Object::Subscript), map( parser_with_context!(superscript)(context), @@ -116,6 +117,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!(timestamp)(context), Object::Timestamp), map(parser_with_context!(subscript)(context), Object::Subscript), map( parser_with_context!(superscript)(context), @@ -170,8 +172,12 @@ pub fn regular_link_description_object_set<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Object<'s>> { - // TODO: add export snippets. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] + // TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] alt(( + map( + parser_with_context!(export_snippet)(context), + Object::ExportSnippet, + ), map( parser_with_context!(statistics_cookie)(context), Object::StatisticsCookie, diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs new file mode 100644 index 0000000..0c2bacf --- /dev/null +++ b/src/parser/timestamp.rs @@ -0,0 +1,357 @@ +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::character::complete::anychar; +use nom::character::complete::digit1; +use nom::character::complete::one_of; +use nom::character::complete::space0; +use nom::character::complete::space1; +use nom::combinator::opt; +use nom::combinator::recognize; +use nom::combinator::verify; +use nom::multi::many_till; +use nom::sequence::tuple; + +use super::Context; +use crate::error::Res; +use crate::parser::exiting::ExitClass; +use crate::parser::parser_context::ContextElement; +use crate::parser::parser_context::ContextTree; +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::Timestamp; + +#[tracing::instrument(ret, level = "debug")] +pub fn timestamp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Timestamp<'s>> { + // TODO: This would be more efficient if we didn't throw away the parse result of the first half of an active/inactive date range timestamp if the parse fails (as in, the first thing active_date_range_timestamp parses is a active_timestamp but then we throw that away if it doesn't turn out to be a full active_date_range_timestamp despite the active_timestamp parse being completely valid). I am going with the simplest/cleanest approach for the first implementation. + alt(( + // Order matters here. If its a date range, we need to parse the entire date range instead of just the first timestamp. If its a time range, we need to make sure thats parsed as a time range instead of as the "rest" portion of a single timestamp. + parser_with_context!(diary_timestamp)(context), + parser_with_context!(active_time_range_timestamp)(context), + parser_with_context!(inactive_time_range_timestamp)(context), + parser_with_context!(active_date_range_timestamp)(context), + parser_with_context!(inactive_date_range_timestamp)(context), + parser_with_context!(active_timestamp)(context), + parser_with_context!(inactive_timestamp)(context), + ))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn diary_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _) = tag("<%%(")(input)?; + let (remaining, _body) = sexp(context, remaining)?; + let (remaining, _) = tag(")>")(remaining)?; + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + let parser_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &sexp_end, + })); + + let (remaining, body) = recognize(verify( + many_till( + anychar, + parser_with_context!(exit_matcher_parser)(&parser_context), + ), + |(body, _end_contents)| !body.is_empty(), + ))(input)?; + + Ok((remaining, body)) +} + +#[tracing::instrument(ret, level = "debug")] +fn sexp_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + alt((tag(")>"), recognize(one_of(">\n"))))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn active_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _) = tag("<")(input)?; + let (remaining, _date) = date(context, remaining)?; + let time_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &active_time_rest_end, + })); + let (remaining, _time) = + opt(tuple((space1, parser_with_context!(time)(&time_context))))(remaining)?; + let (remaining, _repeater) = + opt(tuple((space1, parser_with_context!(repeater)(context))))(remaining)?; + let (remaining, _warning_delay) = opt(tuple(( + space1, + parser_with_context!(warning_delay)(context), + )))(remaining)?; + let (remaining, _) = tag(">")(remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _) = tag("[")(input)?; + let (remaining, _date) = date(context, remaining)?; + let time_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &inactive_time_rest_end, + })); + let (remaining, _time) = + opt(tuple((space1, parser_with_context!(time)(&time_context))))(remaining)?; + let (remaining, _repeater) = + opt(tuple((space1, parser_with_context!(repeater)(context))))(remaining)?; + let (remaining, _warning_delay) = opt(tuple(( + space1, + parser_with_context!(warning_delay)(context), + )))(remaining)?; + let (remaining, _) = tag("]")(remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn active_date_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _first_timestamp) = active_timestamp(context, input)?; + // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace + let (remaining, _separator) = tag("--")(remaining)?; + let (remaining, _second_timestamp) = active_timestamp(context, remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn active_time_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _) = tag("<")(input)?; + let (remaining, _date) = date(context, remaining)?; + let time_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &active_time_rest_end, + })); + let first_time_context = + time_context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &time_range_rest_end, + })); + let (remaining, _first_time) = + tuple((space1, parser_with_context!(time)(&first_time_context)))(remaining)?; + let (remaining, _) = tag("-")(remaining)?; + let (remaining, _second_time) = parser_with_context!(time)(&time_context)(remaining)?; + let (remaining, _repeater) = + opt(tuple((space1, parser_with_context!(repeater)(context))))(remaining)?; + let (remaining, _warning_delay) = opt(tuple(( + space1, + parser_with_context!(warning_delay)(context), + )))(remaining)?; + let (remaining, _) = tag(">")(remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_date_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _first_timestamp) = inactive_timestamp(context, input)?; + // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace + let (remaining, _separator) = tag("--")(remaining)?; + let (remaining, _second_timestamp) = inactive_timestamp(context, remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_time_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + let (remaining, _) = tag("[")(input)?; + let (remaining, _date) = date(context, remaining)?; + let time_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &inactive_time_rest_end, + })); + let first_time_context = + time_context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &time_range_rest_end, + })); + let (remaining, _first_time) = + tuple((space1, parser_with_context!(time)(&first_time_context)))(remaining)?; + let (remaining, _) = tag("-")(remaining)?; + let (remaining, _second_time) = parser_with_context!(time)(&time_context)(remaining)?; + let (remaining, _repeater) = + opt(tuple((space1, parser_with_context!(repeater)(context))))(remaining)?; + let (remaining, _warning_delay) = opt(tuple(( + space1, + parser_with_context!(warning_delay)(context), + )))(remaining)?; + let (remaining, _) = tag("]")(remaining)?; + + let (remaining, _) = space0(remaining)?; + let source = get_consumed(input, remaining); + + Ok((remaining, Timestamp { source })) +} + +#[tracing::instrument(ret, level = "debug")] +fn date<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + let (remaining, _year) = verify(digit1, |year: &str| year.len() == 4)(input)?; + let (remaining, _) = tag("-")(remaining)?; + let (remaining, _month) = verify(digit1, |month: &str| month.len() == 2)(remaining)?; + let (remaining, _) = tag("-")(remaining)?; + let (remaining, _day_of_month) = + verify(digit1, |day_of_month: &str| day_of_month.len() == 2)(remaining)?; + let (remaining, _dayname) = + opt(tuple((space1, parser_with_context!(dayname)(context))))(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +#[tracing::instrument(ret, level = "debug")] +fn dayname<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + let parser_context = + context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &dayname_end, + })); + + let (remaining, body) = recognize(verify( + many_till( + anychar, + parser_with_context!(exit_matcher_parser)(&parser_context), + ), + |(body, _end_contents)| !body.is_empty(), + ))(input)?; + + Ok((remaining, body)) +} + +#[tracing::instrument(ret, level = "debug")] +fn dayname_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + recognize(verify(anychar, |c| { + c.is_whitespace() || "+-]>0123456789\n".contains(*c) + }))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn time<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + let (remaining, _hour) = + verify(digit1, |hour: &str| hour.len() >= 1 && hour.len() <= 2)(input)?; + let (remaining, _) = tag(":")(remaining)?; + let (remaining, _minute) = verify(digit1, |minute: &str| minute.len() == 2)(remaining)?; + let (remaining, _time_rest) = opt(parser_with_context!(time_rest)(context))(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +#[tracing::instrument(ret, level = "debug")] +fn time_rest<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + let (remaining, body) = recognize(verify( + many_till(anychar, parser_with_context!(exit_matcher_parser)(context)), + |(body, _end_contents)| !body.is_empty(), + ))(input)?; + + Ok((remaining, body)) +} + +#[tracing::instrument(ret, level = "debug")] +fn active_time_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + alt(( + recognize(verify(anychar, |c| ">\n".contains(*c))), + recognize(tuple((space1, parser_with_context!(repeater)(context)))), + recognize(tuple(( + space1, + parser_with_context!(warning_delay)(context), + ))), + ))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_time_rest_end<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, &'s str> { + alt(( + recognize(verify(anychar, |c| "]\n".contains(*c))), + recognize(tuple((space1, parser_with_context!(repeater)(context)))), + recognize(tuple(( + space1, + parser_with_context!(warning_delay)(context), + ))), + ))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn time_range_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + // We pop off the most recent context element to get a context tree with just the active/inactive_time_rest_end exit matcher (removing this function from the exit matcher chain) because the 2nd time in the range does not end when a "-TIME" pattern is found. + let parent_node = context.iter().next().expect("Two context elements are added to the tree when adding this exit matcher, so it should be impossible for this to return None."); + let parent_tree = ContextTree::branch_from(parent_node); + let exit_contents = + recognize(tuple((tag("-"), parser_with_context!(time)(&parent_tree))))(input); + exit_contents +} + +#[tracing::instrument(ret, level = "debug")] +fn repeater<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + // + for cumulative type + // ++ for catch-up type + // .+ for restart type + let (remaining, _mark) = alt((tag("++"), tag("+"), tag(".+")))(input)?; + let (remaining, _value) = digit1(remaining)?; + // h = hour, d = day, w = week, m = month, y = year + let (remaining, _unit) = recognize(one_of("hdwmy"))(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +#[tracing::instrument(ret, level = "debug")] +fn warning_delay<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + // - for all type + // -- for first type + let (remaining, _mark) = alt((tag("--"), tag("-")))(input)?; + let (remaining, _value) = digit1(remaining)?; + // h = hour, d = day, w = week, m = month, y = year + let (remaining, _unit) = recognize(one_of("hdwmy"))(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} diff --git a/src/parser/token.rs b/src/parser/token.rs index 45f51ba..cc62562 100644 --- a/src/parser/token.rs +++ b/src/parser/token.rs @@ -67,6 +67,7 @@ impl<'r, 's> Token<'r, 's> { Object::StatisticsCookie(_) => Box::new(std::iter::empty()), Object::Subscript(_) => Box::new(std::iter::empty()), // TODO: Iterate over children Object::Superscript(_) => Box::new(std::iter::empty()), // TODO: Iterate over children + Object::Timestamp(_) => Box::new(std::iter::empty()), }, Token::Element(elem) => match elem { Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),