mirror of
https://github.com/servo/servo.git
synced 2025-08-12 00:45:33 +01:00
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:
parent
4f8731d562
commit
2e5f5e7d1c
5 changed files with 121 additions and 76 deletions
|
@ -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<Expr, OwnedParserError> {
|
||||
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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue