iterator.Try previously copied the entire iterator and synced it back on success, causing an unnecessary heap allocation on every call.
This PR simplifies Try to save and restore the index directly, and removes the now-unused Copy and Sync methods.
Rewrite ScanRune and ParseRawToken as peek-then-advance, eliminating the need for Try at leaf level.
Remove redundant Try wrappers from parseExpression, parseAbstraction, parseApplication, parseLet, and parseToken, which are already disambiguated by their callers.
Keep Try only where true backtracking is needed: parseStatement, which must choose between parseLet and parseDeclare.
Fix pre-existing panic in saccharine parseExpression when the iterator is exhausted (added Done() guard).
Decisions
Try now operates on the original iterator instead of a copy, removing the confusing pattern where the callback's i was a different object than the caller's i.
Removed parseSoftBreak and parseHardBreak helper functions since ParseRawToken no longer needs Try wrapping.
Benefits
Eliminates a heap allocation per Try call.
Reduces nesting and indirection in all parse functions.
Makes the code easier to follow by removing the shadow-i pattern.
Try is now only used at genuine choice points in the grammar.
Checklist
Code follows conventional commit format.
Branch follows naming convention (<type>/<description>). Always use underscores.
Tests pass (if applicable).
Documentation updated (if applicable).
## Description
`iterator.Try` previously copied the entire iterator and synced it back on success, causing an unnecessary heap allocation on every call.
This PR simplifies `Try` to save and restore the index directly, and removes the now-unused `Copy` and `Sync` methods.
- Rewrite `ScanRune` and `ParseRawToken` as peek-then-advance, eliminating the need for `Try` at leaf level.
- Remove redundant `Try` wrappers from `parseExpression`, `parseAbstraction`, `parseApplication`, `parseLet`, and `parseToken`, which are already disambiguated by their callers.
- Keep `Try` only where true backtracking is needed: `parseStatement`, which must choose between `parseLet` and `parseDeclare`.
- Fix pre-existing panic in saccharine `parseExpression` when the iterator is exhausted (added `Done()` guard).
### Decisions
- `Try` now operates on the original iterator instead of a copy, removing the confusing pattern where the callback's `i` was a different object than the caller's `i`.
- Removed `parseSoftBreak` and `parseHardBreak` helper functions since `ParseRawToken` no longer needs `Try` wrapping.
## Benefits
- Eliminates a heap allocation per `Try` call.
- Reduces nesting and indirection in all parse functions.
- Makes the code easier to follow by removing the shadow-`i` pattern.
- `Try` is now only used at genuine choice points in the grammar.
## Checklist
- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`<type>/<description>`). Always use underscores.
- [x] Tests pass (if applicable).
- [x] Documentation updated (if applicable).
Simplify Try to save/restore the index directly instead of
copying and syncing the entire iterator. Remove the now-unused
Copy and Sync methods.
Rewrite ScanRune and ParseRawToken as peek-then-advance so they
no longer need Try at all. Remove redundant Try wrappers from
parse functions that are already disambiguated by their callers.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mvhutz
merged commit aca197ef51 into main2026-02-12 01:04:27 +00:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Description
iterator.Trypreviously copied the entire iterator and synced it back on success, causing an unnecessary heap allocation on every call.This PR simplifies
Tryto save and restore the index directly, and removes the now-unusedCopyandSyncmethods.ScanRuneandParseRawTokenas peek-then-advance, eliminating the need forTryat leaf level.Trywrappers fromparseExpression,parseAbstraction,parseApplication,parseLet, andparseToken, which are already disambiguated by their callers.Tryonly where true backtracking is needed:parseStatement, which must choose betweenparseLetandparseDeclare.parseExpressionwhen the iterator is exhausted (addedDone()guard).Decisions
Trynow operates on the original iterator instead of a copy, removing the confusing pattern where the callback'siwas a different object than the caller'si.parseSoftBreakandparseHardBreakhelper functions sinceParseRawTokenno longer needsTrywrapping.Benefits
Trycall.ipattern.Tryis now only used at genuine choice points in the grammar.Checklist
<type>/<description>). Always use underscores.