---
title: "Doctrine Lexer: resetPosition() and token offsets | VulCode"
description: "A fix in Doctrine Lexer in PHP: resetPosition() takes the character offset from Token::$position and returns to the right token instead of a wrong index."
url: "https://vulcode.com/en/opensource/contributions/doctrine-lexer"
lang: "en"
---

[All contributions](https://vulcode.com/en/opensource/contributions)

doctrine/lexer

# Doctrine Lexer

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

[See the repository](https://github.com/doctrine/lexer) [All contributions](https://vulcode.com/en/opensource/contributions)

- **Language**: PHP
- **GitHub stars**: 11,141
- **Merged changes**: 1
- **Project site**: [doctrine-project.org](https://www.doctrine-project.org/projects/lexer.html)

## 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](https://github.com/doctrine/lexer/pull/176)

## Merged change 1

- [#176 fix: make resetPosition() accept a token character offset Merged 1 October 2026](https://github.com/doctrine/lexer/pull/176)

## 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](https://vulcode.com/en/technologies/php)

## Other projects we write code for

- [**Composer** PHP · 29,590 stars The dependency manager for PHP: it installs and updates a project's libraries from its composer.json.](https://vulcode.com/en/opensource/contributions/composer)

- [**PHPMailer** PHP · 22,347 stars The classic PHP library for sending email, used by WordPress and many other systems.](https://vulcode.com/en/opensource/contributions/phpmailer)

- [**Monolog** PHP · 21,407 stars A PHP logging library that sends records to files, sockets, inboxes, databases and web services. Symfony and Laravel use it, among others.](https://vulcode.com/en/opensource/contributions/monolog)

- [**Carbon** PHP · 16,583 stars A PHP DateTime extension for working with dates, standard in the Laravel ecosystem.](https://vulcode.com/en/opensource/contributions/carbon)

- [**Whoops** PHP · 13,231 stars A PHP library that turns uncaught exceptions into a readable error page with the stack trace, known from Laravel.](https://vulcode.com/en/opensource/contributions/whoops)

- [**ramsey/uuid** PHP · 12,642 stars A PHP library for generating universally unique identifiers (UUIDs).](https://vulcode.com/en/opensource/contributions/ramsey-uuid)

## Describe your project.

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

### Rather skip the form?

Ask in the chat or write to us directly.

Open the chat

- **Email**: [hello@vulcode.com](mailto:hello@vulcode.com)
- **Phone**: [+48 576 043 920](tel:+48576043920)
- **Discord**: [discord.gg/vulcode](https://discord.gg/vulcode)
