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