-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from nextras/udpate-dependencies
Udpate dependencies
- Loading branch information
Showing
7 changed files
with
209 additions
and
75 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Entity\Reflection\Parser; | ||
|
||
|
||
/** | ||
* @internal | ||
*/ | ||
class Token | ||
{ | ||
public const KEYWORD = 1; | ||
public const STRING = 2; | ||
public const LBRACKET = 3; | ||
public const RBRACKET = 4; | ||
public const EQUAL = 5; | ||
public const SEPARATOR = 6; | ||
public const WHITESPACE = 7; | ||
|
||
public function __construct( | ||
public readonly string $value, | ||
public readonly int $type, | ||
public readonly int $offset, | ||
) | ||
{ | ||
} | ||
} |
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,101 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Entity\Reflection\Parser; | ||
|
||
|
||
use Nextras\Orm\Exception\InvalidStateException; | ||
|
||
|
||
/** | ||
* @internal | ||
*/ | ||
class TokenLexer | ||
{ | ||
/** @const regular expression for single & double quoted PHP string */ | ||
public const RE_STRING = '\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"'; | ||
|
||
private string $tokenizerRegexp; | ||
/** @var list<int> */ | ||
private array $tokenizerTypes; | ||
|
||
|
||
public function __construct() | ||
{ | ||
$patterns = [ | ||
Token::STRING => self::RE_STRING, | ||
Token::LBRACKET => '\[', | ||
Token::RBRACKET => '\]', | ||
Token::EQUAL => '=', | ||
Token::KEYWORD => '[a-zA-Z0-9_:$.*>\\\\-]+', | ||
Token::SEPARATOR => ',', | ||
Token::WHITESPACE => '\s+', | ||
]; | ||
$this->tokenizerRegexp = '~(' . implode(')|(', $patterns) . ')~A'; | ||
$this->tokenizerTypes = array_keys($patterns); | ||
} | ||
|
||
|
||
public function lex(string $input): TokenStream | ||
{ | ||
$tokens = $this->tokenize($input); | ||
$tokens = array_filter($tokens, function ($token): bool { | ||
return $token->type !== Token::WHITESPACE; | ||
}); | ||
$tokens = array_values($tokens); | ||
return new TokenStream($tokens); | ||
} | ||
|
||
|
||
/** | ||
* @return list<Token> | ||
*/ | ||
private function tokenize(string $input): array | ||
{ | ||
preg_match_all($this->tokenizerRegexp, $input, $tokens, PREG_SET_ORDER); | ||
if (preg_last_error() !== PREG_NO_ERROR) { | ||
throw new InvalidStateException(array_flip(get_defined_constants(true)['pcre'])[preg_last_error()]); | ||
} | ||
|
||
$len = 0; | ||
$result = []; | ||
$count = count($this->tokenizerTypes); | ||
foreach ($tokens as $token) { | ||
$type = null; | ||
for ($i = 1; $i <= $count; $i++) { | ||
if (!isset($token[$i])) { | ||
break; | ||
} elseif ($token[$i] !== '') { | ||
$type = $this->tokenizerTypes[$i - 1]; | ||
break; | ||
} | ||
} | ||
if ($type === null) { | ||
throw new InvalidStateException("Unknown token type for '$token[0]'."); | ||
} | ||
|
||
$token = new Token($token[0], $type, $len); | ||
$result[] = $token; | ||
$len += strlen($token->value); | ||
} | ||
|
||
if ($len !== strlen($input)) { | ||
[$line, $col] = self::getCoordinates($input, $len); | ||
$unexpectedToken = str_replace("\n", '\n', substr($input, $len, 10)); | ||
throw new InvalidStateException("Unexpected '$unexpectedToken' on line $line, column $col."); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
|
||
/** | ||
* Returns (line, column) position of token in input string. | ||
* @return array{int, int} | ||
*/ | ||
private static function getCoordinates(string $text, int $offset): array | ||
{ | ||
$text = substr($text, 0, $offset); | ||
$pos = strrpos("\n" . $text, "\n"); | ||
return [substr_count($text, "\n") + 1, $offset - ($pos === false ? 0 : $pos) + 1]; | ||
} | ||
} |
Oops, something went wrong.