2 * oconfig - src/parser.y
3 * Copyright (C) 2007,2008 Florian octo Forster <octo at verplant.org>
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.
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
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
23 #include "aux_types.h"
25 static char *unquote (const char *orig);
26 static int yyerror (const char *s);
32 extern oconfig_item_t *ci_root;
48 %token <number> NUMBER
49 %token <boolean> BTRUE BFALSE
50 %token <string> QUOTED_STRING UNQUOTED_STRING
51 %token SLASH OPENBRAC CLOSEBRAC EOL
54 %type <string> identifier
57 %type <al> argument_list
59 %type <ci> block_begin
61 %type <string> block_end
65 %type <sl> statement_list
66 %type <ci> entire_file
68 /* pass an verbose, specific error message to yyerror() */
73 QUOTED_STRING {$$ = unquote ($1);}
74 | UNQUOTED_STRING {$$ = strdup ($1);}
78 NUMBER {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
79 | BTRUE {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
80 | BFALSE {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
81 | string {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
85 argument_list argument
89 $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
90 $$.argument[$$.argument_num-1] = $2;
94 $$.argument = malloc (sizeof (oconfig_value_t));
101 UNQUOTED_STRING {$$ = strdup ($1);}
105 identifier argument_list EOL
107 memset (&$$, '\0', sizeof ($$));
109 $$.values = $2.argument;
110 $$.values_num = $2.argument_num;
115 OPENBRAC identifier CLOSEBRAC EOL
117 memset (&$$, '\0', sizeof ($$));
121 OPENBRAC identifier argument_list CLOSEBRAC EOL
123 memset (&$$, '\0', sizeof ($$));
125 $$.values = $3.argument;
126 $$.values_num = $3.argument_num;
131 OPENBRAC SLASH identifier CLOSEBRAC EOL
138 block_begin statement_list block_end
140 if (strcmp ($1.key, $3) != 0)
142 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
143 yyerror ("Block not closed..\n");
146 free ($3); $3 = NULL;
148 $$.children = $2.statement;
149 $$.children_num = $2.statement_num;
156 | EOL {$$.values_num = 0;}
160 statement_list statement
163 if (($2.values_num > 0) || ($2.children_num > 0))
166 $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
167 $$.statement[$$.statement_num-1] = $2;
172 if (($1.values_num > 0) || ($1.children_num > 0))
174 $$.statement = malloc (sizeof (oconfig_item_t));
175 $$.statement[0] = $1;
176 $$.statement_num = 1;
181 $$.statement_num = 0;
189 ci_root = malloc (sizeof (oconfig_item_t));
190 memset (ci_root, '\0', sizeof (oconfig_item_t));
191 ci_root->children = $1.statement;
192 ci_root->children_num = $1.statement_num;
197 static int yyerror (const char *s)
206 fprintf (stderr, "Parse error in file `%s', line %i near `%s': %s\n",
207 c_file, yylineno, text, s);
211 static char *unquote (const char *orig)
213 char *ret = strdup (orig);
222 if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
226 memmove (ret, ret + 1, len);
229 for (i = 0; i < len; i++)
233 memmove (ret + i, ret + (i + 1), len - i);
239 } /* char *unquote */