Report columns in syntax errors

This commit is contained in:
Fabien Potencier
2026-06-05 20:20:32 +02:00
parent 4943bb405c
commit a82782ac30
13 changed files with 149 additions and 20 deletions
+19
View File
@@ -44,4 +44,23 @@ final class Source
{
return $this->path;
}
/**
* Returns the 1-based column for a 0-based byte offset in the source code.
*
* A negative offset means the position is unknown and yields null.
*
* @return positive-int|null
*/
public function getColumn(int $offset): ?int
{
if ($offset < 0) {
return null;
}
$before = str_replace(["\r\n", "\r"], "\n", substr($this->code, 0, $offset));
$lineStart = strrpos($before, "\n");
return false === $lineStart ? \strlen($before) + 1 : \strlen($before) - $lineStart;
}
}