All contributions

doctrine/lexer

Doctrine Lexer

A base for lexers used by recursive descent parsers in PHP, used by Doctrine ORM among others.

Language
PHP
GitHub stars
11,141
Merged changes
1
Project site
doctrine-project.org

What we changed

resetPosition() accepts a token's character offset from Token::$position, so you can return exactly to the place in the text the token came from.

What this work shows

  • Lexers and recursive descent parsers
  • API backward compatibility analysis
  • PHPUnit edge case tests
What was broken

resetPosition() landed on the wrong token

The only position the API exposes is Token::$position, a character offset into the input. resetPosition() wrote that number straight into the token array index, so for "hello world foo" resetPosition(6) jumped past the end of the list instead of to "world", and the lexer looked empty.

How we fixed it

The offset is mapped to a token index

resetPosition() finds the first token whose position is at or after the given offset and puts the cursor there, without an extra map in memory. resetPosition(0) and lexers with one-character tokens behave as before, and the tests cover multi-character tokens.

#176

Merged change 1

  1. #176 fix: make resetPosition() accept a token character offset Merged 1 October 2026

What it taught us

A lexer position is two different things, a token index and a place in the text, and an API must say clearly which one it expects. Before changing what the parameter means, we worked out which existing calls could notice.

More about this technology: PHP

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.