Skip to content

Commit

Permalink
Merge pull request #7227 from cakebaker/bump_nom
Browse files Browse the repository at this point in the history
Bump `nom` and adapt `tr` to API changes
  • Loading branch information
sylvestre authored Jan 27, 2025
2 parents bcc406a + 5efd51b commit 7747351
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
25 changes: 17 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ lscolors = { version = "0.20.0", default-features = false, features = [
memchr = "2.7.2"
memmap2 = "0.9.4"
nix = { version = "0.29", default-features = false }
nom = "7.1.3"
nom = "8.0.0"
notify = { version = "=8.0.0", features = ["macos_kqueue"] }
num-bigint = "0.4.4"
num-prime = "0.4.4"
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ skip = [
{ name = "itertools", version = "0.13.0" },
# indexmap
{ name = "hashbrown", version = "0.14.5" },
# parse_datetime, cexpr (via bindgen)
{ name = "nom", version = "7.1.3" },
]
# spell-checker: enable

Expand Down
66 changes: 39 additions & 27 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nom::{
combinator::{map, map_opt, peek, recognize, value},
multi::{many0, many_m_n},
sequence::{delimited, preceded, separated_pair},
IResult,
IResult, Parser,
};
use std::{
char,
Expand Down Expand Up @@ -298,7 +298,8 @@ impl Sequence {
map(Self::parse_backslash_or_char_with_warning, |s| {
Ok(Self::Char(s))
}),
)))(input)
)))
.parse(input)
.map(|(_, r)| r)
.unwrap()
.into_iter()
Expand All @@ -308,7 +309,7 @@ impl Sequence {
fn parse_octal(input: &[u8]) -> IResult<&[u8], u8> {
// For `parse_char_range`, `parse_char_star`, `parse_char_repeat`, `parse_char_equal`.
// Because in these patterns, there's no ambiguous cases.
preceded(tag("\\"), Self::parse_octal_up_to_three_digits)(input)
preceded(tag("\\"), Self::parse_octal_up_to_three_digits).parse(input)
}

fn parse_octal_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
Expand All @@ -321,7 +322,8 @@ impl Sequence {
// See test `test_multibyte_octal_sequence`
Self::parse_octal_two_digits,
)),
)(input)
)
.parse(input)
}

fn parse_octal_up_to_three_digits(input: &[u8]) -> IResult<&[u8], u8> {
Expand All @@ -331,7 +333,8 @@ impl Sequence {
let str_to_parse = std::str::from_utf8(out).unwrap();
u8::from_str_radix(str_to_parse, 8).ok()
},
)(input)
)
.parse(input)
}

fn parse_octal_up_to_three_digits_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
Expand All @@ -353,42 +356,46 @@ impl Sequence {
}
result
},
)(input)
).parse(input)
}

fn parse_octal_two_digits(input: &[u8]) -> IResult<&[u8], u8> {
map_opt(
recognize(many_m_n(2, 2, one_of("01234567"))),
|out: &[u8]| u8::from_str_radix(std::str::from_utf8(out).unwrap(), 8).ok(),
)(input)
)
.parse(input)
}

fn parse_backslash(input: &[u8]) -> IResult<&[u8], u8> {
preceded(tag("\\"), Self::single_char)(input).map(|(l, a)| {
let c = match a {
b'a' => unicode_table::BEL,
b'b' => unicode_table::BS,
b'f' => unicode_table::FF,
b'n' => unicode_table::LF,
b'r' => unicode_table::CR,
b't' => unicode_table::HT,
b'v' => unicode_table::VT,
x => x,
};
(l, c)
})
preceded(tag("\\"), Self::single_char)
.parse(input)
.map(|(l, a)| {
let c = match a {
b'a' => unicode_table::BEL,
b'b' => unicode_table::BS,
b'f' => unicode_table::FF,
b'n' => unicode_table::LF,
b'r' => unicode_table::CR,
b't' => unicode_table::HT,
b'v' => unicode_table::VT,
x => x,
};
(l, c)
})
}

fn parse_backslash_or_char(input: &[u8]) -> IResult<&[u8], u8> {
alt((Self::parse_octal, Self::parse_backslash, Self::single_char))(input)
alt((Self::parse_octal, Self::parse_backslash, Self::single_char)).parse(input)
}

fn parse_backslash_or_char_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
alt((
Self::parse_octal_with_warning,
Self::parse_backslash,
Self::single_char,
))(input)
))
.parse(input)
}

fn single_char(input: &[u8]) -> IResult<&[u8], u8> {
Expand All @@ -400,7 +407,8 @@ impl Sequence {
Self::parse_backslash_or_char,
tag("-"),
Self::parse_backslash_or_char,
)(input)
)
.parse(input)
.map(|(l, (a, b))| {
(l, {
let (start, end) = (u32::from(a), u32::from(b));
Expand All @@ -417,7 +425,8 @@ impl Sequence {
}

fn parse_char_star(input: &[u8]) -> IResult<&[u8], Result<Self, BadSequence>> {
delimited(tag("["), Self::parse_backslash_or_char, tag("*]"))(input)
delimited(tag("["), Self::parse_backslash_or_char, tag("*]"))
.parse(input)
.map(|(l, a)| (l, Ok(Self::CharStar(a))))
}

Expand All @@ -433,7 +442,8 @@ impl Sequence {
take_till(|ue| matches!(ue, b']' | b'\\')),
),
tag("]"),
)(input)
)
.parse(input)
.map(|(l, (c, cnt_str))| {
let s = String::from_utf8_lossy(cnt_str);
let result = if cnt_str.starts_with(b"0") {
Expand Down Expand Up @@ -477,7 +487,8 @@ impl Sequence {
value(Err(BadSequence::MissingCharClassName), tag("")),
)),
tag(":]"),
)(input)
)
.parse(input)
}

fn parse_char_equal(input: &[u8]) -> IResult<&[u8], Result<Self, BadSequence>> {
Expand All @@ -491,7 +502,8 @@ impl Sequence {
map(Self::parse_backslash_or_char, |c| Ok(Self::Char(c))),
)),
tag("=]"),
)(input)
)
.parse(input)
}
}

Expand Down

0 comments on commit 7747351

Please sign in to comment.