4 ms·
It doesn't feel very declarative in Rust. Personally, I'm finding it hard to see the intent (I haven't written a line of Rust in my life, so take that with a pi
by louthy 8y ago
It doesn't feel very declarative in Rust. Personally, I'm finding it hard to see the intent (I haven't written a line of Rust in my life, so take that with a pinch of salt, but I am a polyglot programmer).
Really, Haskell's do notation is the big winner when it comes to parser combinators, as the direction of the flow of the parser is easy to follow, but also you can capture variables mid-flight for use later in the expression without obvious nested scope blocks.
It's possible to capture variables with `and_then` by the looks of it, but any suitably complex parser will start to end up quite an ugly mess of nested scopes.
I ported Haskell's Parsec to C# [1], it has LINQ which is similar to Haskell's Do notation. Simple parsers [2] are beautifully declarative, and even complex ones, like this floating point number parser [3], are trivial to follow.
[1] https://github.com/louthy/language-ext https://github.com/louthy/language-ext
[2] https://github.com/louthy/language-ext/blob/master/LanguageExt.Parsec/Parsers/Prim.cs#L452 https://github.com/louthy/language-ext/blob/master/LanguageE...
[2] https://github.com/louthy/language-ext/blob/master/LanguageExt.Parsec/Parsers/Token.cs#L287 https://github.com/louthy/language-ext/blob/master/LanguageE...
- pornel 8y agoThere are libraries like nom: https://lib.rs/crates/nom https://lib.rs/crates/nom or combine https://github.com/Marwes/combine/blob/master/examples/date.rs https://github.com/Marwes/combine/blob/master/examples/date.... that have more declarative-looking syntax. In TFA you intentionally get "raw Rust" to avoid syntax sugar obscuring what's going on.
- Matthias247 8y agoSince you mention parsec and .NET: fparsec [1] for F# is also a great library for building parser combinators. [1] https://www.quanttec.com/fparsec/ https://www.quanttec.com/fparsec/