AbstractLexer.php 7.9 KB

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