add peek functionality? #80
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Greetings - and thank you for this awesome module! Amazing demonstration of the power of Zig comptime!
I built a "peek" parser that does not consume anything, but checks to see if the nested parser parsed. It returns .ok if the nested parser parses with an index = 0. If the nested parser fails it returns .err with index = 0.
This was useful for parsing an identifier where I needed the first character to be alphabetic and the following characters could be alphanumeric. When I initially built this pattern, using a combine, the resulting type was a {u8, []u8} and required additional handling (e.g. didn't play well with other branches of a oneOf as the other branch returned []u8. Using peek inside the combine I was able to insist on the first character being alpha and yet still easily generate a []u8 return type.
Seems like this could be useful in other circumstances...?