adf3dc019bc41f7324604c7950a465684d711571
[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="libmemcached_"
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/options/context.h>
43 #include <libmemcached/options/string.h>
44 #include <libmemcached/options/symbol.h>
45
46 #pragma GCC diagnostic ignored "-Wold-style-cast"
47 #include <libmemcached/options/scanner.h>
48
49 int libmemcached_lex(YYSTYPE* lvalp, void* scanner);
50
51 #define parser_abort(A, B) do { parser_abort_func((A), (B)); YYABORT; } while (0)
52
53 inline void parser_abort_func(Context *context, const char *error)
54 {
55 (void)error;
56 if (context->rc == MEMCACHED_SUCCESS)
57 context->rc= MEMCACHED_PARSE_ERROR;
58
59 std::string error_message;
60 error_message+= "Error occured while parsing: ";
61 error_message+= context->begin;
62 error_message+= " (";
63 if (context->rc == MEMCACHED_PARSE_ERROR and error)
64 {
65 error_message+= error;
66 }
67 else
68 {
69 error_message+= memcached_strerror(NULL, context->rc);
70 }
71 error_message+= ")";
72
73 memcached_set_error_string(context->memc, context->rc, error_message.c_str(), error_message.size());
74 }
75
76 inline void libmemcached_error(Context *context, yyscan_t *scanner, const char *error)
77 {
78 if (not context->end())
79 parser_abort_func(context, error);
80 }
81
82 int libmemcached_parse(Context*, yyscan_t *);
83 void Context::start()
84 {
85 libmemcached_parse(this, scanner);
86 }
87
88 %}
89
90 %token COMMENT
91 %token END
92 %token ERROR
93 %token RESET
94 %token PARSER_DEBUG
95 %token INCLUDE
96 %token CONFIGURE_FILE
97 %token EMPTY_LINE
98 %token SERVER
99 %token SERVERS
100 %token SERVERS_OPTION
101 %token UNKNOWN_OPTION
102 %token UNKNOWN
103
104 /* All behavior options */
105 %token AUTO_EJECT_HOSTS
106 %token BINARY_PROTOCOL
107 %token BUFFER_REQUESTS
108 %token CACHE_LOOKUPS
109 %token CONNECT_TIMEOUT
110 %token _CORK
111 %token DISTRIBUTION
112 %token HASH
113 %token HASH_WITH_PREFIX_KEY
114 %token IO_BYTES_WATERMARK
115 %token IO_KEY_PREFETCH
116 %token IO_MSG_WATERMARK
117 %token KETAMA
118 %token KETAMA_HASH
119 %token KETAMA_WEIGHTED
120 %token NOREPLY
121 %token NUMBER_OF_REPLICAS
122 %token POLL_TIMEOUT
123 %token RANDOMIZE_REPLICA_READ
124 %token RCV_TIMEOUT
125 %token RETRY_TIMEOUT
126 %token SERVER_FAILURE_LIMIT
127 %token SND_TIMEOUT
128 %token SOCKET_RECV_SIZE
129 %token SOCKET_SEND_SIZE
130 %token SORT_HOSTS
131 %token SUPPORT_CAS
132 %token _TCP_NODELAY
133 %token _TCP_KEEPALIVE
134 %token _TCP_KEEPIDLE
135 %token USER_DATA
136 %token USE_UDP
137 %token VERIFY_KEY
138
139 /* Callbacks */
140 %token PREFIX_KEY
141
142 /* Hash types */
143 %token MD5
144 %token CRC
145 %token FNV1_64
146 %token FNV1A_64
147 %token FNV1_32
148 %token FNV1A_32
149 %token HSIEH
150 %token MURMUR
151 %token JENKINS
152
153 /* Distributions */
154 %token CONSISTENT
155 %token MODULA
156 %token RANDOM
157
158 /* Boolean values */
159 %token <boolean> TRUE
160 %token <boolean> FALSE
161
162 %nonassoc ','
163 %nonassoc '='
164
165 %token <number> NUMBER
166 %token <number> FLOAT
167 %token <string> HOSTNAME
168 %token <string> HOSTNAME_WITH_PORT
169 %token <string> IPADDRESS
170 %token <string> IPADDRESS_WITH_PORT
171 %token <string> STRING
172 %token <string> QUOTED_STRING
173 %token <string> FILE_PATH
174
175 %type <server> server
176 %type <string> string
177 %type <distribution> distribution
178 %type <hash> hash
179 %type <behavior> behavior_boolean
180 %type <behavior> behavior_number
181
182 %%
183
184 begin:
185 statement
186 | begin ' ' statement
187 ;
188
189 statement:
190 expression
191 { }
192 | COMMENT
193 { }
194 | EMPTY_LINE
195 { }
196 | END
197 {
198 context->set_end();
199 YYACCEPT;
200 }
201 | ERROR
202 {
203 context->rc= MEMCACHED_PARSE_USER_ERROR;
204 parser_abort(context, NULL);
205 }
206 | RESET
207 {
208 memcached_reset(context->memc);
209 }
210 | PARSER_DEBUG
211 {
212 yydebug= 1;
213 }
214 | INCLUDE ' ' string
215 {
216 if ((context->rc= memcached_parse_configure_file(context->memc, $3.c_str, $3.length)) != MEMCACHED_SUCCESS)
217 {
218 parser_abort(context, NULL);
219 }
220 }
221 ;
222
223
224 expression:
225 SERVER '=' server
226 {
227 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
228 {
229 parser_abort(context, NULL);
230 }
231 }
232 | SERVERS_OPTION '=' server_list
233 {
234 }
235 | CONFIGURE_FILE '=' string
236 {
237 memcached_set_configuration_file(context->memc, $3.c_str, $3.length);
238 }
239 | behaviors
240 ;
241
242 behaviors:
243 PREFIX_KEY '=' string
244 {
245 if ((context->rc= memcached_callback_set(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, std::string($3.c_str, $3.length).c_str())) != MEMCACHED_SUCCESS)
246 {
247 parser_abort(context, NULL);;
248 }
249 }
250 | DISTRIBUTION '=' distribution
251 {
252 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3)) != MEMCACHED_SUCCESS)
253 {
254 parser_abort(context, NULL);;
255 }
256 }
257 | HASH '=' hash
258 {
259 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $3)) != MEMCACHED_SUCCESS)
260 {
261 parser_abort(context, NULL);;
262 }
263 }
264 | KETAMA_HASH '=' hash
265 {
266 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3)) != MEMCACHED_SUCCESS)
267 {
268 parser_abort(context, NULL);;
269 }
270 }
271 | behavior_number '=' NUMBER
272 {
273 if ((context->rc= memcached_behavior_set(context->memc, $1, $3)) != MEMCACHED_SUCCESS)
274 {
275 parser_abort(context, NULL);;
276 }
277 }
278 | behavior_boolean
279 {
280 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
281 {
282 parser_abort(context, NULL);;
283 }
284 }
285 | USER_DATA
286 {
287 }
288 ;
289
290 behavior_number:
291 CONNECT_TIMEOUT
292 {
293 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
294 }
295 | IO_MSG_WATERMARK
296 {
297 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
298 }
299 | IO_BYTES_WATERMARK
300 {
301 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
302 }
303 | IO_KEY_PREFETCH
304 {
305 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
306 }
307 | NUMBER_OF_REPLICAS
308 {
309 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
310 }
311 | POLL_TIMEOUT
312 {
313 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
314 }
315 | RCV_TIMEOUT
316 {
317 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
318 }
319 | RETRY_TIMEOUT
320 {
321 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
322 }
323 | SERVER_FAILURE_LIMIT
324 {
325 $$= MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT;
326 }
327 | SND_TIMEOUT
328 {
329 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
330 }
331 | SOCKET_RECV_SIZE
332 {
333 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
334 }
335 | SOCKET_SEND_SIZE
336 {
337 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
338 }
339 ;
340
341 behavior_boolean:
342 AUTO_EJECT_HOSTS
343 {
344 $$= MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS;
345 }
346 | BINARY_PROTOCOL
347 {
348 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
349 }
350 | BUFFER_REQUESTS
351 {
352 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
353 }
354 | CACHE_LOOKUPS
355 {
356 $$= MEMCACHED_BEHAVIOR_CACHE_LOOKUPS;
357 }
358 | _CORK
359 {
360 $$= MEMCACHED_BEHAVIOR_CORK;
361 }
362 | HASH_WITH_PREFIX_KEY
363 {
364 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
365 }
366 | KETAMA
367 {
368 $$= MEMCACHED_BEHAVIOR_KETAMA;
369 }
370 | KETAMA_WEIGHTED
371 {
372 $$= MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
373 }
374 | NOREPLY
375 {
376 $$= MEMCACHED_BEHAVIOR_NOREPLY;
377 }
378 | RANDOMIZE_REPLICA_READ
379 {
380 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
381 }
382 | SORT_HOSTS
383 {
384 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
385 }
386 | SUPPORT_CAS
387 {
388 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
389 }
390 | _TCP_NODELAY
391 {
392 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
393 }
394 | _TCP_KEEPALIVE
395 {
396 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
397 }
398 | _TCP_KEEPIDLE
399 {
400 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
401 }
402 | USE_UDP
403 {
404 $$= MEMCACHED_BEHAVIOR_USE_UDP;
405 }
406 | VERIFY_KEY
407 {
408 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
409 }
410
411
412 server_list:
413 server
414 {
415 if ((context->rc= memcached_server_add_parsed(context->memc, $1.c_str, $1.length, $1.port, 0)) != MEMCACHED_SUCCESS)
416 {
417 parser_abort(context, NULL);;
418 }
419 }
420 | server_list ',' server
421 {
422 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
423 {
424 parser_abort(context, NULL);;
425 }
426 }
427 ;
428
429 server:
430 HOSTNAME_WITH_PORT NUMBER
431 {
432 $$.c_str= $1.c_str;
433 $$.length= $1.length -1; // -1 to remove :
434 $$.port= $2;
435 }
436 | HOSTNAME
437 {
438 $$.c_str= $1.c_str;
439 $$.length= $1.length;
440 $$.port= MEMCACHED_DEFAULT_PORT;
441 }
442 | STRING /* a match can be against "localhost" which is just a string */
443 {
444 $$.c_str= $1.c_str;
445 $$.length= $1.length;
446 $$.port= MEMCACHED_DEFAULT_PORT;
447 }
448 | IPADDRESS_WITH_PORT NUMBER
449 {
450 $$.c_str= $1.c_str;
451 $$.length= $1.length -1; // -1 to remove :
452 $$.port= $2;
453 }
454 | IPADDRESS
455 {
456 $$.c_str= $1.c_str;
457 $$.length= $1.length;
458 $$.port= MEMCACHED_DEFAULT_PORT;
459 }
460 ;
461
462 hash:
463 MD5
464 {
465 $$= MEMCACHED_HASH_MD5;
466 }
467 | CRC
468 {
469 $$= MEMCACHED_HASH_CRC;
470 }
471 | FNV1_64
472 {
473 $$= MEMCACHED_HASH_FNV1_64;
474 }
475 | FNV1A_64
476 {
477 $$= MEMCACHED_HASH_FNV1A_64;
478 }
479 | FNV1_32
480 {
481 $$= MEMCACHED_HASH_FNV1_32;
482 }
483 | FNV1A_32
484 {
485 $$= MEMCACHED_HASH_FNV1A_32;
486 }
487 | HSIEH
488 {
489 $$= MEMCACHED_HASH_HSIEH;
490 }
491 | MURMUR
492 {
493 $$= MEMCACHED_HASH_MURMUR;
494 }
495 | JENKINS
496 {
497 $$= MEMCACHED_HASH_JENKINS;
498 }
499 ;
500
501 string:
502 STRING
503 {
504 $$= $1;
505 }
506 | QUOTED_STRING
507 {
508 $$.c_str= $1.c_str +1; // +1 to move use passed the initial quote
509 $$.length= $1.length -1; // -1 removes the end quote
510 }
511 ;
512
513 distribution:
514 CONSISTENT
515 {
516 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
517 }
518 | MODULA
519 {
520 $$= MEMCACHED_DISTRIBUTION_MODULA;
521 }
522 | RANDOM
523 {
524 $$= MEMCACHED_DISTRIBUTION_RANDOM;
525 }
526 ;