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