Skip to content

Fix token 'end' offset for quoted identifiers, raw strings, and literals - #393

Open
ApparentlyTejas wants to merge 1 commit into
jmespath:developfrom
ApparentlyTejas:fix/lexer-token-end-offset
Open

ApparentlyTejas wants to merge 1 commit into
jmespath:developfrom
ApparentlyTejas:fix/lexer-token-end-offset

Conversation

@ApparentlyTejas

@ApparentlyTejas ApparentlyTejas commented Nov 4, 2026 •

Copy link
Copy Markdown

Fixes #380.

_consume_literal, _consume_quoted_identifier, and _consume_raw_string_literal all set the token's end to token_len (the token's length) instead of start + token_len, so end was wrong — and less than start — whenever the token wasn't at the start of the expression:

>>> from jmespath.lexer import Lexer
>>> [t for t in Lexer().tokenize('foo."q"') if t['type'] == 'quoted_identifier']
[{'type': 'quoted_identifier', 'value': 'q', 'start': 4, 'end': 2}]

Every other token type in the lexer already uses start + len(...) for end (see the other 'end': assignments in lexer.py).

The reported issue was specifically about quoted identifiers, but the same copy-pasted bug affects two more token types: backtick literals (_consume_literal) and raw string literals (_consume_raw_string_literal). Fixed and covered all three rather than just the one reported.

Search results aren't affected, only the position information (start/end), so this shouldn't change behavior for anyone who isn't inspecting raw token positions.

_consume_literal, _consume_quoted_identifier, and
_consume_raw_string_literal all set the token's 'end' to the token's
length instead of start + length, so 'end' was wrong (and less than
'start') whenever the token wasn't at the start of the expression.
Every other token type in the lexer already uses start + len(...).

Fixes the quoted-identifier case from the report; the same bug also
affects backtick literals and raw string literals, so those are
fixed and covered too. Search results aren't affected, only the
position information.
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.

Lexer gives quoted identifiers an end that is the token length, not its end offset

1 participant