1 | <?php |
||
19 | class Token |
||
20 | { |
||
21 | // Types of tokens (a vague description of a token's purpose). |
||
22 | |||
23 | /** |
||
24 | * This type is used when the token is invalid or its type cannot be |
||
25 | * determined because of the ambiguous context. Further analysis might be |
||
26 | * required to detect its type. |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | const TYPE_NONE = 0; |
||
31 | |||
32 | /** |
||
33 | * SQL specific keywords: SELECT, UPDATE, INSERT, etc. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | const TYPE_KEYWORD = 1; |
||
38 | |||
39 | /** |
||
40 | * Any type of legal operator. |
||
41 | * |
||
42 | * Arithmetic operators: +, -, *, /, etc. |
||
43 | * Logical operators: ===, <>, !==, etc. |
||
44 | * Bitwise operators: &, |, ^, etc. |
||
45 | * Assignment operators: =, +=, -=, etc. |
||
46 | * SQL specific operators: . (e.g. .. WHERE database.table ..), |
||
47 | * * (e.g. SELECT * FROM ..) |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | const TYPE_OPERATOR = 2; |
||
52 | |||
53 | /** |
||
54 | * Spaces, tabs, new lines, etc. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | const TYPE_WHITESPACE = 3; |
||
59 | |||
60 | /** |
||
61 | * Any type of legal comment. |
||
62 | * |
||
63 | * Bash (#), C (/* *\/) or SQL (--) comments: |
||
64 | * |
||
65 | * -- SQL-comment |
||
66 | * |
||
67 | * #Bash-like comment |
||
68 | * |
||
69 | * /*C-like comment*\/ |
||
70 | * |
||
71 | * or: |
||
72 | * |
||
73 | * /*C-like |
||
74 | * comment*\/ |
||
75 | * |
||
76 | * Backslashes were added to respect PHP's comments syntax. |
||
77 | * |
||
78 | * @var int |
||
79 | */ |
||
80 | const TYPE_COMMENT = 4; |
||
81 | |||
82 | /** |
||
83 | * Boolean values: true or false. |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | const TYPE_BOOL = 5; |
||
88 | |||
89 | /** |
||
90 | * Numbers: 4, 0x8, 15.16, 23e42, etc. |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | const TYPE_NUMBER = 6; |
||
95 | |||
96 | /** |
||
97 | * Literal strings: 'string', "test". |
||
98 | * Some of these strings are actually symbols. |
||
99 | * |
||
100 | * @var int |
||
101 | */ |
||
102 | const TYPE_STRING = 7; |
||
103 | |||
104 | /** |
||
105 | * Database, table names, variables, etc. |
||
106 | * For example: ```SELECT `foo`, `bar` FROM `database`.`table`;```. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | const TYPE_SYMBOL = 8; |
||
111 | |||
112 | /** |
||
113 | * Delimits an unknown string. |
||
114 | * For example: ```SELECT * FROM test;```, `test` is a delimiter. |
||
115 | * |
||
116 | * @var int |
||
117 | */ |
||
118 | const TYPE_DELIMITER = 9; |
||
119 | |||
120 | /** |
||
121 | * Labels in LOOP statement, ITERATE statement etc. |
||
122 | * For example (only for begin label): |
||
123 | * begin_label: BEGIN [statement_list] END [end_label] |
||
124 | * begin_label: LOOP [statement_list] END LOOP [end_label] |
||
125 | * begin_label: REPEAT [statement_list] ... END REPEAT [end_label] |
||
126 | * begin_label: WHILE ... DO [statement_list] END WHILE [end_label]. |
||
127 | * |
||
128 | * @var int |
||
129 | */ |
||
130 | const TYPE_LABEL = 10; |
||
131 | |||
132 | // Flags that describe the tokens in more detail. |
||
133 | // All keywords must have flag 1 so `Context::isKeyword` method doesn't |
||
134 | // require strict comparison. |
||
135 | const FLAG_KEYWORD_RESERVED = 2; |
||
136 | const FLAG_KEYWORD_COMPOSED = 4; |
||
137 | const FLAG_KEYWORD_DATA_TYPE = 8; |
||
138 | const FLAG_KEYWORD_KEY = 16; |
||
139 | const FLAG_KEYWORD_FUNCTION = 32; |
||
140 | |||
141 | // Numbers related flags. |
||
142 | const FLAG_NUMBER_HEX = 1; |
||
143 | const FLAG_NUMBER_FLOAT = 2; |
||
144 | const FLAG_NUMBER_APPROXIMATE = 4; |
||
145 | const FLAG_NUMBER_NEGATIVE = 8; |
||
146 | const FLAG_NUMBER_BINARY = 16; |
||
147 | |||
148 | // Strings related flags. |
||
149 | const FLAG_STRING_SINGLE_QUOTES = 1; |
||
150 | const FLAG_STRING_DOUBLE_QUOTES = 2; |
||
151 | |||
152 | // Comments related flags. |
||
153 | const FLAG_COMMENT_BASH = 1; |
||
154 | const FLAG_COMMENT_C = 2; |
||
155 | const FLAG_COMMENT_SQL = 4; |
||
156 | const FLAG_COMMENT_MYSQL_CMD = 8; |
||
157 | |||
158 | // Operators related flags. |
||
159 | const FLAG_OPERATOR_ARITHMETIC = 1; |
||
160 | const FLAG_OPERATOR_LOGICAL = 2; |
||
161 | const FLAG_OPERATOR_BITWISE = 4; |
||
162 | const FLAG_OPERATOR_ASSIGNMENT = 8; |
||
163 | const FLAG_OPERATOR_SQL = 16; |
||
164 | |||
165 | // Symbols related flags. |
||
166 | const FLAG_SYMBOL_VARIABLE = 1; |
||
167 | const FLAG_SYMBOL_BACKTICK = 2; |
||
168 | const FLAG_SYMBOL_USER = 4; |
||
169 | const FLAG_SYMBOL_SYSTEM = 8; |
||
170 | const FLAG_SYMBOL_PARAMETER = 16; |
||
171 | |||
172 | /** |
||
173 | * The token it its raw string representation. |
||
174 | * |
||
175 | * @var string |
||
176 | */ |
||
177 | public $token; |
||
178 | |||
179 | /** |
||
180 | * The value this token contains (i.e. token after some evaluation). |
||
181 | * |
||
182 | * @var mixed |
||
183 | */ |
||
184 | public $value; |
||
185 | |||
186 | /** |
||
187 | * The keyword value this token contains, always uppercase. |
||
188 | * |
||
189 | * @var mixed |
||
190 | */ |
||
191 | public $keyword; |
||
192 | |||
193 | /** |
||
194 | * The type of this token. |
||
195 | * |
||
196 | * @var int |
||
197 | */ |
||
198 | public $type; |
||
199 | |||
200 | /** |
||
201 | * The flags of this token. |
||
202 | * |
||
203 | * @var int |
||
204 | */ |
||
205 | public $flags; |
||
206 | |||
207 | /** |
||
208 | * The position in the initial string where this token started. |
||
209 | * |
||
210 | * @var int |
||
211 | */ |
||
212 | public $position; |
||
213 | |||
214 | /** |
||
215 | * Constructor. |
||
216 | * |
||
217 | * @param string $token the value of the token |
||
218 | * @param int $type the type of the token |
||
219 | * @param int $flags the flags of the token |
||
220 | */ |
||
221 | 388 | public function __construct($token, $type = 0, $flags = 0) |
|
229 | |||
230 | /** |
||
231 | * Does little processing to the token to extract a value. |
||
232 | * |
||
233 | * If no processing can be done it will return the initial string. |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 388 | public function extract() |
|
321 | |||
322 | /** |
||
323 | * Converts the token into an inline token by replacing tabs and new lines. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 1 | public function getInlineToken() |
|
335 | } |
||
336 |