Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Parser: exactly #46

Closed
jeffrose opened this issue Aug 4, 2020 · 2 comments
Closed

New Parser: exactly #46

jeffrose opened this issue Aug 4, 2020 · 2 comments

Comments

@jeffrose
Copy link
Contributor

jeffrose commented Aug 4, 2020

I found myself in a situation where based on the results of a lookAhead() I need to take either x or x-1 characters. I couldn't find a convenient way to do that with the current API, so I wrote a new parser, exactly.

exactly( 3, digit ).run( '12345' );
//=> { result: [ '1', '2', '3' ] ... }

Now this does open the door to some anti-patterns. While exactly( 3, letter ) makes sense, exactly( 3, letters ) does not.

First of all, is there a way I could've done this with the current API that I simply didn't see?
Secondly, would you want a PR for the new parser? Admittedly it's not very complicated but it works.

@francisrstokes
Copy link
Owner

Hey Jeff,

exactly() would be an excellent addition to the library. I've also found myself needing it, and normally i use:

const exactly = (n, parser) => sequenceOf(Array.from({ length: n}, () => parser);

But it would be good to have a proper parser with descriptive error messages. So I'd welcome a PR.

As for anti-patterns - since these are already possible with things like many(letters) I would say it's on the library user to understand what they're constructing.

jeffrose added a commit to jeffrose/arcsecond that referenced this issue Aug 14, 2020
francisrstokes pushed a commit that referenced this issue Aug 16, 2020
@francisrstokes
Copy link
Owner

francisrstokes commented Aug 16, 2020

Merged and published in 3.1.0 🎉, thanks @jeffrose !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants