parser-helper: Make parser_helper_file_t private
[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 <collections/array.h>
25 #include <bio/bio_writer.h>
26
27 typedef struct parser_helper_t parser_helper_t;
28
29 /**
30 * Helper class for flex/bison based parsers.
31 *
32 * <code>PREFIX</code> equals whatever is configure with
33 * <code>%option prefix</code> resp. <code>%name-prefix</code>.
34 */
35 struct parser_helper_t {
36
37 /**
38 * A user defined parser context object.
39 */
40 const void *context;
41
42 /**
43 * Opaque object allocated by the lexer, should be set with:
44 * @code
45 * PREFIXlex_init_extra(helper, &helper->scanner).
46 * @endcode
47 */
48 void *scanner;
49
50 /**
51 * Function to determine the current line number (defined by the lexer).
52 *
53 * Basically, this should be assigned to <code>PREFIXget_lineno</code>.
54 *
55 * @param scanner the lexer
56 * @return current line number
57 */
58 int (*get_lineno)(void *scanner);
59
60 /**
61 * Resolves the given include pattern, relative to the location of the
62 * current file.
63 *
64 * Call file_next() to open the next file.
65 *
66 * @param pattern file pattern
67 */
68 void (*file_include)(parser_helper_t *this, char *pattern);
69
70 /**
71 * Get the next file to process.
72 *
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.
76 *
77 * When hitting <code>\<\<EOF\>\></code> first call
78 * @code
79 * PREFIXpop_buffer_state(yyscanner);
80 * @endcode
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
83 * @code
84 * PREFIXset_in(file, helper->scanner);
85 * PREFIXpush_buffer_state(PREFIX_create_buffer(file, YY_BUF_SIZE,
86 * helper->scanner), helper->scanner);
87 * @endcode
88 *
89 * If there are no more files to process check
90 * <code>YY_CURRENT_BUFFER</code> and if it is FALSE call yyterminate().
91 *
92 * @return next file to process, or NULL (see comment)
93 */
94 FILE *(*file_next)(parser_helper_t *this);
95
96 /**
97 * Start parsing a string, discards any currently stored data.
98 */
99 void (*string_init)(parser_helper_t *this);
100
101 /**
102 * Append the given string.
103 *
104 * @param str string to append
105 */
106 void (*string_add)(parser_helper_t *this, char *str);
107
108 /**
109 * Extract the current string buffer as null-terminated string. Can only
110 * be called once per string.
111 *
112 * @return allocated string
113 */
114 char *(*string_get)(parser_helper_t *this);
115
116 /**
117 * Destroy this instance.
118 */
119 void (*destroy)(parser_helper_t *this);
120 };
121
122 /**
123 * Log the given message either as error or warning
124 *
125 * @param level log level
126 * @param ctx current parser context
127 * @param fmt error message format
128 * @param ... additional arguments
129 */
130 void parser_helper_log(int level, parser_helper_t *ctx, char *fmt, ...);
131
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__)
135
136 /**
137 * Create a parser helper object
138 *
139 * @param context user defined parser context
140 * @return parser helper
141 */
142 parser_helper_t *parser_helper_create(void *context);
143
144 #endif /** PARSER_HELPER_H_ @}*/