Skip to content

Commit

Permalink
upgrade to nom v7
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Sep 10, 2021
1 parent c462683 commit f0b3dfe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version = "0.1.36"
default-features = false

[dependencies.nom]
version = "6"
version = "7.0.0"
default-features = false

[dependencies.chrono]
Expand Down
29 changes: 24 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ pub fn take_n_digits(n: usize) -> impl FnMut(&str) -> StrResult<&str> {
pub fn two_digits<T: FromStr>(remain: &str) -> StrResult<T> {
let (remain, two) = take_n_digits(2)(remain)?;
// NonZeroU8's FromStr implementation rejects 00.
let (_, parsed) = nom::parse_to!(two, T)?;
let parsed = two.parse_to_err()?;
Ok((remain, parsed))
}

/// Level 0 month or day. Two digits, and the range is not checked here.
pub fn two_digits_zero_none(remain: &str) -> StrResult<Option<NonZeroU8>> {
let (remain, two) = take_n_digits(2).parse(remain)?;
// NonZeroU8's FromStr implementation rejects 00.
let (_, parsed) = nom::parse_to!(two, u8)?;
let parsed = two.parse_to_err()?;
Ok((remain, NonZeroU8::new(parsed)))
}

Expand All @@ -124,10 +124,29 @@ pub enum UnvalidatedTz {
HoursMinutes { positive: bool, hh: u8, mm: u8 },
}

pub(crate) trait ParseToExt<R>: ParseTo<R>
where
R: FromStr,
{
fn parse_to_err(self) -> Result<R, nom::Err<nom::error::Error<Self>>>
where
Self: Sized,
{
self.parse_to().ok_or_else(|| {
nom::Err::Error(NomParseError::from_error_kind(
self,
// whatever. nom should have an Other errorkind.
nom::error::ErrorKind::MapRes,
))
})
}
}
impl<S: ParseTo<R>, R: FromStr> ParseToExt<R> for S {}

pub fn year_n(n: usize) -> impl FnMut(&str) -> StrResult<i32> {
move |remain| {
let (remain, four) = take_n_digits(n)(remain)?;
let (_, parsed) = nom::parse_to!(four, i32)?;
let parsed: i32 = four.parse_to_err()?;
Ok((remain, parsed))
}
}
Expand All @@ -136,7 +155,7 @@ pub fn year_n_signed(n: usize) -> impl FnMut(&str) -> StrResult<i32> {
move |remain| {
let (remain, sign) = minus_sign(-1i32, 1)(remain)?;
let (remain, four) = take_n_digits(n)(remain)?;
let (_, parsed) = nom::parse_to!(four, i32)?;
let parsed: i32 = four.parse_to_err()?;
if sign == -1 && parsed == 0 {
return Err(nom::Err::Error(NomParseError::from_error_kind(
remain,
Expand All @@ -153,7 +172,7 @@ pub fn signed_year_min_n(n: usize) -> impl FnMut(&str) -> StrResult<i64> {
let nonzero_digit = ncc::satisfy(|c| c.is_ascii_digit() && c != '0');
let (remain, sign) = minus_sign(-1i64, 1i64).parse(remain)?;
let (remain, digs) = nc::recognize(nonzero_digit.and(take_min_n_digits(n - 1)))(remain)?;
let (_, parsed) = nom::parse_to!(digs, i64)?;
let parsed: i64 = digs.parse_to_err()?;
Ok((remain, parsed * sign))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/level0/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//
// Copyright © 2021 Corporation for Digital Scholarship

use crate::helpers::ParserExt;
use crate::ParseError;
use crate::{common::ParseToExt, helpers::ParserExt};

#[allow(unused_imports)]
use nom::{
Expand Down Expand Up @@ -78,7 +78,7 @@ pub(crate) fn date(remain: &str) -> StrResult<Date> {
/// Level 0 year only, so simply exactly four digits 0-9. That's it.
fn year4(remain: &str) -> StrResult<i32> {
let (remain, four) = take_n_digits(4)(remain)?;
let (_, parsed) = nom::parse_to!(four, i32)?;
let parsed = four.parse_to_err()?;
Ok((remain, parsed))
}

Expand Down
14 changes: 4 additions & 10 deletions src/level2/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nom::{
};

use crate::{
common::{signed_year_min_n, year_n_signed, StrResult},
common::{signed_year_min_n, year_n_signed, ParseToExt, StrResult},
helpers::ParserExt,
ParseError,
};
Expand Down Expand Up @@ -80,14 +80,8 @@ fn scientific_y(remain: &str) -> StrResult<ScientificYear> {
.or(ns::preceded(ncc::char('Y'), signed_year_min_n(5)).map(|y| (y, None)))
.and(ns::preceded(s, ncc::digit1).optional())
.parse(remain)?;
let exponent = opt_e
.map(|e| nom::parse_to!(e, u16))
.transpose()?
.map(|x| x.1);
let sig_digits = opt_s
.map(|s| nom::parse_to!(s, u16))
.transpose()?
.map(|x| x.1);
let exponent = opt_e.map(ParseToExt::<u16>::parse_to_err).transpose()?;
let sig_digits = opt_s.map(ParseToExt::<u16>::parse_to_err).transpose()?;
Ok((
remain,
ScientificYear {
Expand All @@ -103,7 +97,7 @@ fn scientific_4digit(remain: &str) -> StrResult<ScientificYear> {
let (remain, (year, sd)) = year_n_signed(4)
.and(ns::preceded(s, ncc::digit1))
.parse(remain)?;
let (_, sd) = nom::parse_to!(sd, u16)?;
let sd: u16 = sd.parse_to_err()?;
Ok((
remain,
ScientificYear {
Expand Down

0 comments on commit f0b3dfe

Please sign in to comment.