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 */
19 %{
20 #include <stdlib.h>
21 #include <string.h>
22 #include "oconfig.h"
23 #include "aux_types.h"
25 static char *unquote (const char *orig);
26 static int yyerror (const char *s);
28 /* Lexer variables */
29 extern int yylineno;
30 extern char *yytext;
32 extern oconfig_item_t *ci_root;
33 %}
35 %start entire_file
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 }
47 %token <number> NUMBER
48 %token <boolean> TRUE FALSE
49 %token <string> QUOTED_STRING UNQUOTED_STRING
50 %token SLASH OPENBRAC CLOSEBRAC EOL
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
67 %%
68 string:
69 QUOTED_STRING {$$ = unquote ($1);}
70 | UNQUOTED_STRING {$$ = strdup ($1);}
71 ;
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 ;
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 ;
96 identifier:
97 UNQUOTED_STRING {$$ = strdup ($1);}
98 ;
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 ;
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 ;
120 block_end:
121 OPENBRAC SLASH identifier CLOSEBRAC EOL
122 {
123 $$ = $3;
124 }
125 ;
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 free ($3); $3 = NULL;
137 $$ = $1;
138 $$.children = $2.statement;
139 $$.children_num = $2.statement_num;
140 }
141 ;
143 statement:
144 option {$$ = $1;}
145 | block {$$ = $1;}
146 | EOL {$$.values_num = 0;}
147 ;
149 statement_list:
150 statement_list statement
151 {
152 $$ = $1;
153 if ($2.values_num > 0)
154 {
155 $$.statement_num++;
156 $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
157 $$.statement[$$.statement_num-1] = $2;
158 }
159 }
160 | statement
161 {
162 if ($1.values_num > 0)
163 {
164 $$.statement = malloc (sizeof (oconfig_item_t));
165 $$.statement[0] = $1;
166 $$.statement_num = 1;
167 }
168 else
169 {
170 $$.statement = NULL;
171 $$.statement_num = 0;
172 }
173 }
174 ;
176 entire_file:
177 statement_list
178 {
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;
183 }
184 ;
186 %%
187 static int yyerror (const char *s)
188 {
189 fprintf (stderr, "Error in line %i near `%s': %s\n", yylineno, yytext, s);
190 return (-1);
191 } /* int yyerror */
193 static char *unquote (const char *orig)
194 {
195 char *ret = strdup (orig);
196 int len;
197 int i;
199 if (ret == NULL)
200 return (NULL);
202 len = strlen (ret);
204 if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
205 return (ret);
207 ret++;
208 len -= 2;
209 ret[len] = '\0';
211 for (i = 0; i < len; i++)
212 {
213 if (ret[i] == '\\')
214 {
215 memmove (ret + i, ret + (i + 1), len - i);
216 len--;
217 }
218 }
220 return (ret);
221 } /* char *unquote */