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);
39 extern int yylex (void);
41 extern oconfig_item_t *ci_root;
57 %token <number> NUMBER
58 %token <boolean> BTRUE BFALSE
59 %token <string> QUOTED_STRING UNQUOTED_STRING
60 %token SLASH OPENBRAC CLOSEBRAC EOL
63 %type <string> identifier
66 %type <al> argument_list
68 %type <ci> block_begin
70 %type <string> block_end
74 %type <sl> statement_list
75 %type <ci> entire_file
77 /* pass an verbose, specific error message to yyerror() */
82 QUOTED_STRING {$$ = unquote ($1);}
83 | UNQUOTED_STRING {$$ = strdup ($1);}
87 NUMBER {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
88 | BTRUE {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
89 | BFALSE {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
90 | string {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
94 argument_list argument
98 $$.argument = realloc ($$.argument, $$.argument_num * sizeof (oconfig_value_t));
99 $$.argument[$$.argument_num-1] = $2;
103 $$.argument = malloc (sizeof (oconfig_value_t));
110 UNQUOTED_STRING {$$ = strdup ($1);}
114 identifier argument_list EOL
116 memset (&$$, '\0', sizeof ($$));
118 $$.values = $2.argument;
119 $$.values_num = $2.argument_num;
124 OPENBRAC identifier CLOSEBRAC EOL
126 memset (&$$, '\0', sizeof ($$));
130 OPENBRAC identifier argument_list CLOSEBRAC EOL
132 memset (&$$, '\0', sizeof ($$));
134 $$.values = $3.argument;
135 $$.values_num = $3.argument_num;
140 OPENBRAC SLASH identifier CLOSEBRAC EOL
147 block_begin statement_list block_end
149 if (strcmp ($1.key, $3) != 0)
151 printf ("block_begin = %s; block_end = %s;\n", $1.key, $3);
152 yyerror ("Block not closed..\n");
155 free ($3); $3 = NULL;
157 $$.children = $2.statement;
158 $$.children_num = $2.statement_num;
160 | block_begin block_end
162 if (strcmp ($1.key, $2) != 0)
164 printf ("block_begin = %s; block_end = %s;\n", $1.key, $2);
165 yyerror ("Block not closed..\n");
168 free ($2); $2 = NULL;
178 | EOL {$$.values_num = 0;}
182 statement_list statement
185 if (($2.values_num > 0) || ($2.children_num > 0))
188 $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t));
189 $$.statement[$$.statement_num-1] = $2;
194 if (($1.values_num > 0) || ($1.children_num > 0))
196 $$.statement = malloc (sizeof (oconfig_item_t));
197 $$.statement[0] = $1;
198 $$.statement_num = 1;
203 $$.statement_num = 0;
211 ci_root = calloc (1, sizeof (*ci_root));
212 ci_root->children = $1.statement;
213 ci_root->children_num = $1.statement_num;
217 ci_root = calloc (1, sizeof (*ci_root));
218 ci_root->children = NULL;
219 ci_root->children_num = 0;
224 static int yyerror (const char *s)
233 fprintf (stderr, "Parse error in file `%s', line %i near `%s': %s\n",
234 c_file, yylineno, text, s);
238 static char *unquote (const char *orig)
240 char *ret = strdup (orig);
249 if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"'))
253 memmove (ret, ret + 1, len);
256 for (i = 0; i < len; i++)
260 memmove (ret + i, ret + (i + 1), len - i);
266 } /* char *unquote */