2 Copyright (c) 2014, Intel Corporation
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
16 * Neither the name of Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <murphy/common/macros.h>
41 #include <murphy/common/log.h>
43 #include "ini-parser.h"
45 static void yyerror(ini_parser_context_t *ctx, void *yyscanner, const char *err);
47 /* this has always to be of type yyscan_t */
48 #define YYLEX_PARAM scanner
55 %parse-param { ini_parser_context_t *ctx }
56 %parse-param { void *scanner }
69 %token <string> STRING
70 %token <string> BOOLEAN
71 /* %token <number> NUMBER */
72 %token <string> NUMBER
79 sections : section sections
83 section : sectionname sectionitems
87 sectionname : OBRACKET WORD EBRACKET { add_section(ctx->r, $2); }
90 sectionitems : sectionitem sectionitems
94 sectionitem : keyvaluepair
97 keyvaluepair : key EQUAL value
100 key : keybase OBRACKET modifier EBRACKET
104 keybase : WORD { add_key(ctx->r, $1); }
107 modifier : WORD { add_modifier(ctx->r, $1); }
110 value : STRING { add_value(ctx->r, $1); }
111 | BOOLEAN { add_value(ctx->r, $1); }
112 | NUMBER { add_value(ctx->r, $1); }
117 static void yyerror(ini_parser_context_t *ctx, void *yyscanner, const char *err)
119 MRP_UNUSED(yyscanner);
120 mrp_log_error("ini file parser error: '%s'", err);
125 bool mrp_parse_ini_file(const char *filename, ini_parser_result_t *result) {
127 /* contains the buffers required for saving the string data */
128 ini_parser_context_t ctx;
131 /* contains the flex-initialized parser data */
134 input = fopen(filename, "r");
139 initialize_parser_buffer(&ctx);
143 ini_parser_lex_init(&scanner);
145 /* set the input file */
146 ini_parser_set_in(input, scanner);
148 /* set the parameter to be passed to flex (yyextra) */
149 ini_parser_set_extra(&ctx, scanner);
151 ini_parser_parse(&ctx, scanner);
156 ini_parser_lex_destroy(scanner);
157 delete_parser_buffer(&ctx);