File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2424
2525use PackageFactory \ComponentEngine \Parser \Source \Position ;
2626
27+ /**
28+ * @internal
29+ */
2730final 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 2424
2525use PackageFactory \ComponentEngine \Parser \Source \Position ;
2626
27+ /**
28+ * @internal
29+ */
2730final 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments