Skip to content

Doctrine Lexer upgraded to 3.0 #477

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

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ext-simplexml": "*",
"aeon-php/calendar": "^1.0",
"coduo/php-to-string": "^3",
"doctrine/lexer": "^1.0||^2.0"
"doctrine/lexer": "^3.0"
},
"require-dev": {
"ext-pcov": "*",
Expand Down
74 changes: 13 additions & 61 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,18 @@ protected function getType(&$value) : int
}

if ($this->isBooleanToken($value)) {
$value = \strtolower($value) === 'true';
$value = \strtolower($value);

return self::T_BOOLEAN;
}

if ($this->isNullToken($value)) {
$value = null;
$value = \strtolower($value);

return self::T_NULL;
}

if (\is_numeric($value)) {
if (\is_string($value)) {
$value = (\strpos($value, '.') === false) ? (int) $value : (float) $value;
}

return self::T_NUMBER;
}

Expand Down
68 changes: 47 additions & 21 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Coduo\PHPMatcher\Exception\PatternException;
use Coduo\PHPMatcher\Matcher\Pattern;
use Coduo\PHPMatcher\Parser\ExpanderInitializer;
use Doctrine\Common\Lexer\Token;

final class Parser
{
Expand Down Expand Up @@ -66,8 +67,8 @@ private function getPattern() : AST\Pattern

$pattern = null;

if ($this->lexer->lookahead['type'] == Lexer::T_TYPE_PATTERN) {
$pattern = new AST\Pattern(new AST\Type($this->lexer->lookahead['value']));
if ($this->lexer->lookahead->type == Lexer::T_TYPE_PATTERN) {
$pattern = new AST\Pattern(new AST\Type($this->lexer->lookahead->value));
} else {
throw PatternException::syntaxError($this->unexpectedSyntaxError($this->lexer->lookahead, '@type@ pattern'));
}
Expand Down Expand Up @@ -119,23 +120,19 @@ private function getNextExpanderNode() : ?AST\Expander

private function getExpanderName() : string
{
if ($this->lexer->lookahead['type'] !== Lexer::T_EXPANDER_NAME) {
throw PatternException::syntaxError($this->unexpectedSyntaxError($this->lexer->lookahead, '.expanderName(args) definition'));
}
$lookahead = $this->lexer->lookahead;

$expander = $this->lexer->lookahead['value'];
if ($lookahead === null) {
throw PatternException::syntaxError($this->unexpectedSyntaxError($lookahead, '.expanderName(args) definition'));
}

if ($expander === null) {
throw PatternException::syntaxError($this->unexpectedSyntaxError($this->lexer->lookahead, '.expanderName(args) definition'));
if ($lookahead->type !== Lexer::T_EXPANDER_NAME) {
throw PatternException::syntaxError($this->unexpectedSyntaxError($lookahead, '.expanderName(args) definition'));
}

$this->lexer->moveNext();

if (\is_int($expander)) {
return (string) $expander;
}

return $expander;
return $lookahead->value;
}

/**
Expand All @@ -144,8 +141,9 @@ private function getExpanderName() : string
private function addArgumentValues(AST\Expander $expander) : void
{
while (($argument = $this->getNextArgumentValue()) !== null) {
$argument = ($argument === self::NULL_VALUE) ? null : $argument;
$expander->addArgument($argument);
$coercedArgument = $this->coerceArgumentValue($argument);

$expander->addArgument($coercedArgument);

if (!$this->lexer->isNextToken(Lexer::T_COMMA)) {
break;
Expand All @@ -159,6 +157,33 @@ private function addArgumentValues(AST\Expander $expander) : void
}
}

private function coerceArgumentValue(mixed $argument) : mixed
{
$coercedArgument = $argument;

if (\is_string($argument)) {
$lowercaseArgument = \strtolower($argument);

if ($lowercaseArgument === self::NULL_VALUE) {
$coercedArgument = null;
} elseif ($lowercaseArgument === 'true') {
$coercedArgument = true;
} elseif ($lowercaseArgument === 'false') {
$coercedArgument = false;
} elseif (\is_numeric($argument)) {
$coercedArgument = (\strpos($argument, '.') === false) ? (int) $argument : (float) $argument;
}
} elseif (\is_array($argument)) {
$coercedArgument = [];

foreach ($argument as $key => $arg) {
$coercedArgument[$key] = $this->coerceArgumentValue($arg);
}
}

return $coercedArgument;
}

/**
* Try to get next argument. Return false if there are no arguments left before ")".
*/
Expand Down Expand Up @@ -187,8 +212,8 @@ private function getNextArgumentValue()
throw PatternException::syntaxError($this->unexpectedSyntaxError($this->lexer->lookahead, 'string, number, boolean or null argument'));
}

$tokenType = $this->lexer->lookahead['type'];
$argument = $this->lexer->lookahead['value'];
$tokenType = $this->lexer->lookahead->type;
$argument = $this->lexer->lookahead->value;
$this->lexer->moveNext();

if ($tokenType === Lexer::T_NULL) {
Expand Down Expand Up @@ -258,15 +283,15 @@ private function isNextCloseParenthesis() : bool
}

/**
* @param mixed $unexpectedToken
* @param ?Token<Lexer::T_*, string> $unexpectedToken
* @param string $expected
*/
private function unexpectedSyntaxError($unexpectedToken, string $expected = null) : string
{
$tokenPos = (isset($unexpectedToken['position'])) ? $unexpectedToken['position'] : '-1';
$tokenPos = $unexpectedToken !== null ? $unexpectedToken->position : '-1';
$message = \sprintf('line 0, col %d: Error: ', $tokenPos);
$message .= (isset($expected)) ? \sprintf('Expected "%s", got ', $expected) : 'Unexpected';
$message .= \sprintf('"%s"', $unexpectedToken['value']);
$message .= \sprintf('"%s"', $unexpectedToken->value);

return $message;
}
Expand All @@ -278,7 +303,8 @@ private function unexpectedSyntaxError($unexpectedToken, string $expected = null
*/
private function unexpectedEndOfString(string $expected = null) : void
{
$tokenPos = (isset($this->lexer->token['position'])) ? $this->lexer->token['position'] + \strlen((string) $this->lexer->token['value']) : '-1';
$tokenPos = (isset($this->lexer->token->position))
? $this->lexer->token->position + \strlen((string) $this->lexer->token->value) : '-1';
$message = \sprintf('line 0, col %d: Error: ', $tokenPos);
$message .= (isset($expected)) ? \sprintf('Expected "%s", got end of string.', $expected) : 'Unexpected';
$message .= 'end of string';
Expand Down
Loading