Microsoft is writing a PHP parser that handles error recovery well, and the internals docs are really accessible. It includes a discussion of handling trivia and even some sample parsing code. https://github.com/microsoft/tolerant-php-parser/blob/main/docs/HowItWorks.md