2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * @defgroup parser_helper parser_helper
21 #ifndef PARSER_HELPER_H_
22 #define PARSER_HELPER_H_
24 #include <collections/array.h>
25 #include <bio/bio_writer.h>
27 typedef struct parser_helper_t parser_helper_t
;
30 * Helper class for flex/bison based parsers.
32 * <code>PREFIX</code> equals whatever is configure with
33 * <code>%option prefix</code> resp. <code>%name-prefix</code>.
35 struct parser_helper_t
{
38 * A user defined parser context object.
43 * Opaque object allocated by the lexer, should be set with:
45 * PREFIXlex_init_extra(helper, &helper->scanner).
51 * Function to determine the current line number (defined by the lexer).
53 * Basically, this should be assigned to <code>PREFIXget_lineno</code>.
55 * @param scanner the lexer
56 * @return current line number
58 int (*get_lineno
)(void *scanner
);
61 * Resolves the given include pattern, relative to the location of the
64 * Call file_next() to open the next file.
66 * @param pattern file pattern
68 void (*file_include
)(parser_helper_t
*this, char *pattern
);
71 * Get the next file to process.
73 * This will return NULL if all files matching the most recent pattern
74 * have been handled. If there are other patterns the next call will then
75 * return the next file matching the previous pattern.
77 * When hitting <code>\<\<EOF\>\></code> first call
79 * PREFIXpop_buffer_state(yyscanner);
81 * then call this method to check if there are more files to include for
82 * the most recent call to file_include(), if so, call
84 * PREFIXset_in(file, helper->scanner);
85 * PREFIXpush_buffer_state(PREFIX_create_buffer(file, YY_BUF_SIZE,
86 * helper->scanner), helper->scanner);
89 * If there are no more files to process check
90 * <code>YY_CURRENT_BUFFER</code> and if it is FALSE call yyterminate().
92 * @return next file to process, or NULL (see comment)
94 FILE *(*file_next
)(parser_helper_t
*this);
97 * Start parsing a string, discards any currently stored data.
99 void (*string_init
)(parser_helper_t
*this);
102 * Append the given string.
104 * @param str string to append
106 void (*string_add
)(parser_helper_t
*this, char *str
);
109 * Extract the current string buffer as null-terminated string. Can only
110 * be called once per string.
112 * @return allocated string
114 char *(*string_get
)(parser_helper_t
*this);
117 * Destroy this instance.
119 void (*destroy
)(parser_helper_t
*this);
123 * Log the given message either as error or warning
125 * @param level log level
126 * @param ctx current parser context
127 * @param fmt error message format
128 * @param ... additional arguments
130 void parser_helper_log(int level
, parser_helper_t
*ctx
, char *fmt
, ...);
132 #define PARSER_DBG1(ctx, fmt, ...) parser_helper_log(1, ctx, fmt, ##__VA_ARGS__)
133 #define PARSER_DBG2(ctx, fmt, ...) parser_helper_log(2, ctx, fmt, ##__VA_ARGS__)
134 #define PARSER_DBG3(ctx, fmt, ...) parser_helper_log(3, ctx, fmt, ##__VA_ARGS__)
137 * Create a parser helper object
139 * @param context user defined parser context
140 * @return parser helper
142 parser_helper_t
*parser_helper_create(void *context
);
144 #endif /** PARSER_HELPER_H_ @}*/