WiP cmake-ify
[m6w6/libmemcached] / libmemcached / csl / parser.yy
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 %{
38
39 #include <libmemcached/csl/common.h>
40
41 class Context;
42
43 %}
44
45 %define parse.error verbose
46 %define api.prefix {config_}
47 %define api.pure
48 %debug
49 %defines
50 %expect 0
51 %lex-param { yyscan_t *scanner }
52 %parse-param { class Context *context }
53 %parse-param { yyscan_t *scanner }
54 %require "2.5"
55 %start begin
56 %verbose
57
58 %{
59
60 #include <libmemcached/options.hpp>
61
62 #include <libmemcached/csl/context.h>
63 #include <libmemcached/csl/symbol.h>
64 #include <libmemcached/csl/scanner.h>
65
66 #ifndef __INTEL_COMPILER
67 # pragma GCC diagnostic ignored "-Wold-style-cast"
68 #endif
69
70 #ifndef __INTEL_COMPILER
71 # ifndef __clang__
72 # pragma GCC diagnostic ignored "-Wlogical-op"
73 # pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
74 # endif
75 #endif
76
77 int conf_lex(YYSTYPE* lvalp, void* scanner);
78
79 #define select_yychar(__context) yychar == UNKNOWN ? ( (__context)->previous_token == END ? UNKNOWN : (__context)->previous_token ) : yychar
80
81 #define stryytname(__yytokentype) ((__yytokentype) < YYNTOKENS ) ? yytname[(__yytokentype)] : ""
82
83 #define parser_abort(__context, __error_message) do { (__context)->abort((__error_message), config_tokentype(select_yychar(__context)), stryytname(YYTRANSLATE(select_yychar(__context)))); YYABORT; } while (0)
84
85 // This is bison calling error.
86 inline void __config_error(Context *context, yyscan_t *scanner, const char *error, int last_token, const char *last_token_str)
87 {
88 if (not context->end())
89 {
90 context->error(error, config_tokentype(last_token), last_token_str);
91 }
92 else
93 {
94 context->error(error, config_tokentype(last_token), last_token_str);
95 }
96 }
97
98 #define config_error(__context, __scanner, __error_msg) do { __config_error((__context), (__scanner), (__error_msg), select_yychar(__context), stryytname(YYTRANSLATE(select_yychar(__context)))); } while (0)
99
100
101 %}
102
103 %token COMMENT
104 %token END
105 %token CSL_ERROR
106 %token RESET
107 %token PARSER_DEBUG
108 %token INCLUDE
109 %token CONFIGURE_FILE
110 %token EMPTY_LINE
111 %token SERVER
112 %token CSL_SOCKET
113 %token SERVERS
114 %token SERVERS_OPTION
115 %token UNKNOWN_OPTION
116 %token UNKNOWN
117
118 /* All behavior options */
119 %token BINARY_PROTOCOL
120 %token BUFFER_REQUESTS
121 %token CONNECT_TIMEOUT
122 %token DISTRIBUTION
123 %token HASH
124 %token HASH_WITH_NAMESPACE
125 %token IO_BYTES_WATERMARK
126 %token IO_KEY_PREFETCH
127 %token IO_MSG_WATERMARK
128 %token KETAMA_HASH
129 %token KETAMA_WEIGHTED
130 %token NOREPLY
131 %token NUMBER_OF_REPLICAS
132 %token POLL_TIMEOUT
133 %token RANDOMIZE_REPLICA_READ
134 %token RCV_TIMEOUT
135 %token REMOVE_FAILED_SERVERS
136 %token RETRY_TIMEOUT
137 %token SND_TIMEOUT
138 %token SOCKET_RECV_SIZE
139 %token SOCKET_SEND_SIZE
140 %token SORT_HOSTS
141 %token SUPPORT_CAS
142 %token USER_DATA
143 %token USE_UDP
144 %token VERIFY_KEY
145 %token _TCP_KEEPALIVE
146 %token _TCP_KEEPIDLE
147 %token _TCP_NODELAY
148 %token FETCH_VERSION
149
150 /* Callbacks */
151 %token NAMESPACE
152
153 /* Pool */
154 %token POOL_MIN
155 %token POOL_MAX
156
157 /* Hash types */
158 %token MD5
159 %token CRC
160 %token FNV1_64
161 %token FNV1A_64
162 %token FNV1_32
163 %token FNV1A_32
164 %token HSIEH
165 %token MURMUR
166 %token JENKINS
167
168 /* Distributions */
169 %token CONSISTENT
170 %token MODULA
171 %token RANDOM
172
173 /* Boolean values */
174 %token <boolean> CSL_TRUE
175 %token <boolean> CSL_FALSE
176
177 %nonassoc ','
178 %nonassoc '='
179
180 %token <number> CSL_FLOAT
181 %token <number> NUMBER
182 %token <number> PORT
183 %token <number> WEIGHT_START
184 %token <server> IPADDRESS
185 %token <server> HOSTNAME
186 %token <string> STRING
187 %token <string> QUOTED_STRING
188 %token <string> FILE_PATH
189
190 %type <behavior> behavior_boolean
191 %type <behavior> behavior_number
192 %type <distribution> distribution
193 %type <hash> hash
194 %type <number> optional_port
195 %type <number> optional_weight
196 %type <string> string
197
198 %%
199
200 begin:
201 statement
202 | begin ' ' statement
203 ;
204
205 statement:
206 expression
207 { }
208 | COMMENT
209 { }
210 | EMPTY_LINE
211 { }
212 | END
213 {
214 context->set_end();
215 YYACCEPT;
216 }
217 | CSL_ERROR
218 {
219 context->rc= MEMCACHED_PARSE_USER_ERROR;
220 parser_abort(context, "ERROR called directly");
221 }
222 | RESET
223 {
224 memcached_reset(context->memc);
225 }
226 | PARSER_DEBUG
227 {
228 yydebug= 1;
229 }
230 | INCLUDE ' ' string
231 {
232 if ((context->rc= memcached_parse_configure_file(*context->memc, $3.c_str, $3.size)) != MEMCACHED_SUCCESS)
233 {
234 parser_abort(context, "Failed to parse configuration file");
235 }
236 }
237 ;
238
239
240 expression:
241 SERVER HOSTNAME optional_port optional_weight
242 {
243 if (memcached_failed(context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $3, uint32_t($4))))
244 {
245 char buffer[1024];
246 snprintf(buffer, sizeof(buffer), "Failed to add server: %s:%u", $2.c_str, uint32_t($3));
247 parser_abort(context, buffer);
248 }
249 context->unset_server();
250 }
251 | SERVER IPADDRESS optional_port optional_weight
252 {
253 if (memcached_failed(context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $3, uint32_t($4))))
254 {
255 char buffer[1024];
256 snprintf(buffer, sizeof(buffer), "Failed to add server: %s:%u", $2.c_str, uint32_t($3));
257 parser_abort(context, buffer);
258 }
259 context->unset_server();
260 }
261 | CSL_SOCKET string optional_weight
262 {
263 if (memcached_failed(context->rc= memcached_server_add_unix_socket_with_weight(context->memc, $2.c_str, uint32_t($3))))
264 {
265 char buffer[1024];
266 snprintf(buffer, sizeof(buffer), "Failed to add socket: %s", $2.c_str);
267 parser_abort(context, buffer);
268 }
269 }
270 | CONFIGURE_FILE string
271 {
272 memcached_set_configuration_file(context->memc, $2.c_str, $2.size);
273 }
274 | POOL_MIN NUMBER
275 {
276 context->memc->configure.initial_pool_size= uint32_t($2);
277 }
278 | POOL_MAX NUMBER
279 {
280 context->memc->configure.max_pool_size= uint32_t($2);
281 }
282 | behaviors
283 ;
284
285 behaviors:
286 NAMESPACE string
287 {
288 if (memcached_callback_get(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL))
289 {
290 parser_abort(context, "--NAMESPACE can only be called once");
291 }
292
293 if ((context->rc= memcached_set_namespace(*context->memc, $2.c_str, $2.size)) != MEMCACHED_SUCCESS)
294 {
295 parser_abort(context, memcached_last_error_message(context->memc));
296 }
297 }
298 | FETCH_VERSION
299 {
300 memcached_flag(*context->memc, MEMCACHED_FLAG_IS_FETCHING_VERSION, true);
301 }
302 | DISTRIBUTION distribution
303 {
304 // Check to see if DISTRIBUTION has already been set
305 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
306 {
307 parser_abort(context, "--DISTRIBUTION can only be called once");
308 }
309
310 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
311 {
312 parser_abort(context, memcached_last_error_message(context->memc));;
313 }
314 }
315 | DISTRIBUTION distribution ',' hash
316 {
317 // Check to see if DISTRIBUTION has already been set
318 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
319 {
320 parser_abort(context, "--DISTRIBUTION can only be called once");
321 }
322
323 if ((context->rc= memcached_behavior_set_distribution_hash(context->memc, $4)) != MEMCACHED_SUCCESS)
324 {
325 parser_abort(context, "Unable to set the hash for the DISTRIBUTION requested");
326 }
327 }
328 | HASH hash
329 {
330 if (context->set_hash($2) == false)
331 {
332 parser_abort(context, "--HASH can only be set once");
333 }
334 }
335 | behavior_number NUMBER
336 {
337 if ((context->rc= memcached_behavior_set(context->memc, $1, $2)) != MEMCACHED_SUCCESS)
338 {
339 parser_abort(context, "Unable to set behavior");
340 }
341 }
342 | behavior_boolean
343 {
344 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
345 {
346 char buffer[1024];
347 snprintf(buffer, sizeof(buffer), "Could not set: %s", libmemcached_string_behavior($1));
348 parser_abort(context, buffer);
349 }
350 }
351 | USER_DATA
352 {
353 }
354 ;
355
356 behavior_number:
357 REMOVE_FAILED_SERVERS
358 {
359 $$= MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS;
360 }
361 | CONNECT_TIMEOUT
362 {
363 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
364 }
365 | IO_MSG_WATERMARK
366 {
367 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
368 }
369 | IO_BYTES_WATERMARK
370 {
371 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
372 }
373 | IO_KEY_PREFETCH
374 {
375 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
376 }
377 | NUMBER_OF_REPLICAS
378 {
379 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
380 }
381 | POLL_TIMEOUT
382 {
383 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
384 }
385 | RCV_TIMEOUT
386 {
387 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
388 }
389 | RETRY_TIMEOUT
390 {
391 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
392 }
393 | SND_TIMEOUT
394 {
395 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
396 }
397 | SOCKET_RECV_SIZE
398 {
399 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
400 }
401 | SOCKET_SEND_SIZE
402 {
403 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
404 }
405 ;
406
407 behavior_boolean:
408 BINARY_PROTOCOL
409 {
410 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
411 }
412 | BUFFER_REQUESTS
413 {
414 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
415 }
416 | HASH_WITH_NAMESPACE
417 {
418 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
419 }
420 | NOREPLY
421 {
422 $$= MEMCACHED_BEHAVIOR_NOREPLY;
423 }
424 | RANDOMIZE_REPLICA_READ
425 {
426 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
427 }
428 | SORT_HOSTS
429 {
430 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
431 }
432 | SUPPORT_CAS
433 {
434 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
435 }
436 | _TCP_NODELAY
437 {
438 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
439 }
440 | _TCP_KEEPALIVE
441 {
442 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
443 }
444 | _TCP_KEEPIDLE
445 {
446 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
447 }
448 | USE_UDP
449 {
450 $$= MEMCACHED_BEHAVIOR_USE_UDP;
451 }
452 | VERIFY_KEY
453 {
454 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
455 }
456
457
458 optional_port:
459 { $$= MEMCACHED_DEFAULT_PORT;}
460 | PORT
461 { };
462 ;
463
464 optional_weight:
465 { $$= 1; }
466 | WEIGHT_START
467 { }
468 ;
469
470 hash:
471 MD5
472 {
473 $$= MEMCACHED_HASH_MD5;
474 }
475 | CRC
476 {
477 $$= MEMCACHED_HASH_CRC;
478 }
479 | FNV1_64
480 {
481 $$= MEMCACHED_HASH_FNV1_64;
482 }
483 | FNV1A_64
484 {
485 $$= MEMCACHED_HASH_FNV1A_64;
486 }
487 | FNV1_32
488 {
489 $$= MEMCACHED_HASH_FNV1_32;
490 }
491 | FNV1A_32
492 {
493 $$= MEMCACHED_HASH_FNV1A_32;
494 }
495 | HSIEH
496 {
497 $$= MEMCACHED_HASH_HSIEH;
498 }
499 | MURMUR
500 {
501 $$= MEMCACHED_HASH_MURMUR;
502 }
503 | JENKINS
504 {
505 $$= MEMCACHED_HASH_JENKINS;
506 }
507 ;
508
509 string:
510 STRING
511 {
512 $$= $1;
513 }
514 | QUOTED_STRING
515 {
516 $$= $1;
517 }
518 ;
519
520 distribution:
521 CONSISTENT
522 {
523 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
524 }
525 | MODULA
526 {
527 $$= MEMCACHED_DISTRIBUTION_MODULA;
528 }
529 | RANDOM
530 {
531 $$= MEMCACHED_DISTRIBUTION_RANDOM;
532 }
533 ;
534
535 %%
536
537 void Context::start()
538 {
539 config_parse(this, (void **)scanner);
540 }
541