2 * collectd - src/liboconfig/parser.y
3 * Copyright (C) 2007,2008 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
31 #include "aux_types.h"
33 static char *unquote (const char *orig);
34 static int yyerror (const char *s);
40 extern oconfig_item_t *ci_root;
56 %token <number> NUMBER
57 %token <boolean> BTRUE BFALSE
58 %token <string> QUOTED_STRING UNQUOTED_STRING
59 %token SLASH OPENBRAC CLOSEBRAC EOL
62 %type <string> identifier
65 %type <al> argument_list
67 %type <ci> block_begin
69 %type <string> block_end
73 %type <sl> statement_list
74 %type <ci> entire_file
76 /* pass an verbose, specific error message to yyerror() */
81 QUOTED_STRING {$$ = unquote ($1);}
82 | UNQUOTED_STRING {$$ = strdup ($1);}
86 NUMBER {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
87 | BTRUE {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
88 | BFALSE {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
89 | string {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
93 argument_list argument
97 $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
98 $$.argument[$$.argument_num-1] = $2;
102 $$.argument = malloc (sizeof (oconfig_value_t));
109 UNQUOTED_STRING {$$ = strdup ($1);}
113 identifier argument_list EOL
115 memset (&$$, '\0', sizeof ($$));
117 $$.values = $2.argument;
118 $$.values_num = $2.argument_num;
123 OPENBRAC identifier CLOSEBRAC EOL
125 memset (&$$, '\0', sizeof ($$));
129 OPENBRAC identifier argument_list CLOSEBRAC EOL
131 memset (&$$, '\0', sizeof ($$));
133 $$.values = $3.argument;
134 $$.values_num = $3.argument_num;
139 OPENBRAC SLASH identifier CLOSEBRAC EOL
146 block_begin statement_list block_end
148 if (strcmp ($1.key, $3) != 0)
150 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
151 yyerror ("Block not closed..\n");
154 free ($3); $3 = NULL;
156 $$.children = $2.statement;
157 $$.children_num = $2.statement_num;
159 | block_begin block_end
161 if (strcmp ($1.key, $2) != 0)
163 printf ("block_begin = %s; block_end = %s;\n", $1.key, $2);
164 yyerror ("Block not closed..\n");
167 free ($2); $2 = NULL;
177 | EOL {$$.values_num = 0;}
181 statement_list statement
184 if (($2.values_num > 0) || ($2.children_num > 0))
187 $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
188 $$.statement[$$.statement_num-1] = $2;
193 if (($1.values_num > 0) || ($1.children_num > 0))
195 $$.statement = malloc (sizeof (oconfig_item_t));
196 $$.statement[0] = $1;
197 $$.statement_num = 1;
202 $$.statement_num = 0;
210 ci_root = malloc (sizeof (oconfig_item_t));
211 memset (ci_root, '\0', sizeof (oconfig_item_t));
212 ci_root->children = $1.statement;
213 ci_root->children_num = $1.statement_num;
217 ci_root = malloc (sizeof (oconfig_item_t));
218 memset (ci_root, '\0', sizeof (oconfig_item_t));
219 ci_root->children = NULL;
220 ci_root->children_num = 0;
225 static int yyerror (const char *s)
234 fprintf (stderr, "Parse error in file `%s', line %i near `%s': %s\n",
235 c_file, yylineno, text, s);
239 static char *unquote (const char *orig)
241 char *ret = strdup (orig);
250 if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
254 memmove (ret, ret + 1, len);
257 for (i = 0; i < len; i++)
261 memmove (ret + i, ret + (i + 1), len - i);
267 } /* char *unquote */