GithubHelp home page GithubHelp logo

Comments (7)

safasofuoglu avatar safasofuoglu commented on June 2, 2024

Initial attempt:

fn f32vec<'a, E: Error<'a, &'a [u8]> + 'a, T: FromLexical>(
) -> impl Parser<'a, &'a [u8], (), extra::Full<E, bumpalo::collections::Vec<'a, T>, ()>> {
    number::<{ lexical::format::STANDARD }, _, _, _>()
        .padded()
        .map_with_state(|number, _, vec: &mut bumpalo::collections::Vec<T>| vec.push(number))
        .separated_by(just(b","))
        .collect::<()>()
        .delimited_by(just(b'['), just(b']'))
}

let str = b"[2, 5]";
let bump = Bump::new();
let mut vec = bumpalo::collections::Vec::new_in(&bump);
let res = f32vec::<Rich<u8>, f32>().parse_with_state(str, &mut vec);
dbg!(res);
dbg!(vec);

While it's technically possible to invoke f32vec manually, it's unclear how to use it in combinators, especially when and how the Vec gets created. Too eager creation would defeat the purpose. Ideally, f32vec would take a reference to bump and return an owned bumpalo::collections::Vec.

from chumsky.

Zij-IT avatar Zij-IT commented on June 2, 2024

Here is the best I got. It's not ideal, as it would be nice to have foldr/foldl where the initial value is constructed with access to the state with something like: Fn(&mut E::State) -> A

use bumpalo::collections;
use bumpalo::Bump;
use chumsky::{error::Error, prelude::*};
use lexical::FromLexical;

fn f32vec<'a, 'b: 'a, E: Error<'a, &'a [u8]> + 'a, T: FromLexical>(
) -> impl Parser<'a, &'a [u8], collections::Vec<'a, T>, extra::Full<E, &'b bumpalo::Bump, ()>> {
    let number = number::<{ lexical::format::STANDARD }, _, _, _>().padded();

    empty()
        .map_with_state(|_, _, bump| bumpalo::vec![in *bump])
        .foldl(number.separated_by(just(b",")), |mut vec, i| {
            vec.push(i);
            vec
        })
        .delimited_by(just(b'['), just(b']'))
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty() {
        let str = b"[]";
        let bump = Bump::new();
        let res = f32vec::<Rich<u8>, f32>()
            .parse_with_state(str, &mut &bump)
            .into_result();

        assert_eq!(res, Ok(bumpalo::vec![in &bump;]));
    }

    #[test]
    fn one_elem() {
        let str = b"[2]";
        let bump = Bump::new();
        let res = f32vec::<Rich<u8>, f32>()
            .parse_with_state(str, &mut &bump)
            .into_result();

        assert_eq!(res, Ok(bumpalo::vec![in &bump; 2.0]));
    }

    #[test]
    fn two_elem() {
        let str = b"[2, 5]";
        let bump = Bump::new();
        let res = f32vec::<Rich<u8>, f32>()
            .parse_with_state(str, &mut &bump)
            .into_result();

        assert_eq!(res, Ok(bumpalo::vec![in &bump; 2.0, 5.0]));
    }

    #[test]
    fn five_elem() {
        let str = b"[1, 2, 3, 4, 5]";
        let bump = Bump::new();
        let res = f32vec::<Rich<u8>, f32>()
            .parse_with_state(str, &mut &bump)
            .into_result();

        assert_eq!(res, Ok(bumpalo::vec![in &bump; 1.0, 2.0, 3.0, 4.0, 5.0]));
    }
}

While it's technically possible to invoke f32vec manually, it's unclear how to use it in combinators, especially when and how the Vec gets created. Too eager creation would defeat the purpose. Ideally, f32vec would take a reference to bump and return an owned bumpalo::collections::Vec.

If you are referring to the creation of the Vec, you should know that just like std::collections::Vec, bumpalo::collections::Vec don't allocate until elements are pushed into them

from chumsky.

zesterer avatar zesterer commented on June 2, 2024

It's not ideal, as it would be nice to have foldr/foldl where the initial value is constructed with access to the state

And foldl/r_with_state isn't suitable?

from chumsky.

Zij-IT avatar Zij-IT commented on June 2, 2024

I'm using foldl but I have to lead the parser with an awkard empty().map_with_state(...) and drop the two first arguments @zesterer

Perhaps something like this:

    number::<{ lexical::format::STANDARD }, _, _, _>()
        .padded()
        .separated_by(just(b","))
        // Assume `bumpalo::Vec` impl `Collection`
        .collect_from_state(|bump| bumpalo::vec![in *bump])
        .delimited_by(just(b'['), just(b']'))

from chumsky.

zesterer avatar zesterer commented on June 2, 2024

Oh, I see. Yes, that's a bit annoying. I wonder whether it would be feasible to write some sort of Container impl for bumpalo's vector (extending the Container trait to have access to the parser state).

from chumsky.

zesterer avatar zesterer commented on June 2, 2024

Ah, you got there first 😀

from chumsky.

safasofuoglu avatar safasofuoglu commented on June 2, 2024

@Zij-IT wonderful example!

I guess things can be made more ergonomic, but nice to know there's a way with the current facilities and it's not so bad.

from chumsky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.