2 * oconfig - src/parser.y
3 * Copyright (C) 2007 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;
47 %token <number> NUMBER
48 %token <boolean> TRUE FALSE
49 %token <string> QUOTED_STRING UNQUOTED_STRING
50 %token SLASH OPENBRAC CLOSEBRAC EOL
53 %type <string> identifier
56 %type <al> argument_list
58 %type <ci> block_begin
60 %type <string> block_end
64 %type <sl> statement_list
65 %type <ci> entire_file
69 QUOTED_STRING {$$ = unquote ($1);}
70 | UNQUOTED_STRING {$$ = strdup ($1);}
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;}
81 argument_list argument
85 $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
86 $$.argument[$$.argument_num-1] = $2;
90 $$.argument = malloc (sizeof (oconfig_value_t));
97 UNQUOTED_STRING {$$ = strdup ($1);}
101 identifier argument_list EOL
103 memset (&$$, '\0', sizeof ($$));
105 $$.values = $2.argument;
106 $$.values_num = $2.argument_num;
111 OPENBRAC identifier argument_list CLOSEBRAC EOL
113 memset (&$$, '\0', sizeof ($$));
115 $$.values = $3.argument;
116 $$.values_num = $3.argument_num;
121 OPENBRAC SLASH identifier CLOSEBRAC EOL
128 block_begin statement_list block_end
130 if (strcmp ($1.key, $3) != 0)
132 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
133 yyerror ("Block not closed..\n");
136 free ($3); $3 = NULL;
138 $$.children = $2.statement;
139 $$.children_num = $2.statement_num;
146 | EOL {$$.values_num = 0;}
150 statement_list statement
153 if ($2.values_num > 0)
156 $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
157 $$.statement[$$.statement_num-1] = $2;
162 if ($1.values_num > 0)
164 $$.statement = malloc (sizeof (oconfig_item_t));
165 $$.statement[0] = $1;
166 $$.statement_num = 1;
171 $$.statement_num = 0;
179 ci_root = malloc (sizeof (oconfig_item_t));
180 memset (ci_root, '\0', sizeof (oconfig_item_t));
181 ci_root->children = $1.statement;
182 ci_root->children_num = $1.statement_num;
187 static int yyerror (const char *s)
189 fprintf (stderr, "Error in line %i near `%s': %s\n", yylineno, yytext, s);
193 static char *unquote (const char *orig)
195 char *ret = strdup (orig);
204 if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
211 for (i = 0; i < len; i++)
215 memmove (ret + i, ret + (i + 1), len - i);
221 } /* char *unquote */