Open
Conversation
Author
|
Added also a not() version of peek that operates the same, but returns the inverse err/ok values. |
Hejsil
reviewed
Feb 9, 2025
Owner
Hejsil
left a comment
There was a problem hiding this comment.
Seems useful. Just a few comments
| pub fn peek(comptime parser: anytype) Parser(void) { | ||
| return _peek(parser, false); | ||
| } | ||
|
|
Owner
There was a problem hiding this comment.
A doc comment for peek and not please :)
Comment on lines
+387
to
+393
| test "peek" { | ||
| const fa = testing.failing_allocator; | ||
| const p1 = comptime ascii.range('a', 'z').peek(); | ||
| try expectOk(void, 0, {}, try p1.parse(fa, "a")); | ||
| try expectOk(void, 0, {}, try p1.parse(fa, "aa")); | ||
| try expectErr(void, 0, try p1.parse(fa, "1")); | ||
| } |
Owner
There was a problem hiding this comment.
I'd like to see a test case for not as well in case some refactor occurs and not no longer uses the same logic as peek
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...?