Skip to content

Commit e4e8cda

Browse files
author
Kirill Nesmeyanov
committed
Improve serialization logic
1 parent 9d31dbc commit e4e8cda

File tree

70 files changed

+437
-22
lines changed

Some content is hidden

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

70 files changed

+437
-22
lines changed

src/Exception/ParseException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace TypeLang\Parser\Exception;
66

7+
use Phplrt\Contracts\Source\SourceExceptionInterface;
8+
79
class ParseException extends \LogicException implements ParserExceptionInterface
810
{
911
final public const ERROR_CODE_UNEXPECTED_TOKEN = 0x01;
@@ -28,6 +30,7 @@ final public function __construct(string $message, int $code = 0, ?\Throwable $p
2830
* unexpected source location.
2931
*
3032
* @param int<0, max> $offset
33+
* @throws SourceExceptionInterface
3134
*/
3235
public static function fromUnexpectedToken(string $char, string $statement, int $offset): static
3336
{
@@ -44,6 +47,7 @@ public static function fromUnexpectedToken(string $char, string $statement, int
4447
* This error occurs when unable to recognize tokens in source code.
4548
*
4649
* @param int<0, max> $offset
50+
* @throws SourceExceptionInterface
4751
*/
4852
public static function fromUnrecognizedToken(string $token, string $statement, int $offset): static
4953
{
@@ -58,6 +62,7 @@ public static function fromUnrecognizedToken(string $token, string $statement, i
5862

5963
/**
6064
* @param int<0, max> $offset
65+
* @throws SourceExceptionInterface
6166
*/
6267
public static function fromUnrecognizedSyntaxError(string $statement, int $offset): static
6368
{
@@ -71,6 +76,7 @@ public static function fromUnrecognizedSyntaxError(string $statement, int $offse
7176

7277
/**
7378
* @param int<0, max> $offset
79+
* @throws SourceExceptionInterface
7480
*/
7581
public static function fromSemanticError(string $message, string $statement, int $offset, int $code = 0): static
7682
{

src/Node/Identifier.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,20 @@ public function jsonSerialize(): string
128128
{
129129
return $this->toString();
130130
}
131+
132+
public function __serialize(): array
133+
{
134+
return [$this->offset, $this->value];
135+
}
136+
137+
public function __unserialize(array $data): void
138+
{
139+
$this->offset = $data[0] ?? throw new \UnexpectedValueException(
140+
message: 'Unable to unserialize Identifier offset',
141+
);
142+
143+
$this->value = $data[1] ?? throw new \UnexpectedValueException(
144+
message: 'Unable to unserialize Identifier value',
145+
);
146+
}
131147
}

src/Node/Name.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,20 @@ public function jsonSerialize(): string
300300
{
301301
return $this->toString();
302302
}
303+
304+
public function __serialize(): array
305+
{
306+
return [$this->offset, $this->parts];
307+
}
308+
309+
public function __unserialize(array $data): void
310+
{
311+
$this->offset = $data[0] ?? throw new \UnexpectedValueException(
312+
message: 'Unable to unserialize Name offset',
313+
);
314+
315+
$this->parts = $data[1] ?? throw new \UnexpectedValueException(
316+
message: 'Unable to unserialize Name identifier parts',
317+
);
318+
}
303319
}

src/Node/Stmt/ClassConstMaskNode.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99

1010
class ClassConstMaskNode extends TypeStatement
1111
{
12+
public ?Identifier $constant;
13+
14+
/**
15+
* @param Identifier|non-empty-string|null $constant
16+
*/
1217
public function __construct(
1318
public Name $class,
14-
public ?Identifier $constant = null,
15-
) {}
19+
Identifier|string|null $constant = null,
20+
) {
21+
$this->constant = \is_string($constant) ? new Identifier($constant) : $constant;
22+
}
1623

1724
public function toArray(): array
1825
{

src/Node/Stmt/ClassConstNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ClassConstNode extends ClassConstMaskNode
1111
{
12-
public function __construct(Name $class, Identifier $constant)
12+
public function __construct(Name $class, Identifier|string $constant)
1313
{
1414
parent::__construct($class, $constant);
1515
}

src/Node/Stmt/GenericTypeStmt.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,22 @@ public function toArray(): array
2323
'type' => $this->type->toArray(),
2424
];
2525
}
26+
27+
public function __serialize(): array
28+
{
29+
return [$this->offset, $this->type];
30+
}
31+
32+
public function __unserialize(array $data): void
33+
{
34+
$this->offset = $data[0] ?? throw new \UnexpectedValueException(\sprintf(
35+
'Unable to unserialize %s offset',
36+
static::class,
37+
));
38+
39+
$this->type = $data[1] ?? throw new \UnexpectedValueException(\sprintf(
40+
'Unable to unserialize %s type',
41+
static::class,
42+
));
43+
}
2644
}

src/Node/Stmt/LogicalTypeNode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ public function toArray(): array
7070
'items' => $items,
7171
];
7272
}
73+
74+
public function __serialize(): array
75+
{
76+
return [$this->offset, $this->statements];
77+
}
78+
79+
public function __unserialize(array $data): void
80+
{
81+
$this->offset = $data[0] ?? throw new \UnexpectedValueException(\sprintf(
82+
'Unable to unserialize %s offset',
83+
static::class,
84+
));
85+
86+
$this->statements = $data[1] ?? throw new \UnexpectedValueException(\sprintf(
87+
'Unable to unserialize %s statements',
88+
static::class,
89+
));
90+
}
7391
}

src/Node/Stmt/NamedTypeNode.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
namespace TypeLang\Parser\Node\Stmt;
66

7+
use TypeLang\Parser\Node\Identifier;
78
use TypeLang\Parser\Node\Name;
89
use TypeLang\Parser\Node\Stmt\Shape\FieldsListNode;
910
use TypeLang\Parser\Node\Stmt\Template\ArgumentsListNode;
1011

1112
class NamedTypeNode extends TypeStatement
1213
{
14+
public Name $name;
15+
16+
/**
17+
* @param Name|Identifier|non-empty-string $name
18+
*/
1319
public function __construct(
14-
public Name $name,
20+
Name|Identifier|string $name,
1521
public ?ArgumentsListNode $arguments = null,
1622
public ?FieldsListNode $fields = null,
17-
) {}
23+
) {
24+
$this->name = $name instanceof Name ? $name : new Name($name);
25+
}
1826

1927
public function toArray(): array
2028
{

src/Node/Stmt/Shape/NamedFieldNode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
final class NamedFieldNode extends ExplicitFieldNode
1111
{
12+
public Identifier $key;
13+
14+
/**
15+
* @param Identifier|non-empty-string $key
16+
*/
1217
public function __construct(
13-
public Identifier $key,
18+
Identifier|string $key,
1419
TypeStatement $of,
1520
bool $optional = false,
1621
) {
22+
$this->key = \is_string($key) ? new Identifier($key) : $key;
23+
1724
parent::__construct($of, $optional);
1825
}
1926

src/Node/Stmt/Template/ArgumentNode.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010

1111
class ArgumentNode extends Node
1212
{
13+
public ?Identifier $hint;
14+
15+
/**
16+
* @param Identifier|non-empty-string|null $hint
17+
*/
1318
public function __construct(
1419
public TypeStatement $value,
15-
public ?Identifier $hint = null,
16-
) {}
20+
Identifier|string|null $hint = null,
21+
) {
22+
$this->hint = \is_string($hint) ? new Identifier($hint) : $hint;
23+
}
1724

1825
public function toArray(): array
1926
{

0 commit comments

Comments
 (0)