From 73e15286dc9128f1efe4ea7d1f964ed6752ebe68 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 24 Jul 2023 17:15:27 -0400 Subject: [PATCH 01/11] Add test cases. --- org_mode_samples/timestamp/simple.org | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 org_mode_samples/timestamp/simple.org 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] From fa5fc411215c613ababf1b83dc2e69123699ecdf Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 24 Jul 2023 17:34:07 -0400 Subject: [PATCH 02/11] Create structure for timestamps. --- src/compare/diff.rs | 25 +++++++++++++++++++++++++ src/parser/mod.rs | 2 ++ src/parser/object.rs | 13 +++++++++++++ src/parser/object_parser.rs | 10 ++++++++-- src/parser/timestamp.rs | 10 ++++++++++ src/parser/token.rs | 1 + 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/parser/timestamp.rs 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..74f4bf1 --- /dev/null +++ b/src/parser/timestamp.rs @@ -0,0 +1,10 @@ +use super::Context; +use crate::error::Res; +use crate::parser::util::not_yet_implemented; +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>> { + not_yet_implemented()?; + todo!() +} 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)), From 49d5a4e4b5321faf37614c18676665f0c041cc5a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 24 Jul 2023 17:54:49 -0400 Subject: [PATCH 03/11] Call out to the parsers for the various timestamp types. The parsers for those types remain unimplemented. --- src/parser/timestamp.rs | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 74f4bf1..3aed253 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -1,10 +1,85 @@ +use nom::branch::alt; + use super::Context; use crate::error::Res; +use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::not_yet_implemented; 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>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn active_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn active_date_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn active_time_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_date_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { + not_yet_implemented()?; + todo!() +} + +#[tracing::instrument(ret, level = "debug")] +fn inactive_time_range_timestamp<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Timestamp<'s>> { not_yet_implemented()?; todo!() } From 70f2747291ce24e3664ff6250156b913eb92cd46 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 18:59:22 -0400 Subject: [PATCH 04/11] Implement parser for diary timestamps. --- src/parser/timestamp.rs | 45 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 3aed253..6e83eae 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -1,8 +1,20 @@ use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::character::complete::anychar; +use nom::character::complete::one_of; +use nom::character::complete::space0; +use nom::combinator::recognize; +use nom::combinator::verify; +use nom::multi::many_till; use super::Context; 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::not_yet_implemented; use crate::parser::Timestamp; @@ -26,8 +38,37 @@ fn diary_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] From d4a3628481314c5c2f4ff6691f10c7a389512bbd Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 19:20:28 -0400 Subject: [PATCH 05/11] Implement parser for date. --- src/parser/timestamp.rs | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 6e83eae..821933d 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -1,11 +1,15 @@ 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; @@ -76,6 +80,8 @@ 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)?; not_yet_implemented()?; todo!() } @@ -124,3 +130,43 @@ fn inactive_time_range_timestamp<'r, 's>( not_yet_implemented()?; todo!() } + +#[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) +} From 529418a9d123bda47c7a2b268e3d42524c95f4b9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 19:45:57 -0400 Subject: [PATCH 06/11] Implement parser for active timestamps. --- src/parser/timestamp.rs | 80 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 821933d..33d0122 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -82,8 +82,20 @@ fn active_timestamp<'r, 's>( ) -> Res<&'s str, Timestamp<'s>> { let (remaining, _) = tag("<")(input)?; let (remaining, _date) = date(context, remaining)?; - not_yet_implemented()?; - todo!() + let (remaining, _time) = + opt(tuple((space1, parser_with_context!(active_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")] @@ -170,3 +182,67 @@ fn dayname_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, c.is_whitespace() || "+-]>0123456789\n".contains(*c) }))(input) } + +#[tracing::instrument(ret, level = "debug")] +fn active_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!(active_time_rest)(context))(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +#[tracing::instrument(ret, level = "debug")] +fn active_time_rest<'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: &active_time_rest_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 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))), + parser_with_context!(repeater)(context), + parser_with_context!(warning_delay)(context), + ))(input) +} + +#[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)) +} From c5f81298bac081512fae333cd1dc6d6462f67bb7 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 19:52:35 -0400 Subject: [PATCH 07/11] Switch to passing in the exit matcher from a higher context to reduce the permutations of functions. --- src/parser/timestamp.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 33d0122..450610a 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -82,8 +82,13 @@ fn active_timestamp<'r, 's>( ) -> 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!(active_time)(context))))(remaining)?; + 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(( @@ -184,29 +189,20 @@ fn dayname_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[tracing::instrument(ret, level = "debug")] -fn active_time<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +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!(active_time_rest)(context))(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 active_time_rest<'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: &active_time_rest_end, - })); - +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)(&parser_context), - ), + many_till(anychar, parser_with_context!(exit_matcher_parser)(context)), |(body, _end_contents)| !body.is_empty(), ))(input)?; From b5a029e2bf2aff3df84ebeeb7cd0510729e31a83 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 19:54:33 -0400 Subject: [PATCH 08/11] Implement parser for inactive timestamps. --- src/parser/timestamp.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 450610a..b56b267 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -108,8 +108,27 @@ fn inactive_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] @@ -218,6 +237,18 @@ fn active_time_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res ))(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))), + parser_with_context!(repeater)(context), + parser_with_context!(warning_delay)(context), + ))(input) +} + #[tracing::instrument(ret, level = "debug")] fn repeater<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { // + for cumulative type From 1a5b7ca30c82b7cf004b76a057a5f96cb352e65f Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 19:59:36 -0400 Subject: [PATCH 09/11] Implement parser for active/inactive timestamp date ranges. --- src/parser/timestamp.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index b56b267..fc21198 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -136,8 +136,15 @@ fn active_date_range_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] @@ -154,8 +161,15 @@ fn inactive_date_range_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] From ece8fcd0c441d04b2449cac6f315fa3580fb37e2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 20:26:56 -0400 Subject: [PATCH 10/11] Implement parser for active/inactive timestamp time ranges. --- src/parser/timestamp.rs | 72 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index fc21198..0ee6865 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -15,11 +15,11 @@ 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::util::not_yet_implemented; use crate::parser::Timestamp; #[tracing::instrument(ret, level = "debug")] @@ -152,8 +152,34 @@ fn active_time_range_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] @@ -177,8 +203,34 @@ fn inactive_time_range_timestamp<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Timestamp<'s>> { - not_yet_implemented()?; - todo!() + 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")] @@ -263,6 +315,16 @@ fn inactive_time_rest_end<'r, 's>( ))(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 From 17e523b74c2ea8c431582488535b0e92e47e3191 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 27 Jul 2023 20:39:13 -0400 Subject: [PATCH 11/11] Do not consume space in time rest when leading into a repeater or delay. --- src/parser/timestamp.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 0ee6865..0c2bacf 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -298,8 +298,11 @@ fn time_rest<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, & 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))), - parser_with_context!(repeater)(context), - parser_with_context!(warning_delay)(context), + recognize(tuple((space1, parser_with_context!(repeater)(context)))), + recognize(tuple(( + space1, + parser_with_context!(warning_delay)(context), + ))), ))(input) } @@ -310,8 +313,11 @@ fn inactive_time_rest_end<'r, 's>( ) -> Res<&'s str, &'s str> { alt(( recognize(verify(anychar, |c| "]\n".contains(*c))), - parser_with_context!(repeater)(context), - parser_with_context!(warning_delay)(context), + recognize(tuple((space1, parser_with_context!(repeater)(context)))), + recognize(tuple(( + space1, + parser_with_context!(warning_delay)(context), + ))), ))(input) }