You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5c257d7
Browse filesBrowse the repository at this point in the historyBrowse files
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
Copy file name to clipboardExpand all lines: CLAUDE.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -185,6 +185,17 @@ Collects structural information about a class during AST traversal: properties,
185
185
-**`PHPStan\Cache`** - Caching infrastructure
186
186
-**`PHPStan\Testing`** - `RuleTestCase`, `TypeInferenceTestCase`, and other test utilities
187
187
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
+
188
199
## Extension points
189
200
190
201
PHPStan is highly extensible. Key extension interfaces:
@@ -339,4 +350,4 @@ When adding or editing PHPDoc comments in this codebase, follow these guidelines
-`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`)
0 commit comments