Adjustments of the language, added copyright notice and license information, ...
[liboconfig.git] / src / parser.y
1 /**
2  * oconfig - src/parser.y
3  * Copyright (C) 2007  Florian octo Forster <octo at verplant.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18
19 %{
20 #include <stdlib.h>
21 #include <string.h>
22 #include "oconfig.h"
23 #include "aux_types.h"
24
25 static char *unquote (const char *orig);
26 static int yyerror (const char *s);
27
28 /* Lexer variables */
29 extern int yylineno;
30 extern char *yytext;
31
32 extern oconfig_item_t *ci_root;
33 %}
34
35 %start entire_file
36
37 %union {
38         double  number;
39         int     boolean;
40         char   *string;
41         oconfig_value_t  cv;
42         oconfig_item_t   ci;
43         argument_list_t  al;
44         statement_list_t sl;
45 }
46
47 %token <number> NUMBER
48 %token <boolean> TRUE FALSE
49 %token <string> QUOTED_STRING UNQUOTED_STRING
50 %token SLASH OPENBRAC CLOSEBRAC EOL
51
52 %type <string> string
53 %type <string> identifier
54 /* arguments */
55 %type <cv> argument
56 %type <al> argument_list
57 /* blocks */
58 %type <ci> block_begin
59 %type <ci> block
60 %type <string> block_end
61 /* statements */
62 %type <ci> option
63 %type <ci> statement
64 %type <sl> statement_list
65 %type <ci> entire_file
66
67 %%
68 string:
69         QUOTED_STRING           {$$ = unquote ($1);}
70         | UNQUOTED_STRING       {$$ = strdup ($1);}
71         ;
72
73 argument:
74         NUMBER                  {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
75         | TRUE                  {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
76         | FALSE                 {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
77         | string                {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
78         ;
79
80 argument_list:
81         argument_list argument
82         {
83          $$ = $1;
84          $$.argument_num++;
85          $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
86          $$.argument[$$.argument_num-1] = $2;
87         }
88         | argument
89         {
90          $$.argument = malloc (sizeof (oconfig_value_t));
91          $$.argument[0] = $1;
92          $$.argument_num = 1;
93         }
94         ;
95
96 identifier:
97         UNQUOTED_STRING                 {$$ = strdup ($1);}
98         ;
99
100 option:
101         identifier argument_list EOL
102         {
103          memset (&$$, '\0', sizeof ($$));
104          $$.key = $1;
105          $$.values = $2.argument;
106          $$.values_num = $2.argument_num;
107         }
108         ;
109
110 block_begin:
111         OPENBRAC identifier argument_list CLOSEBRAC EOL
112         {
113          memset (&$$, '\0', sizeof ($$));
114          $$.key = $2;
115          $$.values = $3.argument;
116          $$.values_num = $3.argument_num;
117         }
118         ;
119
120 block_end:
121         OPENBRAC SLASH identifier CLOSEBRAC EOL
122         {
123          $$ = $3;
124         }
125         ;
126
127 block:
128         block_begin statement_list block_end
129         {
130          if (strcmp ($1.key, $3) != 0)
131          {
132                 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
133                 yyerror ("Block not closed..\n");
134                 exit (1);
135          }
136          $$ = $1;
137          $$.children = $2.statement;
138          $$.children_num = $2.statement_num;
139         }
140         ;
141
142 statement:
143         option          {$$ = $1;}
144         | block         {$$ = $1;}
145         | EOL           {$$.values_num = 0;}
146         ;
147
148 statement_list:
149         statement_list statement
150         {
151          $$ = $1;
152          if ($2.values_num > 0)
153          {
154                  $$.statement_num++;
155                  $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
156                  $$.statement[$$.statement_num-1] = $2;
157          }
158         }
159         | statement
160         {
161          if ($1.values_num > 0)
162          {
163                  $$.statement = malloc (sizeof (oconfig_item_t));
164                  $$.statement[0] = $1;
165                  $$.statement_num = 1;
166          }
167          else
168          {
169                 $$.statement = NULL;
170                 $$.statement_num = 0;
171          }
172         }
173         ;
174
175 entire_file:
176         statement_list
177         {
178          ci_root = malloc (sizeof (oconfig_item_t));
179          memset (ci_root, '\0', sizeof (oconfig_item_t));
180          ci_root->children = $1.statement;
181          ci_root->children_num = $1.statement_num;
182         }
183         ;
184
185 %%
186 static int yyerror (const char *s)
187 {
188         fprintf (stderr, "Error in line %i near `%s': %s\n", yylineno, yytext, s);
189         return (-1);
190 } /* int yyerror */
191
192 static char *unquote (const char *orig)
193 {
194         char *ret = strdup (orig);
195         int len;
196         int i;
197
198         if (ret == NULL)
199                 return (NULL);
200
201         len = strlen (ret);
202
203         if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
204                 return (ret);
205
206         ret++;
207         len -= 2;
208         ret[len] = '\0';
209
210         for (i = 0; i < len; i++)
211         {
212                 if (ret[i] == '\\')
213                 {
214                         memmove (ret + i, ret + (i + 1), len - i);
215                         len--;
216                 }
217         }
218
219         return (ret);
220 } /* char *unquote */