abd832b5103208e14609fb82cb5a14e9fbff1266
[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="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 libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanner, const char *error)
56 {
57 memcached_string_t local_string;
58 local_string.size= strlen(context->begin);
59 local_string.c_str= context->begin;
60 if (context->rc == MEMCACHED_SUCCESS)
61 context->rc= MEMCACHED_PARSE_ERROR;
62 memcached_set_error(context->memc, context->rc, &local_string);
63 }
64
65 %}
66
67 %token COMMENT
68 %token CONFIGURE_FILE
69 %token EMPTY_LINE
70 %token SERVER
71 %token SERVERS
72 %token UNKNOWN_OPTION
73 %token UNKNOWN
74
75 /* All behavior options */
76 %token AUTO_EJECT_HOSTS
77 %token BINARY_PROTOCOL
78 %token BUFFER_REQUESTS
79 %token CACHE_LOOKUPS
80 %token CONNECT_TIMEOUT
81 %token _CORK
82 %token DISTRIBUTION
83 %token HASH
84 %token HASH_WITH_PREFIX_KEY
85 %token IO_BYTES_WATERMARK
86 %token IO_KEY_PREFETCH
87 %token IO_MSG_WATERMARK
88 %token KETAMA
89 %token KETAMA_HASH
90 %token KETAMA_WEIGHTED
91 %token NOREPLY
92 %token NUMBER_OF_REPLICAS
93 %token POLL_TIMEOUT
94 %token RANDOMIZE_REPLICA_READ
95 %token RCV_TIMEOUT
96 %token RETRY_TIMEOUT
97 %token SERVER_FAILURE_LIMIT
98 %token SND_TIMEOUT
99 %token SOCKET_RECV_SIZE
100 %token SOCKET_SEND_SIZE
101 %token SORT_HOSTS
102 %token SUPPORT_CAS
103 %token _TCP_NODELAY
104 %token _TCP_KEEPALIVE
105 %token _TCP_KEEPIDLE
106 %token USER_DATA
107 %token USE_UDP
108 %token VERIFY_KEY
109
110 /* Callbacks */
111 %token PREFIX_KEY
112
113 /* Hash types */
114 %token MD5
115 %token CRC
116 %token FNV1_64
117 %token FNV1A_64
118 %token FNV1_32
119 %token FNV1A_32
120 %token HSIEH
121 %token MURMUR
122 %token JENKINS
123
124 /* Distributions */
125 %token CONSISTENT
126 %token MODULA
127 %token RANDOM
128
129 %nonassoc ','
130 %nonassoc '='
131
132 %token <number> NUMBER
133 %token <number> FLOAT
134 %token <string> HOSTNAME
135 %token <string> HOSTNAME_WITH_PORT
136 %token <string> IPADDRESS
137 %token <string> IPADDRESS_WITH_PORT
138 %token <string> STRING
139 %token <string> QUOTED_STRING
140
141 %type <server> server
142 %type <string> string
143 %type <distribution> distribution
144 %type <hash> hash
145 %type <behavior> behavior_boolean
146 %type <behavior> behavior_number
147
148 %%
149
150 statement:
151 expression
152 { }
153 | statement ' ' expression
154 { }
155 | COMMENT
156 { }
157 | EMPTY_LINE
158 { }
159 ;
160
161
162 expression:
163 SERVER '=' server
164 {
165 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
166 {
167 YYERROR;
168 }
169 }
170 | SERVERS '=' server_list
171 {
172 }
173 | CONFIGURE_FILE '=' string
174 {
175 memcached_set_configuration_file(context->memc, $3.c_str, $3.length);
176 }
177 | behaviors
178 ;
179
180 behaviors:
181 PREFIX_KEY '=' string
182 {
183 if ((context->rc= memcached_callback_set(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, std::string($3.c_str, $3.length).c_str())) != MEMCACHED_SUCCESS)
184 {
185 YYERROR;
186 }
187 }
188 | DISTRIBUTION '=' distribution
189 {
190 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3)) != MEMCACHED_SUCCESS)
191 {
192 YYERROR;
193 }
194 }
195 | HASH '=' hash
196 {
197 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $3)) != MEMCACHED_SUCCESS)
198 {
199 YYERROR;
200 }
201 }
202 | KETAMA_HASH '=' hash
203 {
204 if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3)) != MEMCACHED_SUCCESS)
205 {
206 YYERROR;
207 }
208 }
209 | behavior_number '=' NUMBER
210 {
211 if ((context->rc= memcached_behavior_set(context->memc, $1, $3)) != MEMCACHED_SUCCESS)
212 {
213 YYERROR;
214 }
215 }
216 | behavior_boolean
217 {
218 if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
219 {
220 YYERROR;
221 }
222 }
223 | USER_DATA
224 {
225 }
226 ;
227
228 behavior_number:
229 CONNECT_TIMEOUT
230 {
231 $$= MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT;
232 }
233 | IO_MSG_WATERMARK
234 {
235 $$= MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK;
236 }
237 | IO_BYTES_WATERMARK
238 {
239 $$= MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK;
240 }
241 | IO_KEY_PREFETCH
242 {
243 $$= MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH;
244 }
245 | NUMBER_OF_REPLICAS
246 {
247 $$= MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS;
248 }
249 | POLL_TIMEOUT
250 {
251 $$= MEMCACHED_BEHAVIOR_POLL_TIMEOUT;
252 }
253 | RCV_TIMEOUT
254 {
255 $$= MEMCACHED_BEHAVIOR_RCV_TIMEOUT;
256 }
257 | RETRY_TIMEOUT
258 {
259 $$= MEMCACHED_BEHAVIOR_RETRY_TIMEOUT;
260 }
261 | SERVER_FAILURE_LIMIT
262 {
263 $$= MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT;
264 }
265 | SND_TIMEOUT
266 {
267 $$= MEMCACHED_BEHAVIOR_SND_TIMEOUT;
268 }
269 | SOCKET_RECV_SIZE
270 {
271 $$= MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE;
272 }
273 | SOCKET_SEND_SIZE
274 {
275 $$= MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE;
276 }
277 ;
278
279 behavior_boolean:
280 AUTO_EJECT_HOSTS
281 {
282 $$= MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS;
283 }
284 | BINARY_PROTOCOL
285 {
286 $$= MEMCACHED_BEHAVIOR_BINARY_PROTOCOL;
287 }
288 | BUFFER_REQUESTS
289 {
290 $$= MEMCACHED_BEHAVIOR_BUFFER_REQUESTS;
291 }
292 | CACHE_LOOKUPS
293 {
294 $$= MEMCACHED_BEHAVIOR_CACHE_LOOKUPS;
295 }
296 | _CORK
297 {
298 $$= MEMCACHED_BEHAVIOR_CORK;
299 }
300 | HASH_WITH_PREFIX_KEY
301 {
302 $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
303 }
304 | KETAMA
305 {
306 $$= MEMCACHED_BEHAVIOR_KETAMA;
307 }
308 | KETAMA_WEIGHTED
309 {
310 $$= MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
311 }
312 | NOREPLY
313 {
314 $$= MEMCACHED_BEHAVIOR_NOREPLY;
315 }
316 | RANDOMIZE_REPLICA_READ
317 {
318 $$= MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ;
319 }
320 | SORT_HOSTS
321 {
322 $$= MEMCACHED_BEHAVIOR_SORT_HOSTS;
323 }
324 | SUPPORT_CAS
325 {
326 $$= MEMCACHED_BEHAVIOR_SUPPORT_CAS;
327 }
328 | _TCP_NODELAY
329 {
330 $$= MEMCACHED_BEHAVIOR_TCP_NODELAY;
331 }
332 | _TCP_KEEPALIVE
333 {
334 $$= MEMCACHED_BEHAVIOR_TCP_KEEPALIVE;
335 }
336 | _TCP_KEEPIDLE
337 {
338 $$= MEMCACHED_BEHAVIOR_TCP_KEEPIDLE;
339 }
340 | USE_UDP
341 {
342 $$= MEMCACHED_BEHAVIOR_USE_UDP;
343 }
344 | VERIFY_KEY
345 {
346 $$= MEMCACHED_BEHAVIOR_VERIFY_KEY;
347 }
348
349
350 server_list:
351 server
352 {
353 if ((context->rc= memcached_server_add_parsed(context->memc, $1.c_str, $1.length, $1.port, 0)) != MEMCACHED_SUCCESS)
354 {
355 YYERROR;
356 }
357 }
358 | server_list ',' server
359 {
360 if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
361 {
362 YYERROR;
363 }
364 }
365 ;
366
367 server:
368 HOSTNAME_WITH_PORT NUMBER
369 {
370 $$.c_str= $1.c_str;
371 $$.length= $1.length -1;
372 $$.port= $2;
373 }
374 | HOSTNAME
375 {
376 $$.c_str= $1.c_str;
377 $$.length= $1.length;
378 $$.port= MEMCACHED_DEFAULT_PORT;
379 }
380 | STRING /* a match can be against "localhost" which is just a string */
381 {
382 $$.c_str= $1.c_str;
383 $$.length= $1.length;
384 $$.port= MEMCACHED_DEFAULT_PORT;
385 }
386 | IPADDRESS_WITH_PORT NUMBER
387 {
388 $$.c_str= $1.c_str;
389 $$.length= $1.length -1;
390 $$.port= $2;
391 }
392 | IPADDRESS
393 {
394 $$.c_str= $1.c_str;
395 $$.length= $1.length;
396 $$.port= MEMCACHED_DEFAULT_PORT;
397 }
398 ;
399
400 hash:
401 MD5
402 {
403 $$= MEMCACHED_HASH_MD5;
404 }
405 | CRC
406 {
407 $$= MEMCACHED_HASH_CRC;
408 }
409 | FNV1_64
410 {
411 $$= MEMCACHED_HASH_FNV1_64;
412 }
413 | FNV1A_64
414 {
415 $$= MEMCACHED_HASH_FNV1A_64;
416 }
417 | FNV1_32
418 {
419 $$= MEMCACHED_HASH_FNV1_32;
420 }
421 | FNV1A_32
422 {
423 $$= MEMCACHED_HASH_FNV1A_32;
424 }
425 | HSIEH
426 {
427 $$= MEMCACHED_HASH_HSIEH;
428 }
429 | MURMUR
430 {
431 $$= MEMCACHED_HASH_MURMUR;
432 }
433 | JENKINS
434 {
435 $$= MEMCACHED_HASH_JENKINS;
436 }
437 ;
438
439 string:
440 STRING
441 {
442 $$= $1;
443 }
444 | QUOTED_STRING
445 {
446 $$.c_str= $1.c_str +1;
447 $$.length= $1.length -2;
448 }
449 ;
450
451 distribution:
452 CONSISTENT
453 {
454 $$= MEMCACHED_DISTRIBUTION_CONSISTENT;
455 }
456 | MODULA
457 {
458 $$= MEMCACHED_DISTRIBUTION_MODULA;
459 }
460 | RANDOM
461 {
462 $$= MEMCACHED_DISTRIBUTION_RANDOM;
463 }
464 ;