diff --git a/Cargo.lock b/Cargo.lock index f10af83e..c86c0c98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,6 +487,7 @@ version = "0.17.0" dependencies = [ "codspeed-criterion-compat", "jiff", + "jiff-core", "num-traits", "rstest", "winnow", diff --git a/Cargo.toml b/Cargo.toml index 6606f1f5..430ea5f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ rust-version = "1.71.1" [dependencies] winnow = "1.0.0" num-traits = "0.2.19" -jiff = { version = "0.2.15", default-features = false, features = ["tz-system", "tzdb-bundle-platform", "tzdb-zoneinfo"] } +jiff = { version = "0.2.43", default-features = false, features = ["tz-system", "tzdb-bundle-platform", "tzdb-zoneinfo"] } +jiff-core = "0.1.5" [dev-dependencies] rstest = "0.28" diff --git a/src/items/date.rs b/src/items/date.rs index 69cbf382..5a2078fc 100644 --- a/src/items/date.rs +++ b/src/items/date.rs @@ -28,15 +28,15 @@ use winnow::{ ascii::alpha1, - combinator::{alt, eof, opt, preceded, terminated}, - error::ErrMode, + combinator::{alt, dispatch, eof, fail, not, opt, peek, preceded, terminated}, + error::{ContextError, ErrMode}, stream::AsChar, - token::take_while, + token::{any, one_of, take_while}, ModalResult, Parser, }; use super::{ - primitive::{ctx_err, dec_uint, multispace1, s}, + primitive::{ctx_err, dec_uint, dec_uint_str, multispace1, s}, year::{year_from_str, year_str}, }; @@ -130,13 +130,30 @@ impl TryFrom for jiff::civil::Date { /// Parse a date that is not in ISO format. ISO dates are parsed by [`iso1`] /// and [`iso2`], which the caller tries first. pub(super) fn parse_non_iso(input: &mut &str) -> ModalResult { - alt((dotted, us, literal1, literal2, literal3)).parse_next(input) + dispatch! { s(peek(any)); + '0'..='9' => numeric, + 'a'..='z' => literal_month_first, + _ => fail, + } + .parse_next(input) } -/// Parse the European `DD.MM.[YYYY]` spelling. -fn dotted(input: &mut &str) -> ModalResult { - let (day, _, month, _, year) = ( - s(dec_uint), +fn numeric(input: &mut &str) -> ModalResult { + let first = s(dec_uint_str).parse_next(input)?; + + dispatch! { s(peek(any)); + '.' => move |input: &mut &str| dotted_after(first, input), + '/' => move |input: &mut &str| us_after(first, input), + '-' | 'a'..='z' => move |input: &mut &str| literal1_after(first, input), + _ => fail, + } + .parse_next(input) +} + +/// Parse the European `DD.MM.[YYYY]` spelling after the shared numeric prefix. +fn dotted_after(first: &str, input: &mut &str) -> ModalResult { + let day = day_from_str(first)?; + let (_, month, _, year) = ( s('.'), s(dec_uint), s('.'), @@ -181,10 +198,33 @@ pub(super) fn iso2(input: &mut &str) -> ModalResult { .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } -/// Parse `[year]/[month]/[day]` or `[month]/[day]/[year]` or `[month]/[day]`. -fn us(input: &mut &str) -> ModalResult { - let (s1, _, n, s2) = ( - s(take_while(1.., AsChar::is_dec_digit)), +/// Parse either of the compact or separated ISO date forms. +/// +/// Compact dates contain at least five consecutive digits and do not have a +/// possible ISO separator after that digit run. In that case, skip the failed +/// `iso1` attempt. Inputs that could use whitespace/comments before a separator +/// retain the original alternative ordering for compatibility. +pub(super) fn iso(input: &mut &str) -> ModalResult { + if compact_iso_candidate(input) { + iso2.parse_next(input) + } else { + alt((iso1, iso2)).parse_next(input) + } +} + +fn compact_iso_candidate(input: &str) -> bool { + ( + take_while(5.., AsChar::is_dec_digit), + peek(not(one_of::<_, _, ErrMode>([ + ' ', '\t', '\n', '\x0B', '\x0C', '\r', '(', '+', '-', + ]))), + ) + .parse_peek(input) + .is_ok() +} + +fn us_after(first: &str, input: &mut &str) -> ModalResult { + let (_, n, s2) = ( s('/'), s(dec_uint), opt(preceded(s('/'), s(take_while(1.., AsChar::is_dec_digit)))), @@ -192,26 +232,26 @@ fn us(input: &mut &str) -> ModalResult { .parse_next(input)?; match s2 { - Some(s2) if s1.len() >= 4 => { + Some(s2) if first.len() >= 4 => { // [year]/[month]/[day] // // GNU quirk: interpret as [year]/[month]/[day] if the first part is at // least 4 characters long. let day = day_from_str(s2)?; - (s1, n, day) + (first, n, day) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } Some(s2) => { // [month]/[day]/[year] - let month = month_from_str(s1)?; + let month = month_from_str(first)?; (s2, month, n) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } None => { // [month]/[day] - let month = month_from_str(s1)?; + let month = month_from_str(first)?; (month, n) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) @@ -219,11 +259,9 @@ fn us(input: &mut &str) -> ModalResult { } } -/// Parse `14 November 2022`, `14 Nov 2022`, "14nov2022", "14-nov-2022", -/// "14-nov2022", "14nov-2022". -fn literal1(input: &mut &str) -> ModalResult { - let (day, _, month, year) = ( - s(dec_uint), +fn literal1_after(first: &str, input: &mut &str) -> ModalResult { + let day = day_from_str(first)?; + let (_, month, year) = ( opt(s('-')), s(literal_month), opt(terminated( @@ -244,10 +282,19 @@ fn literal1(input: &mut &str) -> ModalResult { } } -/// Parse `November 14, 2022`, `Nov 14, 2022`, and `Nov 14 2022`. -fn literal2(input: &mut &str) -> ModalResult { - let (month, day, year) = ( - s(literal_month), +fn literal_month_first(input: &mut &str) -> ModalResult { + let month = s(literal_month).parse_next(input)?; + + dispatch! { s(peek(any)); + '-' => move |input: &mut &str| literal3_after(month, input), + '0'..='9' => move |input: &mut &str| literal2_after(month, input), + _ => fail, + } + .parse_next(input) +} + +fn literal2_after(month: u8, input: &mut &str) -> ModalResult { + let (day, year) = ( s(dec_uint), opt(terminated( preceded( @@ -273,11 +320,8 @@ fn literal2(input: &mut &str) -> ModalResult { } } -/// Parse `November-14-2022` and `Nov-14-2022`. Unlike `literal2`, the year is mandatory -/// to match the behavior of GNU `date`. -fn literal3(input: &mut &str) -> ModalResult { - let (month, _, day, _, year) = - (s(literal_month), s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?; +fn literal3_after(month: u8, input: &mut &str) -> ModalResult { + let (_, day, _, year) = (s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?; // Map err to Backtrack instead of Cut to avoid early termination of parsing (year, month, day) diff --git a/src/items/mod.rs b/src/items/mod.rs index d107f0e6..c36d7fb1 100644 --- a/src/items/mod.rs +++ b/src/items/mod.rs @@ -54,7 +54,7 @@ use crate::ParsedDateTime; use jiff::Zoned; use primitive::space; use winnow::{ - combinator::{alt, eof, preceded, repeat_till, terminated, trace}, + combinator::{alt, eof, opt, preceded, repeat_till, terminated, trace}, error::{AddContext, ContextError, ErrMode, StrContext, StrContextValue}, stream::Stream, ModalResult, Parser, @@ -216,7 +216,7 @@ fn parse(input: &mut &str) -> ModalResult { /// - Trailing input (aside from optional whitespaces) is rejected. fn parse_timestamp(input: &mut &str) -> ModalResult { // Parse and ignore an optional leading timezone rule. - let _ = timezone::parse(input); + let _ = opt(timezone::parse).parse_next(input)?; trace( "parse_timestamp", @@ -238,7 +238,7 @@ fn parse_timestamp(input: &mut &str) -> ModalResult { /// - Trailing input (aside from optional whitespaces) is rejected. fn parse_items(input: &mut &str) -> ModalResult { // Parse and consume an optional leading timezone rule. - let tz = timezone::parse(input).map(Item::TimeZone); + let tz = opt(timezone::parse).parse_next(input)?.map(Item::TimeZone); // Convert input to lowercase for case-insensitive parsing. let lower: Cow = if input.bytes().any(|b| b.is_ascii_uppercase()) { @@ -255,7 +255,7 @@ fn parse_items(input: &mut &str) -> ModalResult { ) .parse_next(input)?; - if let Ok(tz) = tz { + if let Some(tz) = tz { items.push(tz); } @@ -267,7 +267,7 @@ fn parse_item(input: &mut &str) -> ModalResult { // An ISO date is either the start of a combined date and time item or a // date item on its own. Parse it once instead of once per alternative. let start = input.checkpoint(); - if let Ok(date) = alt((date::iso1, date::iso2)).parse_next(input) { + if let Ok(date) = date::iso.parse_next(input) { let after_date = input.checkpoint(); match combined::parse_time_after_date(input, date.clone()) { Ok(dt) => return Ok(Item::DateTime(dt)), diff --git a/src/items/offset.rs b/src/items/offset.rs index ad2b3597..a240db44 100644 --- a/src/items/offset.rs +++ b/src/items/offset.rs @@ -28,10 +28,10 @@ use std::fmt::Display; use winnow::{ - combinator::{alt, peek}, + combinator::{alt, dispatch, eof, opt, peek, repeat}, error::{ContextError, ErrMode}, stream::{AsChar, Stream}, - token::take_while, + token::{take, take_while}, ModalResult, Parser, }; @@ -196,7 +196,7 @@ pub(super) fn timezone_offset(input: &mut &str) -> ModalResult { // Like GNU, read "+8 years" as a relative item, not as "+8" and "years". // The costlier relative check only runs once an offset has parsed. let start = input.checkpoint(); - let result = alt((timezone_offset_colon, timezone_offset_colonless)).parse_next(input); + let result = timezone_offset_parts.parse_next(input); if matches!(result, Err(ErrMode::Backtrack(_))) { return result; } @@ -242,57 +242,67 @@ fn timezone_name_offset(input: &mut &str) -> ModalResult { Ok(tz) } -/// Parse a timezone offset with a colon separating hours and minutes, e.g., -/// `+08:00`, `+8:00`, `+8:0`. -fn timezone_offset_colon(input: &mut &str) -> ModalResult { - (plus_or_minus, s(dec_uint), s(colon), s(dec_uint)) - .parse_next(input) - .and_then(|(sign, hours, _, minutes)| { - (sign == '-', hours, minutes) - .try_into() - .map_err(|e| ErrMode::Cut(ctx_err(e))) - }) +fn timezone_offset_parts(input: &mut &str) -> ModalResult { + let (sign, digits) = (plus_or_minus, s(dec_uint_str)).parse_next(input)?; + + dispatch! { peek(opt(colon)); + Some(()) => move |input: &mut &str| timezone_offset_colon_after(sign, digits, input), + None => move |_: &mut &str| timezone_offset_colonless_after(sign, digits), + } + .parse_next(input) } -/// Parse a timezone offset without colon, e.g., `+0800`, `+800`, `+08`, `+8`. -fn timezone_offset_colonless(input: &mut &str) -> ModalResult { - (plus_or_minus, s(dec_uint_str)) - .verify_map(|(sign, s)| { - // GNU date accepts numeric offset strings with leading zeroes. For - // example, `+000000110` is valid. In such cases, the string is - // truncated to the last four characters. Thus, `+000000110` becomes - // `+0110` (note that one leading zero is kept). - let s = if s.len() > 4 && s.trim_start_matches('0').len() <= 4 { - &s[s.len() - 4..] - } else { - s - }; - - // Hour and minute values are dependent on the length of the string. - // For example: - // - // - "5" -> 05:00 - // - "05" -> 05:00 - // - "530" -> 05:30 (the minute is the last two characters here) - // - "0530"-> 05:30 - // - "0000530" -> 05:30 - let (h_str, m_str) = match s.len() { - 1 | 2 => (s, "0"), - 3 => s.split_at(1), - 4 => s.split_at(2), - _ => return None, - }; - - let hours = h_str.parse::().ok()?; - let minutes = m_str.parse::().ok()?; - Some((sign, hours, minutes)) - }) - .parse_next(input) - .and_then(|(sign, hours, minutes)| { - (sign == '-', hours, minutes) - .try_into() - .map_err(|e| ErrMode::Cut(ctx_err(e))) - }) +fn timezone_offset_colon_after(sign: char, digits: &str, input: &mut &str) -> ModalResult { + let (_, hours) = dec_uint:: + .parse_peek(digits) + .map_err(|_| ErrMode::Backtrack(ctx_err("timezone hour must be a valid u8 number")))?; + let (_, minutes) = (s(colon), s(dec_uint)).parse_next(input)?; + (sign == '-', hours, minutes) + .try_into() + .map_err(|e| ErrMode::Cut(ctx_err(e))) +} + +fn timezone_offset_colonless_after(sign: char, digits: &str) -> ModalResult { + // GNU date reads one or two digits as hours, three digits as HMM, and four + // digits as HHMM. Extra digits are accepted only when they are leading + // zeroes; the final four digits then provide the HHMM value. + let (_, (hours, minutes)) = alt(( + // One or two digits: hours only, with zero minutes. + ( + take_while(1..=2, AsChar::is_dec_digit).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, _)| (hours, 0)), + // Three digits: one hour digit followed by two minute digits. + ( + take(1usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, minutes, _)| (hours, minutes)), + // Four digits: two hour digits followed by two minute digits. + ( + take(2usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, minutes, _)| (hours, minutes)), + // Longer values: consume leading zeroes while four digits remain, + // then split those final four digits into hours and minutes. + ( + repeat::<_, _, (), _, _>(0.., ('0', peek(take(4usize)))).void(), + take(2usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(_, hours, minutes, _)| (hours, minutes)), + )) + .parse_peek(digits) + .map_err(|_| ErrMode::Backtrack(ctx_err("timezone offset must be 1-4 digits")))?; + + (sign == '-', hours, minutes) + .try_into() + .map_err(|e| ErrMode::Cut(ctx_err(e))) } /// Named timezone list. @@ -439,17 +449,19 @@ mod tests { #[test] fn timezone_offset_without_colon() { for (input, expected) in [ - ("+0000", off(false, 0, 0)), // UTC - ("-0000", off(true, 0, 0)), // UTC - ("+0100", off(false, 1, 0)), // positive offset - ("-0600", off(true, 6, 0)), // negative offset - ("+0530", off(false, 5, 30)), // positive offset with non-zero minutes - ("-0330", off(true, 3, 30)), // negative offset with non-zero minutes - ("- 0330", off(true, 3, 30)), // space after sign - ("+530", off(false, 5, 30)), // single-digit hours - ("+05", off(false, 5, 0)), // double-digit hours and no minutes - ("+5", off(false, 5, 0)), // single-digit hours and no minutes - ("+00000530", off(false, 5, 30)), // leading zeroes + ("+0000", off(false, 0, 0)), // UTC + ("-0000", off(true, 0, 0)), // UTC + ("+0100", off(false, 1, 0)), // positive offset + ("-0600", off(true, 6, 0)), // negative offset + ("+0530", off(false, 5, 30)), // positive offset with non-zero minutes + ("-0330", off(true, 3, 30)), // negative offset with non-zero minutes + ("- 0330", off(true, 3, 30)), // space after sign + ("+530", off(false, 5, 30)), // single-digit hours + ("+05", off(false, 5, 0)), // double-digit hours and no minutes + ("+5", off(false, 5, 0)), // single-digit hours and no minutes + ("+00000530", off(false, 5, 30)), // leading zeroes + ("+000000110", off(false, 1, 10)), // leading zeroes before four digits + ("+00000", off(false, 0, 0)), // leading zeroes before zero offset ("+0000abc", off(false, 0, 0)), // space separator can be ignored if immediately followed by alphas (GNU date behavior) ] { let mut s = input; @@ -460,6 +472,7 @@ mod tests { "+2500", // invalid: hours > 24 "-2361", // invalid: minutes > 60 "+2401", // invalid: minutes > 0 when hours == 24 + "+12345", // invalid: too many non-zero digits "+23 days", // invalid: ambiguous with relative time parsing ] { let mut s = input; @@ -467,6 +480,16 @@ mod tests { } } + #[test] + fn timezone_offset_reports_invalid_minutes() { + let mut input = "+0061"; + let error = timezone_offset(&mut input).unwrap_err(); + + assert!(error + .to_string() + .contains("timezone minute must be between 0 and 60")); + } + #[test] fn timezone_name_without_offset() { for (input, expected) in [ diff --git a/src/items/ordinal.rs b/src/items/ordinal.rs index d80b32a6..bc90aff2 100644 --- a/src/items/ordinal.rs +++ b/src/items/ordinal.rs @@ -3,14 +3,20 @@ use winnow::{ ascii::alpha1, - combinator::{alt, opt}, + combinator::{alt, dispatch, fail, opt, peek}, + token::any, ModalResult, Parser, }; use super::primitive::{dec_uint, s}; pub(super) fn ordinal(input: &mut &str) -> ModalResult { - alt((text_ordinal, number_ordinal)).parse_next(input) + dispatch! { s(peek(any)); + 'a'..='z' => text_ordinal, + '+' | '-' | '0'..='9' => number_ordinal, + _ => fail, + } + .parse_next(input) } fn number_ordinal(input: &mut &str) -> ModalResult { diff --git a/src/items/relative.rs b/src/items/relative.rs index 93bed098..599a7c85 100644 --- a/src/items/relative.rs +++ b/src/items/relative.rs @@ -33,7 +33,8 @@ use winnow::{ ascii::alpha1, - combinator::{alt, opt}, + combinator::{alt, dispatch, opt, peek}, + token::any, ModalResult, Parser, }; @@ -78,18 +79,31 @@ impl TryFrom for jiff::Span { } pub(super) fn parse(input: &mut &str) -> ModalResult { - alt(( - s("tomorrow").value(Relative::Days(1)), - s("yesterday").value(Relative::Days(-1)), - // For "today" and "now", the unit is arbitrary - s("today").value(Relative::Days(0)), - s("now").value(Relative::Days(0)), - seconds, - displacement, - )) + dispatch! { s(peek(any)); + 't' => alt(( + s("tomorrow").value(Relative::Days(1)), + // For "today", the unit is arbitrary + s("today").value(Relative::Days(0)), + displacement, + )), + 'y' => alt(( + s("yesterday").value(Relative::Days(-1)), + displacement, + )), + 'n' => alt(( + // For "now", the unit is arbitrary + s("now").value(Relative::Days(0)), + displacement, + )), + _ => numeric_or_displacement, + } .parse_next(input) } +fn numeric_or_displacement(input: &mut &str) -> ModalResult { + alt((seconds, displacement)).parse_next(input) +} + fn seconds(input: &mut &str) -> ModalResult { ( opt(alt((s('+').value(1), s('-').value(-1)))), diff --git a/src/items/time.rs b/src/items/time.rs index 777ca5e9..c4078344 100644 --- a/src/items/time.rs +++ b/src/items/time.rs @@ -38,7 +38,7 @@ //! > Either ‘am’/‘pm’ or a time zone correction may be specified, but not both. use winnow::{ - combinator::{alt, opt, preceded}, + combinator::{alt, dispatch, fail, opt, peek, preceded}, error::ErrMode, ModalResult, Parser, }; @@ -46,7 +46,7 @@ use winnow::{ use super::{ epoch::sec_and_nsec, offset::{timezone_offset, Offset}, - primitive::{colon, ctx_err, dec_uint, s}, + primitive::{colon, ctx_err, dec_uint, s, space}, }; #[derive(PartialEq, Clone, Debug, Default)] @@ -86,30 +86,42 @@ pub(crate) fn parse(input: &mut &str) -> ModalResult