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