Comments (7)
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.
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 ≎]));
}
#[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 ≎ 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 ≎ 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 ≎ 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.
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.
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.
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.
Ah, you got there first 😀
from chumsky.
@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)
- Indentation example: incorrect "end of input" HOT 2
- Idea: conditonal parser, which takes a parser and a condition HOT 4
- or combinator prefers output of second parser, even if first parser succeeds HOT 14
- when use tokio::spawn i meet problem anyone can help HOT 6
- `nested_in()` + `repeated()` produce incorrect error spans. HOT 5
- Failed to compile benches: no method named `slice` found
- `ContainerExactly` impls for `Rc` and `Arc` are unsound HOT 2
- Clone trait bounds not satisfied HOT 2
- Trait bound not satisfied on foldl HOT 2
- Input lifetimes HOT 4
- bug: couldn't build `examples/foo.rs` HOT 1
- Query : Create a parser that parses a vec of patterns HOT 7
- Crashed while parsing markup text HOT 4
- Fix SpannedInput HOT 1
- Trying to run the `nano_rust.rs` example using `chumsky = "1.0.0-alpha.6"` HOT 4
- Question: Using `map_with` to get spans HOT 2
- Question: Writing a Ternary Operator Parser HOT 5
- Can't build example HOT 1
- `/examples/pythonic.rs` is commented out HOT 1
- `/examples/foo.rs` is different from the tutorial HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chumsky.