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