Skip to content

Commit

Permalink
chore: upgrade to winnow 0.7 (#403)
Browse files Browse the repository at this point in the history
* chore: bump winnow to 0.6.26

* chore: address winnow deprecations

* chore: bump winnow to 0.7

https://github.com/winnow-rs/winnow/blob/main/CHANGELOG.md#070---2025-01-30
  • Loading branch information
martinohmann authored Feb 1, 2025
1 parent 481f32e commit 792b737
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 109 deletions.
4 changes: 2 additions & 2 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 crates/hcl-edit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ perf = ["hcl-primitives/perf"]
fnv = "1.0"
hcl-primitives = { version = "0.1.8", path = "../hcl-primitives" }
vecmap-rs = "0.2"
winnow = "0.6"
winnow = "0.7"

[dev-dependencies]
indoc = "2.0"
Expand Down
92 changes: 56 additions & 36 deletions crates/hcl-edit/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use winnow::combinator::{
};
use winnow::token::{any, none_of, one_of, take};

fn ws_or_sp<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn ws_or_sp<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
if state.borrow().newlines_allowed() {
ws.parse_next(input)
Expand All @@ -34,25 +36,25 @@ fn ws_or_sp<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), C
}
}

pub(super) fn expr(input: &mut Input) -> PResult<Expression> {
pub(super) fn expr(input: &mut Input) -> ModalResult<Expression> {
parse_expr(RefCell::default(), input)
}

fn expr_with_state<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, Expression, ContextError> + '_ {
) -> impl ModalParser<Input<'i>, Expression, ContextError> + '_ {
move |input: &mut Input<'i>| parse_expr(state.clone(), input)
}

#[inline]
fn parse_expr(state: RefCell<ExprParseState>, input: &mut Input) -> PResult<Expression> {
fn parse_expr(state: RefCell<ExprParseState>, input: &mut Input) -> ModalResult<Expression> {
expr_inner(&state).parse_next(input)?;
Ok(state.into_inner().into_expr())
}

fn expr_inner<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
let span = expr_term(state).span().parse_next(input)?;
state.borrow_mut().on_span(span);
Expand Down Expand Up @@ -111,7 +113,9 @@ fn expr_inner<'i>(
}
}

fn expr_term<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn expr_term<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
dispatch! {peek(any);
'"' => stringlike(state),
Expand Down Expand Up @@ -142,7 +146,7 @@ fn expr_term<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (),

fn stringlike<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
alt((
string.map(|string| {
Expand All @@ -160,7 +164,9 @@ fn stringlike<'i>(
}
}

fn number<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn number<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
num.with_taken()
.map(|(num, repr)| {
Expand All @@ -174,7 +180,7 @@ fn number<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), Con

fn neg_number<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
preceded(('-', sp), num)
.with_taken()
Expand All @@ -187,7 +193,9 @@ fn neg_number<'i>(
}
}

fn traversal<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn traversal<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
repeat(
1..,
Expand All @@ -203,7 +211,7 @@ fn traversal<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (),
}
}

fn traversal_operator(input: &mut Input) -> PResult<TraversalOperator> {
fn traversal_operator(input: &mut Input) -> ModalResult<TraversalOperator> {
dispatch! {any;
'.' => prefix_decorated(
ws,
Expand Down Expand Up @@ -234,7 +242,9 @@ fn traversal_operator(input: &mut Input) -> PResult<TraversalOperator> {
.parse_next(input)
}

fn unary_op<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn unary_op<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
preceded(
(spanned(unary_operator.map(Spanned::new)), sp.span())
Expand All @@ -246,7 +256,7 @@ fn unary_op<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), C
}
}

fn unary_operator(input: &mut Input) -> PResult<UnaryOperator> {
fn unary_operator(input: &mut Input) -> ModalResult<UnaryOperator> {
dispatch! {any;
'-' => empty.value(UnaryOperator::Neg),
'!' => empty.value(UnaryOperator::Not),
Expand All @@ -255,7 +265,9 @@ fn unary_operator(input: &mut Input) -> PResult<UnaryOperator> {
.parse_next(input)
}

fn binary_op<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn binary_op<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
(
spanned(binary_operator.map(Spanned::new)),
Expand All @@ -266,7 +278,7 @@ fn binary_op<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (),
}
}

fn binary_operator(input: &mut Input) -> PResult<BinaryOperator> {
fn binary_operator(input: &mut Input) -> ModalResult<BinaryOperator> {
dispatch! {any;
'=' => '='.value(BinaryOperator::Eq),
'!' => '='.value(BinaryOperator::NotEq),
Expand All @@ -292,7 +304,7 @@ fn binary_operator(input: &mut Input) -> PResult<BinaryOperator> {

fn conditional<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
(
preceded(
Expand All @@ -309,7 +321,9 @@ fn conditional<'i>(
}
}

fn array<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn array<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
state.borrow_mut().allow_newlines(true);
delimited(
Expand All @@ -323,7 +337,7 @@ fn array<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), Cont

fn for_list_expr<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
(
for_intro,
Expand All @@ -344,7 +358,7 @@ fn for_list_expr<'i>(

fn array_items<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
let values = separated(
0..,
Expand All @@ -366,7 +380,9 @@ fn array_items<'i>(
}
}

fn object<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn object<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
delimited(
'{',
Expand All @@ -379,7 +395,7 @@ fn object<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), Con

fn for_object_expr<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
state.borrow_mut().allow_newlines(true);
(
Expand Down Expand Up @@ -415,7 +431,7 @@ fn for_object_expr<'i>(

fn object_items<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
let mut object = Object::new();

Expand Down Expand Up @@ -487,7 +503,7 @@ fn object_items<'i>(
}
}

fn object_key(input: &mut Input) -> PResult<ObjectKey> {
fn object_key(input: &mut Input) -> ModalResult<ObjectKey> {
suffix_decorated(
expr.map(|expr| {
// Variable identifiers without traversal are treated as identifier object keys.
Expand All @@ -506,7 +522,7 @@ fn object_key(input: &mut Input) -> PResult<ObjectKey> {
.parse_next(input)
}

fn object_value(input: &mut Input) -> PResult<ObjectValue> {
fn object_value(input: &mut Input) -> ModalResult<ObjectValue> {
(object_value_assignment, decorated(sp, expr, sp))
.map(|(assignment, expr)| {
let mut value = ObjectValue::new(expr);
Expand All @@ -516,7 +532,7 @@ fn object_value(input: &mut Input) -> PResult<ObjectValue> {
.parse_next(input)
}

fn object_value_assignment(input: &mut Input) -> PResult<ObjectValueAssignment> {
fn object_value_assignment(input: &mut Input) -> ModalResult<ObjectValueAssignment> {
dispatch! {any;
'=' => empty.value(ObjectValueAssignment::Equals),
':' => empty.value(ObjectValueAssignment::Colon),
Expand All @@ -531,10 +547,10 @@ fn object_value_assignment(input: &mut Input) -> PResult<ObjectValueAssignment>
fn for_expr_or_items<'i, F, I>(
mut for_expr_parser: F,
mut items_parser: I,
) -> impl Parser<Input<'i>, (), ContextError>
) -> impl ModalParser<Input<'i>, (), ContextError>
where
F: Parser<Input<'i>, (), ContextError>,
I: Parser<Input<'i>, (), ContextError>,
F: ModalParser<Input<'i>, (), ContextError>,
I: ModalParser<Input<'i>, (), ContextError>,
{
move |input: &mut Input<'i>| {
// The `for` tag needs to be followed by either a space character or a comment start to
Expand All @@ -548,7 +564,7 @@ where
}
}

fn for_intro(input: &mut Input) -> PResult<ForIntro> {
fn for_intro(input: &mut Input) -> ModalResult<ForIntro> {
prefix_decorated(
ws,
delimited(
Expand All @@ -574,7 +590,7 @@ fn for_intro(input: &mut Input) -> PResult<ForIntro> {

fn for_cond<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, ForCond, ContextError> + '_ {
) -> impl ModalParser<Input<'i>, ForCond, ContextError> + '_ {
move |input: &mut Input| {
prefix_decorated(
ws,
Expand All @@ -586,7 +602,7 @@ fn for_cond<'i>(

fn parenthesis<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + '_ {
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
state.borrow_mut().allow_newlines(true);
delimited(
Expand All @@ -599,7 +615,9 @@ fn parenthesis<'i>(
}
}

fn heredoc<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn heredoc<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
let (indented, delim) = heredoc_start(input)?;

Expand Down Expand Up @@ -629,7 +647,7 @@ fn heredoc<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), Co
}
}

fn heredoc_start<'a>(input: &mut Input<'a>) -> PResult<(bool, &'a str)> {
fn heredoc_start<'a>(input: &mut Input<'a>) -> ModalResult<(bool, &'a str)> {
terminated(
(
preceded("<<", opt('-')).map(|indent| indent.is_some()),
Expand All @@ -642,7 +660,9 @@ fn heredoc_start<'a>(input: &mut Input<'a>) -> PResult<(bool, &'a str)> {
.parse_next(input)
}

fn identlike<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (), ContextError> + '_ {
fn identlike<'i>(
state: &RefCell<ExprParseState>,
) -> impl ModalParser<Input<'i>, (), ContextError> + '_ {
move |input: &mut Input<'i>| {
let (ident, span) = str_ident.with_span().parse_next(input)?;

Expand Down Expand Up @@ -698,7 +718,7 @@ fn identlike<'i>(state: &RefCell<ExprParseState>) -> impl Parser<Input<'i>, (),

fn func_namespace_components<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, Vec<Decorated<Ident>>, ContextError> + '_ {
) -> impl ModalParser<Input<'i>, Vec<Decorated<Ident>>, ContextError> + '_ {
move |input: &mut Input<'i>| {
repeat(
1..,
Expand All @@ -719,7 +739,7 @@ fn func_namespace_components<'i>(

fn func_args<'i>(
state: &RefCell<ExprParseState>,
) -> impl Parser<Input<'i>, FuncArgs, ContextError> + '_ {
) -> impl ModalParser<Input<'i>, FuncArgs, ContextError> + '_ {
move |input: &mut Input| {
#[derive(Copy, Clone)]
enum Trailer {
Expand Down
6 changes: 3 additions & 3 deletions crates/hcl-edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::template::Template;
mod prelude {
pub(super) use winnow::error::{ContextError, StrContext, StrContextValue};
pub(super) use winnow::stream::Stream;
pub(super) use winnow::{dispatch, PResult, Parser};
pub(super) use winnow::{dispatch, ModalParser, ModalResult, Parser};

pub(super) type Input<'a> = winnow::stream::Located<&'a str>;
pub(super) type Input<'a> = winnow::stream::LocatingSlice<&'a str>;
}

use self::prelude::*;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn parse_template(input: &str) -> Result<Template, Error> {

fn parse_complete<'a, P, O>(input: &'a str, mut parser: P) -> Result<O, Error>
where
P: Parser<Input<'a>, O, ContextError>,
P: ModalParser<Input<'a>, O, ContextError>,
{
let input = Input::new(input);

Expand Down
8 changes: 4 additions & 4 deletions crates/hcl-edit/src/parser/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ use winnow::ascii::digit1;
use winnow::combinator::{alt, cut_err, opt, preceded, terminated};
use winnow::token::one_of;

pub(super) fn number(input: &mut Input) -> PResult<Number> {
pub(super) fn number(input: &mut Input) -> ModalResult<Number> {
alt((
float.verify_map(Number::from_f64),
integer.map(Number::from),
))
.parse_next(input)
}

fn integer(input: &mut Input) -> PResult<u64> {
fn integer(input: &mut Input) -> ModalResult<u64> {
digit1.try_map(|s: &str| u64::from_str(s)).parse_next(input)
}

fn float(input: &mut Input) -> PResult<f64> {
fn float(input: &mut Input) -> ModalResult<f64> {
let fraction = preceded('.', digit1);

terminated(digit1, alt((terminated(fraction, opt(exponent)), exponent)))
Expand All @@ -28,7 +28,7 @@ fn float(input: &mut Input) -> PResult<f64> {
.parse_next(input)
}

fn exponent<'a>(input: &mut Input<'a>) -> PResult<&'a str> {
fn exponent<'a>(input: &mut Input<'a>) -> ModalResult<&'a str> {
(
one_of(b"eE"),
opt(one_of(b"+-")),
Expand Down
Loading

0 comments on commit 792b737

Please sign in to comment.