AbstractLexer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Lexer;
  20. /**
  21. * Base class for writing simple lexers, i.e. for creating small DSLs.
  22. *
  23. * @since 2.0
  24. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  25. * @author Jonathan Wage <jonwage@gmail.com>
  26. * @author Roman Borschel <roman@code-factory.org>
  27. */
  28. abstract class AbstractLexer
  29. {
  30. /**
  31. * Array of scanned tokens.
  32. *
  33. * @var array
  34. */
  35. private $tokens = array();
  36. /**
  37. * Current lexer position in input string.
  38. *
  39. * @var integer
  40. */
  41. private $position = 0;
  42. /**
  43. * Current peek of current lexer position.
  44. *
  45. * @var integer
  46. */
  47. private $peek = 0;
  48. /**
  49. * The next token in the input.
  50. *
  51. * @var array
  52. */
  53. public $lookahead;
  54. /**
  55. * The last matched/seen token.
  56. *
  57. * @var array
  58. */
  59. public $token;
  60. /**
  61. * Sets the input data to be tokenized.
  62. *
  63. * The Lexer is immediately reset and the new input tokenized.
  64. * Any unprocessed tokens from any previous input are lost.
  65. *
  66. * @param string $input The input to be tokenized.
  67. *
  68. * @return void
  69. */
  70. public function setInput($input)
  71. {
  72. $this->tokens = array();
  73. $this->reset();
  74. $this->scan($input);
  75. }
  76. /**
  77. * Resets the lexer.
  78. *
  79. * @return void
  80. */
  81. public function reset()
  82. {
  83. $this->lookahead = null;
  84. $this->token = null;
  85. $this->peek = 0;
  86. $this->position = 0;
  87. }
  88. /**
  89. * Resets the peek pointer to 0.
  90. *
  91. * @return void
  92. */
  93. public function resetPeek()
  94. {
  95. $this->peek = 0;
  96. }
  97. /**
  98. * Resets the lexer position on the input to the given position.
  99. *
  100. * @param integer $position Position to place the lexical scanner.
  101. *
  102. * @return void
  103. */
  104. public function resetPosition($position = 0)
  105. {
  106. $this->position = $position;
  107. }
  108. /**
  109. * Checks whether a given token matches the current lookahead.
  110. *
  111. * @param integer|string $token
  112. *
  113. * @return boolean
  114. */
  115. public function isNextToken($token)
  116. {
  117. return null !== $this->lookahead && $this->lookahead['type'] === $token;
  118. }
  119. /**
  120. * Checks whether any of the given tokens matches the current lookahead.
  121. *
  122. * @param array $tokens
  123. *
  124. * @return boolean
  125. */
  126. public function isNextTokenAny(array $tokens)
  127. {
  128. return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
  129. }
  130. /**
  131. * Moves to the next token in the input string.
  132. *
  133. * A token is an associative array containing three items:
  134. * - 'value' : the string value of the token in the input string
  135. * - 'type' : the type of the token (identifier, numeric, string, input
  136. * parameter, none)
  137. * - 'position' : the position of the token in the input string
  138. *
  139. * @return array|null The next token; null if there is no more tokens left.
  140. */
  141. public function moveNext()
  142. {
  143. $this->peek = 0;
  144. $this->token = $this->lookahead;
  145. $this->lookahead = (isset($this->tokens[$this->position]))
  146. ? $this->tokens[$this->position++] : null;
  147. return $this->lookahead !== null;
  148. }
  149. /**
  150. * Tells the lexer to skip input tokens until it sees a token with the given value.
  151. *
  152. * @param string $type The token type to skip until.
  153. *
  154. * @return void
  155. */
  156. public function skipUntil($type)
  157. {
  158. while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
  159. $this->moveNext();
  160. }
  161. }
  162. /**
  163. * Checks if given value is identical to the given token.
  164. *
  165. * @param mixed $value
  166. * @param integer $token
  167. *
  168. * @return boolean
  169. */
  170. public function isA($value, $token)
  171. {
  172. return $this->getType($value) === $token;
  173. }
  174. /**
  175. * Moves the lookahead token forward.
  176. *
  177. * @return array|null The next token or NULL if there are no more tokens ahead.
  178. */
  179. public function peek()
  180. {
  181. if (isset($this->tokens[$this->position + $this->peek])) {
  182. return $this->tokens[$this->position + $this->peek++];
  183. } else {
  184. return null;
  185. }
  186. }
  187. /**
  188. * Peeks at the next token, returns it and immediately resets the peek.
  189. *
  190. * @return array|null The next token or NULL if there are no more tokens ahead.
  191. */
  192. public function glimpse()
  193. {
  194. $peek = $this->peek();
  195. $this->peek = 0;
  196. return $peek;
  197. }
  198. /**
  199. * Scans the input string for tokens.
  200. *
  201. * @param string $input A query string.
  202. *
  203. * @return void
  204. */
  205. protected function scan($input)
  206. {
  207. static $regex;
  208. if ( ! isset($regex)) {
  209. $regex = '/(' . implode(')|(', $this->getCatchablePatterns()) . ')|'
  210. . implode('|', $this->getNonCatchablePatterns()) . '/i';
  211. }
  212. $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
  213. $matches = preg_split($regex, $input, -1, $flags);
  214. foreach ($matches as $match) {
  215. // Must remain before 'value' assignment since it can change content
  216. $type = $this->getType($match[0]);
  217. $this->tokens[] = array(
  218. 'value' => $match[0],
  219. 'type' => $type,
  220. 'position' => $match[1],
  221. );
  222. }
  223. }
  224. /**
  225. * Gets the literal for a given token.
  226. *
  227. * @param integer $token
  228. *
  229. * @return string
  230. */
  231. public function getLiteral($token)
  232. {
  233. $className = get_class($this);
  234. $reflClass = new \ReflectionClass($className);
  235. $constants = $reflClass->getConstants();
  236. foreach ($constants as $name => $value) {
  237. if ($value === $token) {
  238. return $className . '::' . $name;
  239. }
  240. }
  241. return $token;
  242. }
  243. /**
  244. * Lexical catchable patterns.
  245. *
  246. * @return array
  247. */
  248. abstract protected function getCatchablePatterns();
  249. /**
  250. * Lexical non-catchable patterns.
  251. *
  252. * @return array
  253. */
  254. abstract protected function getNonCatchablePatterns();
  255. /**
  256. * Retrieve token type. Also processes the token value if necessary.
  257. *
  258. * @param string $value
  259. *
  260. * @return integer
  261. */
  262. abstract protected function getType(&$value);
  263. }