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