Yaffs site version 1.1
[yaffs-website] / vendor / symfony / expression-language / Lexer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ExpressionLanguage;
13
14 /**
15  * Lexes an expression.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class Lexer
20 {
21     /**
22      * Tokenizes an expression.
23      *
24      * @param string $expression The expression to tokenize
25      *
26      * @return TokenStream A token stream instance
27      *
28      * @throws SyntaxError
29      */
30     public function tokenize($expression)
31     {
32         $expression = str_replace(array("\r", "\n", "\t", "\v", "\f"), ' ', $expression);
33         $cursor = 0;
34         $tokens = array();
35         $brackets = array();
36         $end = strlen($expression);
37
38         while ($cursor < $end) {
39             if (' ' == $expression[$cursor]) {
40                 ++$cursor;
41
42                 continue;
43             }
44
45             if (preg_match('/[0-9]+(?:\.[0-9]+)?/A', $expression, $match, 0, $cursor)) {
46                 // numbers
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
50                 }
51                 $tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);
52                 $cursor += strlen($match[0]);
53             } elseif (false !== strpos('([{', $expression[$cursor])) {
54                 // opening bracket
55                 $brackets[] = array($expression[$cursor], $cursor);
56
57                 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
58                 ++$cursor;
59             } elseif (false !== strpos(')]}', $expression[$cursor])) {
60                 // closing bracket
61                 if (empty($brackets)) {
62                     throw new SyntaxError(sprintf('Unexpected "%s"', $expression[$cursor]), $cursor, $expression);
63                 }
64
65                 list($expect, $cur) = array_pop($brackets);
66                 if ($expression[$cursor] != strtr($expect, '([{', ')]}')) {
67                     throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
68                 }
69
70                 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
71                 ++$cursor;
72             } elseif (preg_match('/"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As', $expression, $match, 0, $cursor)) {
73                 // strings
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)) {
77                 // operators
78                 $tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
79                 $cursor += strlen($match[0]);
80             } elseif (false !== strpos('.,?:', $expression[$cursor])) {
81                 // punctuation
82                 $tokens[] = new Token(Token::PUNCTUATION_TYPE, $expression[$cursor], $cursor + 1);
83                 ++$cursor;
84             } elseif (preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A', $expression, $match, 0, $cursor)) {
85                 // names
86                 $tokens[] = new Token(Token::NAME_TYPE, $match[0], $cursor + 1);
87                 $cursor += strlen($match[0]);
88             } else {
89                 // unlexable
90                 throw new SyntaxError(sprintf('Unexpected character "%s"', $expression[$cursor]), $cursor, $expression);
91             }
92         }
93
94         $tokens[] = new Token(Token::EOF_TYPE, null, $cursor + 1);
95
96         if (!empty($brackets)) {
97             list($expect, $cur) = array_pop($brackets);
98             throw new SyntaxError(sprintf('Unclosed "%s"', $expect), $cur, $expression);
99         }
100
101         return new TokenStream($tokens, $expression);
102     }
103 }