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

Add more general primitive combinator. #46

Closed
wants to merge 1 commit into from

Conversation

scott-fleischman
Copy link

I came across a need for a more general primitive combinator than tokenPrimEx when parsing more complicated structures than characters. In that case, the test function argument may return more information in the error case. The new tokenPrimEx' (any ideas for a better name?) takes a function that returns Either instead of Maybe to allow for any type (instead of Nothing) as error information. The errMsg function argument (formerly showToken) is also more general so it takes the token, position and error type and returns a ParseError.

It's easy to define tokenPrimEx in terms of tokenPrimEx' where the error type is ().

@mrkkrp
Copy link
Contributor

mrkkrp commented Sep 9, 2015

@scott-fleischman, Can you give an example of such a “more complicated structure”? As I understand you're returning error info in Left so it can be used in error message. I like this idea. Could you open an issue describing this consideration on Megaparsec's page? If you don't have time or desire, I can do it as well.

@scott-fleischman
Copy link
Author

An example is parsing XML Events. One could have an EventParser that is ParsecT [Event] () Identity, so that the token is the Event. A simple combinator for the EventBeginElement case would want to check the name and attributes (the attributes themselves could be checked with an AttributeParser). Checking the name and especially attributes could return more error information than Nothing, such as which attributes were expected but not found.

Feel free to add the idea/code to the Megaparsec project. I don't intend to use that package.

@mrkkrp
Copy link
Contributor

mrkkrp commented Sep 9, 2015

@scott-fleischman, Thank you. Good luck with this pull request.

@scott-fleischman
Copy link
Author

Closing old pull requests. I still think this approach is useful though.

int-index pushed a commit to int-index/parsec that referenced this pull request Sep 18, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants