Parsing recovers spans by pointer subtraction and settles the currency across three variables #37

Closed
opened 2026-08-16 00:35:29 +00:00 by rosa · 0 comments
Owner

What to build

Two things inside the parser make it harder to follow than the job requires.

The first is how a span is found. span_of subtracts one string's pointer from
another's to work out where a token sits in the text the caller handed in. It
only answers correctly when the token is a slice of that exact text, and nothing
in the type system says so — a separate string holding the same characters gives
a wrong answer, or a panic. Carry the offset instead: a small slice type that
holds its own position and keeps it through trimming and splitting, so a span is
correct by construction and span_of is not needed.

The second is how the currency is settled. identify spreads one decision
across three pieces of state: a two-slot array standing in for the prefix and
suffix, a separate record of whichever side held an ISO code, and a fold over
what is left. Classify each side once as empty, a code, or a symbol, then match
on the pair.

Neither changes what the parser accepts or refuses. The same errors come out,
pointing at the same byte ranges, counted from the untrimmed start as they are
today.

Acceptance criteria

  • Spans are carried rather than recovered by pointer arithmetic, and span_of is gone
  • The type that carries a span keeps it correct through trimming and splitting
  • identify settles the currency by matching two classified sides, not by threading three pieces of state
  • Every parsing test passes unchanged, including the byte ranges each error points at
  • Spans still count from the start of the text as handed in, whitespace included
  • mise run ci passes

Blocked by

None — can start immediately.

## What to build Two things inside the parser make it harder to follow than the job requires. The first is how a span is found. `span_of` subtracts one string's pointer from another's to work out where a token sits in the text the caller handed in. It only answers correctly when the token is a slice of that exact text, and nothing in the type system says so — a separate string holding the same characters gives a wrong answer, or a panic. Carry the offset instead: a small slice type that holds its own position and keeps it through trimming and splitting, so a span is correct by construction and `span_of` is not needed. The second is how the currency is settled. `identify` spreads one decision across three pieces of state: a two-slot array standing in for the prefix and suffix, a separate record of whichever side held an ISO code, and a fold over what is left. Classify each side once as empty, a code, or a symbol, then match on the pair. Neither changes what the parser accepts or refuses. The same errors come out, pointing at the same byte ranges, counted from the untrimmed start as they are today. ## Acceptance criteria - [ ] Spans are carried rather than recovered by pointer arithmetic, and `span_of` is gone - [ ] The type that carries a span keeps it correct through trimming and splitting - [ ] `identify` settles the currency by matching two classified sides, not by threading three pieces of state - [ ] Every parsing test passes unchanged, including the byte ranges each error points at - [ ] Spans still count from the start of the text as handed in, whitespace included - [ ] `mise run ci` passes ## Blocked by None — can start immediately.
rosa closed this issue 2026-08-21 21:30:39 +00:00
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre#37
No description provided.