Skip to content

fix: inject Pos/EndPos/Tokens into Capture and TextUnmarshaler values - #470

Open
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-132-capture-position
Open

fix: inject Pos/EndPos/Tokens into Capture and TextUnmarshaler values#470
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-132-capture-position

Conversation

@Pastalikek65

Copy link
Copy Markdown

Closes #132

Pos/EndPos/Tokens are injected automatically for struct nodes parsed via @@, but user-defined capture targets implementing Capture or encoding.TextUnmarshaler never had them populated — the Pos lexer.Position field stayed zero (verified on master with @Ident @Ident).

Changes:

  • nodes.go: new applyCapturePosition helper populates Pos, EndPos and Tokens on capture targets (guarding non-struct types such as type count int), called from the Capture and TextUnmarshaler branches of setField, and per-element for []Capture slices.
  • parser_test.go: TestCaptureStructPosition covering Pos, EndPos and Tokens on a Capture struct.

EndPos is computed as the last captured token's position advanced by its length (byte offset, rune column) — an approximation of the next-token position the struct path uses; it doesn't account for embedded newlines in captured values.

User-defined capture targets that implement Capture or TextUnmarshaler never
had their Pos/EndPos/Tokens fields populated, unlike struct nodes parsed via
@@. Populate them at set time, mirroring strct.maybeInject*. Fixes alecthomas#132.
@Pastalikek65 Pastalikek65 mentioned this pull request Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression on #35

1 participant