diff --git a/Cargo.lock b/Cargo.lock index e00262ca8e0..e4f58669bcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,7 +1150,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" dependencies = [ - "nom", + "nom 7.1.3", ] [[package]] @@ -5427,7 +5427,7 @@ dependencies = [ "mime", "mime_guess", "net_traits", - "nom", + "nom 8.0.0", "pixels", "profile_traits", "rayon", @@ -5536,6 +5536,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -7167,7 +7176,7 @@ dependencies = [ "mozangle", "mozjs", "net_traits", - "nom", + "nom 8.0.0", "num-traits", "num_cpus", "parking_lot", diff --git a/Cargo.toml b/Cargo.toml index 3b348d3f1b9..4f272e13a28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,7 @@ mime_guess = "2.0.5" mozangle = "0.5.3" net_traits = { path = "components/shared/net" } nix = "0.29" -nom = "7.1.3" +nom = "8.0.0" num-traits = "0.2" num_cpus = "1.17.0" openxr = "0.19" diff --git a/components/net/cookie.rs b/components/net/cookie.rs index a5cf597505e..21d2e4a883f 100644 --- a/components/net/cookie.rs +++ b/components/net/cookie.rs @@ -13,12 +13,12 @@ use cookie::Cookie; use log::{Level, debug, log_enabled}; use net_traits::CookieSource; use net_traits::pub_domains::is_pub_domain; -use nom::IResult; use nom::branch::alt; use nom::bytes::complete::{tag, tag_no_case, take, take_while_m_n}; use nom::combinator::{opt, recognize}; use nom::multi::{many0, many1, separated_list1}; -use nom::sequence::{delimited, preceded, terminated, tuple}; +use nom::sequence::{delimited, preceded, terminated}; +use nom::{IResult, Parser}; use serde::{Deserialize, Serialize}; use servo_url::ServoUrl; use time::{Date, Duration, Month, OffsetDateTime, Time}; @@ -431,23 +431,26 @@ impl ServoCookie { } }; // time-field = 1*2DIGIT - let time_field = |input| take_while_m_n(1, 2, |byte: u8| byte.is_ascii_digit())(input); + let time_field = + |input| take_while_m_n(1, 2, |byte: u8| byte.is_ascii_digit()).parse(input); // hms-time = time-field ":" time-field ":" time-field let hms_time = |input| { - tuple(( + ( time_field, preceded(tag(":"), time_field), preceded(tag(":"), time_field), - ))(input) + ) + .parse(input) }; // time = hms-time [ non-digit *OCTET ] - let time = |input| terminated(hms_time, opt(tuple((non_digit, any_octets))))(input); + let time = |input| terminated(hms_time, opt((non_digit, any_octets))).parse(input); // year = 2*4DIGIT [ non-digit *OCTET ] let year = |input| { terminated( take_while_m_n(2, 4, |byte: u8| byte.is_ascii_digit()), - opt(tuple((non_digit, any_octets))), - )(input) + opt((non_digit, any_octets)), + ) + .parse(input) }; // month = ( "jan" / "feb" / "mar" / "apr" / // "may" / "jun" / "jul" / "aug" / @@ -469,22 +472,24 @@ impl ServoCookie { tag_no_case("dec"), )), any_octets, - )(input) + ) + .parse(input) }; // day-of-month = 1*2DIGIT [ non-digit *OCTET ] let day_of_month = |input| { terminated( take_while_m_n(1, 2, |byte: u8| byte.is_ascii_digit()), - opt(tuple((non_digit, any_octets))), - )(input) + opt((non_digit, any_octets)), + ) + .parse(input) }; // date-token = 1*non-delimiter - let date_token = |input| recognize(many1(non_delimiter))(input); + let date_token = |input| recognize(many1(non_delimiter)).parse(input); // date-token-list = date-token *( 1*delimiter date-token ) - let date_token_list = |input| separated_list1(delimiter, date_token)(input); + let date_token_list = |input| separated_list1(delimiter, date_token).parse(input); // cookie-date = *delimiter date-token-list *delimiter let cookie_date = - |input| delimited(many0(delimiter), date_token_list, many0(delimiter))(input); + |input| delimited(many0(delimiter), date_token_list, many0(delimiter)).parse(input); // Step 2. Process each date-token sequentially in the order the date-tokens appear in the cookie-date: let mut time_value: Option<(u8, u8, u8)> = None; // Also represents found-time flag. diff --git a/components/script/xpath/parser.rs b/components/script/xpath/parser.rs index 272fa41dcdf..9294a25a28f 100644 --- a/components/script/xpath/parser.rs +++ b/components/script/xpath/parser.rs @@ -6,10 +6,10 @@ use nom::branch::alt; use nom::bytes::complete::{tag, take_while1}; use nom::character::complete::{alpha1, alphanumeric1, char, digit1, multispace0}; use nom::combinator::{map, opt, recognize, value}; -use nom::error::{Error as NomError, ErrorKind as NomErrorKind}; +use nom::error::{Error as NomError, ErrorKind as NomErrorKind, ParseError as NomParseError}; use nom::multi::{many0, separated_list0}; -use nom::sequence::{delimited, pair, preceded, tuple}; -use nom::{Finish, IResult}; +use nom::sequence::{delimited, pair, preceded}; +use nom::{Finish, IResult, Parser}; pub(crate) fn parse(input: &str) -> Result { let (_, ast) = expr(input).finish().map_err(OwnedParserError::from)?; @@ -390,7 +390,7 @@ fn expr_single(input: &str) -> IResult<&str, Expr> { fn or_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = and_expr(input)?; - let (input, rest) = many0(preceded(ws(tag("or")), and_expr))(input)?; + let (input, rest) = many0(preceded(ws(tag("or")), and_expr)).parse(input)?; Ok(( input, @@ -401,7 +401,7 @@ fn or_expr(input: &str) -> IResult<&str, Expr> { fn and_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = equality_expr(input)?; - let (input, rest) = many0(preceded(ws(tag("and")), equality_expr))(input)?; + let (input, rest) = many0(preceded(ws(tag("and")), equality_expr)).parse(input)?; Ok(( input, @@ -412,13 +412,14 @@ fn and_expr(input: &str) -> IResult<&str, Expr> { fn equality_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = relational_expr(input)?; - let (input, rest) = many0(tuple(( + let (input, rest) = many0(( ws(alt(( map(tag("="), |_| EqualityOp::Eq), map(tag("!="), |_| EqualityOp::NotEq), ))), relational_expr, - )))(input)?; + )) + .parse(input)?; Ok(( input, @@ -430,7 +431,7 @@ fn equality_expr(input: &str) -> IResult<&str, Expr> { fn relational_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = additive_expr(input)?; - let (input, rest) = many0(tuple(( + let (input, rest) = many0(( ws(alt(( map(tag("<="), |_| RelationalOp::LtEq), map(tag(">="), |_| RelationalOp::GtEq), @@ -438,7 +439,8 @@ fn relational_expr(input: &str) -> IResult<&str, Expr> { map(tag(">"), |_| RelationalOp::Gt), ))), additive_expr, - )))(input)?; + )) + .parse(input)?; Ok(( input, @@ -450,13 +452,14 @@ fn relational_expr(input: &str) -> IResult<&str, Expr> { fn additive_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = multiplicative_expr(input)?; - let (input, rest) = many0(tuple(( + let (input, rest) = many0(( ws(alt(( map(tag("+"), |_| AdditiveOp::Add), map(tag("-"), |_| AdditiveOp::Sub), ))), multiplicative_expr, - )))(input)?; + )) + .parse(input)?; Ok(( input, @@ -468,14 +471,15 @@ fn additive_expr(input: &str) -> IResult<&str, Expr> { fn multiplicative_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = unary_expr(input)?; - let (input, rest) = many0(tuple(( + let (input, rest) = many0(( ws(alt(( map(tag("*"), |_| MultiplicativeOp::Mul), map(tag("div"), |_| MultiplicativeOp::Div), map(tag("mod"), |_| MultiplicativeOp::Mod), ))), unary_expr, - )))(input)?; + )) + .parse(input)?; Ok(( input, @@ -486,7 +490,7 @@ fn multiplicative_expr(input: &str) -> IResult<&str, Expr> { } fn unary_expr(input: &str) -> IResult<&str, Expr> { - let (input, minus_count) = many0(ws(char('-')))(input)?; + let (input, minus_count) = many0(ws(char('-'))).parse(input)?; let (input, expr) = union_expr(input)?; Ok(( @@ -497,7 +501,7 @@ fn unary_expr(input: &str) -> IResult<&str, Expr> { fn union_expr(input: &str) -> IResult<&str, Expr> { let (input, first) = path_expr(input)?; - let (input, rest) = many0(preceded(ws(char('|')), path_expr))(input)?; + let (input, rest) = many0(preceded(ws(char('|')), path_expr)).parse(input)?; Ok(( input, @@ -541,7 +545,8 @@ fn path_expr(input: &str) -> IResult<&str, Expr> { ), // RelativePathExpr move |i| relative_path_expr(false, i), - ))(input) + )) + .parse(input) } fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, Expr> { @@ -549,7 +554,8 @@ fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, Expr> { let (input, steps) = many0(pair( ws(alt((value(true, tag("//")), value(false, char('/'))))), move |i| step_expr(is_descendant, i), - ))(input)?; + )) + .parse(input)?; let mut all_steps = vec![first]; for (is_descendant, step) in steps { @@ -578,14 +584,16 @@ fn step_expr(is_descendant: bool, input: &str) -> IResult<&str, StepExpr> { alt(( map(filter_expr, StepExpr::Filter), map(|i| axis_step(is_descendant, i), StepExpr::Axis), - ))(input) + )) + .parse(input) } fn axis_step(is_descendant: bool, input: &str) -> IResult<&str, AxisStep> { let (input, (step, predicates)) = pair( alt((move |i| forward_step(is_descendant, i), reverse_step)), predicate_list, - )(input)?; + ) + .parse(input)?; let (axis, node_test) = step; Ok(( @@ -601,7 +609,8 @@ fn axis_step(is_descendant: bool, input: &str) -> IResult<&str, AxisStep> { fn forward_step(is_descendant: bool, input: &str) -> IResult<&str, (Axis, NodeTest)> { alt((pair(forward_axis, node_test), move |i| { abbrev_forward_step(is_descendant, i) - }))(input) + })) + .parse(input) } fn forward_axis(input: &str) -> IResult<&str, Axis> { @@ -614,13 +623,14 @@ fn forward_axis(input: &str) -> IResult<&str, Axis> { value(Axis::FollowingSibling, tag("following-sibling::")), value(Axis::Following, tag("following::")), value(Axis::Namespace, tag("namespace::")), - ))(input)?; + )) + .parse(input)?; Ok((input, axis)) } fn abbrev_forward_step(is_descendant: bool, input: &str) -> IResult<&str, (Axis, NodeTest)> { - let (input, attr) = opt(char('@'))(input)?; + let (input, attr) = opt(char('@')).parse(input)?; let (input, test) = node_test(input)?; Ok(( @@ -644,7 +654,8 @@ fn reverse_step(input: &str) -> IResult<&str, (Axis, NodeTest)> { pair(reverse_axis, node_test), // AbbrevReverseStep abbrev_reverse_step, - ))(input) + )) + .parse(input) } fn reverse_axis(input: &str) -> IResult<&str, Axis> { @@ -654,13 +665,15 @@ fn reverse_axis(input: &str) -> IResult<&str, Axis> { value(Axis::PrecedingSibling, tag("preceding-sibling::")), value(Axis::Preceding, tag("preceding::")), value(Axis::AncestorOrSelf, tag("ancestor-or-self::")), - ))(input) + )) + .parse(input) } fn abbrev_reverse_step(input: &str) -> IResult<&str, (Axis, NodeTest)> { map(tag(".."), |_| { (Axis::Parent, NodeTest::Kind(KindTest::Node)) - })(input) + }) + .parse(input) } fn node_test(input: &str) -> IResult<&str, NodeTest> { @@ -670,7 +683,8 @@ fn node_test(input: &str) -> IResult<&str, NodeTest> { NameTest::Wildcard => NodeTest::Wildcard, NameTest::QName(qname) => NodeTest::Name(qname), }), - ))(input) + )) + .parse(input) } #[derive(Clone, Debug, PartialEq)] @@ -682,7 +696,7 @@ enum NameTest { fn name_test(input: &str) -> IResult<&str, NameTest> { alt(( // NCName ":" "*" - map(tuple((ncname, char(':'), char('*'))), |(prefix, _, _)| { + map((ncname, char(':'), char('*')), |(prefix, _, _)| { NameTest::QName(QName { prefix: Some(prefix.to_string()), local_part: "*".to_string(), @@ -692,7 +706,8 @@ fn name_test(input: &str) -> IResult<&str, NameTest> { value(NameTest::Wildcard, char('*')), // QName map(qname, NameTest::QName), - ))(input) + )) + .parse(input) } fn filter_expr(input: &str) -> IResult<&str, FilterExpr> { @@ -709,13 +724,13 @@ fn filter_expr(input: &str) -> IResult<&str, FilterExpr> { } fn predicate_list(input: &str) -> IResult<&str, PredicateListExpr> { - let (input, predicates) = many0(predicate)(input)?; + let (input, predicates) = many0(predicate).parse(input)?; Ok((input, PredicateListExpr { predicates })) } fn predicate(input: &str) -> IResult<&str, PredicateExpr> { - let (input, expr) = delimited(ws(char('[')), expr, ws(char(']')))(input)?; + let (input, expr) = delimited(ws(char('[')), expr, ws(char(']'))).parse(input)?; Ok((input, PredicateExpr { expr })) } @@ -728,31 +743,33 @@ fn primary_expr(input: &str) -> IResult<&str, PrimaryExpr> { }), context_item_expr, function_call, - ))(input) + )) + .parse(input) } fn literal(input: &str) -> IResult<&str, PrimaryExpr> { map(alt((numeric_literal, string_literal)), |lit| { PrimaryExpr::Literal(lit) - })(input) + }) + .parse(input) } fn numeric_literal(input: &str) -> IResult<&str, Literal> { - alt((decimal_literal, integer_literal))(input) + alt((decimal_literal, integer_literal)).parse(input) } fn var_ref(input: &str) -> IResult<&str, PrimaryExpr> { - let (input, _) = char('$')(input)?; + let (input, _) = char('$').parse(input)?; let (input, name) = qname(input)?; Ok((input, PrimaryExpr::Variable(name))) } fn parenthesized_expr(input: &str) -> IResult<&str, Expr> { - delimited(ws(char('(')), expr, ws(char(')')))(input) + delimited(ws(char('(')), expr, ws(char(')'))).parse(input) } fn context_item_expr(input: &str) -> IResult<&str, PrimaryExpr> { - map(char('.'), |_| PrimaryExpr::ContextItem)(input) + map(char('.'), |_| PrimaryExpr::ContextItem).parse(input) } fn function_call(input: &str) -> IResult<&str, PrimaryExpr> { @@ -761,7 +778,8 @@ fn function_call(input: &str) -> IResult<&str, PrimaryExpr> { ws(char('(')), separated_list0(ws(char(',')), expr_single), ws(char(')')), - )(input)?; + ) + .parse(input)?; // Helper to create error let arity_error = || nom::Err::Error(NomError::new(input, NomErrorKind::Verify)); @@ -852,63 +870,69 @@ fn function_call(input: &str) -> IResult<&str, PrimaryExpr> { } fn kind_test(input: &str) -> IResult<&str, KindTest> { - alt((pi_test, comment_test, text_test, any_kind_test))(input) + alt((pi_test, comment_test, text_test, any_kind_test)).parse(input) } fn any_kind_test(input: &str) -> IResult<&str, KindTest> { - map(tuple((tag("node"), ws(char('(')), ws(char(')')))), |_| { + map((tag("node"), ws(char('(')), ws(char(')'))), |_| { KindTest::Node - })(input) + }) + .parse(input) } fn text_test(input: &str) -> IResult<&str, KindTest> { - map(tuple((tag("text"), ws(char('(')), ws(char(')')))), |_| { + map((tag("text"), ws(char('(')), ws(char(')'))), |_| { KindTest::Text - })(input) + }) + .parse(input) } fn comment_test(input: &str) -> IResult<&str, KindTest> { - map( - tuple((tag("comment"), ws(char('(')), ws(char(')')))), - |_| KindTest::Comment, - )(input) + map((tag("comment"), ws(char('(')), ws(char(')'))), |_| { + KindTest::Comment + }) + .parse(input) } fn pi_test(input: &str) -> IResult<&str, KindTest> { map( - tuple(( + ( tag("processing-instruction"), ws(char('(')), opt(ws(string_literal)), ws(char(')')), - )), + ), |(_, _, literal, _)| { KindTest::PI(literal.map(|l| match l { Literal::String(s) => s, _ => unreachable!(), })) }, - )(input) + ) + .parse(input) } -fn ws<'a, F, O>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O> +fn ws<'a, F, O, E>(inner: F) -> impl Parser<&'a str, Output = O, Error = E> where - F: FnMut(&'a str) -> IResult<&'a str, O>, + E: NomParseError<&'a str>, + F: Parser<&'a str, Output = O, Error = E>, { delimited(multispace0, inner, multispace0) } fn integer_literal(input: &str) -> IResult<&str, Literal> { - map(recognize(tuple((opt(char('-')), digit1))), |s: &str| { + map(recognize((opt(char('-')), digit1)), |s: &str| { Literal::Numeric(NumericLiteral::Integer(s.parse().unwrap())) - })(input) + }) + .parse(input) } fn decimal_literal(input: &str) -> IResult<&str, Literal> { map( - recognize(tuple((opt(char('-')), opt(digit1), char('.'), digit1))), + recognize((opt(char('-')), opt(digit1), char('.'), digit1)), |s: &str| Literal::Numeric(NumericLiteral::Decimal(s.parse().unwrap())), - )(input) + ) + .parse(input) } fn string_literal(input: &str) -> IResult<&str, Literal> { @@ -927,12 +951,13 @@ fn string_literal(input: &str) -> IResult<&str, Literal> { }), char('\''), ), - ))(input) + )) + .parse(input) } // QName parser fn qname(input: &str) -> IResult<&str, QName> { - let (input, prefix) = opt(tuple((ncname, char(':'))))(input)?; + let (input, prefix) = opt((ncname, char(':'))).parse(input)?; let (input, local) = ncname(input)?; Ok(( @@ -949,7 +974,8 @@ fn ncname(input: &str) -> IResult<&str, &str> { recognize(pair( alpha1, many0(alt((alphanumeric1, tag("-"), tag("_")))), - ))(input) + )) + .parse(input) } // Test functions to verify the parsers: diff --git a/deny.toml b/deny.toml index fc10048734f..3d739eb3e7a 100644 --- a/deny.toml +++ b/deny.toml @@ -189,6 +189,11 @@ skip = [ # duplicated by zbus-xml "quick-xml", + + # duplicated by bindgen as build dependency + # Remove when cexpr updates its nom version + # and bindgen updates the cexpr version + "nom", ] # github.com organizations to allow git sources for