chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

Strange padding issue?

Open Sniper10754 opened this issue 2 months ago • 1 comments

This issue is about this code:

use std::convert::Infallible;

use chumsky::{extra::Err, prelude::*};

#[derive(Debug)]
enum Expr<'a> {
    Ident(&'a str),
    Int(i32),

    Let { name: &'a str, expr: Box<Self> },
}

fn expr_<'a>() -> impl Parser<'a, &'a str, Expr<'a>, Err<Rich<'a, char>>> {
    recursive(|expr| {
        let let_ = text::keyword("let")
            .ignore_then(text::ident())
            .then_ignore(just('=').padded())
            .then(expr.clone())
            .map(|(name, expr)| Expr::Let {
                name,
                expr: Box::new(expr),
            });

        choice((
            let_,
            text::ident().map(Expr::Ident),
            text::int(10)
                .map(|string: &str| string.parse::<i32>().unwrap())
                .map(Expr::Int),
        ))
    })
}

fn main() {
    println!("{:#?}", expr_().parse("let a = 5").into_result());
}

i'd expect it to just return Expr::Let { name: "a", expr: Expr::Int(5) }, but it returns this: found ' ' at 3..4 expected end of input. this is a minimal example of the code i wrote, which worked before the switch to 1.0.0-alpha.6

Sniper10754 avatar Apr 07 '24 18:04 Sniper10754

I believe you are missing a .padded() after text::ident(). Your parser doesn’t expect whitespace after the let keyword, but instead expects another identifier, which is impossible.

I can’t test currently, but give that a shot (:

Zij-IT avatar Apr 07 '24 20:04 Zij-IT