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