b67052b34edef7dd15b1378c1ad49551b41f9aea
[awesomized/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="libmemcached_"
29 %parse-param { Context *context }
30 %parse-param { yyscan_t *scanner }
31 %locations
32 %pure-parser
33 %require "2.2"
34 %start statement
35 %verbose
36
37 %{
38
39 #include <config.h>
40
41 #include <stdint.h>
42 #include <iostream>
43 #include <sstream>
44 #include <string>
45
46 #include <libmemcached/options/context.h>
47 #include <libmemcached/options/string.h>
48 #include <libmemcached/options/symbol.h>
49
50 #pragma GCC diagnostic ignored "-Wold-style-cast"
51 #include <libmemcached/options/scanner.h>
52
53 int libmemcached_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
54
55 inline void parser_abort(Context *context, const char *error)
56 {
57 (void)error;
58 if (context->rc == MEMCACHED_SUCCESS)
59 context->rc= MEMCACHED_PARSE_ERROR;
60
61 std::string error_message;
62 error_message+= context->begin;
63 error_message+= " (";
64 error_message+= memcached_strerror(NULL, context->rc);
65 error_message+= ")";
66
67 memcached_set_error_string(context->memc, context->rc, error_message.c_str(), error_message.size());
68 }
69
70 inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanner, const char *error)
71 {
72 parser_abort(context, error);
73 }
74
75 %}
76
77 %token COMMENT
78 %token CONFIGURE_FILE
79 %token EMPTY_LINE
80 %token SERVER
81 %token SERVERS
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 %nonassoc ','
140 %nonassoc '='
141
142 %token <number> NUMBER
143 %token <number> FLOAT
144 %token <string> HOSTNAME
145 %token <string> HOSTNAME_WITH_PORT
146 %token <string> IPADDRESS
147 %token <string> IPADDRESS_WITH_PORT
148 %token <string> STRING
149 %token <string> QUOTED_STRING
150
151 %type <server> server
152 %type <string> string
153 %type <distribution> distribution
154 %type <hash> hash
155 %type <behavior> behavior_boolean
156 %type <behavior> behavior_number
157
158 %%
159
160 statement:
161 expression
162 { }
163 | statement ' ' expression
164 { }
165 | COMMENT
166 { }
167 | EMPTY_LINE
168 { }
169 ;
170
171
172 expression:
173 SERVER '=' server
174 {
175 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
176 {
177 parser_abort(context, NULL);
178 }
179 }
180 | SERVERS '=' server_list
181 {
182 }
183 | CONFIGURE_FILE '=' string
184 {
185 memcached_set_configuration_file(context->memc, $3.c_str, $3.length);
186 }
187 | behaviors
188 ;
189
190 behaviors:
191 PREFIX_KEY '=' string
192 {
193 if ((context->rc= memcached_callback_set(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, std::string($3.c_str, $3.length).c_str())) != MEMCACHED_SUCCESS)
194 {
195 parser_abort(context, NULL);;
196 }
197 }
198 | DISTRIBUTION '=' distribution
199 {
200 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3)) != MEMCACHED_SUCCESS)
201 {
202 parser_abort(context, NULL);;
203 }
204 }
205 | HASH '=' hash
206 {
207 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $3)) != MEMCACHED_SUCCESS)
208 {
209 parser_abort(context, NULL);;
210 }
211 }
212 | KETAMA_HASH '=' hash
213 {
214 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3)) != MEMCACHED_SUCCESS)
215 {
216 parser_abort(context, NULL);;
217 }
218 }
219 | behavior_number '=' NUMBER
220 {
221 if ((context->rc= memcached_behavior_set(context->memc, $1, $3)) != MEMCACHED_SUCCESS)
222 {
223 parser_abort(context, NULL);;
224 }
225 }
226 | behavior_boolean
227 {
228 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
229 {
230 parser_abort(context, NULL);;
231 }
232 }
233 | USER_DATA
234 {
235 }
236 ;
237
238 behavior_number:
239 CONNECT_TIMEOUT
240 {
241 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
242 }
243 | IO_MSG_WATERMARK
244 {
245 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
246 }
247 | IO_BYTES_WATERMARK
248 {
249 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
250 }
251 | IO_KEY_PREFETCH
252 {
253 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
254 }
255 | NUMBER_OF_REPLICAS
256 {
257 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
258 }
259 | POLL_TIMEOUT
260 {
261 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
262 }
263 | RCV_TIMEOUT
264 {
265 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
266 }
267 | RETRY_TIMEOUT
268 {
269 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
270 }
271 | SERVER_FAILURE_LIMIT
272 {
273 $$= MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT;
274 }
275 | SND_TIMEOUT
276 {
277 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
278 }
279 | SOCKET_RECV_SIZE
280 {
281 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
282 }
283 | SOCKET_SEND_SIZE
284 {
285 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
286 }
287 ;
288
289 behavior_boolean:
290 AUTO_EJECT_HOSTS
291 {
292 $$= MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS;
293 }
294 | BINARY_PROTOCOL
295 {
296 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
297 }
298 | BUFFER_REQUESTS
299 {
300 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
301 }
302 | CACHE_LOOKUPS
303 {
304 $$= MEMCACHED_BEHAVIOR_CACHE_LOOKUPS;
305 }
306 | _CORK
307 {
308 $$= MEMCACHED_BEHAVIOR_CORK;
309 }
310 | HASH_WITH_PREFIX_KEY
311 {
312 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
313 }
314 | KETAMA
315 {
316 $$= MEMCACHED_BEHAVIOR_KETAMA;
317 }
318 | KETAMA_WEIGHTED
319 {
320 $$= MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
321 }
322 | NOREPLY
323 {
324 $$= MEMCACHED_BEHAVIOR_NOREPLY;
325 }
326 | RANDOMIZE_REPLICA_READ
327 {
328 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
329 }
330 | SORT_HOSTS
331 {
332 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
333 }
334 | SUPPORT_CAS
335 {
336 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
337 }
338 | _TCP_NODELAY
339 {
340 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
341 }
342 | _TCP_KEEPALIVE
343 {
344 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
345 }
346 | _TCP_KEEPIDLE
347 {
348 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
349 }
350 | USE_UDP
351 {
352 $$= MEMCACHED_BEHAVIOR_USE_UDP;
353 }
354 | VERIFY_KEY
355 {
356 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
357 }
358
359
360 server_list:
361 server
362 {
363 if ((context->rc= memcached_server_add_parsed(context->memc, $1.c_str, $1.length, $1.port, 0)) != MEMCACHED_SUCCESS)
364 {
365 parser_abort(context, NULL);;
366 }
367 }
368 | server_list ',' server
369 {
370 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
371 {
372 parser_abort(context, NULL);;
373 }
374 }
375 ;
376
377 server:
378 HOSTNAME_WITH_PORT NUMBER
379 {
380 $$.c_str= $1.c_str;
381 $$.length= $1.length -1;
382 $$.port= $2;
383 }
384 | HOSTNAME
385 {
386 $$.c_str= $1.c_str;
387 $$.length= $1.length;
388 $$.port= MEMCACHED_DEFAULT_PORT;
389 }
390 | STRING /* a match can be against "localhost" which is just a string */
391 {
392 $$.c_str= $1.c_str;
393 $$.length= $1.length;
394 $$.port= MEMCACHED_DEFAULT_PORT;
395 }
396 | IPADDRESS_WITH_PORT NUMBER
397 {
398 $$.c_str= $1.c_str;
399 $$.length= $1.length -1;
400 $$.port= $2;
401 }
402 | IPADDRESS
403 {
404 $$.c_str= $1.c_str;
405 $$.length= $1.length;
406 $$.port= MEMCACHED_DEFAULT_PORT;
407 }
408 ;
409
410 hash:
411 MD5
412 {
413 $$= MEMCACHED_HASH_MD5;
414 }
415 | CRC
416 {
417 $$= MEMCACHED_HASH_CRC;
418 }
419 | FNV1_64
420 {
421 $$= MEMCACHED_HASH_FNV1_64;
422 }
423 | FNV1A_64
424 {
425 $$= MEMCACHED_HASH_FNV1A_64;
426 }
427 | FNV1_32
428 {
429 $$= MEMCACHED_HASH_FNV1_32;
430 }
431 | FNV1A_32
432 {
433 $$= MEMCACHED_HASH_FNV1A_32;
434 }
435 | HSIEH
436 {
437 $$= MEMCACHED_HASH_HSIEH;
438 }
439 | MURMUR
440 {
441 $$= MEMCACHED_HASH_MURMUR;
442 }
443 | JENKINS
444 {
445 $$= MEMCACHED_HASH_JENKINS;
446 }
447 ;
448
449 string:
450 STRING
451 {
452 $$= $1;
453 }
454 | QUOTED_STRING
455 {
456 $$.c_str= $1.c_str +1;
457 $$.length= $1.length -2;
458 }
459 ;
460
461 distribution:
462 CONSISTENT
463 {
464 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
465 }
466 | MODULA
467 {
468 $$= MEMCACHED_DISTRIBUTION_MODULA;
469 }
470 | RANDOM
471 {
472 $$= MEMCACHED_DISTRIBUTION_RANDOM;
473 }
474 ;