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.
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 instd
, there is anext()
function which returns the nextT
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 runsmanyRange
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
manyIterator
andmanyRangeIterator
maybe some one has a better idea bout the organization of these things.