Skip to content

Commit 953953a

Browse files
author
Nicolas Hart
committed
update nette/php-generator to last version
1 parent 76f5023 commit 953953a

File tree

9 files changed

+61
-54
lines changed

9 files changed

+61
-54
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"require-dev": {
1717
"friendsofphp/php-cs-fixer": "^3.4",
18-
"nette/php-generator": "^3.2",
18+
"nette/php-generator": "^4.1",
1919
"phpunit/php-file-iterator": "^3.0",
2020
"phpunit/phpunit": "^9.5",
2121
"symfony/process": "^5.0",

composer.lock

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
>
1010
<projectFiles>
1111
<file name="bin/php-lox" />
12+
<file name="tools/generate-ast" />
1213
<directory name="src" />
1314
<ignoreFiles>
1415
<directory name="vendor" />

src/Expr/Assign.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public function name(): Token
2626
return $this->name;
2727
}
2828

29-
public function accept(Visitor $visitor)
29+
public function value(): Expr
3030
{
31-
return $visitor->visitAssignExpr($this);
31+
return $this->value;
3232
}
3333

34-
public function value(): Expr
34+
public function accept(Visitor $visitor)
3535
{
36-
return $this->value;
36+
return $visitor->visitAssignExpr($this);
3737
}
3838
}

src/Expr/Binary.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public function left(): Expr
2828
return $this->left;
2929
}
3030

31-
public function accept(Visitor $visitor)
32-
{
33-
return $visitor->visitBinaryExpr($this);
34-
}
35-
3631
public function operator(): Token
3732
{
3833
return $this->operator;
@@ -42,4 +37,9 @@ public function right(): Expr
4237
{
4338
return $this->right;
4439
}
40+
41+
public function accept(Visitor $visitor)
42+
{
43+
return $visitor->visitBinaryExpr($this);
44+
}
4545
}

src/Expr/Unary.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public function operator(): Token
2626
return $this->operator;
2727
}
2828

29-
public function accept(Visitor $visitor)
29+
public function right(): Expr
3030
{
31-
return $visitor->visitUnaryExpr($this);
31+
return $this->right;
3232
}
3333

34-
public function right(): Expr
34+
public function accept(Visitor $visitor)
3535
{
36-
return $this->right;
36+
return $visitor->visitUnaryExpr($this);
3737
}
3838
}

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private function synchronize(): void
299299
case TokenType::WHILE():
300300
case TokenType::PRINT():
301301
case TokenType::RETURN():
302-
return;
302+
return;
303303
}
304304

305305
$this->advance();

src/Stmt/VarStmt.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class VarStmt extends Stmt
1616
private Token $name;
1717
private ?Expr $initializer;
1818

19-
public function __construct(Token $name, Expr $initializer = null)
19+
public function __construct(Token $name, ?Expr $initializer)
2020
{
2121
$this->name = $name;
2222
$this->initializer = $initializer;
@@ -27,13 +27,13 @@ public function name(): Token
2727
return $this->name;
2828
}
2929

30-
public function accept(Visitor $visitor)
30+
public function initializer(): ?Expr
3131
{
32-
return $visitor->visitVarStmt($this);
32+
return $this->initializer;
3333
}
3434

35-
public function initializer(): ?Expr
35+
public function accept(Visitor $visitor)
3636
{
37-
return $this->initializer;
37+
return $visitor->visitVarStmt($this);
3838
}
3939
}

tools/generate-ast

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_once __DIR__ . '/../vendor/autoload.php';
55

66
use Nette\PhpGenerator\ClassType;
7+
use Nette\PhpGenerator\InterfaceType;
78
use Nette\PhpGenerator\PhpFile;
89
use Nette\PhpGenerator\PsrPrinter;
910

@@ -22,7 +23,7 @@ defineAst(EXPR_DIR, 'Expr', [
2223
defineAst(STMT_DIR, 'Stmt', [
2324
'ExpressionStmt : Lox\Expr\Expr expression',
2425
'PrintStmt : Lox\Expr\Expr expression',
25-
'VarStmt : Lox\Token name, Lox\Expr\Expr initializer',
26+
'VarStmt : Lox\Token name, ?Lox\Expr\Expr initializer',
2627
]);
2728

2829
function defineAst(string $outputDir, string $baseName, array $types): void
@@ -62,10 +63,9 @@ function generateBaseClass(string $baseName): ClassType
6263
return $class;
6364
}
6465

65-
function generateVisitor(string $baseName, array $types): ClassType
66+
function generateVisitor(string $baseName, array $types): InterfaceType
6667
{
67-
$class = new ClassType('Visitor');
68-
$class->setType('interface');
68+
$class = new InterfaceType('Visitor');
6969

7070
foreach ($types as $type) {
7171
$parts = explode(':', $type);
@@ -88,7 +88,7 @@ function generateVisitor(string $baseName, array $types): ClassType
8888
function generateType(string $baseName, string $className, array $properties = []): ClassType
8989
{
9090
$class = new ClassType($className);
91-
$class->addExtend(sprintf('\Lox\%s\%1$s', $baseName));
91+
$class->setExtends(sprintf('\Lox\%s\%1$s', $baseName));
9292
$class->setFinal();
9393

9494
$constructor = $class->addMethod('__construct');
@@ -122,23 +122,23 @@ function generateType(string $baseName, string $className, array $properties = [
122122
$getter->setReturnType($type);
123123
}
124124
$getter->addBody('return $this->?;', [$propertyName]);
125+
}
125126

126-
// implements accept method
127-
$method = $class->addMethod('accept');
128-
$param = $method->addParameter('visitor');
129-
$param->setType(sprintf('\Lox\%s\Visitor', $baseName));
127+
// implements accept method
128+
$method = $class->addMethod('accept');
129+
$param = $method->addParameter('visitor');
130+
$param->setType(sprintf('\Lox\%s\Visitor', $baseName));
130131

131-
if (strpos($className, $baseName)) {
132-
$method->addBody(sprintf('return $visitor->visit%s($this);', $className));
133-
} else {
134-
$method->addBody(sprintf('return $visitor->visit%s%s($this);', $className, $baseName));
135-
}
132+
if (strpos($className, $baseName)) {
133+
$method->addBody(sprintf('return $visitor->visit%s($this);', $className));
134+
} else {
135+
$method->addBody(sprintf('return $visitor->visit%s%s($this);', $className, $baseName));
136136
}
137137

138138
return $class;
139139
}
140140

141-
function writeClass(string $outputDir, string $baseName, ClassType $class): void
141+
function writeClass(string $outputDir, string $baseName, ClassType|InterfaceType $class): void
142142
{
143143
$file = new PhpFile();
144144
$file->addComment('This file is auto-generated.');
@@ -147,17 +147,22 @@ function writeClass(string $outputDir, string $baseName, ClassType $class): void
147147
$namespace = $file->addNamespace(sprintf('Lox\%s', $baseName));
148148
$namespace->add($class);
149149

150-
if ($class->hasMethod('__construct')) {
150+
if ($class instanceof ClassType && $class->hasMethod('__construct')) {
151151
$constructor = $class->getMethod('__construct');
152152
foreach ($constructor->getParameters() as $parameter) {
153153
if (null !== $parameter->getType() && !in_array($parameter->getType(), $namespace->getUses())) {
154-
$namespace->addUse($parameter->getType());
154+
$namespace->addUse((string) $parameter->getType());
155155
}
156156
}
157157
}
158158

159+
$className = $class->getName();
160+
if (null === $className) {
161+
throw new \InvalidArgumentException('Class or Interface must have a name.');
162+
}
163+
159164
file_put_contents(
160-
sprintf('%s/%s.php', $outputDir, $class->getName()),
165+
sprintf('%s/%s.php', $outputDir, $className),
161166
(new PsrPrinter)->printFile($file)
162167
);
163168
}

0 commit comments

Comments
 (0)