More fiddling with the automake stuff.
[liboconfig.git] / src / parser.y
index a4ebce0..7f048d0 100644 (file)
@@ -2,21 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include "oconfig.h"
+#include "aux_types.h"
 
-struct statement_list_s
-{
-       oconfig_item_t *statement;
-       int             statement_num;
-};
-typedef struct statement_list_s statement_list_t;
-
-struct argument_list_s
-{
-       oconfig_value_t *argument;
-       int              argument_num;
-};
-typedef struct argument_list_s argument_list_t;
-
+static char *unquote (const char *orig);
 static void dump_ci (oconfig_item_t *ci, int shift);
 %}
 
@@ -50,11 +38,12 @@ static void dump_ci (oconfig_item_t *ci, int shift);
 %type <ci> option
 %type <ci> statement
 %type <sl> statement_list
+%type <ci> entire_file
 
 %%
 string:
-       QUOTED_STRING           {$$ = $1;}
-       | UNQUOTED_STRING       {$$ = $1;}
+       QUOTED_STRING           {$$ = unquote ($1);}
+       | UNQUOTED_STRING       {$$ = strdup ($1);}
        ;
 
 argument:
@@ -81,7 +70,7 @@ argument_list:
        ;
 
 identifier:
-       UNQUOTED_STRING                 {$$ = $1;}
+       UNQUOTED_STRING                 {$$ = strdup ($1);}
        ;
 
 option:
@@ -115,7 +104,11 @@ block:
        block_begin statement_list block_end
        {
         if (strcmp ($1.key, $3) != 0)
-               yyerror ("Block %s not closed..\n", $1);
+        {
+               printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
+               yyerror ("Block not closed..\n");
+               exit (1);
+        }
         $$ = $1;
         $$.children = $2.statement;
         $$.children_num = $2.statement_num;
@@ -125,54 +118,78 @@ block:
 statement:
        option          {$$ = $1;}
        | block         {$$ = $1;}
-       | EOL           {/* ignore */}
+       | EOL           {$$.values_num = 0;}
        ;
 
 statement_list:
        statement_list statement
        {
         $$ = $1;
-        $$.statement_num++;
-        $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
-        $$.statement[$$.statement_num-1] = $2;
+        if ($2.values_num > 0)
+        {
+                $$.statement_num++;
+                $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
+                $$.statement[$$.statement_num-1] = $2;
+        }
        }
        | statement
        {
-        $$.statement = malloc (sizeof (oconfig_item_t));
-        $$.statement[0] = $1;
-        $$.statement_num = 1;
+        if ($1.values_num > 0)
+        {
+                $$.statement = malloc (sizeof (oconfig_item_t));
+                $$.statement[0] = $1;
+                $$.statement_num = 1;
+        }
+        else
+        {
+               $$.statement = NULL;
+               $$.statement_num = 0;
+        }
        }
        ;
 
 entire_file:
        statement_list
        {
-        int i;
-        for (i = 0; i < $1.statement_num; i++)
-               dump_ci ($1.statement + i, 0);
+        $$.children = $1.statement;
+        $$.children_num = $1.statement_num;
        }
        ;
 
 %%
-#include "lex.yy.c"
+#if 0
+#include "scanner.c"
+#endif
 
-/*
-void yyerror (char *s)
+static char *unquote (const char *orig)
 {
-       fprintf (stderr, "%s\n", s);
-}
+       char *ret = strdup (orig);
+       int len;
+       int i;
 
-int yylex (void)
-{
-       return (getc (stdin));
-}
-*/
+       if (ret == NULL)
+               return (NULL);
 
-int main (int argc, char **argv)
-{
-       yyparse ();
-       return (0);
-}
+       len = strlen (ret);
+
+       if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
+               return (ret);
+
+       ret++;
+       len -= 2;
+       ret[len] = '\0';
+
+       for (i = 0; i < len; i++)
+       {
+               if (ret[i] == '\\')
+               {
+                       memmove (ret + i, ret + (i + 1), len - i);
+                       len--;
+               }
+       }
+
+       return (ret);
+} /* char *unquote */
 
 static void dump_ci (oconfig_item_t *ci, int shift)
 {