Skip to content

Commit ba5a324

Browse files
committed
TASK: Prepare Lexer interface for parser use cases
1 parent 5022652 commit ba5a324

File tree

10 files changed

+547
-157
lines changed

10 files changed

+547
-157
lines changed

src/Language/Lexer/CharacterStream/CharacterStream.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
use PackageFactory\ComponentEngine\Parser\Source\Position;
2626

27+
/**
28+
* @internal
29+
*/
2730
final class CharacterStream
2831
{
2932
private int $byte;
@@ -81,4 +84,25 @@ public function getPreviousPosition(): Position
8184
{
8285
return $this->cursor->getPreviousPosition();
8386
}
87+
88+
public function makeSnapshot(): CharacterStreamSnapshot
89+
{
90+
return new CharacterStreamSnapshot(
91+
byte: $this->byte,
92+
cursor: $this->cursor->makeSnapshot(),
93+
characterUnderCursor: $this->characterUnderCursor
94+
);
95+
}
96+
97+
public function restoreSnapshot(CharacterStreamSnapshot $snapshot): void
98+
{
99+
$this->byte = $snapshot->byte;
100+
$this->cursor->restoreSnapshot($snapshot->cursor);
101+
$this->characterUnderCursor = $snapshot->characterUnderCursor;
102+
}
103+
104+
public function getRest(): string
105+
{
106+
return $this->characterUnderCursor . substr($this->source, $this->byte);
107+
}
84108
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Language\Lexer\CharacterStream;
24+
25+
/**
26+
* @internal
27+
*/
28+
final class CharacterStreamSnapshot
29+
{
30+
public function __construct(
31+
public readonly int $byte,
32+
public readonly CursorSnapshot $cursor,
33+
public readonly ?string $characterUnderCursor = null
34+
) {
35+
}
36+
}

src/Language/Lexer/CharacterStream/Cursor.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
use PackageFactory\ComponentEngine\Parser\Source\Position;
2626

27+
/**
28+
* @internal
29+
*/
2730
final class Cursor
2831
{
2932
private int $currentLineNumber = 0;
@@ -58,4 +61,22 @@ public function getPreviousPosition(): Position
5861

5962
return new Position($this->previousLineNumber, $this->previousColumnNumber);
6063
}
64+
65+
public function makeSnapshot(): CursorSnapshot
66+
{
67+
return new CursorSnapshot(
68+
currentLineNumber: $this->currentLineNumber,
69+
currentColumnNumber: $this->currentColumnNumber,
70+
previousLineNumber: $this->previousLineNumber,
71+
previousColumnNumber: $this->previousColumnNumber
72+
);
73+
}
74+
75+
public function restoreSnapshot(CursorSnapshot $snapshot): void
76+
{
77+
$this->currentLineNumber = $snapshot->currentLineNumber;
78+
$this->currentColumnNumber = $snapshot->currentColumnNumber;
79+
$this->previousLineNumber = $snapshot->previousLineNumber;
80+
$this->previousColumnNumber = $snapshot->previousColumnNumber;
81+
}
6182
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Language\Lexer\CharacterStream;
24+
25+
/**
26+
* @internal
27+
*/
28+
final class CursorSnapshot
29+
{
30+
public function __construct(
31+
public readonly int $currentLineNumber,
32+
public readonly int $currentColumnNumber,
33+
public readonly int $previousLineNumber,
34+
public readonly int $previousColumnNumber
35+
) {
36+
}
37+
}

0 commit comments

Comments
 (0)