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