-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TASK: Implement alternative approach to lexical analysis #34
Draft
grebaldi
wants to merge
19
commits into
main
Choose a base branch
from
task/3/cleanup-tokenizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c2122b5
TASK: Implement Lexer
grebaldi 5022652
TASK: Enable zend.assertions in CI
grebaldi 4f5b603
TASK: Prepare Lexer interface for parser use cases
grebaldi dd1625d
TASK: Reform all parsers to use new Lexer
grebaldi 66e0e7c
TASK: Remove Tokenizer and all related obsolete concepts
grebaldi 158fbc1
TASK: Expose buffer of lexer and remove Token class
grebaldi 0883066
TASK: Rename TokenType -> Rule
grebaldi c50ac6a
TASK: Split Scanner from Lexer
grebaldi 5552c70
TASK: Replace remaining references to "TokenType"
grebaldi 2ef8989
TASK: Remove method Lexer::getRuleUnderCursor and replace call-sites
grebaldi 0d35b1f
TASK: Streamline Lexer interface by exposing buffer object directly
grebaldi 6ce0e9a
TASK: Remove superfluous classes under CharacterStream\\*
grebaldi 714673b
TASK: Remove Rules class
grebaldi 1009734
TASK: Merge scan and scanOneOf methods into unified scan method
grebaldi f7e3382
TASK: Replace all `*OneOf` methods of Lexer class
grebaldi 9b1d34d
TASK: Turn abstract Matcher class into interface
grebaldi dcbf92f
TASK: Add method `getRemainder` to Scanner class
grebaldi d7e1799
TASK: Parse negative integer literals
grebaldi 9fc4801
BUGFIX: Fix shebang in scripts
grebaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Language/AST/Node/TemplateLiteral/TemplateLiteralLine.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\Language\AST\Node\TemplateLiteral; | ||
|
||
final class TemplateLiteralLine | ||
{ | ||
public function __construct( | ||
public readonly int $indentation, | ||
public readonly TemplateLiteralSegments $segments | ||
) { | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Language/AST/Node/TemplateLiteral/TemplateLiteralLines.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\Language\AST\Node\TemplateLiteral; | ||
|
||
final class TemplateLiteralLines | ||
{ | ||
/** | ||
* @var TemplateLiteralLine[] | ||
*/ | ||
public readonly array $items; | ||
|
||
public function __construct(TemplateLiteralLine ...$items) | ||
{ | ||
$this->items = $items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\Language\Lexer\Buffer; | ||
|
||
use PackageFactory\ComponentEngine\Parser\Source\Position; | ||
use PackageFactory\ComponentEngine\Parser\Source\Range; | ||
|
||
final class Buffer | ||
{ | ||
private Position $start; | ||
private int $endLineNumber; | ||
private int $nextEndLineNumber; | ||
private int $endColumnNumber; | ||
private int $nextEndColumnNumber; | ||
private string $contents; | ||
|
||
public function __construct() | ||
{ | ||
$this->start = Position::zero(); | ||
$this->endLineNumber = 0; | ||
$this->nextEndLineNumber = 0; | ||
$this->endColumnNumber = 0; | ||
$this->nextEndColumnNumber = 0; | ||
$this->contents = ''; | ||
} | ||
|
||
public function getStart(): Position | ||
{ | ||
return $this->start; | ||
} | ||
|
||
public function getEnd(): Position | ||
{ | ||
return Position::from($this->endLineNumber, $this->endColumnNumber); | ||
} | ||
|
||
public function getRange(): Range | ||
{ | ||
return Range::from($this->getStart(), $this->getEnd()); | ||
} | ||
|
||
public function getContents(): string | ||
{ | ||
return $this->contents; | ||
} | ||
|
||
public function append(?string $character): void | ||
{ | ||
if ($character === null) { | ||
return; | ||
} | ||
|
||
$this->contents .= $character; | ||
|
||
$this->endLineNumber = $this->nextEndLineNumber; | ||
$this->endColumnNumber = $this->nextEndColumnNumber; | ||
|
||
if ($character === "\n") { | ||
$this->nextEndLineNumber++; | ||
$this->nextEndColumnNumber = 0; | ||
} else { | ||
$this->nextEndColumnNumber++; | ||
} | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
$this->start = Position::from( | ||
$this->endLineNumber = $this->nextEndLineNumber, | ||
$this->endColumnNumber = $this->nextEndColumnNumber | ||
); | ||
|
||
$this->contents = ''; | ||
} | ||
|
||
public function overwrite(Buffer $other): void | ||
{ | ||
$other->start = $this->start; | ||
$other->endLineNumber = $this->endLineNumber; | ||
$other->nextEndLineNumber = $this->nextEndLineNumber; | ||
$other->endColumnNumber = $this->endColumnNumber; | ||
$other->nextEndColumnNumber = $this->nextEndColumnNumber; | ||
$other->contents = $this->contents; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->endLineNumber = $this->nextEndLineNumber = $this->start->lineNumber; | ||
$this->endColumnNumber = $this->nextEndColumnNumber = $this->start->columnNumber; | ||
$this->contents = ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\Language\Lexer\CharacterStream; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CharacterStream | ||
{ | ||
private int $byte; | ||
private ?string $characterUnderCursor = ''; | ||
|
||
public function __construct(private readonly string $source) | ||
{ | ||
$this->byte = 0; | ||
$this->next(); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
if ($this->characterUnderCursor === null) { | ||
return; | ||
} | ||
|
||
$nextCharacter = $this->source[$this->byte++] ?? null; | ||
if ($nextCharacter === null) { | ||
$this->characterUnderCursor = null; | ||
return; | ||
} | ||
|
||
$ord = ord($nextCharacter); | ||
if ($ord >= 0x80) { | ||
$nextCharacter .= $this->source[$this->byte++] ?? ''; | ||
} | ||
if ($ord >= 0xe0) { | ||
$nextCharacter .= $this->source[$this->byte++] ?? ''; | ||
} | ||
if ($ord >= 0xf0) { | ||
$nextCharacter .= $this->source[$this->byte++] ?? ''; | ||
} | ||
|
||
$this->characterUnderCursor = $nextCharacter; | ||
} | ||
|
||
public function current(): ?string | ||
{ | ||
return $this->characterUnderCursor; | ||
} | ||
|
||
public function isEnd(): bool | ||
{ | ||
return $this->characterUnderCursor === null; | ||
} | ||
|
||
public function overwrite(CharacterStream $other): void | ||
{ | ||
$other->byte = $this->byte; | ||
$other->characterUnderCursor = $this->characterUnderCursor; | ||
} | ||
|
||
public function getRest(): string | ||
{ | ||
return $this->characterUnderCursor . substr($this->source, $this->byte); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Language/Lexer/CharacterStream/CharacterStreamSnapshot.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\Language\Lexer\CharacterStream; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CharacterStreamSnapshot | ||
{ | ||
public function __construct( | ||
public readonly int $byte, | ||
public readonly ?string $characterUnderCursor = null | ||
) { | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm im just wondering what are the pros and cons of making this thing mutable ...
on the one side, the lexer can expose it as public readonly member but methods like
override
andreset
might always be smelly. Then again, this mutable buffer might be a performance optimization, as we dont need a new object every time.