Update, add class keyword to solve build issue for Bison 2.7 on OSX.
[awesomized/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 %error-verbose
46 %debug
47 %defines
48 %expect 0
49 %output "libmemcached/csl/parser.cc"
50 %defines "libmemcached/csl/parser.h"
51 %lex-param { yyscan_t *scanner }
52 %name-prefix="config_"
53 %parse-param { class Context *context }
54 %parse-param { yyscan_t *scanner }
55 %pure-parser
56 %require "2.4"
57 %start begin
58 %verbose
59
60 %{
61
62 #include <libmemcached/options.hpp>
63
64 #include <libmemcached/csl/context.h>
65 #include <libmemcached/csl/symbol.h>
66 #include <libmemcached/csl/scanner.h>
67
68 #ifndef __INTEL_COMPILER
69 #pragma GCC diagnostic ignored "-Wold-style-cast"
70 #endif
71
72 #ifndef __INTEL_COMPILER
73 #ifndef __clang__
74 #pragma GCC diagnostic ignored "-Wlogical-op"
75 #endif
76 #endif
77
78 int conf_lex(YYSTYPE* lvalp, void* scanner);
79
80 #define select_yychar(__context) yychar == UNKNOWN ? ( (__context)->previous_token == END ? UNKNOWN : (__context)->previous_token ) : yychar
81
82 #define stryytname(__yytokentype) ((__yytokentype) < YYNTOKENS ) ? yytname[(__yytokentype)] : ""
83
84 #define parser_abort(__context, __error_message) do { (__context)->abort((__error_message), yytokentype(select_yychar(__context)), stryytname(YYTRANSLATE(select_yychar(__context)))); YYABORT; } while (0)
85
86 // This is bison calling error.
87 inline void __config_error(Context *context, yyscan_t *scanner, const char *error, int last_token, const char *last_token_str)
88 {
89 if (not context->end())
90 {
91 context->error(error, yytokentype(last_token), last_token_str);
92 }
93 else
94 {
95 context->error(error, yytokentype(last_token), last_token_str);
96 }
97 }
98
99 #define config_error(__context, __scanner, __error_msg) do { __config_error((__context), (__scanner), (__error_msg), select_yychar(__context), stryytname(YYTRANSLATE(select_yychar(__context)))); } while (0)
100
101
102 %}
103
104 %token COMMENT
105 %token END
106 %token CSL_ERROR
107 %token RESET
108 %token PARSER_DEBUG
109 %token INCLUDE
110 %token CONFIGURE_FILE
111 %token EMPTY_LINE
112 %token SERVER
113 %token CSL_SOCKET
114 %token SERVERS
115 %token SERVERS_OPTION
116 %token UNKNOWN_OPTION
117 %token UNKNOWN
118
119 /* All behavior options */
120 %token BINARY_PROTOCOL
121 %token BUFFER_REQUESTS
122 %token CONNECT_TIMEOUT
123 %token DISTRIBUTION
124 %token HASH
125 %token HASH_WITH_NAMESPACE
126 %token IO_BYTES_WATERMARK
127 %token IO_KEY_PREFETCH
128 %token IO_MSG_WATERMARK
129 %token KETAMA_HASH
130 %token KETAMA_WEIGHTED
131 %token NOREPLY
132 %token NUMBER_OF_REPLICAS
133 %token POLL_TIMEOUT
134 %token RANDOMIZE_REPLICA_READ
135 %token RCV_TIMEOUT
136 %token REMOVE_FAILED_SERVERS
137 %token RETRY_TIMEOUT
138 %token SND_TIMEOUT
139 %token SOCKET_RECV_SIZE
140 %token SOCKET_SEND_SIZE
141 %token SORT_HOSTS
142 %token SUPPORT_CAS
143 %token USER_DATA
144 %token USE_UDP
145 %token VERIFY_KEY
146 %token _TCP_KEEPALIVE
147 %token _TCP_KEEPIDLE
148 %token _TCP_NODELAY
149 %token FETCH_VERSION
150
151 /* Callbacks */
152 %token NAMESPACE
153
154 /* Pool */
155 %token POOL_MIN
156 %token POOL_MAX
157
158 /* Hash types */
159 %token MD5
160 %token CRC
161 %token FNV1_64
162 %token FNV1A_64
163 %token FNV1_32
164 %token FNV1A_32
165 %token HSIEH
166 %token MURMUR
167 %token JENKINS
168
169 /* Distributions */
170 %token CONSISTENT
171 %token MODULA
172 %token RANDOM
173
174 /* Boolean values */
175 %token <boolean> CSL_TRUE
176 %token <boolean> CSL_FALSE
177
178 %nonassoc ','
179 %nonassoc '='
180
181 %token <number> CSL_FLOAT
182 %token <number> NUMBER
183 %token <number> PORT
184 %token <number> WEIGHT_START
185 %token <server> IPADDRESS
186 %token <server> HOSTNAME
187 %token <string> STRING
188 %token <string> QUOTED_STRING
189 %token <string> FILE_PATH
190
191 %type <behavior> behavior_boolean
192 %type <behavior> behavior_number
193 %type <distribution> distribution
194 %type <hash> hash
195 %type <number> optional_port
196 %type <number> optional_weight
197 %type <string> string
198
199 %%
200
201 begin:
202 statement
203 | begin ' ' statement
204 ;
205
206 statement:
207 expression
208 { }
209 | COMMENT
210 { }
211 | EMPTY_LINE
212 { }
213 | END
214 {
215 context->set_end();
216 YYACCEPT;
217 }
218 | CSL_ERROR
219 {
220 context->rc= MEMCACHED_PARSE_USER_ERROR;
221 parser_abort(context, "ERROR called directly");
222 }
223 | RESET
224 {
225 memcached_reset(context->memc);
226 }
227 | PARSER_DEBUG
228 {
229 yydebug= 1;
230 }
231 | INCLUDE ' ' string
232 {
233 if ((context->rc= memcached_parse_configure_file(*context->memc, $3.c_str, $3.size)) != MEMCACHED_SUCCESS)
234 {
235 parser_abort(context, "Failed to parse configuration file");
236 }
237 }
238 ;
239
240
241 expression:
242 SERVER HOSTNAME optional_port optional_weight
243 {
244 if (memcached_failed(context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $3, uint32_t($4))))
245 {
246 char buffer[1024];
247 snprintf(buffer, sizeof(buffer), "Failed to add server: %s:%u", $2.c_str, uint32_t($3));
248 parser_abort(context, buffer);
249 }
250 context->unset_server();
251 }
252 | SERVER IPADDRESS optional_port optional_weight
253 {
254 if (memcached_failed(context->rc= memcached_server_add_with_weight(context->memc, $2.c_str, $3, uint32_t($4))))
255 {
256 char buffer[1024];
257 snprintf(buffer, sizeof(buffer), "Failed to add server: %s:%u", $2.c_str, uint32_t($3));
258 parser_abort(context, buffer);
259 }
260 context->unset_server();
261 }
262 | CSL_SOCKET string optional_weight
263 {
264 if (memcached_failed(context->rc= memcached_server_add_unix_socket_with_weight(context->memc, $2.c_str, uint32_t($3))))
265 {
266 char buffer[1024];
267 snprintf(buffer, sizeof(buffer), "Failed to add socket: %s", $2.c_str);
268 parser_abort(context, buffer);
269 }
270 }
271 | CONFIGURE_FILE string
272 {
273 memcached_set_configuration_file(context->memc, $2.c_str, $2.size);
274 }
275 | POOL_MIN NUMBER
276 {
277 context->memc->configure.initial_pool_size= uint32_t($2);
278 }
279 | POOL_MAX NUMBER
280 {
281 context->memc->configure.max_pool_size= uint32_t($2);
282 }
283 | behaviors
284 ;
285
286 behaviors:
287 NAMESPACE string
288 {
289 if (memcached_callback_get(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL))
290 {
291 parser_abort(context, "--NAMESPACE can only be called once");
292 }
293
294 if ((context->rc= memcached_set_namespace(context->memc, $2.c_str, $2.size)) != MEMCACHED_SUCCESS)
295 {
296 parser_abort(context, memcached_last_error_message(context->memc));
297 }
298 }
299 | FETCH_VERSION
300 {
301 memcached_flag(*context->memc, MEMCACHED_FLAG_IS_FETCHING_VERSION, true);
302 }
303 | DISTRIBUTION distribution
304 {
305 // Check to see if DISTRIBUTION has already been set
306 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
307 {
308 parser_abort(context, "--DISTRIBUTION can only be called once");
309 }
310
311 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
312 {
313 parser_abort(context, memcached_last_error_message(context->memc));;
314 }
315 }
316 | DISTRIBUTION distribution ',' hash
317 {
318 // Check to see if DISTRIBUTION has already been set
319 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
320 {
321 parser_abort(context, "--DISTRIBUTION can only be called once");
322 }
323
324 if ((context->rc= memcached_behavior_set_distribution_hash(context->memc, $4)) != MEMCACHED_SUCCESS)
325 {
326 parser_abort(context, "Unable to set the hash for the DISTRIBUTION requested");
327 }
328 }
329 | HASH hash
330 {
331 if (context->set_hash($2) == false)
332 {
333 parser_abort(context, "--HASH can only be set once");
334 }
335 }
336 | behavior_number NUMBER
337 {
338 if ((context->rc= memcached_behavior_set(context->memc, $1, $2)) != MEMCACHED_SUCCESS)
339 {
340 parser_abort(context, "Unable to set behavior");
341 }
342 }
343 | behavior_boolean
344 {
345 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
346 {
347 char buffer[1024];
348 snprintf(buffer, sizeof(buffer), "Could not set: %s", libmemcached_string_behavior($1));
349 parser_abort(context, buffer);
350 }
351 }
352 | USER_DATA
353 {
354 }
355 ;
356
357 behavior_number:
358 REMOVE_FAILED_SERVERS
359 {
360 $$= MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS;
361 }
362 | CONNECT_TIMEOUT
363 {
364 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
365 }
366 | IO_MSG_WATERMARK
367 {
368 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
369 }
370 | IO_BYTES_WATERMARK
371 {
372 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
373 }
374 | IO_KEY_PREFETCH
375 {
376 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
377 }
378 | NUMBER_OF_REPLICAS
379 {
380 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
381 }
382 | POLL_TIMEOUT
383 {
384 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
385 }
386 | RCV_TIMEOUT
387 {
388 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
389 }
390 | RETRY_TIMEOUT
391 {
392 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
393 }
394 | SND_TIMEOUT
395 {
396 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
397 }
398 | SOCKET_RECV_SIZE
399 {
400 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
401 }
402 | SOCKET_SEND_SIZE
403 {
404 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
405 }
406 ;
407
408 behavior_boolean:
409 BINARY_PROTOCOL
410 {
411 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
412 }
413 | BUFFER_REQUESTS
414 {
415 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
416 }
417 | HASH_WITH_NAMESPACE
418 {
419 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
420 }
421 | NOREPLY
422 {
423 $$= MEMCACHED_BEHAVIOR_NOREPLY;
424 }
425 | RANDOMIZE_REPLICA_READ
426 {
427 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
428 }
429 | SORT_HOSTS
430 {
431 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
432 }
433 | SUPPORT_CAS
434 {
435 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
436 }
437 | _TCP_NODELAY
438 {
439 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
440 }
441 | _TCP_KEEPALIVE
442 {
443 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
444 }
445 | _TCP_KEEPIDLE
446 {
447 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
448 }
449 | USE_UDP
450 {
451 $$= MEMCACHED_BEHAVIOR_USE_UDP;
452 }
453 | VERIFY_KEY
454 {
455 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
456 }
457
458
459 optional_port:
460 { $$= MEMCACHED_DEFAULT_PORT;}
461 | PORT
462 { };
463 ;
464
465 optional_weight:
466 { $$= 1; }
467 | WEIGHT_START
468 { }
469 ;
470
471 hash:
472 MD5
473 {
474 $$= MEMCACHED_HASH_MD5;
475 }
476 | CRC
477 {
478 $$= MEMCACHED_HASH_CRC;
479 }
480 | FNV1_64
481 {
482 $$= MEMCACHED_HASH_FNV1_64;
483 }
484 | FNV1A_64
485 {
486 $$= MEMCACHED_HASH_FNV1A_64;
487 }
488 | FNV1_32
489 {
490 $$= MEMCACHED_HASH_FNV1_32;
491 }
492 | FNV1A_32
493 {
494 $$= MEMCACHED_HASH_FNV1A_32;
495 }
496 | HSIEH
497 {
498 $$= MEMCACHED_HASH_HSIEH;
499 }
500 | MURMUR
501 {
502 $$= MEMCACHED_HASH_MURMUR;
503 }
504 | JENKINS
505 {
506 $$= MEMCACHED_HASH_JENKINS;
507 }
508 ;
509
510 string:
511 STRING
512 {
513 $$= $1;
514 }
515 | QUOTED_STRING
516 {
517 $$= $1;
518 }
519 ;
520
521 distribution:
522 CONSISTENT
523 {
524 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
525 }
526 | MODULA
527 {
528 $$= MEMCACHED_DISTRIBUTION_MODULA;
529 }
530 | RANDOM
531 {
532 $$= MEMCACHED_DISTRIBUTION_RANDOM;
533 }
534 ;
535
536 %%
537
538 void Context::start()
539 {
540 config_parse(this, (void **)scanner);
541 }
542