e7fb84ed972b09c02299fa1e197cdab50b84142a
[awesomized/libmemcached] / libmemcached / options / parser.yy
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Scanner and Parser
4 *
5 * Copyright (C) 2011 DataDifferental, http://datadifferential.com
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 %error-verbose
22 %debug
23 %defines
24 %expect 0
25 %output "libmemcached/options/parser.cc"
26 %defines "libmemcached/options/parser.h"
27 %lex-param { yyscan_t *scanner }
28 %name-prefix="config_"
29 %parse-param { Context *context }
30 %parse-param { yyscan_t *scanner }
31 %pure-parser
32 %require "2.2"
33 %start begin
34 %verbose
35
36 %{
37
38 #include <config.h>
39
40 #include <stdint.h>
41
42 #include <libmemcached/common.h>
43 #include <libmemcached/options/context.h>
44 #include <libmemcached/options/symbol.h>
45 #include <libmemcached/options/scanner.h>
46
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
48
49 int conf_lex(YYSTYPE* lvalp, void* scanner);
50
51 #define parser_abort(A, B) do { (A)->abort((B)); YYABORT; } while (0)
52
53 inline void config_error(Context *context, yyscan_t *scanner, const char *error)
54 {
55 if (not context->end())
56 context->abort(error);
57 }
58
59 %}
60
61 %token COMMENT
62 %token END
63 %token ERROR
64 %token RESET
65 %token PARSER_DEBUG
66 %token INCLUDE
67 %token CONFIGURE_FILE
68 %token EMPTY_LINE
69 %token SERVER
70 %token SERVERS
71 %token SERVERS_OPTION
72 %token UNKNOWN_OPTION
73 %token UNKNOWN
74
75 /* All behavior options */
76 %token BINARY_PROTOCOL
77 %token BUFFER_REQUESTS
78 %token CONNECT_TIMEOUT
79 %token DISTRIBUTION
80 %token HASH
81 %token HASH_WITH_NAMESPACE
82 %token IO_BYTES_WATERMARK
83 %token IO_KEY_PREFETCH
84 %token IO_MSG_WATERMARK
85 %token KETAMA_HASH
86 %token KETAMA_WEIGHTED
87 %token NOREPLY
88 %token NUMBER_OF_REPLICAS
89 %token POLL_TIMEOUT
90 %token RANDOMIZE_REPLICA_READ
91 %token RCV_TIMEOUT
92 %token REMOVE_FAILED_SERVERS
93 %token RETRY_TIMEOUT
94 %token SND_TIMEOUT
95 %token SOCKET_RECV_SIZE
96 %token SOCKET_SEND_SIZE
97 %token SORT_HOSTS
98 %token SUPPORT_CAS
99 %token USER_DATA
100 %token USE_UDP
101 %token VERIFY_KEY
102 %token _TCP_KEEPALIVE
103 %token _TCP_KEEPIDLE
104 %token _TCP_NODELAY
105
106 /* Callbacks */
107 %token NAMESPACE
108
109 /* Pool */
110 %token POOL_MIN
111 %token POOL_MAX
112
113 /* Hash types */
114 %token MD5
115 %token CRC
116 %token FNV1_64
117 %token FNV1A_64
118 %token FNV1_32
119 %token FNV1A_32
120 %token HSIEH
121 %token MURMUR
122 %token JENKINS
123
124 /* Distributions */
125 %token CONSISTENT
126 %token MODULA
127 %token RANDOM
128
129 /* Boolean values */
130 %token <boolean> TRUE
131 %token <boolean> FALSE
132
133 %nonassoc ','
134 %nonassoc '='
135
136 %token <number> FLOAT
137 %token <number> NUMBER
138 %token PORT
139 %token WEIGHT_START
140 %token <server> IPADDRESS
141 %token <server> HOSTNAME
142 %token <string> STRING
143 %token <string> QUOTED_STRING
144 %token <string> FILE_PATH
145
146 %type <string> string
147 %type <distribution> distribution
148 %type <hash> hash
149 %type <behavior> behavior_boolean
150 %type <behavior> behavior_number
151
152 %%
153
154 begin:
155 statement
156 | begin ' ' statement
157 ;
158
159 statement:
160 expression
161 { }
162 | COMMENT
163 { }
164 | EMPTY_LINE
165 { }
166 | END
167 {
168 context->set_end();
169 YYACCEPT;
170 }
171 | ERROR
172 {
173 context->rc= MEMCACHED_PARSE_USER_ERROR;
174 parser_abort(context, NULL);
175 }
176 | RESET
177 {
178 memcached_reset(context->memc);
179 }
180 | PARSER_DEBUG
181 {
182 yydebug= 1;
183 }
184 | INCLUDE ' ' string
185 {
186 if ((context->rc= memcached_parse_configure_file(context->memc, $3.c_str, $3.size)) != MEMCACHED_SUCCESS)
187 {
188 parser_abort(context, NULL);
189 }
190 }
191 ;
192
193
194 expression:
195 SERVER HOSTNAME optional_port optional_weight
196 {
197 if ((context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $2.port, $2.weight)) != MEMCACHED_SUCCESS)
198 {
199 parser_abort(context, NULL);
200 }
201 context->unset_server();
202 }
203 | SERVER IPADDRESS optional_port optional_weight
204 {
205 if ((context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $2.port, $2.weight)) != MEMCACHED_SUCCESS)
206 {
207 parser_abort(context, NULL);
208 }
209 context->unset_server();
210 }
211 | CONFIGURE_FILE string
212 {
213 memcached_set_configuration_file(context->memc, $2.c_str, $2.size);
214 }
215 | POOL_MIN NUMBER
216 {
217 context->memc->configure.initial_pool_size= $2;
218 }
219 | POOL_MAX NUMBER
220 {
221 context->memc->configure.max_pool_size= $2;
222 }
223 | behaviors
224 ;
225
226 behaviors:
227 NAMESPACE string
228 {
229 if ((context->rc= memcached_set_prefix_key(context->memc, $2.c_str, $2.size)) != MEMCACHED_SUCCESS)
230 {
231 parser_abort(context, NULL);;
232 }
233 }
234 | DISTRIBUTION distribution
235 {
236 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
237 {
238 parser_abort(context, NULL);;
239 }
240 }
241 | DISTRIBUTION distribution ',' hash
242 {
243 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
244 {
245 parser_abort(context, NULL);;
246 }
247 if ((context->rc= memcached_behavior_set_distribution_hash(context->memc, $4)) != MEMCACHED_SUCCESS)
248 {
249 parser_abort(context, NULL);;
250 }
251 }
252 | HASH hash
253 {
254 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $2)) != MEMCACHED_SUCCESS)
255 {
256 parser_abort(context, NULL);;
257 }
258 }
259 | behavior_number NUMBER
260 {
261 if ((context->rc= memcached_behavior_set(context->memc, $1, $2)) != MEMCACHED_SUCCESS)
262 {
263 parser_abort(context, NULL);;
264 }
265 }
266 | behavior_boolean
267 {
268 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
269 {
270 parser_abort(context, NULL);;
271 }
272 }
273 | USER_DATA
274 {
275 }
276 ;
277
278 behavior_number:
279 REMOVE_FAILED_SERVERS
280 {
281 $$= MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS;
282 }
283 | CONNECT_TIMEOUT
284 {
285 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
286 }
287 | IO_MSG_WATERMARK
288 {
289 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
290 }
291 | IO_BYTES_WATERMARK
292 {
293 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
294 }
295 | IO_KEY_PREFETCH
296 {
297 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
298 }
299 | NUMBER_OF_REPLICAS
300 {
301 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
302 }
303 | POLL_TIMEOUT
304 {
305 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
306 }
307 | RCV_TIMEOUT
308 {
309 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
310 }
311 | RETRY_TIMEOUT
312 {
313 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
314 }
315 | SND_TIMEOUT
316 {
317 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
318 }
319 | SOCKET_RECV_SIZE
320 {
321 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
322 }
323 | SOCKET_SEND_SIZE
324 {
325 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
326 }
327 ;
328
329 behavior_boolean:
330 BINARY_PROTOCOL
331 {
332 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
333 }
334 | BUFFER_REQUESTS
335 {
336 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
337 }
338 | HASH_WITH_NAMESPACE
339 {
340 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
341 }
342 | NOREPLY
343 {
344 $$= MEMCACHED_BEHAVIOR_NOREPLY;
345 }
346 | RANDOMIZE_REPLICA_READ
347 {
348 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
349 }
350 | SORT_HOSTS
351 {
352 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
353 }
354 | SUPPORT_CAS
355 {
356 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
357 }
358 | _TCP_NODELAY
359 {
360 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
361 }
362 | _TCP_KEEPALIVE
363 {
364 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
365 }
366 | _TCP_KEEPIDLE
367 {
368 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
369 }
370 | USE_UDP
371 {
372 $$= MEMCACHED_BEHAVIOR_USE_UDP;
373 }
374 | VERIFY_KEY
375 {
376 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
377 }
378
379
380 optional_port:
381 { }
382 | PORT
383 { };
384 ;
385
386 optional_weight:
387 { }
388 | WEIGHT_START
389 { }
390 ;
391
392 hash:
393 MD5
394 {
395 $$= MEMCACHED_HASH_MD5;
396 }
397 | CRC
398 {
399 $$= MEMCACHED_HASH_CRC;
400 }
401 | FNV1_64
402 {
403 $$= MEMCACHED_HASH_FNV1_64;
404 }
405 | FNV1A_64
406 {
407 $$= MEMCACHED_HASH_FNV1A_64;
408 }
409 | FNV1_32
410 {
411 $$= MEMCACHED_HASH_FNV1_32;
412 }
413 | FNV1A_32
414 {
415 $$= MEMCACHED_HASH_FNV1A_32;
416 }
417 | HSIEH
418 {
419 $$= MEMCACHED_HASH_HSIEH;
420 }
421 | MURMUR
422 {
423 $$= MEMCACHED_HASH_MURMUR;
424 }
425 | JENKINS
426 {
427 $$= MEMCACHED_HASH_JENKINS;
428 }
429 ;
430
431 string:
432 STRING
433 {
434 $$= $1;
435 }
436 | QUOTED_STRING
437 {
438 $$.c_str= $1.c_str +1; // +1 to move use passed the initial quote
439 $$.size= $1.size -2; // -2 removes the begin and end quote
440 }
441 ;
442
443 distribution:
444 CONSISTENT
445 {
446 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
447 }
448 | MODULA
449 {
450 $$= MEMCACHED_DISTRIBUTION_MODULA;
451 }
452 | RANDOM
453 {
454 $$= MEMCACHED_DISTRIBUTION_RANDOM;
455 }
456 ;
457
458 %%
459
460 void Context::start()
461 {
462 config_parse(this, (void **)scanner);
463 }
464