-
Notifications
You must be signed in to change notification settings - Fork 79
Proposed changes for 0.1.0 #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a48939
[WIP] v1.0.0 proposal
TysonAndre 465d02f
v0.1.0: Raise minimum php version, change some AST representations
TysonAndre 0b40640
Refactor to use getStartPosition
TysonAndre 5df29ef
Convert array()/list() to short array `[]` with phpcbf
TysonAndre 56f2603
Support php 8.1
TysonAndre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
[](https://travis-ci.org/Microsoft/tolerant-php-parser) | ||
|
||
This is an early-stage PHP parser designed, from the beginning, for IDE usage scenarios (see [Design Goals](#design-goals) for more details). There is | ||
still a ton of work to be done, so at this point, this repo mostly serves as | ||
still a ton of work to be done, so at this point, this repo mostly serves as | ||
an experiment and the start of a conversation. | ||
|
||
 | ||
|
||
This is the v0.1 branch, which changes data structures to support syntax added after the initial 0.0.x release line. | ||
|
||
## Get Started | ||
After you've [configured your machine](docs/GettingStarted.md), you can use the parser to generate and work | ||
After you've [configured your machine](docs/GettingStarted.md), you can use the parser to generate and work | ||
with the Abstract Syntax Tree (AST) via a friendly API. | ||
```php | ||
<?php | ||
|
@@ -38,17 +40,17 @@ foreach ($astNode->getDescendantNodes() as $descendant) { | |
// All Nodes link back to their parents, so it's easy to navigate the tree. | ||
$grandParent = $descendant->getParent()->getParent(); | ||
var_dump($grandParent->getNodeKindName()); | ||
|
||
// The AST is fully-representative, and round-trippable to the original source. | ||
// This enables consumers to build reliable formatting and refactoring tools. | ||
var_dump($grandParent->getLeadingCommentAndWhitespaceText()); | ||
} | ||
|
||
// In addition to retrieving all children or descendants of a Node, | ||
// Nodes expose properties specific to the Node type. | ||
if ($descendant instanceof Node\Expression\EchoExpression) { | ||
$echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition(); | ||
// To cut down on memory consumption, positions are represented as a single integer | ||
// To cut down on memory consumption, positions are represented as a single integer | ||
// index into the document, but their line and character positions are easily retrieved. | ||
$lineCharacterPosition = PositionUtilities::getLineCharacterPositionFromPosition( | ||
$echoKeywordStartPosition, | ||
|
@@ -59,15 +61,15 @@ foreach ($astNode->getDescendantNodes() as $descendant) { | |
} | ||
``` | ||
|
||
> Note: [the API](docs/ApiDocumentation.md) is not yet finalized, so please file issues let us know what functionality you want exposed, | ||
> Note: [the API](docs/ApiDocumentation.md) is not yet finalized, so please file issues let us know what functionality you want exposed, | ||
and we'll see what we can do! Also please file any bugs with unexpected behavior in the parse tree. We're still | ||
in our early stages, and any feedback you have is much appreciated :smiley:. | ||
|
||
## Design Goals | ||
* Error tolerant design - in IDE scenarios, code is, by definition, incomplete. In the case that invalid code is entered, the | ||
parser should still be able to recover and produce a valid + complete tree, as well as relevant diagnostics. | ||
parser should still be able to recover and produce a valid + complete tree, as well as relevant diagnostics. | ||
* Fast and lightweight (should be able to parse several MB of source code per second, | ||
to leave room for other features). | ||
to leave room for other features). | ||
* Memory-efficient data structures | ||
* Allow for incremental parsing in the future | ||
* Adheres to [PHP language spec](https://github.com/php/php-langspec), | ||
|
@@ -83,34 +85,34 @@ so each language server operation should be < 50 ms to leave room for all the | |
confusing, really fast, so readability and debug-ability is high priority. | ||
* Testable - the parser should produce provably valid parse trees. We achieve this by defining and continuously testing | ||
a set of invariants about the tree. | ||
* Friendly and descriptive API to make it easy for others to build on. | ||
* Friendly and descriptive API to make it easy for others to build on. | ||
* Written in PHP - make it as easy as possible for the PHP community to consume and contribute. | ||
|
||
## Current Status and Approach | ||
To ensure a sufficient level of correctness at every step of the way, the | ||
parser is being developed using the following incremental approach: | ||
|
||
* [x] **Phase 1:** Write lexer that does not support PHP grammar, but supports EOF | ||
* [x] **Phase 1:** Write lexer that does not support PHP grammar, but supports EOF | ||
and Unknown tokens. Write tests for all invariants. | ||
* [x] **Phase 2:** Support PHP lexical grammar, lots of tests | ||
* [x] **Phase 3:** Write a parser that does not support PHP grammar, but produces tree of | ||
* [x] **Phase 3:** Write a parser that does not support PHP grammar, but produces tree of | ||
Error Nodes. Write tests for all invariants. | ||
* [x] **Phase 4:** Support PHP syntactic grammar, lots of tests | ||
* [ ] **Phase 5 (in progress :running:):** Real-world validation and optimization | ||
* [ ] _**Correctness:**_ validate that there are no errors produced on sample codebases, benchmark against other parsers (investigate any instance of disagreement), fuzz-testing | ||
* [ ] _**Performance:**_ profile, benchmark against large PHP applications | ||
* [ ] **Phase 6:** Finalize API to make it as easy as possible for people to consume. | ||
* [ ] **Phase 6:** Finalize API to make it as easy as possible for people to consume. | ||
|
||
### Additional notes | ||
A few of the PHP grammatical constructs (namely yield-expression, and template strings) | ||
are not yet supported and there are also other miscellaneous bugs. However, because the parser is error-tolerant, | ||
these errors are handled gracefully, and the resulting tree is otherwise complete. To get a more holistic sense for | ||
where we are, you can run the "validation" test suite (see [Contributing Guidelines](Contributing.md) for more info | ||
where we are, you can run the "validation" test suite (see [Contributing Guidelines](Contributing.md) for more info | ||
on running tests). Or simply, take a look at the current [validation test results](https://travis-ci.org/Microsoft/tolerant-php-parser). | ||
|
||
Even though we haven't yet begun the performance optimization stage, we have seen promising results so far, | ||
and have plenty more room for improvement. See [How It Works](docs/HowItWorks.md) for details on our current | ||
approach, and run the [Performance Tests](Contributing.md#running-performance-tests) on your | ||
Even though we haven't yet begun the performance optimization stage, we have seen promising results so far, | ||
and have plenty more room for improvement. See [How It Works](docs/HowItWorks.md) for details on our current | ||
approach, and run the [Performance Tests](Contributing.md#running-performance-tests) on your | ||
own machine to see for yourself. | ||
|
||
## Learn more | ||
|
@@ -119,7 +121,7 @@ own machine to see for yourself. | |
**:book: [Documentation](docs/GettingStarted.md#getting-started)** - learn how to reference the parser from your project, and how to perform | ||
operations on the AST to answer questions about your code. | ||
|
||
**:eyes: [Syntax Visualizer Tool](syntax-visualizer/client#php-parser-syntax-visualizer-tool)** - get a more tangible feel for the AST. Get creative - see if you can break it! | ||
**:eyes: [Syntax Visualizer Tool](syntax-visualizer/client#php-parser-syntax-visualizer-tool)** - get a more tangible feel for the AST. Get creative - see if you can break it! | ||
|
||
**:chart_with_upwards_trend: [Current Status and Approach](#current-status-and-approach)** - how much of the grammar is supported? Performance? Memory? API stability? | ||
|
||
|
@@ -131,10 +133,10 @@ operations on the AST to answer questions about your code. | |
* [Validation Strategy](docs/HowItWorks.md#validation-strategy) | ||
|
||
**:sparkling_heart: [Contribute!](Contributing.md)** - learn how to get involved, check out some pointers to educational commits that'll | ||
help you ramp up on the codebase (even if you've never worked on a parser before), | ||
help you ramp up on the codebase (even if you've never worked on a parser before), | ||
and recommended workflows that make it easier to iterate. | ||
|
||
--- | ||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). | ||
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact | ||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). | ||
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact | ||
[[email protected]](mailto:[email protected]) with any additional questions or comments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://wiki.php.net/rfc/internal_method_return_types adds support for tentative return types, and
:mixed
was later added to JsonSerializable's method in 8.1 in php/php-src#7051 . This means that implementations will have to return something (or throw), and also that the absence of a return type will emit a warning.vendor/microsoft/tolerant-php-parser/src/Token.php:113 [8192] Declaration of Microsoft\PhpParser\Token::jsonSerialize() should be compatible with JsonSerializable::jsonSerialize(): mixed
(a warning, not an error)Because php 7 is still supported, add an annotation to suppress the warning instead.
Eventually this may have to drop support for 7.x because the mixed type requires 8.0, but that's probably enough time, but that depends on how many 8.x minor releases there are, which is unknown