Skip to content

Commit 8073569

Browse files
committed
Generate the parser from doc/grammars with phplrt
ConstExprParser, TypeParser and PhpDocParser are no longer written by hand. tools/phplrt/build.php compiles doc/grammars/phpdoc.pp3 with phplrt and writes every rule of the compiled grammar down as a method of src/Parser/Generated/GeneratedParser.php, which reads exactly what the phplrt runtime would read with the same tables and reports the same failures. The AST is built by the reducers of AstBuilder, which extends the generated class. The public API of the three parsers is unchanged. Some details of exceptions thrown for invalid input differ, and a few public helper methods of TypeParser are removed (see UPGRADING.md). Amp-Thread-ID: https://ampcode.com/threads/T-01a17820-94d1-75c8-9206-2e802d9c0556
1 parent 4e50147 commit 8073569

26 files changed

Lines changed: 31642 additions & 2706 deletions

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ Makefile export-ignore
1717
phpstan-baseline.neon export-ignore
1818
phpstan.neon export-ignore
1919
phpunit.xml export-ignore
20+
src/Parser/Generated/** linguist-generated=true

‎.github/workflows/build.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ jobs:
175175
- name: "Install the grammar toolchain"
176176
run: "make grammars-install"
177177

178+
- name: "Check the generated parser is up to date"
179+
run: |
180+
make grammars-build
181+
git diff --exit-code src/Parser/Generated
182+
178183
- name: "Tests"
179184
run: "make tests"
180185

‎Makefile‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ phpstan-generate-baseline:
4545
.PHONY: grammars-install
4646
grammars-install:
4747
composer install --no-interaction --working-dir tools/phplrt
48+
49+
# Writes the parser of src/Parser/Generated out of doc/grammars. Run it after
50+
# changing a grammar or a reducer of src/Parser/AstBuilder.php.
51+
.PHONY: grammars-build
52+
grammars-build:
53+
php tools/phplrt/build.php

‎UPGRADING.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
Upgrading from phpstan/phpdoc-parser 2.x to 3.0
2+
=================================
3+
4+
### The parser is generated from the grammars
5+
6+
`ConstExprParser`, `TypeParser` and `PhpDocParser` are no longer written by hand. The parser is generated from the grammars in [`doc/grammars`](doc/grammars) with [phplrt](https://github.com/phplrt/phplrt), and the AST is built by `PHPStan\PhpDocParser\Parser\AstBuilder`. The library now requires `ondrejmirtes/phplrt-runtime`.
7+
8+
Their constructors and `parse()` methods (and `PhpDocParser::parseTag()`, `PhpDocParser::parseTagValue()` and `ConstExprParser::parseDoctrineString()`) are the same, and so is the AST they build.
9+
10+
* Some details of a `ParserException` thrown for an invalid input may be different: the token the parser stopped at, and the token it says it expected there.
11+
* These public methods of `TypeParser`, which were not meant to be called from the outside, are removed: `parseGeneric()`, `parseTemplateTagValue()`, `isHtml()` and `enrichWithAttributes()`.
12+
* The `TypeParser` and `ConstExprParser` given to the constructors of `TypeParser` and `PhpDocParser` are not used anymore.
13+
114
Upgrading from phpstan/phpdoc-parser 1.x to 2.0
215
=================================
316

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"description": "PHPDoc parser with support for nullable, intersection and generic types",
44
"license": "MIT",
55
"require": {
6-
"php": "^7.4 || ^8.0"
6+
"php": "^7.4 || ^8.0",
7+
"ondrejmirtes/phplrt-runtime": "^4.2.4"
78
},
89
"require-dev": {
910
"doctrine/annotations": "^2.0",

‎doc/grammars/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ run are left in `temp/fuzzy` to be looked at.
3636
That is a great deal more of the language than a hand-written corpus covers, and
3737
it is what replaced the `abnfgen`-driven fuzzer this project used before.
3838

39+
## The parser is generated from them
40+
41+
`src/Parser/Generated/GeneratedParser.php` is the parser of `phpdoc.pp3`, and
42+
it is written by `tools/phplrt/build.php` — never edit it by hand:
43+
44+
make grammars-install # the toolchain, which asks for PHP 8.4
45+
make grammars-build
46+
47+
Every rule is a method reading it the way the phplrt runtime would read the
48+
compiled grammar, and building its value as it goes: the rules a node of the
49+
AST is built out of are the ones `src/Parser/AstBuilder.php` has a `reduce`
50+
method for, so a change of a grammar or of a reducer is followed by
51+
`make grammars-build`. The `Grammars` job of the CI checks that the parser in
52+
the repository is the one the grammars write.
53+
3954
## What keeps them in step
4055

4156
The fuzzer walks the grammars one way; `tests/PHPStan/Parser/GrammarSyncTest.php`

‎doc/grammars/const-expr.pp3‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ ConstantArrayItem
8888
* - BAR
8989
*/
9090
ConstantFetch
91-
: !ArrayKeyword() Identifier() <T_DOUBLE_COLON> ClassConstantName()
92-
| !ArrayKeyword() Identifier()
91+
: !ArrayKeyword() Identifier() ( <T_DOUBLE_COLON> ClassConstantName() )?
9392
;
9493

9594
/**

‎doc/grammars/phpdoc-block.pp3‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ PhpDoc
2626
: <T_OPEN_PHPDOC> <T_PHPDOC_EOL>? ( !<T_CLOSE_PHPDOC> PhpDocChildren() )? <T_CLOSE_PHPDOC>
2727
;
2828

29+
/**
30+
* The children one after another, written as a repetition rather than as a
31+
* rule calling itself once per child, so that a PHPDoc of many tags is read
32+
* without nesting a rule per tag.
33+
*/
2934
PhpDocChildren
30-
: UnreadTagChild() <T_PHPDOC_EOL>? MorePhpDocChildren()?
31-
| PhpDocChild() ( <T_PHPDOC_EOL> MorePhpDocChildren()? )?
35+
: PhpDocChildrenItem() ( !<T_CLOSE_PHPDOC> PhpDocChildrenItem() )*
3236
;
3337

34-
MorePhpDocChildren
35-
: !<T_CLOSE_PHPDOC> PhpDocChildren()
38+
PhpDocChildrenItem
39+
: UnreadTagChild() <T_PHPDOC_EOL>?
40+
| PhpDocChild() ( <T_PHPDOC_EOL> | &<T_CLOSE_PHPDOC> )
3641
;
3742

3843
/**
@@ -166,8 +171,7 @@ ParameterNameStart
166171
* space, which is what tells "@var Foo the one" from "@var Foo|the one".
167172
*/
168173
VarTagValue
169-
: Type() VariableName() Description()
170-
| Type() DescriptionAfterType()
174+
: Type() ( VariableName() Description() | DescriptionAfterType() )
171175
;
172176

173177
VariableName
@@ -189,10 +193,13 @@ VariableName
189193
* meant.
190194
*/
191195
MethodTagValue
192-
: StaticType() Type() Identifier() MethodTagValueTail()
193-
| StaticType() Type() MethodTagValueTail()
194-
| Type() Identifier() MethodTagValueTail()
195-
| Type() MethodTagValueTail()
196+
: StaticType() Type() MethodNameAndTail()
197+
| Type() MethodNameAndTail()
198+
;
199+
200+
MethodNameAndTail
201+
: Identifier() MethodTagValueTail()
202+
| MethodTagValueTail()
196203
;
197204

198205
/**
@@ -354,12 +361,15 @@ DoctrineDescription
354361
* to it, while one written after it does not.
355362
*/
356363
Text
357-
: TextLine() TextTail()?
364+
: TextLine() TextTail()*
358365
;
359366

367+
/**
368+
* A line with something on it, along with the blank lines before it, so that
369+
* the blank lines written after the last such line are left unread.
370+
*/
360371
TextTail
361-
: <T_PHPDOC_EOL> !TextStop() TextLineWithSomethingOnIt() TextTail()?
362-
| <T_PHPDOC_EOL> !TextStop() TextTail()
372+
: ( <T_PHPDOC_EOL> !TextStop() )+ TextLineWithSomethingOnIt()
363373
;
364374

365375
TextLine
@@ -579,8 +589,7 @@ NonTagTextToken
579589
* predicate says.
580590
*/
581591
DoctrineTag
582-
: DoctrineTagToken() DoctrineArguments() DoctrineDescription()
583-
| DoctrineTagToken() !ParenthesesOpen() DoctrineDescription()
592+
: DoctrineTagToken() ( DoctrineArguments() | !ParenthesesOpen() ) DoctrineDescription()
584593
| GenericTagToken() DoctrineArguments() DoctrineDescription()
585594
;
586595

‎phpcs.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<arg name="cache" value="tmp/phpcs"/>
99
<arg value="sp"/>
1010

11+
<!-- written by tools/phplrt/build.php -->
12+
<exclude-pattern>src/Parser/Generated/*</exclude-pattern>
13+
1114
<rule ref="build-cs/phpcs.xml">
1215
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
1316
</rule>

‎phpstan.neon‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ parameters:
66
- src
77
- tests
88
excludePaths:
9-
- tests/PHPStan/*/data/*
10-
- tests/PHPStan/Parser/Doctrine/ApiResource.php
9+
analyse:
10+
- src/Parser/Generated/*
11+
analyseAndScan:
12+
- tests/PHPStan/*/data/*
13+
- tests/PHPStan/Parser/Doctrine/ApiResource.php
1114
level: 8
1215
ignoreErrors:
1316
- '#^Dynamic call to static method PHPUnit\\Framework\\Assert#'

0 commit comments

Comments
 (0)