Skip to content

Commit 769b9d9

Browse files
authored
Merge pull request #349 from TysonAndre/v1-refactor
Proposed changes for 0.1.0
2 parents 35646d5 + 56f2603 commit 769b9d9

File tree

316 files changed

+3544
-3712
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

316 files changed

+3544
-3712
lines changed

.travis.yml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
language: php
22

33
php:
4-
- 7.0
5-
- 7.1
6-
- 7.2
7-
- 7.3
8-
- 7.4
9-
# Should be changed to 8.0 once travis officially provides that label.
10-
- nightly
4+
- '7.2'
5+
- '7.3'
6+
- '7.4'
7+
- '8.0'
118

129
env:
1310
- VALIDATION=false
@@ -30,12 +27,7 @@ cache:
3027

3128
before_script:
3229
- if [[ $STATIC_ANALYSIS = true ]]; then composer require phpstan/phpstan --no-update; fi
33-
- |
34-
if php -r 'exit(PHP_MAJOR_VERSION < 8 ? 0 : 1);';
35-
then composer install
36-
else
37-
composer install --ignore-platform-reqs
38-
fi
30+
- composer install
3931
- set -e # Stop on first error.
4032
- phpenv config-rm xdebug.ini || true
4133
- if find . -name "*.php" -path "./src/*" -path "./experiments/*" -path "./tools/*" -path "./syntax-visualizer/server/src/*" -exec php -l {} 2>&1 \; | grep "syntax error, unexpected"; then exit 1; fi

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
[![Build Status](https://travis-ci.org/Microsoft/tolerant-php-parser.svg?branch=master)](https://travis-ci.org/Microsoft/tolerant-php-parser)
33

44
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
5-
still a ton of work to be done, so at this point, this repo mostly serves as
5+
still a ton of work to be done, so at this point, this repo mostly serves as
66
an experiment and the start of a conversation.
77

88
![image](https://cloud.githubusercontent.com/assets/762848/19023070/4ab01c92-889a-11e6-9bb5-ec1a6816aba2.png)
99

10+
This is the v0.1 branch, which changes data structures to support syntax added after the initial 0.0.x release line.
11+
1012
## Get Started
11-
After you've [configured your machine](docs/GettingStarted.md), you can use the parser to generate and work
13+
After you've [configured your machine](docs/GettingStarted.md), you can use the parser to generate and work
1214
with the Abstract Syntax Tree (AST) via a friendly API.
1315
```php
1416
<?php
@@ -38,17 +40,17 @@ foreach ($astNode->getDescendantNodes() as $descendant) {
3840
// All Nodes link back to their parents, so it's easy to navigate the tree.
3941
$grandParent = $descendant->getParent()->getParent();
4042
var_dump($grandParent->getNodeKindName());
41-
43+
4244
// The AST is fully-representative, and round-trippable to the original source.
4345
// This enables consumers to build reliable formatting and refactoring tools.
4446
var_dump($grandParent->getLeadingCommentAndWhitespaceText());
4547
}
46-
48+
4749
// In addition to retrieving all children or descendants of a Node,
4850
// Nodes expose properties specific to the Node type.
4951
if ($descendant instanceof Node\Expression\EchoExpression) {
5052
$echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition();
51-
// To cut down on memory consumption, positions are represented as a single integer
53+
// To cut down on memory consumption, positions are represented as a single integer
5254
// index into the document, but their line and character positions are easily retrieved.
5355
$lineCharacterPosition = PositionUtilities::getLineCharacterPositionFromPosition(
5456
$echoKeywordStartPosition,
@@ -59,15 +61,15 @@ foreach ($astNode->getDescendantNodes() as $descendant) {
5961
}
6062
```
6163

62-
> Note: [the API](docs/ApiDocumentation.md) is not yet finalized, so please file issues let us know what functionality you want exposed,
64+
> Note: [the API](docs/ApiDocumentation.md) is not yet finalized, so please file issues let us know what functionality you want exposed,
6365
and we'll see what we can do! Also please file any bugs with unexpected behavior in the parse tree. We're still
6466
in our early stages, and any feedback you have is much appreciated :smiley:.
6567

6668
## Design Goals
6769
* Error tolerant design - in IDE scenarios, code is, by definition, incomplete. In the case that invalid code is entered, the
68-
parser should still be able to recover and produce a valid + complete tree, as well as relevant diagnostics.
70+
parser should still be able to recover and produce a valid + complete tree, as well as relevant diagnostics.
6971
* Fast and lightweight (should be able to parse several MB of source code per second,
70-
to leave room for other features).
72+
to leave room for other features).
7173
* Memory-efficient data structures
7274
* Allow for incremental parsing in the future
7375
* 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
8385
confusing, really fast, so readability and debug-ability is high priority.
8486
* Testable - the parser should produce provably valid parse trees. We achieve this by defining and continuously testing
8587
a set of invariants about the tree.
86-
* Friendly and descriptive API to make it easy for others to build on.
88+
* Friendly and descriptive API to make it easy for others to build on.
8789
* Written in PHP - make it as easy as possible for the PHP community to consume and contribute.
8890

8991
## Current Status and Approach
9092
To ensure a sufficient level of correctness at every step of the way, the
9193
parser is being developed using the following incremental approach:
9294

93-
* [x] **Phase 1:** Write lexer that does not support PHP grammar, but supports EOF
95+
* [x] **Phase 1:** Write lexer that does not support PHP grammar, but supports EOF
9496
and Unknown tokens. Write tests for all invariants.
9597
* [x] **Phase 2:** Support PHP lexical grammar, lots of tests
96-
* [x] **Phase 3:** Write a parser that does not support PHP grammar, but produces tree of
98+
* [x] **Phase 3:** Write a parser that does not support PHP grammar, but produces tree of
9799
Error Nodes. Write tests for all invariants.
98100
* [x] **Phase 4:** Support PHP syntactic grammar, lots of tests
99101
* [ ] **Phase 5 (in progress :running:):** Real-world validation and optimization
100102
* [ ] _**Correctness:**_ validate that there are no errors produced on sample codebases, benchmark against other parsers (investigate any instance of disagreement), fuzz-testing
101103
* [ ] _**Performance:**_ profile, benchmark against large PHP applications
102-
* [ ] **Phase 6:** Finalize API to make it as easy as possible for people to consume.
104+
* [ ] **Phase 6:** Finalize API to make it as easy as possible for people to consume.
103105

104106
### Additional notes
105107
A few of the PHP grammatical constructs (namely yield-expression, and template strings)
106108
are not yet supported and there are also other miscellaneous bugs. However, because the parser is error-tolerant,
107109
these errors are handled gracefully, and the resulting tree is otherwise complete. To get a more holistic sense for
108-
where we are, you can run the "validation" test suite (see [Contributing Guidelines](Contributing.md) for more info
110+
where we are, you can run the "validation" test suite (see [Contributing Guidelines](Contributing.md) for more info
109111
on running tests). Or simply, take a look at the current [validation test results](https://travis-ci.org/Microsoft/tolerant-php-parser).
110112

111-
Even though we haven't yet begun the performance optimization stage, we have seen promising results so far,
112-
and have plenty more room for improvement. See [How It Works](docs/HowItWorks.md) for details on our current
113-
approach, and run the [Performance Tests](Contributing.md#running-performance-tests) on your
113+
Even though we haven't yet begun the performance optimization stage, we have seen promising results so far,
114+
and have plenty more room for improvement. See [How It Works](docs/HowItWorks.md) for details on our current
115+
approach, and run the [Performance Tests](Contributing.md#running-performance-tests) on your
114116
own machine to see for yourself.
115117

116118
## Learn more
@@ -119,7 +121,7 @@ own machine to see for yourself.
119121
**:book: [Documentation](docs/GettingStarted.md#getting-started)** - learn how to reference the parser from your project, and how to perform
120122
operations on the AST to answer questions about your code.
121123

122-
**: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!
124+
**: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!
123125

124126
**:chart_with_upwards_trend: [Current Status and Approach](#current-status-and-approach)** - how much of the grammar is supported? Performance? Memory? API stability?
125127

@@ -131,10 +133,10 @@ operations on the AST to answer questions about your code.
131133
* [Validation Strategy](docs/HowItWorks.md#validation-strategy)
132134

133135
**:sparkling_heart: [Contribute!](Contributing.md)** - learn how to get involved, check out some pointers to educational commits that'll
134-
help you ramp up on the codebase (even if you've never worked on a parser before),
136+
help you ramp up on the codebase (even if you've never worked on a parser before),
135137
and recommended workflows that make it easier to iterate.
136138

137139
---
138-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
139-
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
140+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
141+
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
140142
[opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"description": "Tolerant PHP-to-AST parser designed for IDE usage scenarios",
44
"type": "library",
55
"require": {
6-
"php": ">=7.0"
6+
"php": ">=7.2"
77
},
88
"require-dev": {
9-
"phpunit/phpunit": "^6.4|^7.5.20"
9+
"phpunit/phpunit": "^8.5.15"
1010
},
1111
"license": "MIT",
1212
"authors": [

docs/ApiDocumentation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
```php
1111
public function getNodeKindName ( ) : string
1212
```
13-
### Node::getStart
13+
### Node::getStartPosition
1414
Gets start position of Node, not including leading comments and whitespace.
1515
```php
16-
public function getStart ( ) : int
16+
public function getStartPosition ( ) : int
1717
```
18-
### Node::getFullStart
18+
### Node::getFullStartPosition
1919
Gets start position of Node, including leading comments and whitespace
2020
```php
21-
public function getFullStart ( ) : int
21+
public function getFullStartPosition ( ) : int
2222
```
2323
### Node::getParent
2424
Gets parent of current node (returns null if has no parent)
@@ -198,11 +198,11 @@ public function getFullText ( string & $document ) : string
198198
```php
199199
public function getStartPosition ( )
200200
```
201-
### Token::getFullStart
201+
### Token::getFullStartPosition
202202
> TODO: add doc comment
203203
204204
```php
205-
public function getFullStart ( )
205+
public function getFullStartPosition ( )
206206
```
207207
### Token::getWidth
208208
> TODO: add doc comment

src/FilePositionMap.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,19 @@ public function __construct(string $file_contents) {
3434
$this->lineForCurrentOffset = 1;
3535
}
3636

37-
/**
38-
* @param Node $node the node to get the start line for.
39-
* TODO deprecate and merge this and getTokenStartLine into getStartLine
40-
* if https://github.com/Microsoft/tolerant-php-parser/issues/166 is fixed,
41-
* (i.e. if there is a consistent way to get the start offset)
42-
*/
43-
public function getNodeStartLine(Node $node) : int {
44-
return $this->getLineNumberForOffset($node->getStart());
45-
}
46-
47-
/**
48-
* @param Token $token the token to get the start line for.
49-
*/
50-
public function getTokenStartLine(Token $token) : int {
51-
return $this->getLineNumberForOffset($token->start);
52-
}
53-
5437
/**
5538
* @param Node|Token $node
5639
*/
5740
public function getStartLine($node) : int {
58-
if ($node instanceof Token) {
59-
$offset = $node->start;
60-
} else {
61-
$offset = $node->getStart();
62-
}
63-
return $this->getLineNumberForOffset($offset);
41+
return $this->getLineNumberForOffset($node->getStartPosition());
6442
}
6543

6644
/**
6745
* @param Node|Token $node
6846
* Similar to getStartLine but includes the column
6947
*/
7048
public function getStartLineCharacterPositionForOffset($node) : LineCharacterPosition {
71-
if ($node instanceof Token) {
72-
$offset = $node->start;
73-
} else {
74-
$offset = $node->getStart();
75-
}
76-
return $this->getLineCharacterPositionForOffset($offset);
49+
return $this->getLineCharacterPositionForOffset($node->getStartPosition());
7750
}
7851

7952
/** @param Node|Token $node */

src/MissingToken.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
namespace Microsoft\PhpParser;
88

9+
use ReturnTypeWillChange;
10+
911
class MissingToken extends Token {
1012
public function __construct(int $kind, int $fullStart) {
1113
parent::__construct($kind, $fullStart, $fullStart, 0);
1214
}
1315

16+
#[ReturnTypeWillChange]
1417
public function jsonSerialize() {
1518
return array_merge(
1619
["error" => $this->getTokenKindNameFromValue(TokenKind::MissingToken)],

0 commit comments

Comments
 (0)