Update Nom to 8.0.0 (#38585)

There are two relevant breaking changes:
* instead of `)(input)`, we now need to call `).parse(input)`
* tuples are no longer a call, but a `()` call.

There is one other usage of nom 7, however that's in the build
dependencies list of mozangle via bindgen. Therefore, we won't have
duplicate nom versions in Servo binary.

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
This commit is contained in:
Tim van der Lippe 2025-08-10 23:25:37 +02:00 committed by GitHub
parent 4f8731d562
commit 2e5f5e7d1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 121 additions and 76 deletions

15
Cargo.lock generated
View file

@ -1150,7 +1150,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [ dependencies = [
"nom", "nom 7.1.3",
] ]
[[package]] [[package]]
@ -5427,7 +5427,7 @@ dependencies = [
"mime", "mime",
"mime_guess", "mime_guess",
"net_traits", "net_traits",
"nom", "nom 8.0.0",
"pixels", "pixels",
"profile_traits", "profile_traits",
"rayon", "rayon",
@ -5536,6 +5536,15 @@ dependencies = [
"minimal-lexical", "minimal-lexical",
] ]
[[package]]
name = "nom"
version = "8.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" version = "0.46.0"
@ -7167,7 +7176,7 @@ dependencies = [
"mozangle", "mozangle",
"mozjs", "mozjs",
"net_traits", "net_traits",
"nom", "nom 8.0.0",
"num-traits", "num-traits",
"num_cpus", "num_cpus",
"parking_lot", "parking_lot",

View file

@ -102,7 +102,7 @@ mime_guess = "2.0.5"
mozangle = "0.5.3" mozangle = "0.5.3"
net_traits = { path = "components/shared/net" } net_traits = { path = "components/shared/net" }
nix = "0.29" nix = "0.29"
nom = "7.1.3" nom = "8.0.0"
num-traits = "0.2" num-traits = "0.2"
num_cpus = "1.17.0" num_cpus = "1.17.0"
openxr = "0.19" openxr = "0.19"

View file

@ -13,12 +13,12 @@ use cookie::Cookie;
use log::{Level, debug, log_enabled}; use log::{Level, debug, log_enabled};
use net_traits::CookieSource; use net_traits::CookieSource;
use net_traits::pub_domains::is_pub_domain; use net_traits::pub_domains::is_pub_domain;
use nom::IResult;
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case, take, take_while_m_n}; use nom::bytes::complete::{tag, tag_no_case, take, take_while_m_n};
use nom::combinator::{opt, recognize}; use nom::combinator::{opt, recognize};
use nom::multi::{many0, many1, separated_list1}; 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 serde::{Deserialize, Serialize};
use servo_url::ServoUrl; use servo_url::ServoUrl;
use time::{Date, Duration, Month, OffsetDateTime, Time}; use time::{Date, Duration, Month, OffsetDateTime, Time};
@ -431,23 +431,26 @@ impl ServoCookie {
} }
}; };
// time-field = 1*2DIGIT // 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 // hms-time = time-field ":" time-field ":" time-field
let hms_time = |input| { let hms_time = |input| {
tuple(( (
time_field, time_field,
preceded(tag(":"), time_field), preceded(tag(":"), time_field),
preceded(tag(":"), time_field), preceded(tag(":"), time_field),
))(input) )
.parse(input)
}; };
// time = hms-time [ non-digit *OCTET ] // 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 ] // year = 2*4DIGIT [ non-digit *OCTET ]
let year = |input| { let year = |input| {
terminated( terminated(
take_while_m_n(2, 4, |byte: u8| byte.is_ascii_digit()), take_while_m_n(2, 4, |byte: u8| byte.is_ascii_digit()),
opt(tuple((non_digit, any_octets))), opt((non_digit, any_octets)),
)(input) )
.parse(input)
}; };
// month = ( "jan" / "feb" / "mar" / "apr" / // month = ( "jan" / "feb" / "mar" / "apr" /
// "may" / "jun" / "jul" / "aug" / // "may" / "jun" / "jul" / "aug" /
@ -469,22 +472,24 @@ impl ServoCookie {
tag_no_case("dec"), tag_no_case("dec"),
)), )),
any_octets, any_octets,
)(input) )
.parse(input)
}; };
// day-of-month = 1*2DIGIT [ non-digit *OCTET ] // day-of-month = 1*2DIGIT [ non-digit *OCTET ]
let day_of_month = |input| { let day_of_month = |input| {
terminated( terminated(
take_while_m_n(1, 2, |byte: u8| byte.is_ascii_digit()), take_while_m_n(1, 2, |byte: u8| byte.is_ascii_digit()),
opt(tuple((non_digit, any_octets))), opt((non_digit, any_octets)),
)(input) )
.parse(input)
}; };
// date-token = 1*non-delimiter // 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 ) // 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 // cookie-date = *delimiter date-token-list *delimiter
let cookie_date = 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: // 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. let mut time_value: Option<(u8, u8, u8)> = None; // Also represents found-time flag.

View file

@ -6,10 +6,10 @@ use nom::branch::alt;
use nom::bytes::complete::{tag, take_while1}; use nom::bytes::complete::{tag, take_while1};
use nom::character::complete::{alpha1, alphanumeric1, char, digit1, multispace0}; use nom::character::complete::{alpha1, alphanumeric1, char, digit1, multispace0};
use nom::combinator::{map, opt, recognize, value}; 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::multi::{many0, separated_list0};
use nom::sequence::{delimited, pair, preceded, tuple}; use nom::sequence::{delimited, pair, preceded};
use nom::{Finish, IResult}; use nom::{Finish, IResult, Parser};
pub(crate) fn parse(input: &str) -> Result<Expr, OwnedParserError> { pub(crate) fn parse(input: &str) -> Result<Expr, OwnedParserError> {
let (_, ast) = expr(input).finish().map_err(OwnedParserError::from)?; 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> { fn or_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = and_expr(input)?; 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(( Ok((
input, input,
@ -401,7 +401,7 @@ fn or_expr(input: &str) -> IResult<&str, Expr> {
fn and_expr(input: &str) -> IResult<&str, Expr> { fn and_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = equality_expr(input)?; 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(( Ok((
input, input,
@ -412,13 +412,14 @@ fn and_expr(input: &str) -> IResult<&str, Expr> {
fn equality_expr(input: &str) -> IResult<&str, Expr> { fn equality_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = relational_expr(input)?; let (input, first) = relational_expr(input)?;
let (input, rest) = many0(tuple(( let (input, rest) = many0((
ws(alt(( ws(alt((
map(tag("="), |_| EqualityOp::Eq), map(tag("="), |_| EqualityOp::Eq),
map(tag("!="), |_| EqualityOp::NotEq), map(tag("!="), |_| EqualityOp::NotEq),
))), ))),
relational_expr, relational_expr,
)))(input)?; ))
.parse(input)?;
Ok(( Ok((
input, input,
@ -430,7 +431,7 @@ fn equality_expr(input: &str) -> IResult<&str, Expr> {
fn relational_expr(input: &str) -> IResult<&str, Expr> { fn relational_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = additive_expr(input)?; let (input, first) = additive_expr(input)?;
let (input, rest) = many0(tuple(( let (input, rest) = many0((
ws(alt(( ws(alt((
map(tag("<="), |_| RelationalOp::LtEq), map(tag("<="), |_| RelationalOp::LtEq),
map(tag(">="), |_| RelationalOp::GtEq), map(tag(">="), |_| RelationalOp::GtEq),
@ -438,7 +439,8 @@ fn relational_expr(input: &str) -> IResult<&str, Expr> {
map(tag(">"), |_| RelationalOp::Gt), map(tag(">"), |_| RelationalOp::Gt),
))), ))),
additive_expr, additive_expr,
)))(input)?; ))
.parse(input)?;
Ok(( Ok((
input, input,
@ -450,13 +452,14 @@ fn relational_expr(input: &str) -> IResult<&str, Expr> {
fn additive_expr(input: &str) -> IResult<&str, Expr> { fn additive_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = multiplicative_expr(input)?; let (input, first) = multiplicative_expr(input)?;
let (input, rest) = many0(tuple(( let (input, rest) = many0((
ws(alt(( ws(alt((
map(tag("+"), |_| AdditiveOp::Add), map(tag("+"), |_| AdditiveOp::Add),
map(tag("-"), |_| AdditiveOp::Sub), map(tag("-"), |_| AdditiveOp::Sub),
))), ))),
multiplicative_expr, multiplicative_expr,
)))(input)?; ))
.parse(input)?;
Ok(( Ok((
input, input,
@ -468,14 +471,15 @@ fn additive_expr(input: &str) -> IResult<&str, Expr> {
fn multiplicative_expr(input: &str) -> IResult<&str, Expr> { fn multiplicative_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = unary_expr(input)?; let (input, first) = unary_expr(input)?;
let (input, rest) = many0(tuple(( let (input, rest) = many0((
ws(alt(( ws(alt((
map(tag("*"), |_| MultiplicativeOp::Mul), map(tag("*"), |_| MultiplicativeOp::Mul),
map(tag("div"), |_| MultiplicativeOp::Div), map(tag("div"), |_| MultiplicativeOp::Div),
map(tag("mod"), |_| MultiplicativeOp::Mod), map(tag("mod"), |_| MultiplicativeOp::Mod),
))), ))),
unary_expr, unary_expr,
)))(input)?; ))
.parse(input)?;
Ok(( Ok((
input, input,
@ -486,7 +490,7 @@ fn multiplicative_expr(input: &str) -> IResult<&str, Expr> {
} }
fn unary_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)?; let (input, expr) = union_expr(input)?;
Ok(( Ok((
@ -497,7 +501,7 @@ fn unary_expr(input: &str) -> IResult<&str, Expr> {
fn union_expr(input: &str) -> IResult<&str, Expr> { fn union_expr(input: &str) -> IResult<&str, Expr> {
let (input, first) = path_expr(input)?; 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(( Ok((
input, input,
@ -541,7 +545,8 @@ fn path_expr(input: &str) -> IResult<&str, Expr> {
), ),
// RelativePathExpr // RelativePathExpr
move |i| relative_path_expr(false, i), move |i| relative_path_expr(false, i),
))(input) ))
.parse(input)
} }
fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, Expr> { 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( let (input, steps) = many0(pair(
ws(alt((value(true, tag("//")), value(false, char('/'))))), ws(alt((value(true, tag("//")), value(false, char('/'))))),
move |i| step_expr(is_descendant, i), move |i| step_expr(is_descendant, i),
))(input)?; ))
.parse(input)?;
let mut all_steps = vec![first]; let mut all_steps = vec![first];
for (is_descendant, step) in steps { for (is_descendant, step) in steps {
@ -578,14 +584,16 @@ fn step_expr(is_descendant: bool, input: &str) -> IResult<&str, StepExpr> {
alt(( alt((
map(filter_expr, StepExpr::Filter), map(filter_expr, StepExpr::Filter),
map(|i| axis_step(is_descendant, i), StepExpr::Axis), map(|i| axis_step(is_descendant, i), StepExpr::Axis),
))(input) ))
.parse(input)
} }
fn axis_step(is_descendant: bool, input: &str) -> IResult<&str, AxisStep> { fn axis_step(is_descendant: bool, input: &str) -> IResult<&str, AxisStep> {
let (input, (step, predicates)) = pair( let (input, (step, predicates)) = pair(
alt((move |i| forward_step(is_descendant, i), reverse_step)), alt((move |i| forward_step(is_descendant, i), reverse_step)),
predicate_list, predicate_list,
)(input)?; )
.parse(input)?;
let (axis, node_test) = step; let (axis, node_test) = step;
Ok(( 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)> { fn forward_step(is_descendant: bool, input: &str) -> IResult<&str, (Axis, NodeTest)> {
alt((pair(forward_axis, node_test), move |i| { alt((pair(forward_axis, node_test), move |i| {
abbrev_forward_step(is_descendant, i) abbrev_forward_step(is_descendant, i)
}))(input) }))
.parse(input)
} }
fn forward_axis(input: &str) -> IResult<&str, Axis> { 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::FollowingSibling, tag("following-sibling::")),
value(Axis::Following, tag("following::")), value(Axis::Following, tag("following::")),
value(Axis::Namespace, tag("namespace::")), value(Axis::Namespace, tag("namespace::")),
))(input)?; ))
.parse(input)?;
Ok((input, axis)) Ok((input, axis))
} }
fn abbrev_forward_step(is_descendant: bool, input: &str) -> IResult<&str, (Axis, NodeTest)> { 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)?; let (input, test) = node_test(input)?;
Ok(( Ok((
@ -644,7 +654,8 @@ fn reverse_step(input: &str) -> IResult<&str, (Axis, NodeTest)> {
pair(reverse_axis, node_test), pair(reverse_axis, node_test),
// AbbrevReverseStep // AbbrevReverseStep
abbrev_reverse_step, abbrev_reverse_step,
))(input) ))
.parse(input)
} }
fn reverse_axis(input: &str) -> IResult<&str, Axis> { 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::PrecedingSibling, tag("preceding-sibling::")),
value(Axis::Preceding, tag("preceding::")), value(Axis::Preceding, tag("preceding::")),
value(Axis::AncestorOrSelf, tag("ancestor-or-self::")), value(Axis::AncestorOrSelf, tag("ancestor-or-self::")),
))(input) ))
.parse(input)
} }
fn abbrev_reverse_step(input: &str) -> IResult<&str, (Axis, NodeTest)> { fn abbrev_reverse_step(input: &str) -> IResult<&str, (Axis, NodeTest)> {
map(tag(".."), |_| { map(tag(".."), |_| {
(Axis::Parent, NodeTest::Kind(KindTest::Node)) (Axis::Parent, NodeTest::Kind(KindTest::Node))
})(input) })
.parse(input)
} }
fn node_test(input: &str) -> IResult<&str, NodeTest> { 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::Wildcard => NodeTest::Wildcard,
NameTest::QName(qname) => NodeTest::Name(qname), NameTest::QName(qname) => NodeTest::Name(qname),
}), }),
))(input) ))
.parse(input)
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -682,7 +696,7 @@ enum NameTest {
fn name_test(input: &str) -> IResult<&str, NameTest> { fn name_test(input: &str) -> IResult<&str, NameTest> {
alt(( alt((
// NCName ":" "*" // NCName ":" "*"
map(tuple((ncname, char(':'), char('*'))), |(prefix, _, _)| { map((ncname, char(':'), char('*')), |(prefix, _, _)| {
NameTest::QName(QName { NameTest::QName(QName {
prefix: Some(prefix.to_string()), prefix: Some(prefix.to_string()),
local_part: "*".to_string(), local_part: "*".to_string(),
@ -692,7 +706,8 @@ fn name_test(input: &str) -> IResult<&str, NameTest> {
value(NameTest::Wildcard, char('*')), value(NameTest::Wildcard, char('*')),
// QName // QName
map(qname, NameTest::QName), map(qname, NameTest::QName),
))(input) ))
.parse(input)
} }
fn filter_expr(input: &str) -> IResult<&str, FilterExpr> { 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> { 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 })) Ok((input, PredicateListExpr { predicates }))
} }
fn predicate(input: &str) -> IResult<&str, PredicateExpr> { 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 })) Ok((input, PredicateExpr { expr }))
} }
@ -728,31 +743,33 @@ fn primary_expr(input: &str) -> IResult<&str, PrimaryExpr> {
}), }),
context_item_expr, context_item_expr,
function_call, function_call,
))(input) ))
.parse(input)
} }
fn literal(input: &str) -> IResult<&str, PrimaryExpr> { fn literal(input: &str) -> IResult<&str, PrimaryExpr> {
map(alt((numeric_literal, string_literal)), |lit| { map(alt((numeric_literal, string_literal)), |lit| {
PrimaryExpr::Literal(lit) PrimaryExpr::Literal(lit)
})(input) })
.parse(input)
} }
fn numeric_literal(input: &str) -> IResult<&str, Literal> { 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> { fn var_ref(input: &str) -> IResult<&str, PrimaryExpr> {
let (input, _) = char('$')(input)?; let (input, _) = char('$').parse(input)?;
let (input, name) = qname(input)?; let (input, name) = qname(input)?;
Ok((input, PrimaryExpr::Variable(name))) Ok((input, PrimaryExpr::Variable(name)))
} }
fn parenthesized_expr(input: &str) -> IResult<&str, Expr> { 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> { 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> { fn function_call(input: &str) -> IResult<&str, PrimaryExpr> {
@ -761,7 +778,8 @@ fn function_call(input: &str) -> IResult<&str, PrimaryExpr> {
ws(char('(')), ws(char('(')),
separated_list0(ws(char(',')), expr_single), separated_list0(ws(char(',')), expr_single),
ws(char(')')), ws(char(')')),
)(input)?; )
.parse(input)?;
// Helper to create error // Helper to create error
let arity_error = || nom::Err::Error(NomError::new(input, NomErrorKind::Verify)); 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> { 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> { 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 KindTest::Node
})(input) })
.parse(input)
} }
fn text_test(input: &str) -> IResult<&str, KindTest> { 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 KindTest::Text
})(input) })
.parse(input)
} }
fn comment_test(input: &str) -> IResult<&str, KindTest> { fn comment_test(input: &str) -> IResult<&str, KindTest> {
map( map((tag("comment"), ws(char('(')), ws(char(')'))), |_| {
tuple((tag("comment"), ws(char('(')), ws(char(')')))), KindTest::Comment
|_| KindTest::Comment, })
)(input) .parse(input)
} }
fn pi_test(input: &str) -> IResult<&str, KindTest> { fn pi_test(input: &str) -> IResult<&str, KindTest> {
map( map(
tuple(( (
tag("processing-instruction"), tag("processing-instruction"),
ws(char('(')), ws(char('(')),
opt(ws(string_literal)), opt(ws(string_literal)),
ws(char(')')), ws(char(')')),
)), ),
|(_, _, literal, _)| { |(_, _, literal, _)| {
KindTest::PI(literal.map(|l| match l { KindTest::PI(literal.map(|l| match l {
Literal::String(s) => s, Literal::String(s) => s,
_ => unreachable!(), _ => 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 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) delimited(multispace0, inner, multispace0)
} }
fn integer_literal(input: &str) -> IResult<&str, Literal> { 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())) Literal::Numeric(NumericLiteral::Integer(s.parse().unwrap()))
})(input) })
.parse(input)
} }
fn decimal_literal(input: &str) -> IResult<&str, Literal> { fn decimal_literal(input: &str) -> IResult<&str, Literal> {
map( 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())), |s: &str| Literal::Numeric(NumericLiteral::Decimal(s.parse().unwrap())),
)(input) )
.parse(input)
} }
fn string_literal(input: &str) -> IResult<&str, Literal> { fn string_literal(input: &str) -> IResult<&str, Literal> {
@ -927,12 +951,13 @@ fn string_literal(input: &str) -> IResult<&str, Literal> {
}), }),
char('\''), char('\''),
), ),
))(input) ))
.parse(input)
} }
// QName parser // QName parser
fn qname(input: &str) -> IResult<&str, QName> { 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)?; let (input, local) = ncname(input)?;
Ok(( Ok((
@ -949,7 +974,8 @@ fn ncname(input: &str) -> IResult<&str, &str> {
recognize(pair( recognize(pair(
alpha1, alpha1,
many0(alt((alphanumeric1, tag("-"), tag("_")))), many0(alt((alphanumeric1, tag("-"), tag("_")))),
))(input) ))
.parse(input)
} }
// Test functions to verify the parsers: // Test functions to verify the parsers:

View file

@ -189,6 +189,11 @@ skip = [
# duplicated by zbus-xml # duplicated by zbus-xml
"quick-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 # github.com organizations to allow git sources for