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