AbstractLexer.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * @var array Array of scanned tokens
  32. */
  33. private $tokens = array();
  34. /**
  35. * @var integer Current lexer position in input string
  36. */
  37. private $position = 0;
  38. /**
  39. * @var integer Current peek of current lexer position
  40. */
  41. private $peek = 0;
  42. /**
  43. * @var array The next token in the input.
  44. */
  45. public $lookahead;
  46. /**
  47. * @var array The last matched/seen token.
  48. */
  49. public $token;
  50. /**
  51. * Sets the input data to be tokenized.
  52. *
  53. * The Lexer is immediately reset and the new input tokenized.
  54. * Any unprocessed tokens from any previous input are lost.
  55. *
  56. * @param string $input The input to be tokenized.
  57. */
  58. public function setInput($input)
  59. {
  60. $this->tokens = array();
  61. $this->reset();
  62. $this->scan($input);
  63. }
  64. /**
  65. * Resets the lexer.
  66. */
  67. public function reset()
  68. {
  69. $this->lookahead = null;
  70. $this->token = null;
  71. $this->peek = 0;
  72. $this->position = 0;
  73. }
  74. /**
  75. * Resets the peek pointer to 0.
  76. */
  77. public function resetPeek()
  78. {
  79. $this->peek = 0;
  80. }
  81. /**
  82. * Resets the lexer position on the input to the given position.
  83. *
  84. * @param integer $position Position to place the lexical scanner
  85. */
  86. public function resetPosition($position = 0)
  87. {
  88. $this->position = $position;
  89. }
  90. /**
  91. * Checks whether a given token matches the current lookahead.
  92. *
  93. * @param integer|string $token
  94. * @return boolean
  95. */
  96. public function isNextToken($token)
  97. {
  98. return null !== $this->lookahead && $this->lookahead['type'] === $token;
  99. }
  100. /**
  101. * Checks whether any of the given tokens matches the current lookahead
  102. *
  103. * @param array $tokens
  104. * @return boolean
  105. */
  106. public function isNextTokenAny(array $tokens)
  107. {
  108. return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
  109. }
  110. /**
  111. * Moves to the next token in the input string.
  112. *
  113. * A token is an associative array containing three items:
  114. * - 'value' : the string value of the token in the input string
  115. * - 'type' : the type of the token (identifier, numeric, string, input
  116. * parameter, none)
  117. * - 'position' : the position of the token in the input string
  118. *
  119. * @return array|null the next token; null if there is no more tokens left
  120. */
  121. public function moveNext()
  122. {
  123. $this->peek = 0;
  124. $this->token = $this->lookahead;
  125. $this->lookahead = (isset($this->tokens[$this->position]))
  126. ? $this->tokens[$this->position++] : null;
  127. return $this->lookahead !== null;
  128. }
  129. /**
  130. * Tells the lexer to skip input tokens until it sees a token with the given value.
  131. *
  132. * @param string $type The token type to skip until.
  133. */
  134. public function skipUntil($type)
  135. {
  136. while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
  137. $this->moveNext();
  138. }
  139. }
  140. /**
  141. * Checks if given value is identical to the given token
  142. *
  143. * @param mixed $value
  144. * @param integer $token
  145. * @return boolean
  146. */
  147. public function isA($value, $token)
  148. {
  149. return $this->getType($value) === $token;
  150. }
  151. /**
  152. * Moves the lookahead token forward.
  153. *
  154. * @return array | null The next token or NULL if there are no more tokens ahead.
  155. */
  156. public function peek()
  157. {
  158. if (isset($this->tokens[$this->position + $this->peek])) {
  159. return $this->tokens[$this->position + $this->peek++];
  160. } else {
  161. return null;
  162. }
  163. }
  164. /**
  165. * Peeks at the next token, returns it and immediately resets the peek.
  166. *
  167. * @return array|null The next token or NULL if there are no more tokens ahead.
  168. */
  169. public function glimpse()
  170. {
  171. $peek = $this->peek();
  172. $this->peek = 0;
  173. return $peek;
  174. }
  175. /**
  176. * Scans the input string for tokens.
  177. *
  178. * @param string $input a query string
  179. */
  180. protected function scan($input)
  181. {
  182. static $regex;
  183. if ( ! isset($regex)) {
  184. $regex = '/(' . implode(')|(', $this->getCatchablePatterns()) . ')|'
  185. . implode('|', $this->getNonCatchablePatterns()) . '/i';
  186. }
  187. $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
  188. $matches = preg_split($regex, $input, -1, $flags);
  189. foreach ($matches as $match) {
  190. // Must remain before 'value' assignment since it can change content
  191. $type = $this->getType($match[0]);
  192. $this->tokens[] = array(
  193. 'value' => $match[0],
  194. 'type' => $type,
  195. 'position' => $match[1],
  196. );
  197. }
  198. }
  199. /**
  200. * Gets the literal for a given token.
  201. *
  202. * @param integer $token
  203. * @return string
  204. */
  205. public function getLiteral($token)
  206. {
  207. $className = get_class($this);
  208. $reflClass = new \ReflectionClass($className);
  209. $constants = $reflClass->getConstants();
  210. foreach ($constants as $name => $value) {
  211. if ($value === $token) {
  212. return $className . '::' . $name;
  213. }
  214. }
  215. return $token;
  216. }
  217. /**
  218. * Lexical catchable patterns.
  219. *
  220. * @return array
  221. */
  222. abstract protected function getCatchablePatterns();
  223. /**
  224. * Lexical non-catchable patterns.
  225. *
  226. * @return array
  227. */
  228. abstract protected function getNonCatchablePatterns();
  229. /**
  230. * Retrieve token type. Also processes the token value if necessary.
  231. *
  232. * @param string $value
  233. * @return integer
  234. */
  235. abstract protected function getType(&$value);
  236. }