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