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