parser-helper: Define debug macros depending on DEBUG_LEVEL
[strongswan.git] / src / libstrongswan / utils / parser_helper.h
1 /*
2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
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
13 * for more details.
14 */
15
16 /**
17 * @defgroup parser_helper parser_helper
18 * @{ @ingroup utils
19 */
20
21 #ifndef PARSER_HELPER_H_
22 #define PARSER_HELPER_H_
23
24 #include <utils/debug.h>
25 #include <collections/array.h>
26 #include <bio/bio_writer.h>
27
28 typedef struct parser_helper_t parser_helper_t;
29
30 /**
31 * Helper class for flex/bison based parsers.
32 *
33 * <code>PREFIX</code> equals whatever is configure with
34 * <code>%option prefix</code> resp. <code>%name-prefix</code>.
35 */
36 struct parser_helper_t {
37
38 /**
39 * A user defined parser context object.
40 */
41 const void *context;
42
43 /**
44 * Opaque object allocated by the lexer, should be set with:
45 * @code
46 * PREFIXlex_init_extra(helper, &helper->scanner).
47 * @endcode
48 */
49 void *scanner;
50
51 /**
52 * Function to determine the current line number (defined by the lexer).
53 *
54 * Basically, this should be assigned to <code>PREFIXget_lineno</code>.
55 *
56 * @param scanner the lexer
57 * @return current line number
58 */
59 int (*get_lineno)(void *scanner);
60
61 /**
62 * Resolves the given include pattern, relative to the location of the
63 * current file.
64 *
65 * Call file_next() to open the next file.
66 *
67 * @param pattern file pattern
68 */
69 void (*file_include)(parser_helper_t *this, char *pattern);
70
71 /**
72 * Get the next file to process.
73 *
74 * This will return NULL if all files matching the most recent pattern
75 * have been handled. If there are other patterns the next call will then
76 * return the next file matching the previous pattern.
77 *
78 * When hitting <code>\<\<EOF\>\></code> first call
79 * @code
80 * PREFIXpop_buffer_state(yyscanner);
81 * @endcode
82 * then call this method to check if there are more files to include for
83 * the most recent call to file_include(), if so, call
84 * @code
85 * PREFIXset_in(file, helper->scanner);
86 * PREFIXpush_buffer_state(PREFIX_create_buffer(file, YY_BUF_SIZE,
87 * helper->scanner), helper->scanner);
88 * @endcode
89 *
90 * If there are no more files to process check
91 * <code>YY_CURRENT_BUFFER</code> and if it is FALSE call yyterminate().
92 *
93 * @return next file to process, or NULL (see comment)
94 */
95 FILE *(*file_next)(parser_helper_t *this);
96
97 /**
98 * Start parsing a string, discards any currently stored data.
99 */
100 void (*string_init)(parser_helper_t *this);
101
102 /**
103 * Append the given string.
104 *
105 * @param str string to append
106 */
107 void (*string_add)(parser_helper_t *this, char *str);
108
109 /**
110 * Extract the current string buffer as null-terminated string. Can only
111 * be called once per string.
112 *
113 * @return allocated string
114 */
115 char *(*string_get)(parser_helper_t *this);
116
117 /**
118 * Destroy this instance.
119 */
120 void (*destroy)(parser_helper_t *this);
121 };
122
123 /**
124 * Log the given message either as error or warning
125 *
126 * @param level log level
127 * @param ctx current parser context
128 * @param fmt error message format
129 * @param ... additional arguments
130 */
131 void parser_helper_log(int level, parser_helper_t *ctx, char *fmt, ...);
132
133 #if DEBUG_LEVEL >= 1
134 # define PARSER_DBG1(ctx, fmt, ...) parser_helper_log(1, ctx, fmt, ##__VA_ARGS__)
135 #endif
136 #if DEBUG_LEVEL >= 2
137 # define PARSER_DBG2(ctx, fmt, ...) parser_helper_log(2, ctx, fmt, ##__VA_ARGS__)
138 #endif
139 #if DEBUG_LEVEL >= 3
140 # define PARSER_DBG3(ctx, fmt, ...) parser_helper_log(3, ctx, fmt, ##__VA_ARGS__)
141 #endif
142
143 #ifndef PARSER_DBG1
144 # define PARSER_DBG1(...) {}
145 #endif
146 #ifndef PARSER_DBG2
147 # define PARSER_DBG2(...) {}
148 #endif
149 #ifndef PARSER_DBG3
150 # define PARSER_DBG3(...) {}
151 #endif
152
153 /**
154 * Create a parser helper object
155 *
156 * @param context user defined parser context
157 * @return parser helper
158 */
159 parser_helper_t *parser_helper_create(void *context);
160
161 #endif /** PARSER_HELPER_H_ @}*/