-
Notifications
You must be signed in to change notification settings - Fork 25
formalize grammar of relative time strings and use nom to parse them #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
use relative_time::relative_times; | ||
use weekday::weekday; | ||
|
||
mod primitive; | ||
mod relative_time; | ||
mod weekday; | ||
|
||
// TODO: more specific errors? | ||
#[derive(Debug)] | ||
pub(crate) struct ParseError; | ||
|
||
pub(crate) use relative_time::RelativeTime; | ||
pub(crate) use relative_time::TimeUnit; | ||
pub(crate) use weekday::WeekdayItem; | ||
|
||
/// Parses a string of relative times into a vector of `RelativeTime` structs. | ||
pub(crate) fn parse_relative_times(input: &str) -> Result<Vec<RelativeTime>, ParseError> { | ||
relative_times(input) | ||
.map(|(_, times)| times) | ||
.map_err(|_| ParseError) | ||
} | ||
|
||
/// Parses a string of weekday into a `WeekdayItem` struct. | ||
pub(crate) fn parse_weekday(input: &str) -> Result<WeekdayItem, ParseError> { | ||
weekday(input) | ||
.map(|(_, weekday_item)| weekday_item) | ||
.map_err(|_| ParseError) | ||
} | ||
|
||
/// Finds a value in a list of pairs by its key. | ||
fn find_in_pairs<T: Clone>(pairs: &[(&str, T)], key: &str) -> Option<T> { | ||
pairs.iter().find_map(|(k, v)| { | ||
if k.eq_ignore_ascii_case(key) { | ||
Some(v.clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
//! Module to parser relative time strings. | ||
//! | ||
//! Grammar definition: | ||
//! | ||
//! ```ebnf | ||
//! ordinal = "last" | "this" | "next" | ||
//! | "first" | "third" | "fourth" | "fifth" | ||
//! | "sixth" | "seventh" | "eighth" | "ninth" | ||
//! | "tenth" | "eleventh" | "twelfth" ; | ||
//! | ||
//! integer = [ sign ] , digit , { digit } ; | ||
//! | ||
//! sign = { ("+" | "-") , { whitespace } } ; | ||
//! | ||
//! digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; | ||
//! ``` | ||
|
||
use nom::{ | ||
bytes::complete::take_while1, | ||
character::complete::{digit1, multispace0, one_of}, | ||
combinator::{map_res, opt}, | ||
multi::fold_many1, | ||
sequence::terminated, | ||
IResult, Parser, | ||
}; | ||
|
||
use super::find_in_pairs; | ||
|
||
const ORDINALS: &[(&str, i64)] = &[ | ||
("last", -1), | ||
("this", 0), | ||
("next", 1), | ||
("first", 1), | ||
// Unfortunately we can't use "second" as ordinal, the keyword is overloaded | ||
("third", 3), | ||
("fourth", 4), | ||
("fifth", 5), | ||
("sixth", 6), | ||
("seventh", 7), | ||
("eighth", 8), | ||
("ninth", 9), | ||
("tenth", 10), | ||
("eleventh", 11), | ||
("twelfth", 12), | ||
]; | ||
|
||
pub(super) fn ordinal(input: &str) -> IResult<&str, i64> { | ||
map_res(take_while1(|c: char| c.is_alphabetic()), |s: &str| { | ||
find_in_pairs(ORDINALS, s).ok_or("unknown ordinal") | ||
}) | ||
.parse(input) | ||
} | ||
|
||
pub(super) fn integer(input: &str) -> IResult<&str, i64> { | ||
let (rest, sign) = opt(sign).parse(input)?; | ||
let (rest, num) = map_res(digit1, str::parse::<i64>).parse(rest)?; | ||
if sign == Some('-') { | ||
Ok((rest, -num)) | ||
} else { | ||
Ok((rest, num)) | ||
} | ||
} | ||
|
||
/// Parses a sign (either + or -) from the input string. The input string must | ||
/// start with a sign character followed by arbitrary number of interleaving | ||
/// sign characters and whitespace characters. All but the last sign character | ||
/// is ignored, and the last sign character is returned as the result. This | ||
/// quirky behavior is to stay consistent with GNU date. | ||
fn sign(input: &str) -> IResult<&str, char> { | ||
fold_many1( | ||
terminated(one_of("+-"), multispace0), | ||
|| '+', | ||
|acc, c| if "+-".contains(c) { c } else { acc }, | ||
) | ||
.parse(input) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_ordinal() { | ||
assert!(ordinal("").is_err()); | ||
assert!(ordinal("invalid").is_err()); | ||
assert!(ordinal(" last").is_err()); | ||
|
||
assert_eq!(ordinal("last"), Ok(("", -1))); | ||
assert_eq!(ordinal("this"), Ok(("", 0))); | ||
assert_eq!(ordinal("next"), Ok(("", 1))); | ||
assert_eq!(ordinal("first"), Ok(("", 1))); | ||
assert_eq!(ordinal("third"), Ok(("", 3))); | ||
assert_eq!(ordinal("fourth"), Ok(("", 4))); | ||
assert_eq!(ordinal("fifth"), Ok(("", 5))); | ||
assert_eq!(ordinal("sixth"), Ok(("", 6))); | ||
assert_eq!(ordinal("seventh"), Ok(("", 7))); | ||
assert_eq!(ordinal("eighth"), Ok(("", 8))); | ||
assert_eq!(ordinal("ninth"), Ok(("", 9))); | ||
assert_eq!(ordinal("tenth"), Ok(("", 10))); | ||
assert_eq!(ordinal("eleventh"), Ok(("", 11))); | ||
assert_eq!(ordinal("twelfth"), Ok(("", 12))); | ||
|
||
// Boundary | ||
assert_eq!(ordinal("last123"), Ok(("123", -1))); | ||
assert_eq!(ordinal("last abc"), Ok((" abc", -1))); | ||
assert!(ordinal("lastabc").is_err()); | ||
|
||
// Case insensitive | ||
assert_eq!(ordinal("THIS"), Ok(("", 0))); | ||
assert_eq!(ordinal("This"), Ok(("", 0))); | ||
} | ||
|
||
#[test] | ||
fn test_integer() { | ||
assert!(integer("").is_err()); | ||
assert!(integer("invalid").is_err()); | ||
assert!(integer(" 123").is_err()); | ||
|
||
assert_eq!(integer("123"), Ok(("", 123))); | ||
assert_eq!(integer("+123"), Ok(("", 123))); | ||
assert_eq!(integer("- 123"), Ok(("", -123))); | ||
|
||
// Boundary | ||
assert_eq!(integer("- 123abc"), Ok(("abc", -123))); | ||
assert_eq!(integer("- +- 123abc"), Ok(("abc", -123))); | ||
} | ||
|
||
#[test] | ||
fn test_sign() { | ||
assert!(sign("").is_err()); | ||
assert!(sign("invalid").is_err()); | ||
assert!(sign(" +").is_err()); | ||
|
||
assert_eq!(sign("+"), Ok(("", '+'))); | ||
assert_eq!(sign("-"), Ok(("", '-'))); | ||
assert_eq!(sign("- + - "), Ok(("", '-'))); | ||
|
||
// Boundary | ||
assert_eq!(sign("- + - abc"), Ok(("abc", '-'))); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the surrounding documentation/comments to reflect the change in API from parse_weekday to parse_weekday_at_date, noting that a reference date is now required for accurate weekday resolution.
Copilot uses AI. Check for mistakes.