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