Skip to content

Commit 65865aa

Browse files
author
Kirill Nesmeyanov
committed
Optimize and add AST serialization support
1 parent 16f0e8b commit 65865aa

Some content is hidden

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

55 files changed

+704
-229
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"require-dev": {
2525
"nikic/php-parser": "^4.17",
2626
"phpdocumentor/reflection-docblock": "^5.3",
27-
"friendsofphp/php-cs-fixer": "^3.35",
27+
"friendsofphp/php-cs-fixer": "^3.42",
2828
"phplrt/compiler": "^3.5",
29-
"phpunit/phpunit": "^10.4",
29+
"phpunit/phpunit": "^10.5",
3030
"rector/rector": "^0.18",
31-
"vimeo/psalm": "^5.15"
31+
"vimeo/psalm": "^5.18"
3232
},
3333
"autoload-dev": {
3434
"psr-4": {
@@ -37,8 +37,8 @@
3737
},
3838
"extra": {
3939
"branch-alias": {
40-
"dev-master": "1.0.x-dev",
41-
"dev-main": "1.0.x-dev"
40+
"dev-master": "2.0.x-dev",
41+
"dev-main": "2.0.x-dev"
4242
}
4343
},
4444
"config": {

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
colors="true"
55
backupGlobals="true"
66
stopOnFailure="false"

resources/grammar.php

Lines changed: 143 additions & 150 deletions
Large diffs are not rendered by default.

resources/grammar.pp2

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,9 @@ TypesList -> {
126126
)*
127127
;
128128

129-
ThisType -> {
130-
return new Node\Stmt\ThisTypeNode();
131-
}
132-
: <T_THIS>
133-
;
134-
135129
PrimaryType
136130
: ::T_PARENTHESIS_OPEN:: Type() ::T_PARENTHESIS_CLOSE::
137131
| Literal()
138132
| CallableType()
139133
| NamedType()
140-
| ThisType()
141134
;

resources/grammar/lexemes.pp2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
// Identifier
1717

18-
%token T_THIS \$this(?![a-zA-Z0-9\-_\x80-\xff])
1918
%token T_NEQ (?i)is\h+not(?![a-zA-Z0-9\-_\x80-\xff])
2019
%token T_EQ (?i)is(?![a-zA-Z0-9\-_\x80-\xff])
2120
%token T_VARIABLE \$[a-zA-Z_\x80-\xff][a-zA-Z0-9\-_\x80-\xff]*

resources/grammar/literals.pp2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ VariableLiteral -> {
2222
return Node\Literal\VariableLiteralNode::parse($token->getValue());
2323
}
2424
: <T_VARIABLE>
25-
| <T_THIS>
2625
;
2726

2827
StringLiteral -> { return $this->stringPool[$token] ??= $children; }

resources/grammar/shape-fields.pp2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ ShapeFieldsList -> {
4343

4444
foreach ($children as $field) {
4545
if ($field instanceof Node\Stmt\Shape\ExplicitFieldNode) {
46-
$identifier = $field->getIdentifier();
46+
$key = $field->getKey();
4747

48-
if (\in_array($identifier, $explicit, true)) {
48+
if (\in_array($key, $explicit, true)) {
4949
throw new SemanticException(
50-
\sprintf('Duplicate key "%s"', $identifier),
50+
\sprintf('Duplicate key "%s"', $key),
5151
$field->offset,
5252
SemanticException::CODE_SHAPE_KEY_DUPLICATION,
5353
);
5454
}
5555

56-
$explicit[] = $identifier;
56+
$explicit[] = $key;
5757
} else {
5858
$implicit = true;
5959
}
6060
}
6161

6262
if ($explicit !== [] && $implicit) {
6363
throw new SemanticException(
64-
\sprintf('Cannot mix explicit and implicit shape keys', $identifier),
64+
\sprintf('Cannot mix explicit and implicit shape keys', $key),
6565
$offset,
6666
SemanticException::CODE_SHAPE_KEY_MIX,
6767
);
@@ -98,7 +98,7 @@ ExplicitField -> {
9898
;
9999

100100
ImplicitField -> {
101-
return new Node\Stmt\Shape\FieldNode($children[0]);
101+
return new Node\Stmt\Shape\ImplicitFieldNode($children[0]);
102102
}
103103
: ShapeValue()
104104
;

src/Node/Identifier.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class Identifier extends Node implements \Stringable
3232
'iterable',
3333
'null',
3434
'true',
35-
'false'
35+
'false',
3636
];
3737

3838
/**
@@ -106,4 +106,19 @@ public function __toString(): string
106106
{
107107
return $this->value;
108108
}
109+
110+
public function toArray(): array
111+
{
112+
return [
113+
'identifier' => $this->toString(),
114+
];
115+
}
116+
117+
/**
118+
* @return non-empty-string
119+
*/
120+
public function jsonSerialize(): string
121+
{
122+
return $this->toString();
123+
}
109124
}

src/Node/Literal/BoolLiteralNode.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ public function getValue(): bool
4040
{
4141
return $this->value;
4242
}
43+
44+
public function toArray(): array
45+
{
46+
return [
47+
...parent::toArray(),
48+
'kind' => LiteralKind::BOOL_KIND,
49+
];
50+
}
4351
}

src/Node/Literal/FloatLiteralNode.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ public function getValue(): float
4040
{
4141
return $this->value;
4242
}
43+
44+
public function toArray(): array
45+
{
46+
return [
47+
...parent::toArray(),
48+
'kind' => LiteralKind::FLOAT_KIND,
49+
];
50+
}
4351
}

0 commit comments

Comments
 (0)