AbstractLexer.php 7.2 KB

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