PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nikic/php-parser/lib/PhpParser/Lexer.php

https://gitlab.com/Laolballs/evotting
PHP | 285 lines | 163 code | 37 blank | 85 comment | 29 complexity | 0a07a4ff03b03b9f512657a68a0f7b4b MD5 | raw file
  1. <?php
  2. namespace PhpParser;
  3. class Lexer
  4. {
  5. protected $code;
  6. protected $tokens;
  7. protected $pos;
  8. protected $line;
  9. protected $filePos;
  10. protected $tokenMap;
  11. protected $dropTokens;
  12. protected $usedAttributes;
  13. /**
  14. * Creates a Lexer.
  15. *
  16. * @param array $options Options array. Currently only the 'usedAttributes' option is supported,
  17. * which is an array of attributes to add to the AST nodes. Possible attributes
  18. * are: 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos',
  19. * 'startFilePos', 'endFilePos'. The option defaults to the first three.
  20. * For more info see getNextToken() docs.
  21. */
  22. public function __construct(array $options = array()) {
  23. // map from internal tokens to PhpParser tokens
  24. $this->tokenMap = $this->createTokenMap();
  25. // map of tokens to drop while lexing (the map is only used for isset lookup,
  26. // that's why the value is simply set to 1; the value is never actually used.)
  27. $this->dropTokens = array_fill_keys(array(T_WHITESPACE, T_OPEN_TAG), 1);
  28. // the usedAttributes member is a map of the used attribute names to a dummy
  29. // value (here "true")
  30. $options += array(
  31. 'usedAttributes' => array('comments', 'startLine', 'endLine'),
  32. );
  33. $this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
  34. }
  35. /**
  36. * Initializes the lexer for lexing the provided source code.
  37. *
  38. * @param string $code The source code to lex
  39. *
  40. * @throws Error on lexing errors (unterminated comment or unexpected character)
  41. */
  42. public function startLexing($code) {
  43. $scream = ini_set('xdebug.scream', 0);
  44. $this->resetErrors();
  45. $this->tokens = @token_get_all($code);
  46. $this->handleErrors();
  47. ini_set('xdebug.scream', $scream);
  48. $this->code = $code; // keep the code around for __halt_compiler() handling
  49. $this->pos = -1;
  50. $this->line = 1;
  51. $this->filePos = 0;
  52. }
  53. protected function resetErrors() {
  54. // set error_get_last() to defined state by forcing an undefined variable error
  55. set_error_handler(function() { return false; }, 0);
  56. @$undefinedVariable;
  57. restore_error_handler();
  58. }
  59. protected function handleErrors() {
  60. $error = error_get_last();
  61. if (preg_match(
  62. '~^Unterminated comment starting line ([0-9]+)$~',
  63. $error['message'], $matches
  64. )) {
  65. throw new Error('Unterminated comment', $matches[1]);
  66. }
  67. if (preg_match(
  68. '~^Unexpected character in input: \'(.)\' \(ASCII=([0-9]+)\)~s',
  69. $error['message'], $matches
  70. )) {
  71. throw new Error(sprintf(
  72. 'Unexpected character "%s" (ASCII %d)',
  73. $matches[1], $matches[2]
  74. ));
  75. }
  76. // PHP cuts error message after null byte, so need special case
  77. if (preg_match('~^Unexpected character in input: \'$~', $error['message'])) {
  78. throw new Error('Unexpected null byte');
  79. }
  80. }
  81. /**
  82. * Fetches the next token.
  83. *
  84. * The available attributes are determined by the 'usedAttributes' option, which can
  85. * be specified in the constructor. The following attributes are supported:
  86. *
  87. * * 'comments' => Array of PhpParser\Comment or PhpParser\Comment\Doc instances,
  88. * representing all comments that occurred between the previous
  89. * non-discarded token and the current one.
  90. * * 'startLine' => Line in which the node starts.
  91. * * 'endLine' => Line in which the node ends.
  92. * * 'startTokenPos' => Offset into the token array of the first token in the node.
  93. * * 'endTokenPos' => Offset into the token array of the last token in the node.
  94. * * 'startFilePos' => Offset into the code string of the first character that is part of the node.
  95. * * 'endFilePos' => Offset into the code string of the last character that is part of the node
  96. *
  97. * @param mixed $value Variable to store token content in
  98. * @param mixed $startAttributes Variable to store start attributes in
  99. * @param mixed $endAttributes Variable to store end attributes in
  100. *
  101. * @return int Token id
  102. */
  103. public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
  104. $startAttributes = array();
  105. $endAttributes = array();
  106. while (isset($this->tokens[++$this->pos])) {
  107. $token = $this->tokens[$this->pos];
  108. if (isset($this->usedAttributes['startTokenPos'])) {
  109. $startAttributes['startTokenPos'] = $this->pos;
  110. }
  111. if (isset($this->usedAttributes['startFilePos'])) {
  112. $startAttributes['startFilePos'] = $this->filePos;
  113. }
  114. if (is_string($token)) {
  115. // bug in token_get_all
  116. if ('b"' === $token) {
  117. $value = 'b"';
  118. $this->filePos += 2;
  119. $id = ord('"');
  120. } else {
  121. $value = $token;
  122. $this->filePos += 1;
  123. $id = ord($token);
  124. }
  125. if (isset($this->usedAttributes['startLine'])) {
  126. $startAttributes['startLine'] = $this->line;
  127. }
  128. if (isset($this->usedAttributes['endLine'])) {
  129. $endAttributes['endLine'] = $this->line;
  130. }
  131. if (isset($this->usedAttributes['endTokenPos'])) {
  132. $endAttributes['endTokenPos'] = $this->pos;
  133. }
  134. if (isset($this->usedAttributes['endFilePos'])) {
  135. $endAttributes['endFilePos'] = $this->filePos - 1;
  136. }
  137. return $id;
  138. } else {
  139. $this->line += substr_count($token[1], "\n");
  140. $this->filePos += strlen($token[1]);
  141. if (T_COMMENT === $token[0]) {
  142. if (isset($this->usedAttributes['comments'])) {
  143. $startAttributes['comments'][] = new Comment($token[1], $token[2]);
  144. }
  145. } elseif (T_DOC_COMMENT === $token[0]) {
  146. if (isset($this->usedAttributes['comments'])) {
  147. $startAttributes['comments'][] = new Comment\Doc($token[1], $token[2]);
  148. }
  149. } elseif (!isset($this->dropTokens[$token[0]])) {
  150. $value = $token[1];
  151. if (isset($this->usedAttributes['startLine'])) {
  152. $startAttributes['startLine'] = $token[2];
  153. }
  154. if (isset($this->usedAttributes['endLine'])) {
  155. $endAttributes['endLine'] = $this->line;
  156. }
  157. if (isset($this->usedAttributes['endTokenPos'])) {
  158. $endAttributes['endTokenPos'] = $this->pos;
  159. }
  160. if (isset($this->usedAttributes['endFilePos'])) {
  161. $endAttributes['endFilePos'] = $this->filePos - 1;
  162. }
  163. return $this->tokenMap[$token[0]];
  164. }
  165. }
  166. }
  167. $startAttributes['startLine'] = $this->line;
  168. // 0 is the EOF token
  169. return 0;
  170. }
  171. /**
  172. * Returns the token array for current code.
  173. *
  174. * The token array is in the same format as provided by the
  175. * token_get_all() function and does not discard tokens (i.e.
  176. * whitespace and comments are included). The token position
  177. * attributes are against this token array.
  178. *
  179. * @return array Array of tokens in token_get_all() format
  180. */
  181. public function getTokens() {
  182. return $this->tokens;
  183. }
  184. /**
  185. * Handles __halt_compiler() by returning the text after it.
  186. *
  187. * @return string Remaining text
  188. */
  189. public function handleHaltCompiler() {
  190. // get the length of the text before the T_HALT_COMPILER token
  191. $textBefore = '';
  192. for ($i = 0; $i <= $this->pos; ++$i) {
  193. if (is_string($this->tokens[$i])) {
  194. $textBefore .= $this->tokens[$i];
  195. } else {
  196. $textBefore .= $this->tokens[$i][1];
  197. }
  198. }
  199. // text after T_HALT_COMPILER, still including ();
  200. $textAfter = substr($this->code, strlen($textBefore));
  201. // ensure that it is followed by ();
  202. // this simplifies the situation, by not allowing any comments
  203. // in between of the tokens.
  204. if (!preg_match('~\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) {
  205. throw new Error('__HALT_COMPILER must be followed by "();"');
  206. }
  207. // prevent the lexer from returning any further tokens
  208. $this->pos = count($this->tokens);
  209. // return with (); removed
  210. return (string) substr($textAfter, strlen($matches[0])); // (string) converts false to ''
  211. }
  212. /**
  213. * Creates the token map.
  214. *
  215. * The token map maps the PHP internal token identifiers
  216. * to the identifiers used by the Parser. Additionally it
  217. * maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'.
  218. *
  219. * @return array The token map
  220. */
  221. protected function createTokenMap() {
  222. $tokenMap = array();
  223. // 256 is the minimum possible token number, as everything below
  224. // it is an ASCII value
  225. for ($i = 256; $i < 1000; ++$i) {
  226. // T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
  227. if (T_DOUBLE_COLON === $i) {
  228. $tokenMap[$i] = Parser::T_PAAMAYIM_NEKUDOTAYIM;
  229. // T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
  230. } elseif(T_OPEN_TAG_WITH_ECHO === $i) {
  231. $tokenMap[$i] = Parser::T_ECHO;
  232. // T_CLOSE_TAG is equivalent to ';'
  233. } elseif(T_CLOSE_TAG === $i) {
  234. $tokenMap[$i] = ord(';');
  235. // and the others can be mapped directly
  236. } elseif ('UNKNOWN' !== ($name = token_name($i))
  237. && defined($name = 'PhpParser\Parser::' . $name)
  238. ) {
  239. $tokenMap[$i] = constant($name);
  240. }
  241. }
  242. // HHVM uses a special token for numbers that overflow to double
  243. if (defined('T_ONUMBER')) {
  244. $tokenMap[T_ONUMBER] = Parser::T_DNUMBER;
  245. }
  246. return $tokenMap;
  247. }
  248. }