liboconfig: Include the filename in error messages.
[collectd.git] / src / liboconfig / 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 extern char           *c_file;
34 %}
35
36 %start entire_file
37
38 %union {
39         double  number;
40         int     boolean;
41         char   *string;
42         oconfig_value_t  cv;
43         oconfig_item_t   ci;
44         argument_list_t  al;
45         statement_list_t sl;
46 }
47
48 %token <number> NUMBER
49 %token <boolean> TRUE FALSE
50 %token <string> QUOTED_STRING UNQUOTED_STRING
51 %token SLASH OPENBRAC CLOSEBRAC EOL
52
53 %type <string> string
54 %type <string> identifier
55 /* arguments */
56 %type <cv> argument
57 %type <al> argument_list
58 /* blocks */
59 %type <ci> block_begin
60 %type <ci> block
61 %type <string> block_end
62 /* statements */
63 %type <ci> option
64 %type <ci> statement
65 %type <sl> statement_list
66 %type <ci> entire_file
67
68 %%
69 string:
70         QUOTED_STRING           {$$ = unquote ($1);}
71         | UNQUOTED_STRING       {$$ = strdup ($1);}
72         ;
73
74 argument:
75         NUMBER                  {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
76         | TRUE                  {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
77         | FALSE                 {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
78         | string                {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
79         ;
80
81 argument_list:
82         argument_list argument
83         {
84          $$ = $1;
85          $$.argument_num++;
86          $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
87          $$.argument[$$.argument_num-1] = $2;
88         }
89         | argument
90         {
91          $$.argument = malloc (sizeof (oconfig_value_t));
92          $$.argument[0] = $1;
93          $$.argument_num = 1;
94         }
95         ;
96
97 identifier:
98         UNQUOTED_STRING                 {$$ = strdup ($1);}
99         ;
100
101 option:
102         identifier argument_list EOL
103         {
104          memset (&$$, '\0', sizeof ($$));
105          $$.key = $1;
106          $$.values = $2.argument;
107          $$.values_num = $2.argument_num;
108         }
109         ;
110
111 block_begin:
112         OPENBRAC identifier argument_list CLOSEBRAC EOL
113         {
114          memset (&$$, '\0', sizeof ($$));
115          $$.key = $2;
116          $$.values = $3.argument;
117          $$.values_num = $3.argument_num;
118         }
119         ;
120
121 block_end:
122         OPENBRAC SLASH identifier CLOSEBRAC EOL
123         {
124          $$ = $3;
125         }
126         ;
127
128 block:
129         block_begin statement_list block_end
130         {
131          if (strcmp ($1.key, $3) != 0)
132          {
133                 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
134                 yyerror ("Block not closed..\n");
135                 exit (1);
136          }
137          free ($3); $3 = NULL;
138          $$ = $1;
139          $$.children = $2.statement;
140          $$.children_num = $2.statement_num;
141         }
142         ;
143
144 statement:
145         option          {$$ = $1;}
146         | block         {$$ = $1;}
147         | EOL           {$$.values_num = 0;}
148         ;
149
150 statement_list:
151         statement_list statement
152         {
153          $$ = $1;
154          if ($2.values_num > 0)
155          {
156                  $$.statement_num++;
157                  $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
158                  $$.statement[$$.statement_num-1] = $2;
159          }
160         }
161         | statement
162         {
163          if ($1.values_num > 0)
164          {
165                  $$.statement = malloc (sizeof (oconfig_item_t));
166                  $$.statement[0] = $1;
167                  $$.statement_num = 1;
168          }
169          else
170          {
171                 $$.statement = NULL;
172                 $$.statement_num = 0;
173          }
174         }
175         ;
176
177 entire_file:
178         statement_list
179         {
180          ci_root = malloc (sizeof (oconfig_item_t));
181          memset (ci_root, '\0', sizeof (oconfig_item_t));
182          ci_root->children = $1.statement;
183          ci_root->children_num = $1.statement_num;
184         }
185         ;
186
187 %%
188 static int yyerror (const char *s)
189 {
190         char *text;
191
192         if (*yytext == '\n')
193                 text = "<newline>";
194         else
195                 text = yytext;
196
197         fprintf (stderr, "Parse error in file `%s', line %i near `%s': %s\n",
198                 c_file, yylineno, text, s);
199         return (-1);
200 } /* int yyerror */
201
202 static char *unquote (const char *orig)
203 {
204         char *ret = strdup (orig);
205         int len;
206         int i;
207
208         if (ret == NULL)
209                 return (NULL);
210
211         len = strlen (ret);
212
213         if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
214                 return (ret);
215
216         ret++;
217         len -= 2;
218         ret[len] = '\0';
219
220         for (i = 0; i < len; i++)
221         {
222                 if (ret[i] == '\\')
223                 {
224                         memmove (ret + i, ret + (i + 1), len - i);
225                         len--;
226                 }
227         }
228
229         return (ret);
230 } /* char *unquote */