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

Iterators #20

Closed
wants to merge 1 commit into from
Closed

Iterators #20

wants to merge 1 commit into from

Conversation

erooke
Copy link
Contributor

@erooke erooke commented Feb 16, 2021

Summary

While using mecha I found my self wanting to parse lists of an unknown length. The pre-exisitng manyRange does this however returns a string instead of the objects parsed. I couldn't find any pre-existing method in mecha to facilitate getting the objects parsed. This pull request implements one way of extracting the parsed elements.

First it adds an Iterator(T) struct which functions like other iterators in std, there is a next() function which returns the next T if it exists and null if the iterator is exhausted. This is backed by a string + parser combination where next just calls the parser on the string to get the next element and parse failure is considered the end of the iterator.

Then it adds a manyRangeIterator parser which first runs manyRange on the input to get the string representing the list of values parsed then wraps it up in an Iterator. The "Many Iterator" test shows the intended use pattern.

Concerns

  • This iterator pattern ends up parsing every element twice, once to find the string to back the iterator then again when the value is extracted. The only way I see around this would be to allow Parsers to allocate which feels like the greater of two evils.
  • Since playing with the iterators I've never found my self actually using the reset, might be un-needed.
  • Couldn't think of better function names other than manyIterator and manyRangeIterator maybe some one has a better idea bout the organization of these things.

First pass at implementing an iterator interface. Can lift a `many`
esque parser to something which returns actual values instead of the
string of succesful parses.
Copy link
Owner

@Hejsil Hejsil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, parsing unknown length list of things in mecha right now is not that great. I don't think this is the right solution though. As you mentioned these functions have to parse the strings twice, which is not ideal.

What i really want at some point is a minor rewrite that allows mecha to allocate memory. This would allow for many and manyRange to actually return something useful. I just haven't thought of a good API for this yet.

  • Should parsers only take allocators or should the be able to take an arbitrary user "context"?
  • How does mecha free memory? Maybe mecha should just require the allocator to be an Arena or simular.
  • How are failures propagated compared to parse errors? Should all parsers return Error!?T or do we merge the parse failure into the error set?

@erooke
Copy link
Contributor Author

erooke commented Feb 19, 2021

Closing this due to #21

@erooke erooke closed this Feb 19, 2021
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.

2 participants