AbstractLexer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Common\Lexer;
  4. use ReflectionClass;
  5. use UnitEnum;
  6. use function get_class;
  7. use function implode;
  8. use function preg_split;
  9. use function sprintf;
  10. use function substr;
  11. use const PREG_SPLIT_DELIM_CAPTURE;
  12. use const PREG_SPLIT_NO_EMPTY;
  13. use const PREG_SPLIT_OFFSET_CAPTURE;
  14. /**
  15. * Base class for writing simple lexers, i.e. for creating small DSLs.
  16. *
  17. * @template T of UnitEnum|string|int
  18. * @template V of string|int
  19. */
  20. abstract class AbstractLexer
  21. {
  22. /**
  23. * Lexer original input string.
  24. *
  25. * @var string
  26. */
  27. private $input;
  28. /**
  29. * Array of scanned tokens.
  30. *
  31. * @var list<Token<T, V>>
  32. */
  33. private $tokens = [];
  34. /**
  35. * Current lexer position in input string.
  36. *
  37. * @var int
  38. */
  39. private $position = 0;
  40. /**
  41. * Current peek of current lexer position.
  42. *
  43. * @var int
  44. */
  45. private $peek = 0;
  46. /**
  47. * The next token in the input.
  48. *
  49. * @var Token<T, V>|null
  50. */
  51. public $lookahead;
  52. /**
  53. * The last matched/seen token.
  54. *
  55. * @var Token<T, V>|null
  56. */
  57. public $token;
  58. /**
  59. * Composed regex for input parsing.
  60. *
  61. * @var non-empty-string|null
  62. */
  63. private $regex;
  64. /**
  65. * Sets the input data to be tokenized.
  66. *
  67. * The Lexer is immediately reset and the new input tokenized.
  68. * Any unprocessed tokens from any previous input are lost.
  69. *
  70. * @param string $input The input to be tokenized.
  71. *
  72. * @return void
  73. */
  74. public function setInput($input)
  75. {
  76. $this->input = $input;
  77. $this->tokens = [];
  78. $this->reset();
  79. $this->scan($input);
  80. }
  81. /**
  82. * Resets the lexer.
  83. *
  84. * @return void
  85. */
  86. public function reset()
  87. {
  88. $this->lookahead = null;
  89. $this->token = null;
  90. $this->peek = 0;
  91. $this->position = 0;
  92. }
  93. /**
  94. * Resets the peek pointer to 0.
  95. *
  96. * @return void
  97. */
  98. public function resetPeek()
  99. {
  100. $this->peek = 0;
  101. }
  102. /**
  103. * Resets the lexer position on the input to the given position.
  104. *
  105. * @param int $position Position to place the lexical scanner.
  106. *
  107. * @return void
  108. */
  109. public function resetPosition($position = 0)
  110. {
  111. $this->position = $position;
  112. }
  113. /**
  114. * Retrieve the original lexer's input until a given position.
  115. *
  116. * @param int $position
  117. *
  118. * @return string
  119. */
  120. public function getInputUntilPosition($position)
  121. {
  122. return substr($this->input, 0, $position);
  123. }
  124. /**
  125. * Checks whether a given token matches the current lookahead.
  126. *
  127. * @param T $type
  128. *
  129. * @return bool
  130. *
  131. * @psalm-assert-if-true !=null $this->lookahead
  132. */
  133. public function isNextToken($type)
  134. {
  135. return $this->lookahead !== null && $this->lookahead->isA($type);
  136. }
  137. /**
  138. * Checks whether any of the given tokens matches the current lookahead.
  139. *
  140. * @param list<T> $types
  141. *
  142. * @return bool
  143. *
  144. * @psalm-assert-if-true !=null $this->lookahead
  145. */
  146. public function isNextTokenAny(array $types)
  147. {
  148. return $this->lookahead !== null && $this->lookahead->isA(...$types);
  149. }
  150. /**
  151. * Moves to the next token in the input string.
  152. *
  153. * @return bool
  154. *
  155. * @psalm-assert-if-true !null $this->lookahead
  156. */
  157. public function moveNext()
  158. {
  159. $this->peek = 0;
  160. $this->token = $this->lookahead;
  161. $this->lookahead = isset($this->tokens[$this->position])
  162. ? $this->tokens[$this->position++] : null;
  163. return $this->lookahead !== null;
  164. }
  165. /**
  166. * Tells the lexer to skip input tokens until it sees a token with the given value.
  167. *
  168. * @param T $type The token type to skip until.
  169. *
  170. * @return void
  171. */
  172. public function skipUntil($type)
  173. {
  174. while ($this->lookahead !== null && ! $this->lookahead->isA($type)) {
  175. $this->moveNext();
  176. }
  177. }
  178. /**
  179. * Checks if given value is identical to the given token.
  180. *
  181. * @param string $value
  182. * @param int|string $token
  183. *
  184. * @return bool
  185. */
  186. public function isA($value, $token)
  187. {
  188. return $this->getType($value) === $token;
  189. }
  190. /**
  191. * Moves the lookahead token forward.
  192. *
  193. * @return Token<T, V>|null The next token or NULL if there are no more tokens ahead.
  194. */
  195. public function peek()
  196. {
  197. if (isset($this->tokens[$this->position + $this->peek])) {
  198. return $this->tokens[$this->position + $this->peek++];
  199. }
  200. return null;
  201. }
  202. /**
  203. * Peeks at the next token, returns it and immediately resets the peek.
  204. *
  205. * @return Token<T, V>|null The next token or NULL if there are no more tokens ahead.
  206. */
  207. public function glimpse()
  208. {
  209. $peek = $this->peek();
  210. $this->peek = 0;
  211. return $peek;
  212. }
  213. /**
  214. * Scans the input string for tokens.
  215. *
  216. * @param string $input A query string.
  217. *
  218. * @return void
  219. */
  220. protected function scan($input)
  221. {
  222. if (! isset($this->regex)) {
  223. $this->regex = sprintf(
  224. '/(%s)|%s/%s',
  225. implode(')|(', $this->getCatchablePatterns()),
  226. implode('|', $this->getNonCatchablePatterns()),
  227. $this->getModifiers()
  228. );
  229. }
  230. $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
  231. $matches = preg_split($this->regex, $input, -1, $flags);
  232. if ($matches === false) {
  233. // Work around https://bugs.php.net/78122
  234. $matches = [[$input, 0]];
  235. }
  236. foreach ($matches as $match) {
  237. // Must remain before 'value' assignment since it can change content
  238. $firstMatch = $match[0];
  239. $type = $this->getType($firstMatch);
  240. $this->tokens[] = new Token(
  241. $firstMatch,
  242. $type,
  243. $match[1]
  244. );
  245. }
  246. }
  247. /**
  248. * Gets the literal for a given token.
  249. *
  250. * @param T $token
  251. *
  252. * @return int|string
  253. */
  254. public function getLiteral($token)
  255. {
  256. if ($token instanceof UnitEnum) {
  257. return get_class($token) . '::' . $token->name;
  258. }
  259. $className = static::class;
  260. $reflClass = new ReflectionClass($className);
  261. $constants = $reflClass->getConstants();
  262. foreach ($constants as $name => $value) {
  263. if ($value === $token) {
  264. return $className . '::' . $name;
  265. }
  266. }
  267. return $token;
  268. }
  269. /**
  270. * Regex modifiers
  271. *
  272. * @return string
  273. */
  274. protected function getModifiers()
  275. {
  276. return 'iu';
  277. }
  278. /**
  279. * Lexical catchable patterns.
  280. *
  281. * @return string[]
  282. */
  283. abstract protected function getCatchablePatterns();
  284. /**
  285. * Lexical non-catchable patterns.
  286. *
  287. * @return string[]
  288. */
  289. abstract protected function getNonCatchablePatterns();
  290. /**
  291. * Retrieve token type. Also processes the token value if necessary.
  292. *
  293. * @param string $value
  294. *
  295. * @return T|null
  296. *
  297. * @param-out V $value
  298. */
  299. abstract protected function getType(&$value);
  300. }