8000 Add byte spans to `SourceLocation` and populate them in the parser · Issue #132 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Add byte spans to SourceLocation and populate them in the parser #132

Closed

Description

@akoskovacs

Part of #131.

SourceLocation (src/lexer/token.hxx:107) carries file, line, and column. That is enough to point at a declaration in an error message, and not enough for anything that has to edit or reproduce source: a text edit is a range plus replacement text, and a losslessness check needs to know exactly which bytes a node spans.

Tokens already carry what is needed — Token::startOffset / Token::endOffset (src/lexer/token.hxx:117) are byte offsets into the original UTF-8 source. The information exists and is discarded at the parser boundary.

What lands

  • SourceLocation gains startOffset and endOffset (exclusive), defaulting to -1 as Token does.
  • The parser fills them from the tokens it consumes: a node's span runs from the first token of the construct to the last, inclusive of closing end/)/].
  • ast::TaggedLiteral::bodyStartOffset / bodyEndOffset (src/ast/ast. 8156 hxx) already do this by hand for one node; that is the pattern, generalised.

Why it is separate

It is a pure core change with no user-visible effect, and every later stage depends on it. Landing it alone keeps the diff reviewable and the blame legible.

Verification

  • make test-all — no behavioural change expected. (Bare ctest misses the spec/*.kex suite.)
  • A new test asserting that for a representative parse, every node has startOffset >= 0, endOffset > startOffset, and a span nested within its parent's. The nesting assertion is what catches a node whose span was filled from the wrong token.

Activity

  1. akoskovacs commented on Aug 21, 2026

    @akoskovacs
    CollaboratorAuthor

    Done. SourceLocation carries startOffset/endOffset (src/lexer/token.hxx:114), the parser fills them from the tokens it consumes, and tests/parser_test.cxx:43 asserts both the offsets and the parent/child span nesting the ticket asked for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0