Skip to content

Commit 5c257d7

Browse files
committed
Replace Hoa with a regex parser generated by phplrt
The grammar is ported to phplrt's PP3 format in build/regex-parser/RegexGrammar.pp3 and compiled with `make regex-parser` into src/Type/Regex/Parser/RegexParser.php. The compiler runs on PHP 8.4+ and is not a dependency of PHPStan. The generated parser only needs the phplrt runtime, provided by ondrejmirtes/phplrt-runtime, a fork downgraded to PHP 7.4. The parser builds the same trees Hoa did, so RegexGroupParser and IgnoredRegexValidator walk them without changes in behaviour. Amp-Thread-ID: https://ampcode.com/threads/T-01a177b5-960f-71d2-a3e2-fa7649f39d2c
1 parent da4d84b commit 5c257d7

40 files changed

Lines changed: 3986 additions & 1604 deletions

‎CLAUDE.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ Collects structural information about a class during AST traversal: properties,
185185
- **`PHPStan\Cache`** - Caching infrastructure
186186
- **`PHPStan\Testing`** - `RuleTestCase`, `TypeInferenceTestCase`, and other test utilities
187187

188+
### Regex parser (`src/Type/Regex/`)
189+
190+
`RegexGroupParser` parses regex patterns to infer `preg_match()` array shapes (via `RegexArrayShapeMatcher`) and `IgnoredRegexValidator` uses it to validate `ignoreErrors` patterns. The parser itself is generated by [phplrt](https://github.com/phplrt/phplrt):
191+
192+
- **Grammar**: `build/regex-parser/RegexGrammar.pp3` (phplrt `.pp3` format, derived from Hoa's regex grammar). The reducers in the grammar build a tree of `PHPStan\Type\Regex\Parser\RegexNode` objects (`#capturing`, `#alternation`, `#quantification`, `token`, ...) that `RegexGroupParser` walks.
193+
- **Generated parser**: `src/Type/Regex/Parser/RegexParser.php`. Never edit it by hand. Change the grammar and regenerate, then commit both files together.
194+
- **Regenerating**: `make regex-parser`. It installs the phplrt compiler into `build/regex-parser/vendor` (separate `build/regex-parser/composer.json`, needs PHP 8.4+) and runs `build/regex-parser/generate.php`. The script targets PHP 8.1 syntax and post-processes the output so that the PHAR's PHP 7.4 downgrader can handle it (first-class callables become `\Closure::fromCallable()`) and adds `@throws` to `parse()`.
195+
- **Runtime**: the generated parser only needs the phplrt runtime, which comes from the `ondrejmirtes/phplrt-runtime` fork (a PHP 7.4 compatible downgrade of the `phplrt/*` runtime packages that replaces them). The compiler is a build-time dependency only and is never shipped.
196+
- Parse failures throw `Phplrt\Contracts\Parser\Exception\ParserExceptionInterface` or `RegexSyntaxException`; callers treat both as "pattern not supported" and infer no shape.
197+
- After changing the grammar or `RegexGroupParser`, run the regex tests: `tests/PHPStan/Analyser/nsrt/preg_*.php`, `tests/PHPStan/Type/Php/`, `tests/PHPStan/Type/Regex/` and `tests/PHPStan/Rules/Regexp/`.
198+
188199
## Extension points
189200

190201
PHPStan is highly extensible. Key extension interfaces:
@@ -339,4 +350,4 @@ When adding or editing PHPDoc comments in this codebase, follow these guidelines
339350
- `nette/neon` - Configuration file format
340351
- `react/child-process`, `react/async` - Parallel analysis
341352
- `symfony/console` - CLI interface
342-
- `hoa/compiler` - Used for regex type parsing
353+
- `ondrejmirtes/phplrt-runtime` - PHP 7.4 compatible fork of the phplrt runtime, runs the regex parser generated from `build/regex-parser/RegexGrammar.pp3` (`make regex-parser`)

‎Makefile‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ phpstan-generate-baseline-php:
177177
phpstan-pro:
178178
php -d memory_limit=450M bin/phpstan --pro
179179

180+
# Regenerates src/Type/Regex/Parser/RegexParser.php from
181+
# build/regex-parser/RegexGrammar.pp3 with the phplrt compiler. The compiler
182+
# needs PHP 8.4+, the generated parser only needs the phplrt runtime.
183+
.PHONY: regex-parser
184+
regex-parser:
185+
composer install --working-dir build/regex-parser
186+
php build/regex-parser/generate.php
187+
180188
name-collision:
181189
php vendor/bin/detect-collisions --configuration build/collision-detector.json
182190

‎bin/phpstan‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ use Symfony\Component\Console\Helper\ProgressBar;
5252
}
5353
$devOrPharLoader->unregister();
5454

55-
// fix missing key for vendor/hoa/protocol/Wrapper.php
56-
$existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files'] ?? [];
57-
$GLOBALS['__composer_autoload_files'] = array_merge(
58-
$existingComposerAutoloadFiles,
59-
array_fill_keys(['3e76f7f02b41af8cea96018933f6b7e3'], true),
60-
);
61-
6255
$autoloaderInWorkingDirectory = $vendorDirectory . '/autoload.php';
6356
$composerAutoloaderProjectPaths = [];
6457

‎build/phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ parameters:
4141
- ../tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php
4242
- ../tests/PHPStan/Command/IgnoredRegexValidatorTest.php
4343
- ../src/Command/IgnoredRegexValidator.php
44+
- ../src/Type/Regex/Parser/RegexParser.php
4445
exceptions:
4546
uncheckedExceptionClasses:
4647
- 'PHPStan\ShouldNotHappenException'

‎build/regex-parser/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

0 commit comments

Comments
 (0)