Version 1
[yaffs-website] / vendor / doctrine / annotations / lib / Doctrine / Common / Annotations / DocLexer.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Annotations;
21
22 use Doctrine\Common\Lexer\AbstractLexer;
23
24 /**
25  * Simple lexer for docblock annotations.
26  *
27  * @author Benjamin Eberlei <kontakt@beberlei.de>
28  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
29  * @author Jonathan Wage <jonwage@gmail.com>
30  * @author Roman Borschel <roman@code-factory.org>
31  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
32  */
33 final class DocLexer extends AbstractLexer
34 {
35     const T_NONE                = 1;
36     const T_INTEGER             = 2;
37     const T_STRING              = 3;
38     const T_FLOAT               = 4;
39
40     // All tokens that are also identifiers should be >= 100
41     const T_IDENTIFIER          = 100;
42     const T_AT                  = 101;
43     const T_CLOSE_CURLY_BRACES  = 102;
44     const T_CLOSE_PARENTHESIS   = 103;
45     const T_COMMA               = 104;
46     const T_EQUALS              = 105;
47     const T_FALSE               = 106;
48     const T_NAMESPACE_SEPARATOR = 107;
49     const T_OPEN_CURLY_BRACES   = 108;
50     const T_OPEN_PARENTHESIS    = 109;
51     const T_TRUE                = 110;
52     const T_NULL                = 111;
53     const T_COLON               = 112;
54
55     /**
56      * @var array
57      */
58     protected $noCase = array(
59         '@'  => self::T_AT,
60         ','  => self::T_COMMA,
61         '('  => self::T_OPEN_PARENTHESIS,
62         ')'  => self::T_CLOSE_PARENTHESIS,
63         '{'  => self::T_OPEN_CURLY_BRACES,
64         '}'  => self::T_CLOSE_CURLY_BRACES,
65         '='  => self::T_EQUALS,
66         ':'  => self::T_COLON,
67         '\\' => self::T_NAMESPACE_SEPARATOR
68     );
69
70     /**
71      * @var array
72      */
73     protected $withCase = array(
74         'true'  => self::T_TRUE,
75         'false' => self::T_FALSE,
76         'null'  => self::T_NULL
77     );
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function getCatchablePatterns()
83     {
84         return array(
85             '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*',
86             '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?',
87             '"(?:""|[^"])*+"',
88         );
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     protected function getNonCatchablePatterns()
95     {
96         return array('\s+', '\*+', '(.)');
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     protected function getType(&$value)
103     {
104         $type = self::T_NONE;
105
106         if ($value[0] === '"') {
107             $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
108
109             return self::T_STRING;
110         }
111
112         if (isset($this->noCase[$value])) {
113             return $this->noCase[$value];
114         }
115
116         if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) {
117             return self::T_IDENTIFIER;
118         }
119
120         $lowerValue = strtolower($value);
121
122         if (isset($this->withCase[$lowerValue])) {
123             return $this->withCase[$lowerValue];
124         }
125
126         // Checking numeric value
127         if (is_numeric($value)) {
128             return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
129                 ? self::T_FLOAT : self::T_INTEGER;
130         }
131
132         return $type;
133     }
134 }