Code Monkey home page Code Monkey logo

Comments (4)

dexterdy avatar dexterdy commented on June 11, 2024 1

Ah yes! Thank you those are exactly what I need

from chumsky.

zesterer avatar zesterer commented on June 11, 2024

Perhaps .filter(...) is what you're looking for? You can do a.filter(|a| a.is_foo()). I think you might be a bit confused about how parser outputs work though: Fallibility is built in to a parser, if the filter closure returns false, then the parser just won't parse anything.

from chumsky.

dexterdy avatar dexterdy commented on June 11, 2024

No, that's not really what i'm looking for. I'm not looking for a fallible parser, but for one that will parse something or not parse something based on a condition independent of what is being parsed. So condition(true, just('a')) is a parser with output Option<char>, specifically Some(a).

from chumsky.

zesterer avatar zesterer commented on June 11, 2024

Ah, are you trying to dynamically change the behaviour of your parser at runtime based on some configuration for the parser? I assumed that the condition was dependent on the output of the parser. In that case, there are a few approaches you can take:

  1. Parsers can be dynamically dispatched, so you can do if condition { a.map(Some).boxed() } else { empty().to(None).boxed() } and this should type-check just fine (the parser in the else branch always succeeds while parsing no tokens)

  2. Use custom like so:

custom(|inp| if condition { inp.parse(a).map(Some) } else { Ok(None) })

this approach avoids the dynamic dispatch of the previous approach.

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.