4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage;
15 * Lexes an expression.
17 * @author Fabien Potencier <fabien@symfony.com>
22 * Tokenizes an expression.
24 * @param string $expression The expression to tokenize
26 * @return TokenStream A token stream instance
30 public function tokenize($expression)
32 $expression = str_replace(array("\r", "\n", "\t", "\v", "\f"), ' ', $expression);
36 $end = strlen($expression);
38 while ($cursor < $end) {
39 if (' ' == $expression[$cursor]) {
45 if (preg_match('/[0-9]+(?:\.[0-9]+)?/A', $expression, $match, 0, $cursor)) {
47 $number = (float) $match[0]; // floats
48 if (preg_match('/^[0-9]+$/', $match[0]) && $number <= PHP_INT_MAX) {
49 $number = (int) $match[0]; // integers lower than the maximum
51 $tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);
52 $cursor += strlen($match[0]);
53 } elseif (false !== strpos('([{', $expression[$cursor])) {
55 $brackets[] = array($expression[$cursor], $cursor);
57 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
59 } elseif (false !== strpos(')]}', $expression[$cursor])) {
61 if (empty($brackets)) {
62 throw new SyntaxError(sprintf('Unexpected "%s"', $expression[$cursor]), $cursor, $expression);
65 list($expect, $cur) = array_pop($brackets);
66 if ($expression[$cursor] != strtr($expect, '([{', ')]}')) {
67 throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
70 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
72 } elseif (preg_match('/"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As', $expression, $match, 0, $cursor)) {
74 $tokens[] = new Token(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)), $cursor + 1);
75 $cursor += strlen($match[0]);
76 } elseif (preg_match('/not in(?=[\s(])|\!\=\=|not(?=[\s(])|and(?=[\s(])|\=\=\=|\>\=|or(?=[\s(])|\<\=|\*\*|\.\.|in(?=[\s(])|&&|\|\||matches|\=\=|\!\=|\*|~|%|\/|\>|\||\!|\^|&|\+|\<|\-/A', $expression, $match, 0, $cursor)) {
78 $tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
79 $cursor += strlen($match[0]);
80 } elseif (false !== strpos('.,?:', $expression[$cursor])) {
82 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
84 } elseif (preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A', $expression, $match, 0, $cursor)) {
86 $tokens[] = new Token(Token::NAME_TYPE, $match[0], $cursor + 1);
87 $cursor += strlen($match[0]);
90 throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor, $expression);
94 $tokens[] = new Token(Token::EOF_TYPE, null, $cursor + 1);
96 if (!empty($brackets)) {
97 list($expect, $cur) = array_pop($brackets);
98 throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
101 return new TokenStream($tokens, $expression);