You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add dedicated control flow combinators (#89)
Closes#86
Add new control flow combinators to improve parser validation,
error handling, and flow control:
- `Verify`: validate parsed results against a predicate
- `Recognize`: return consumed input as output
- `Consumed`: return both raw consumed text and parsed output
- `Eof`: match only at end of input
- `AllConsuming`: ensure entire input is consumed
- `Rest`: return all remaining unconsumed input
- `Value`: return fixed value on parser success
- `Cond`: conditionally apply parser based on boolean
- `Cut`: convert recoverable errors to fatal failures (prevents
backtracking)
- `PeekNot`: negative lookahead (succeed when inner parser fails)
Signed-off-by: purpleclay <purpleclaygh@gmail.com>
Returns a fixed value upon parser success, discarding the actual parse result. Useful for assigning semantic meaning to parsed tokens.
496
+
497
+
```go
498
+
chomp.Value(chomp.Tag("true"), true)("true")
499
+
// rem: ""
500
+
// ext: true
501
+
502
+
chomp.Value(chomp.Tag("null"), nil)("null")
503
+
// rem: ""
504
+
// ext: nil
505
+
```
506
+
507
+
### Cond
508
+
509
+
Conditionally applies a parser based on a boolean flag. If the condition is true, the parser is applied. Otherwise, returns an empty result without consuming input.
Converts recoverable parsing errors into fatal failures, preventing backtracking past decision points. Improves error messaging by committing to a parsing path. When used with `First`, a `CutError` stops backtracking immediately.
524
+
525
+
```go
526
+
// Without Cut, First would try the second alternative and succeed
527
+
// With Cut, once "if" matches, failure on "(" is fatal
528
+
chomp.First(
529
+
chomp.Flatten(chomp.All(
530
+
chomp.Tag("if"),
531
+
chomp.Cut(chomp.Tag("(")))),
532
+
chomp.Tag("if x"))("if x")
533
+
// error: CutError (no backtracking to "if x")
534
+
```
535
+
536
+
### PeekNot
537
+
538
+
Succeeds when the inner parser fails without consuming input. Implements negative lookahead for validation. Pairs with `Peek` for positive lookahead.
0 commit comments