123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- declare(strict_types=1);
- namespace Doctrine\Common\Lexer;
- use ReflectionClass;
- use UnitEnum;
- use function get_class;
- use function implode;
- use function preg_split;
- use function sprintf;
- use function substr;
- use const PREG_SPLIT_DELIM_CAPTURE;
- use const PREG_SPLIT_NO_EMPTY;
- use const PREG_SPLIT_OFFSET_CAPTURE;
- abstract class AbstractLexer
- {
-
- private $input;
-
- private $tokens = [];
-
- private $position = 0;
-
- private $peek = 0;
-
- public $lookahead;
-
- public $token;
-
- private $regex;
-
- public function setInput($input)
- {
- $this->input = $input;
- $this->tokens = [];
- $this->reset();
- $this->scan($input);
- }
-
- public function reset()
- {
- $this->lookahead = null;
- $this->token = null;
- $this->peek = 0;
- $this->position = 0;
- }
-
- public function resetPeek()
- {
- $this->peek = 0;
- }
-
- public function resetPosition($position = 0)
- {
- $this->position = $position;
- }
-
- public function getInputUntilPosition($position)
- {
- return substr($this->input, 0, $position);
- }
-
- public function isNextToken($type)
- {
- return $this->lookahead !== null && $this->lookahead->isA($type);
- }
-
- public function isNextTokenAny(array $types)
- {
- return $this->lookahead !== null && $this->lookahead->isA(...$types);
- }
-
- public function moveNext()
- {
- $this->peek = 0;
- $this->token = $this->lookahead;
- $this->lookahead = isset($this->tokens[$this->position])
- ? $this->tokens[$this->position++] : null;
- return $this->lookahead !== null;
- }
-
- public function skipUntil($type)
- {
- while ($this->lookahead !== null && ! $this->lookahead->isA($type)) {
- $this->moveNext();
- }
- }
-
- public function isA($value, $token)
- {
- return $this->getType($value) === $token;
- }
-
- public function peek()
- {
- if (isset($this->tokens[$this->position + $this->peek])) {
- return $this->tokens[$this->position + $this->peek++];
- }
- return null;
- }
-
- public function glimpse()
- {
- $peek = $this->peek();
- $this->peek = 0;
- return $peek;
- }
-
- protected function scan($input)
- {
- if (! isset($this->regex)) {
- $this->regex = sprintf(
- '/(%s)|%s/%s',
- implode(')|(', $this->getCatchablePatterns()),
- implode('|', $this->getNonCatchablePatterns()),
- $this->getModifiers()
- );
- }
- $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
- $matches = preg_split($this->regex, $input, -1, $flags);
- if ($matches === false) {
-
- $matches = [[$input, 0]];
- }
- foreach ($matches as $match) {
-
- $firstMatch = $match[0];
- $type = $this->getType($firstMatch);
- $this->tokens[] = new Token(
- $firstMatch,
- $type,
- $match[1]
- );
- }
- }
-
- public function getLiteral($token)
- {
- if ($token instanceof UnitEnum) {
- return get_class($token) . '::' . $token->name;
- }
- $className = static::class;
- $reflClass = new ReflectionClass($className);
- $constants = $reflClass->getConstants();
- foreach ($constants as $name => $value) {
- if ($value === $token) {
- return $className . '::' . $name;
- }
- }
- return $token;
- }
-
- protected function getModifiers()
- {
- return 'iu';
- }
-
- abstract protected function getCatchablePatterns();
-
- abstract protected function getNonCatchablePatterns();
-
- abstract protected function getType(&$value);
- }
|