From 8ecde698fa45a09112dd1857fca236c3cbf96c0a Mon Sep 17 00:00:00 2001 From: eatradish Date: Wed, 4 Mar 2026 16:51:50 +0800 Subject: [PATCH] chore: update `nom` to 8 --- Cargo.toml | 2 +- src/parser/compiled.rs | 46 ++++++++++++++++++++++------------------- src/parser/expansion.rs | 34 ++++++++++++++++-------------- tests/parser.rs | 44 ++++++++++++++++++++------------------- 4 files changed, 68 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 918b4be..918d4c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ categories = ["command-line-interface"] build = "build.rs" [dependencies] -nom = { version = "7", default-features = false, features = ["std"] } +nom = { version = "8", default-features = false, features = ["std"] } phf = "0.11" fnv = "1.0" diff --git a/src/parser/compiled.rs b/src/parser/compiled.rs index 241be85..1068236 100644 --- a/src/parser/compiled.rs +++ b/src/parser/compiled.rs @@ -17,7 +17,7 @@ use nom::bytes::streaming::{tag, take, take_until}; use nom::combinator::{complete, cond, map, map_opt, map_parser, opt}; use nom::multi::count; use nom::number::streaming::{le_i16, le_i32}; -use nom::IResult; +use nom::{IResult, Parser}; use std::str; use crate::capability::Value; @@ -126,7 +126,7 @@ fn bit_size(magic: &[u8]) -> usize { } pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> { - let (input, magic) = alt((tag([0x1A, 0x01]), tag([0x1E, 0x02])))(input)?; + let (input, magic) = alt((tag(&[0x1A, 0x01][..]), tag(&[0x1E, 0x02][..]))).parse(input)?; let (input, name_size) = size(input)?; let (input, bool_count) = size(input)?; @@ -134,20 +134,21 @@ pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> { let (input, string_count) = size(input)?; let (input, table_size) = size(input)?; - let (input, names) = map_parser(take(name_size), take_until("\x00"))(input)?; + let (input, names) = map_parser(take(name_size), take_until("\x00")).parse(input)?; - let (input, booleans) = count(boolean, bool_count)(input)?; + let (input, booleans) = count(boolean, bool_count).parse(input)?; - let (input, _) = cond((name_size + bool_count) % 2 != 0, take(1_usize))(input)?; + let (input, _) = cond((name_size + bool_count) % 2 != 0, take(1_usize)).parse(input)?; - let (input, numbers) = count(|input| capability(input, bit_size(magic)), num_count)(input)?; + let (input, numbers) = + count(|input| capability(input, bit_size(magic)), num_count).parse(input)?; - let (input, strings) = count(|input| capability(input, 16), string_count)(input)?; + let (input, strings) = count(|input| capability(input, 16), string_count).parse(input)?; let (input, table) = take(table_size)(input)?; let (input, extended) = opt(complete(|input| { - let (input, _) = cond(table_size % 2 != 0, take(1_usize))(input)?; + let (input, _) = cond(table_size % 2 != 0, take(1_usize)).parse(input)?; let (input, ext_bool_count) = size(input)?; let (input, ext_num_count) = size(input)?; @@ -155,24 +156,25 @@ pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> { let (input, _ext_offset_count) = size(input)?; let (input, ext_table_size) = size(input)?; - let (input, booleans) = count(boolean, ext_bool_count)(input)?; + let (input, booleans) = count(boolean, ext_bool_count).parse(input)?; - let (input, _) = cond(ext_bool_count % 2 != 0, take(1_usize))(input)?; + let (input, _) = cond(ext_bool_count % 2 != 0, take(1_usize)).parse(input)?; let (input, numbers) = - count(|input| capability(input, bit_size(magic)), ext_num_count)(input)?; + count(|input| capability(input, bit_size(magic)), ext_num_count).parse(input)?; - let (input, strings) = count(|input| capability(input, 16), ext_string_count)(input)?; + let (input, strings) = + count(|input| capability(input, 16), ext_string_count).parse(input)?; - let (input, names) = count( - |input| capability(input, 16), - ext_bool_count + ext_num_count + ext_string_count, - )(input)?; + let (input, names) = + count(|input| capability(input, 16), ext_bool_count + ext_num_count + ext_string_count) + .parse(input)?; - let (input, table) = take(ext_table_size)(input)?; + let (input, table) = take(ext_table_size).parse(input)?; Ok((input, Extended { booleans, numbers, strings, names, table })) - }))(input)?; + })) + .parse(input)?; Ok(( input, @@ -181,7 +183,7 @@ pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> { } fn boolean(input: &[u8]) -> IResult<&[u8], bool> { - alt((map(tag([0]), |_| false), map(tag([1]), |_| true)))(input) + alt((map(tag(&[0][..]), |_| false), map(tag(&[1][..]), |_| true))).parse(input) } fn size(input: &[u8]) -> IResult<&[u8], usize> { @@ -189,7 +191,8 @@ fn size(input: &[u8]) -> IResult<&[u8], usize> { -1 => Some(0), n if n >= 0 => Some(n as usize), _ => None, - })(input) + }) + .parse(input) } fn capability(input: &[u8], bits: usize) -> IResult<&[u8], i32> { @@ -199,7 +202,8 @@ fn capability(input: &[u8], bits: usize) -> IResult<&[u8], i32> { |o| o, ), map_opt(cond(bits == 32, map_opt(le_i32, |n| if n >= -2 { Some(n) } else { None })), |o| o), - ))(input) + )) + .parse(input) } #[cfg(test)] diff --git a/src/parser/expansion.rs b/src/parser/expansion.rs index fad768c..5b55d53 100644 --- a/src/parser/expansion.rs +++ b/src/parser/expansion.rs @@ -16,11 +16,10 @@ use crate::parser::util::number; use nom::branch::alt; use nom::bytes::complete; use nom::bytes::streaming::{tag, take, take_while}; -use nom::character::is_digit; use nom::character::streaming::one_of; use nom::combinator::{map, opt, value}; use nom::error::{make_error, ErrorKind}; -use nom::IResult; +use nom::{AsChar, IResult, Parser}; #[derive(Eq, PartialEq, Copy, Clone, Debug)] pub enum Item<'a> { @@ -116,26 +115,27 @@ pub struct Flags { } pub fn parse(input: &[u8]) -> IResult<&[u8], Item<'_>> { - alt((expansion, string))(input) + alt((expansion, string)).parse(input) } fn string(input: &[u8]) -> IResult<&[u8], Item<'_>> { - map(complete::take_till(|b| b == b'%'), Item::String)(input) + map(complete::take_till(|b| b == b'%'), Item::String).parse(input) } fn expansion(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = tag("%")(input)?; - let (input, item) = alt((percent, constant, variable, operation, conditional, print))(input)?; + let (input, item) = + alt((percent, constant, variable, operation, conditional, print)).parse(input)?; Ok((input, item)) } fn percent(input: &[u8]) -> IResult<&[u8], Item<'_>> { - value(Item::String(b"%"), tag("%"))(input) + value(Item::String(b"%"), tag("%")).parse(input) } fn constant(input: &[u8]) -> IResult<&[u8], Item<'_>> { - alt((constant_char, constant_integer))(input) + alt((constant_char, constant_integer)).parse(input) } fn constant_char(input: &[u8]) -> IResult<&[u8], Item<'_>> { @@ -148,7 +148,7 @@ fn constant_char(input: &[u8]) -> IResult<&[u8], Item<'_>> { fn constant_integer(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = tag("{")(input)?; - let (input, digit) = take_while(is_digit)(input)?; + let (input, digit) = take_while(AsChar::is_dec_digit)(input)?; let (input, _) = tag("}")(input)?; Ok((input, Item::Constant(Constant::Integer(number(digit))))) @@ -159,7 +159,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> { match c { b"l" => Ok((input, Item::Variable(Variable::Length))), - b"p" => map(one_of("123456789"), |n| Item::Variable(Variable::Push(n as u8 - b'1')))(input), + b"p" => map(one_of("123456789"), |n| Item::Variable(Variable::Push(n as u8 - b'1'))) + .parse(input), b"P" => alt(( map(one_of("abcdefghijklmnopqrstuvwxyz"), |n| { @@ -168,7 +169,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> { map(one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |n| { Item::Variable(Variable::Set(false, n as u8 - b'A')) }), - ))(input), + )) + .parse(input), b"g" => alt(( map(one_of("abcdefghijklmnopqrstuvwxyz"), |n| { @@ -177,7 +179,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> { map(one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |n| { Item::Variable(Variable::Get(false, n as u8 - b'A')) }), - ))(input), + )) + .parse(input), _ => Err(nom::Err::Error(make_error(input, ErrorKind::Switch))), } @@ -223,16 +226,17 @@ fn conditional(input: &[u8]) -> IResult<&[u8], Item<'_>> { } fn print(input: &[u8]) -> IResult<&[u8], Item<'_>> { - let (input, _) = opt(tag(":"))(input)?; + let (input, _) = opt(tag(":")).parse(input)?; let (input, flags) = take_while(is_flag)(input)?; - let (input, width) = opt(take_while(is_digit))(input)?; + let (input, width) = opt(take_while(AsChar::is_dec_digit)).parse(input)?; let (input, precision) = opt(|input| { let (input, _) = tag(".")(input)?; - let (input, amount) = take_while(is_digit)(input)?; + let (input, amount) = take_while(AsChar::is_dec_digit).parse(input)?; Ok((input, amount)) - })(input)?; + }) + .parse(input)?; let (input, format) = one_of("doxXsc")(input)?; diff --git a/tests/parser.rs b/tests/parser.rs index ca33cae..4810d2f 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -15,11 +15,11 @@ use nom::branch::alt; use nom::bytes::streaming::{tag, take, take_until, take_while}; use nom::character::streaming::char; -use nom::character::{is_digit, streaming::line_ending as eol}; +use nom::character::streaming::line_ending as eol; use nom::combinator::{complete, eof, map, map_res, opt}; use nom::error::{make_error, ErrorKind}; use nom::sequence::terminated; -use nom::IResult; +use nom::{AsChar, IResult, Parser}; use std::borrow::Cow; use std::fs::File; use std::io::Read; @@ -96,11 +96,11 @@ pub fn is_printable_no_control(ch: u8) -> bool { } pub fn ws(input: &[u8]) -> IResult<&[u8], char> { - alt((char(' '), char('\t')))(input) + alt((char(' '), char('\t'))).parse(input) } pub fn end(input: &[u8]) -> IResult<&[u8], &[u8]> { - alt((eof, eol))(input) + alt((eof, eol)).parse(input) } pub fn unescape(i: &[u8]) -> Cow<'_, [u8]> { @@ -134,8 +134,8 @@ pub fn unescape(i: &[u8]) -> Cow<'_, [u8]> { Some(b'0') => output.push(0x00), - Some(a) if is_digit(a) => match (iter.next(), iter.next()) { - (Some(b), Some(c)) if is_digit(b) && is_digit(c) => { + Some(a) if AsChar::is_dec_digit(a) => match (iter.next(), iter.next()) { + (Some(b), Some(c)) if AsChar::is_dec_digit(b) && AsChar::is_dec_digit(c) => { if let Ok(number) = u8::from_str_radix(unsafe { str::from_utf8_unchecked(&[a, b, c]) }, 8) { @@ -215,32 +215,35 @@ pub enum Item<'a> { } pub fn parse(input: &[u8]) -> IResult<&[u8], Item<'_>> { - alt((comment, definition, disable, entry))(input) + alt((comment, definition, disable, entry)).parse(input) } fn comment(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = tag("#")(input)?; - let (input, content) = map_res(terminated(take_until("\n"), tag("\n")), str::from_utf8)(input)?; - let (input, _) = opt(complete(take_while(is_eol)))(input)?; + let (input, content) = + map_res(terminated(take_until("\n"), tag("\n")), str::from_utf8).parse(input)?; + let (input, _) = opt(complete(take_while(is_eol))).parse(input)?; Ok((input, Item::Comment(content.trim()))) } fn definition(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, name) = - map(take_while(is_printable_no_pipe), |n| unsafe { str::from_utf8_unchecked(n) })(input)?; + map(take_while(is_printable_no_pipe), |n| unsafe { str::from_utf8_unchecked(n) }) + .parse(input)?; let (input, _) = tag("|")(input)?; let (input, content) = - map(take_while(is_printable_no_comma), |n| unsafe { str::from_utf8_unchecked(n) })(input)?; + map(take_while(is_printable_no_comma), |n| unsafe { str::from_utf8_unchecked(n) }) + .parse(input)?; let (input, _) = tag(",")(input)?; let (input, _) = take_while(is_ws)(input)?; let (input, _) = eol(input)?; - let (input, _) = opt(complete(take_while(is_eol)))(input)?; + let (input, _) = opt(complete(take_while(is_eol))).parse(input)?; Ok((input, { let mut aliases = content.split('|').map(|n| n.trim()).collect::>(); @@ -255,14 +258,13 @@ fn disable(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = tag("@")(input)?; let (input, name) = - map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })( - input, - )?; + map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) }) + .parse(input)?; let (input, _) = tag(",")(input)?; let (input, _) = take_while(is_ws)(input)?; let (input, _) = end(input)?; - let (input, _) = opt(complete(take_while(is_eol)))(input)?; + let (input, _) = opt(complete(take_while(is_eol))).parse(input)?; Ok((input, Item::Disable(name))) } @@ -272,9 +274,8 @@ fn entry(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = take_while(is_ws)(input)?; let (input, name) = - map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })( - input, - )?; + map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) }) + .parse(input)?; let (input, c) = take(1_usize)(input)?; let (input, value) = match c { @@ -282,7 +283,8 @@ fn entry(input: &[u8]) -> IResult<&[u8], Item<'_>> { b"#" => { let (input, value) = - map(take_while(is_digit), |n| unsafe { str::from_utf8_unchecked(n) })(input)?; + map(take_while(AsChar::is_dec_digit), |n| unsafe { str::from_utf8_unchecked(n) }) + .parse(input)?; let (input, _) = tag(",")(input)?; @@ -302,7 +304,7 @@ fn entry(input: &[u8]) -> IResult<&[u8], Item<'_>> { let (input, _) = take_while(is_ws)(input)?; let (input, _) = end(input)?; - let (input, _) = opt(complete(take_while(is_eol)))(input)?; + let (input, _) = opt(complete(take_while(is_eol))).parse(input)?; Ok((input, value)) }