PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/ExpressionLanguage/Lexer.php

https://gitlab.com/Jlb/jlbdigitalserviceswebsiteproject
PHP | 103 lines | 61 code | 12 blank | 30 comment | 11 complexity | 2ef4e1fa860f588723c23e1276f8c2ac MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage;
  11. /**
  12. * Lexes an expression.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Lexer
  17. {
  18. /**
  19. * Tokenizes an expression.
  20. *
  21. * @param string $expression The expression to tokenize
  22. *
  23. * @return TokenStream A token stream instance
  24. *
  25. * @throws SyntaxError
  26. */
  27. public function tokenize($expression)
  28. {
  29. $expression = str_replace(array("\r", "\n", "\t", "\v", "\f"), ' ', $expression);
  30. $cursor = 0;
  31. $tokens = array();
  32. $brackets = array();
  33. $end = strlen($expression);
  34. while ($cursor < $end) {
  35. if (' ' == $expression[$cursor]) {
  36. ++$cursor;
  37. continue;
  38. }
  39. if (preg_match('/[0-9]+(?:\.[0-9]+)?/A', $expression, $match, null, $cursor)) {
  40. // numbers
  41. $number = (float) $match[0]; // floats
  42. if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) {
  43. $number = (int) $match[0]; // integers lower than the maximum
  44. }
  45. $tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);
  46. $cursor += strlen($match[0]);
  47. } elseif (false !== strpos('([{', $expression[$cursor])) {
  48. // opening bracket
  49. $brackets[] = array($expression[$cursor], $cursor);
  50. $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
  51. ++$cursor;
  52. } elseif (false !== strpos(')]}', $expression[$cursor])) {
  53. // closing bracket
  54. if (empty($brackets)) {
  55. throw new SyntaxError(sprintf('Unexpected "%s"', $expression[$cursor]), $cursor);
  56. }
  57. list($expect, $cur) = array_pop($brackets);
  58. if ($expression[$cursor] != strtr($expect, '([{', ')]}')) {
  59. throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur);
  60. }
  61. $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
  62. ++$cursor;
  63. } elseif (preg_match('/"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As', $expression, $match, null, $cursor)) {
  64. // strings
  65. $tokens[] = new Token(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)), $cursor + 1);
  66. $cursor += strlen($match[0]);
  67. } elseif (preg_match('/not in(?=[\s(])|\!\=\=|not(?=[\s(])|and(?=[\s(])|\=\=\=|\>\=|or(?=[\s(])|\<\=|\*\*|\.\.|in(?=[\s(])|&&|\|\||matches|\=\=|\!\=|\*|~|%|\/|\>|\||\!|\^|&|\+|\<|\-/A', $expression, $match, null, $cursor)) {
  68. // operators
  69. $tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
  70. $cursor += strlen($match[0]);
  71. } elseif (false !== strpos('.,?:', $expression[$cursor])) {
  72. // punctuation
  73. $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
  74. ++$cursor;
  75. } elseif (preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A', $expression, $match, null, $cursor)) {
  76. // names
  77. $tokens[] = new Token(Token::NAME_TYPE, $match[0], $cursor + 1);
  78. $cursor += strlen($match[0]);
  79. } else {
  80. // unlexable
  81. throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor);
  82. }
  83. }
  84. $tokens[] = new Token(Token::EOF_TYPE, null, $cursor + 1);
  85. if (!empty($brackets)) {
  86. list($expect, $cur) = array_pop($brackets);
  87. throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur);
  88. }
  89. return new TokenStream($tokens);
  90. }
  91. }