PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/foody.blogtamsudev.vn/vendor/nikic/php-parser/lib/PhpParser/Lexer.php

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