Merge in changes around setting up better syntax for distribution.
[awesomized/libmemcached] / libmemcached / options / parser.yy
index b67052b34edef7dd15b1378c1ad49551b41f9aea..6aa477920ce5259444af0c33ece6eb5164a6e64f 100644 (file)
 %output "libmemcached/options/parser.cc"
 %defines "libmemcached/options/parser.h"
 %lex-param { yyscan_t *scanner }
-%name-prefix="libmemcached_"
+%name-prefix="config_"
 %parse-param { Context *context }
 %parse-param { yyscan_t *scanner }
-%locations
 %pure-parser
 %require "2.2"
-%start statement
+%start begin
 %verbose
 
 %{
 #include <config.h>
 
 #include <stdint.h>
-#include <iostream>
-#include <sstream>
-#include <string>
 
 #include <libmemcached/options/context.h>
+#include <libmemcached/options/build.h>
 #include <libmemcached/options/string.h>
 #include <libmemcached/options/symbol.h>
+#include <libmemcached/visibility.h>
+#include <libmemcached/prefix_key.h>
 
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 #include <libmemcached/options/scanner.h>
 
-int libmemcached_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
+int conf_lex(YYSTYPE* lvalp, void* scanner);
 
-inline void parser_abort(Context *context, const char *error)
-{
-  (void)error;
-  if (context->rc == MEMCACHED_SUCCESS)
-    context->rc= MEMCACHED_PARSE_ERROR;
-
-  std::string error_message;
-  error_message+= context->begin;
-  error_message+= " (";
-  error_message+= memcached_strerror(NULL, context->rc);
-  error_message+= ")";
+#define parser_abort(A, B) do { parser::abort_func((A), (B)); YYABORT; } while (0) 
 
-  memcached_set_error_string(context->memc, context->rc, error_message.c_str(), error_message.size());
+inline void config_error(Context *context, yyscan_t *scanner, const char *error)
+{
+  if (not context->end())
+    parser::abort_func(context, error);
 }
 
-inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanner, const char *error)
+int config_parse(Context*, yyscan_t *);
+
+void Context::start() 
 {
-  parser_abort(context, error);
+  config_parse(this, scanner);
 }
 
 %}
 
 %token COMMENT
+%token END
+%token ERROR
+%token RESET
+%token PARSER_DEBUG
+%token INCLUDE
 %token CONFIGURE_FILE
 %token EMPTY_LINE
 %token SERVER
 %token SERVERS
+%token SERVERS_OPTION
 %token UNKNOWN_OPTION
 %token UNKNOWN
 
@@ -88,14 +88,12 @@ inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanne
 %token BUFFER_REQUESTS
 %token CACHE_LOOKUPS
 %token CONNECT_TIMEOUT
-%token _CORK
 %token DISTRIBUTION
 %token HASH
 %token HASH_WITH_PREFIX_KEY
 %token IO_BYTES_WATERMARK
 %token IO_KEY_PREFETCH
 %token IO_MSG_WATERMARK
-%token KETAMA
 %token KETAMA_HASH
 %token KETAMA_WEIGHTED
 %token NOREPLY
@@ -136,6 +134,10 @@ inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanne
 %token MODULA
 %token RANDOM
 
+/* Boolean values */
+%token <boolean> TRUE
+%token <boolean> FALSE
+
 %nonassoc ','
 %nonassoc '='
 
@@ -147,6 +149,7 @@ inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanne
 %token <string> IPADDRESS_WITH_PORT
 %token <string> STRING
 %token <string> QUOTED_STRING
+%token <string> FILE_PATH
 
 %type <server> server
 %type <string> string
@@ -157,68 +160,100 @@ inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanne
 
 %%
 
+begin:
+          statement
+        | begin ' ' statement
+        ;
+
 statement:
          expression
           { }
-        | statement ' ' expression
-          { }
         | COMMENT
           { }
         | EMPTY_LINE
           { }
+        | END
+          {
+            context->set_end();
+            YYACCEPT;
+          }
+        | ERROR
+          {
+            context->rc= MEMCACHED_PARSE_USER_ERROR;
+            parser_abort(context, NULL);
+          }
+        | RESET
+          {
+            memcached_reset(context->memc);
+          }
+        | PARSER_DEBUG
+          {
+            yydebug= 1;
+          }
+        | INCLUDE ' ' string
+          {
+            if ((context->rc= memcached_parse_configure_file(context->memc, $3.c_str, $3.length)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);
+            }
+          }
         ;
 
 
 expression:
-          SERVER '=' server
+          SERVER server
           { 
-            if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_server_add_parsed(context->memc, $2.c_str, $2.length, $2.port, 0)) != MEMCACHED_SUCCESS)
             {
               parser_abort(context, NULL);
             }
           }
-        | SERVERS '=' server_list
+        | SERVERS_OPTION server_list
           {
           }
-        | CONFIGURE_FILE '=' string
+        | CONFIGURE_FILE string
           {
-            memcached_set_configuration_file(context->memc, $3.c_str, $3.length);
+            memcached_set_configuration_file(context->memc, $2.c_str, $2.length);
           }
         | behaviors
         ;
 
 behaviors:
-          PREFIX_KEY '=' string
+          PREFIX_KEY string
           {
-            if ((context->rc= memcached_callback_set(context->memc, MEMCACHED_CALLBACK_PREFIX_KEY, std::string($3.c_str, $3.length).c_str())) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_set_prefix_key(context->memc, $2.c_str, $2.length)) != MEMCACHED_SUCCESS)
             {
               parser_abort(context, NULL);;
             }
           }
-        | DISTRIBUTION '=' distribution
+        | DISTRIBUTION distribution
           {
-            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3)) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
             {
               parser_abort(context, NULL);;
             }
           }
-        | HASH '=' hash
+        | DISTRIBUTION distribution ',' hash
           {
-            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $3)) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $2)) != MEMCACHED_SUCCESS)
             {
-              parser_abort(context, NULL);; 
+              parser_abort(context, NULL);;
+            }
+            if ((context->rc= memcached_behavior_set_distribution_hash(context->memc, $4)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
             }
           }
-        | KETAMA_HASH '=' hash
+        | HASH hash
           {
-            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3)) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $2)) != MEMCACHED_SUCCESS)
             {
-              parser_abort(context, NULL);;
+              parser_abort(context, NULL);; 
             }
           }
-        | behavior_number '=' NUMBER
+        | behavior_number NUMBER
           {
-            if ((context->rc= memcached_behavior_set(context->memc, $1, $3)) != MEMCACHED_SUCCESS)
+            if ((context->rc= memcached_behavior_set(context->memc, $1, $2)) != MEMCACHED_SUCCESS)
             {
               parser_abort(context, NULL);;
             }
@@ -303,18 +338,10 @@ behavior_boolean:
           {
             $$= MEMCACHED_BEHAVIOR_CACHE_LOOKUPS;
           }
-        | _CORK
-          {
-            $$= MEMCACHED_BEHAVIOR_CORK;
-          }
         | HASH_WITH_PREFIX_KEY
           {
             $$= MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY;
           }
-        | KETAMA
-          {
-            $$= MEMCACHED_BEHAVIOR_KETAMA;
-          }
         | KETAMA_WEIGHTED
           {
             $$= MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
@@ -378,7 +405,7 @@ server:
           HOSTNAME_WITH_PORT NUMBER
           {
             $$.c_str= $1.c_str;
-            $$.length= $1.length -1;
+            $$.length= $1.length -1; // -1 to remove :
             $$.port= $2;
           }
         | HOSTNAME
@@ -396,7 +423,7 @@ server:
         | IPADDRESS_WITH_PORT NUMBER
           {
             $$.c_str= $1.c_str;
-            $$.length= $1.length -1;
+            $$.length= $1.length -1; // -1 to remove :
             $$.port= $2;
           }
         | IPADDRESS
@@ -453,8 +480,8 @@ string:
           }
         | QUOTED_STRING
           {
-            $$.c_str= $1.c_str +1;
-            $$.length= $1.length -2;
+            $$.c_str= $1.c_str +1; // +1 to move use passed the initial quote
+            $$.length= $1.length -1; // -1 removes the end quote
           }
         ;