AbstractLexer.php
129 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\Common\Lexer; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use ReflectionClass; |
| 6 | use UnitEnum; |
| 7 | use function get_class; |
| 8 | use function implode; |
| 9 | use function preg_split; |
| 10 | use function sprintf; |
| 11 | use function substr; |
| 12 | use const PREG_SPLIT_DELIM_CAPTURE; |
| 13 | use const PREG_SPLIT_NO_EMPTY; |
| 14 | use const PREG_SPLIT_OFFSET_CAPTURE; |
| 15 | abstract class AbstractLexer |
| 16 | { |
| 17 | private $input; |
| 18 | private $tokens = []; |
| 19 | private $position = 0; |
| 20 | private $peek = 0; |
| 21 | public $lookahead; |
| 22 | public $token; |
| 23 | private $regex; |
| 24 | public function setInput($input) |
| 25 | { |
| 26 | $this->input = $input; |
| 27 | $this->tokens = []; |
| 28 | $this->reset(); |
| 29 | $this->scan($input); |
| 30 | } |
| 31 | public function reset() |
| 32 | { |
| 33 | $this->lookahead = null; |
| 34 | $this->token = null; |
| 35 | $this->peek = 0; |
| 36 | $this->position = 0; |
| 37 | } |
| 38 | public function resetPeek() |
| 39 | { |
| 40 | $this->peek = 0; |
| 41 | } |
| 42 | public function resetPosition($position = 0) |
| 43 | { |
| 44 | $this->position = $position; |
| 45 | } |
| 46 | public function getInputUntilPosition($position) |
| 47 | { |
| 48 | return substr($this->input, 0, $position); |
| 49 | } |
| 50 | public function isNextToken($type) |
| 51 | { |
| 52 | return $this->lookahead !== null && $this->lookahead->isA($type); |
| 53 | } |
| 54 | public function isNextTokenAny(array $types) |
| 55 | { |
| 56 | return $this->lookahead !== null && $this->lookahead->isA(...$types); |
| 57 | } |
| 58 | public function moveNext() |
| 59 | { |
| 60 | $this->peek = 0; |
| 61 | $this->token = $this->lookahead; |
| 62 | $this->lookahead = isset($this->tokens[$this->position]) ? $this->tokens[$this->position++] : null; |
| 63 | return $this->lookahead !== null; |
| 64 | } |
| 65 | public function skipUntil($type) |
| 66 | { |
| 67 | while ($this->lookahead !== null && !$this->lookahead->isA($type)) { |
| 68 | $this->moveNext(); |
| 69 | } |
| 70 | } |
| 71 | public function isA($value, $token) |
| 72 | { |
| 73 | return $this->getType($value) === $token; |
| 74 | } |
| 75 | public function peek() |
| 76 | { |
| 77 | if (isset($this->tokens[$this->position + $this->peek])) { |
| 78 | return $this->tokens[$this->position + $this->peek++]; |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | public function glimpse() |
| 83 | { |
| 84 | $peek = $this->peek(); |
| 85 | $this->peek = 0; |
| 86 | return $peek; |
| 87 | } |
| 88 | protected function scan($input) |
| 89 | { |
| 90 | if (!isset($this->regex)) { |
| 91 | $this->regex = sprintf('/(%s)|%s/%s', implode(')|(', $this->getCatchablePatterns()), implode('|', $this->getNonCatchablePatterns()), $this->getModifiers()); |
| 92 | } |
| 93 | $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
| 94 | $matches = preg_split($this->regex, $input, -1, $flags); |
| 95 | if ($matches === \false) { |
| 96 | // Work around https://bugs.php.net/78122 |
| 97 | $matches = [[$input, 0]]; |
| 98 | } |
| 99 | foreach ($matches as $match) { |
| 100 | // Must remain before 'value' assignment since it can change content |
| 101 | $firstMatch = $match[0]; |
| 102 | $type = $this->getType($firstMatch); |
| 103 | $this->tokens[] = new Token($firstMatch, $type, $match[1]); |
| 104 | } |
| 105 | } |
| 106 | public function getLiteral($token) |
| 107 | { |
| 108 | if ($token instanceof UnitEnum) { |
| 109 | return get_class($token) . '::' . $token->name; |
| 110 | } |
| 111 | $className = static::class; |
| 112 | $reflClass = new ReflectionClass($className); |
| 113 | $constants = $reflClass->getConstants(); |
| 114 | foreach ($constants as $name => $value) { |
| 115 | if ($value === $token) { |
| 116 | return $className . '::' . $name; |
| 117 | } |
| 118 | } |
| 119 | return $token; |
| 120 | } |
| 121 | protected function getModifiers() |
| 122 | { |
| 123 | return 'iu'; |
| 124 | } |
| 125 | protected abstract function getCatchablePatterns(); |
| 126 | protected abstract function getNonCatchablePatterns(); |
| 127 | protected abstract function getType(&$value); |
| 128 | } |
| 129 |