Hejsil / mecha

A parser combinator library for Zig
MIT License
413 stars 18 forks source link

Iterators #20

Closed erooke closed 3 years ago

erooke commented 3 years ago

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

erooke commented 3 years ago

Closing this due to #21