Fix problem where hostname would end up with trailing . and be accepted as
[m6w6/libmemcached] / libmemcached / options / parser.yy
index 3a7cee2eed170492600305dba1d031c33aee1fa7..dd19ef8b52d3c98a58722b6fcaff2f3a3b24d645 100644 (file)
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+%error-verbose
+%debug
+%defines
+%expect 0
+%output "libmemcached/options/parser.cc"
+%defines "libmemcached/options/parser.h"
+%lex-param { yyscan_t *scanner }
+%name-prefix="libmemcached_"
+%parse-param { Context *context }
+%parse-param { yyscan_t *scanner }
+%locations
+%pure-parser
+%require "2.2"
+%start begin
+%verbose
+
 %{
 
+#include <config.h>
+
 #include <stdint.h>
 #include <iostream>
+#include <sstream>
+#include <string>
 
-#include <libmemcached/options/type.h>
+#include <libmemcached/options/context.h>
 #include <libmemcached/options/string.h>
 #include <libmemcached/options/symbol.h>
 
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 #include <libmemcached/options/scanner.h>
 
-inline int libmemcached_error(YYLTYPE *locp, type_st *parser, yyscan_t *scanner, const char *str)
+int libmemcached_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
+
+#define parser_abort(A, B) do { parser_abort_func((A), (B)); YYABORT; } while (0) 
+
+inline void parser_abort_func(Context *context, const char *error)
 {
-#if 0
-  std::cerr << str << std::endl;
-#endif
-  return 0;
+  (void)error;
+  if (context->rc == MEMCACHED_SUCCESS)
+    context->rc= MEMCACHED_PARSE_ERROR;
+
+  std::string error_message;
+  error_message+= context->begin;
+  error_message+= " (";
+  if (context->rc == MEMCACHED_PARSE_ERROR and error)
+  {
+    error_message+= error;
+  }
+  else
+  {
+    error_message+= memcached_strerror(NULL, context->rc);
+  }
+  error_message+= ")";
+
+  memcached_set_error_string(context->memc, context->rc, error_message.c_str(), error_message.size());
 }
 
+inline void libmemcached_error(YYLTYPE *locp, Context *context, yyscan_t *scanner, const char *error)
+{
+  if (not context->end())
+    parser_abort_func(context, error);
+}
 
 %}
 
-%error-verbose
-%debug
-%defines
-%expect 0
-%output "libmemcached/options/parser.cc"
-%defines "libmemcached/options/parser.h"
-%lex-param { yyscan_t *scanner }
-%name-prefix="libmemcached_"
-%parse-param { type_st *parser }
-%parse-param { yyscan_t *scanner }
-%locations
-%pure-parser
-%require "2.2"
-%start statement
-%verbose
-
+%token COMMENT
+%token END
+%token RESET
+%token DEBUG
+%token INCLUDE
+%token CONFIGURE_FILE
+%token EMPTY_LINE
 %token SERVER
 %token SERVERS
+%token SERVERS_OPTION
+%token UNKNOWN_OPTION
 %token UNKNOWN
 
-%token DASH_OPTION
-
 /* All behavior options */
 %token AUTO_EJECT_HOSTS
 %token BINARY_PROTOCOL
@@ -98,6 +132,9 @@ inline int libmemcached_error(YYLTYPE *locp, type_st *parser, yyscan_t *scanner,
 %token USE_UDP
 %token VERIFY_KEY
 
+/* Callbacks */
+%token PREFIX_KEY
+
 /* Hash types */
 %token MD5
 %token CRC
@@ -114,17 +151,25 @@ inline int libmemcached_error(YYLTYPE *locp, type_st *parser, yyscan_t *scanner,
 %token MODULA
 %token RANDOM
 
+/* Boolean values */
+%token <boolean> TRUE
+%token <boolean> FALSE
+
 %nonassoc ','
 %nonassoc '='
 
 %token <number> NUMBER
 %token <number> FLOAT
-%token <string> IDENTIFIER
-%token <string> SERVER_WITH_PORT
+%token <string> HOSTNAME
+%token <string> HOSTNAME_WITH_PORT
 %token <string> IPADDRESS
 %token <string> IPADDRESS_WITH_PORT
+%token <string> STRING
+%token <string> QUOTED_STRING
+%token <string> FILE_PATH
 
 %type <server> server
+%type <string> string
 %type <distribution> distribution
 %type <hash> hash
 %type <behavior> behavior_boolean
@@ -132,45 +177,114 @@ inline int libmemcached_error(YYLTYPE *locp, type_st *parser, yyscan_t *scanner,
 
 %%
 
+begin:
+          statement
+        | begin ' ' statement
+        ;
+
 statement:
-         DASH_OPTION expression
+         expression
           { }
-        | statement ' ' DASH_OPTION expression
+        | COMMENT
           { }
+        | EMPTY_LINE
+          { }
+        | END
+          {
+            context->set_end();
+            YYACCEPT;
+          }
+        | RESET
+          {
+            memcached_reset(context->memc);
+          }
+        | RESET SERVERS
+          {
+            memcached_servers_reset(context->memc);
+          }
+        | DEBUG
+          {
+            yydebug= 1;
+          }
+        | DEBUG TRUE
+          {
+            yydebug= 1;
+          }
+        | DEBUG FALSE
+          {
+            yydebug= 0;
+          }
+        | INCLUDE FILE_PATH
+          {
+            std::cerr << "Got into INCLUDE" << std::endl;
+            if ((context->rc= memcached_parse_configure_file(context->memc, $2.c_str, $2.length)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);
+            }
+          }
         ;
 
 
 expression:
           SERVER '=' server
           { 
-            (void) memcached_server_add_parsed(parser->memc, $3.c_str, $3.length, $3.port, 0);
+            if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);
+            }
           }
-        | SERVERS '=' server_list
+        | SERVERS_OPTION '=' server_list
           {
           }
+        | CONFIGURE_FILE '=' string
+          {
+            memcached_set_configuration_file(context->memc, $3.c_str, $3.length);
+          }
         | behaviors
         ;
 
 behaviors:
+          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)
+            {
+              parser_abort(context, NULL);;
+            }
+          }
         | DISTRIBUTION '=' distribution
           {
-            memcached_behavior_set(parser->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3);
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         | HASH '=' hash
           {
-            memcached_behavior_set(parser->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, $3);
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_HASH, $3)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);; 
+            }
           }
         | KETAMA_HASH '=' hash
           {
-            memcached_behavior_set(parser->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3);
+            if ((context->rc= memcached_behavior_set(context->memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, $3)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         | behavior_number '=' NUMBER
           {
-            memcached_behavior_set(parser->memc, $1, $3);
+            if ((context->rc= memcached_behavior_set(context->memc, $1, $3)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         | behavior_boolean
           {
-            memcached_behavior_set(parser->memc, $1, true);
+            if ((context->rc= memcached_behavior_set(context->memc, $1, true)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         |  USER_DATA
           {
@@ -302,22 +416,34 @@ behavior_boolean:
 server_list:
            server
           {
-            (void) memcached_server_add_parsed(parser->memc, $1.c_str, $1.length, $1.port, 0);
+            if ((context->rc= memcached_server_add_parsed(context->memc, $1.c_str, $1.length, $1.port, 0)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         | server_list ',' server
           {
-            (void) memcached_server_add_parsed(parser->memc, $3.c_str, $3.length, $3.port, 0);
+            if ((context->rc= memcached_server_add_parsed(context->memc, $3.c_str, $3.length, $3.port, 0)) != MEMCACHED_SUCCESS)
+            {
+              parser_abort(context, NULL);;
+            }
           }
         ;
 
 server:
-          SERVER_WITH_PORT NUMBER
+          HOSTNAME_WITH_PORT NUMBER
           {
             $$.c_str= $1.c_str;
             $$.length= $1.length -1;
             $$.port= $2;
           }
-        | IDENTIFIER
+        | HOSTNAME
+          {
+            $$.c_str= $1.c_str;
+            $$.length= $1.length;
+            $$.port= MEMCACHED_DEFAULT_PORT;
+          }
+        | STRING /* a match can be against "localhost" which is just a string */
           {
             $$.c_str= $1.c_str;
             $$.length= $1.length;
@@ -376,6 +502,18 @@ hash:
           }
         ;
 
+string:
+          STRING
+          {
+            $$= $1;
+          }
+        | QUOTED_STRING
+          {
+            $$.c_str= $1.c_str +1;
+            $$.length= $1.length -2;
+          }
+        ;
+
 distribution:
           CONSISTENT
           {