ae2f_docs
main.c
Go to the documentation of this file.
1#include "./main.auto.h"
2#include <stdio.h>
3
4#define BOOK (ae2f_MAC_KEYWORD "(")
5
7#define dbg_printf(s, ...) fprintf(stdout, s, __VA_ARGS__)
8#define dbg_puts(s) fputs(s, stdout)
9#else
10#define dbg_printf(s, ...)
11#define dbg_puts(s)
12#endif
13
14#define SZBOOK (sizeof(BOOK) - 1)
15
16char SEE[sizeof(BOOK)] = {
17 0,
18};
19
20struct __prmbool {
21 unsigned m_cmt : 1;
22 unsigned m_space_found : 1;
23 unsigned m_first : 1;
24} prmbool = {0, 0, 0};
25
26#define prm_cmt prmbool.m_cmt
27#define prm_space_found prmbool.m_space_found
28#define prm_first prmbool.m_first
29
30char PARAM[SZPARAM + 2 + (CMT_REQUIRED) * 4] = {
31 0,
32};
33char TPARAMS[SZTPARAM + 1] = {
34 0,
35};
36
37char ENDL[] = " \\\n";
38char prmtype = 0;
39
40int c;
41int l;
42int tprm = 0;
43int prm = 0;
44int stack = 0;
45
46#define STATE_OK 0
47#define STATE_OUTFAILED 1
48#define STATE_UNEXPECTED 2
49#define STATE_OVERRUN 3
50
51#define casenewlines
52 case '\n': case
53 '\r'
54
55#define caseblanks
56 case ' ': case
57 '\t': casenewlines
58
59
60#define isAlph(c)
61 ((((c) >= 'A') && ((c) <= 'Z')) || (((c) >= 'a') && ((c) <= 'z')))
62#define isNumber(c) (((c) >= '0') && ((c) <= '9'))
63#define isVarName(c) ((isAlph(c) || isNumber(c)) || (c) == '_')
64#define isNewLine(c) ((c) == '\n' || (c) == '\r')
65
66int main() {
67
68 puts("#undef\t__ae2f_MACRO_GENERATED\n"
69 "#define\t__ae2f_MACRO_GENERATED\t1");
70 while ((c = fgetc(stdin)) != EOF) {
71
72 goto __START;
73__REST:
74 if (c != (BOOK)[0]) {
75 l = fputc(c, stdout);
76 if (l < 0)
77 return 1;
78 }
79 continue;
80
81__START:
82 if (c == '\"') goto STRING;
83
84 if (c == '#') {
85 const char _INCLUDE[sizeof("#include") - 1] = "#include";
86 char _SEE_INC[sizeof(_INCLUDE)];
87 size_t i = 1;
88 _SEE_INC[0] = '#';
89 fputc('#', stdout);
90 for (; i < sizeof(_INCLUDE); i++) {
91 _SEE_INC[i] = fgetc(stdin);
92 fputc(_SEE_INC[i], stdout);
93
94 if (_SEE_INC[i] != _INCLUDE[i]) {
95 break;
96 }
97 }
98
99 if (i == sizeof(_INCLUDE)) {
100 while ((c = fgetc(stdin)) != EOF && !isNewLine(c)) {
101 fputc(c, stdout);
102 }
103
104 puts("\n#undef __ae2f_MACRO_GENERATED\n"
105 "#define __ae2f_MACRO_GENERATED 1");
106 }
107 }
108
109 else if (c == (BOOK)[0]) {
110 SEE[0] = (BOOK)[0];
111 size_t i = 1;
112 for (; i < SZBOOK; i++) {
113 SEE[i] = fgetc(stdin);
114
115 if (SEE[i] != (BOOK)[i]) {
116 SEE[i + 1] = 0;
117 break;
118 }
119 }
120
121 if (i < SZBOOK) {
122 if (fputs(SEE, stdout) < 0)
123 return 1;
124
125 continue;
126 }
127
128 l = fputs("#define _", stdout);
129
130 if (l < 0)
131 return STATE_OUTFAILED;
132
133 tprm = 0;
134 prm = 0;
135 stack = 0; /** init */
136 prmtype = 1;
137
138 while (1) {
139 switch ((c = fgetc(stdin))) {
140 case EOF:
141 return 2;
142
143 case ')':
144 goto TPARAMED;
145
146 default: {
147 TPARAMS[tprm] = c;
148 if (tprm == SZTPARAM)
149 return STATE_OVERRUN;
150 tprm++;
151 }
152
154 break;
155 }
156 } /** tparam */
157
158TPARAMED:
159
160 while (1) {
161 switch (c = fgetc(stdin)) {
162 case EOF:
163 return 1;
164 default:
165 l = fputc(c, stdout);
166 if (l < 0)
167 return STATE_OUTFAILED;
168 if (c == '(')
169 goto FNED;
171 break;
172 }
173 } /** fn */
174
175FNED:
176 dbg_puts(" \\\n\t/** tparam */ \\\n\t\t");
177 TPARAMS[tprm] = 0;
178 if (fputs(TPARAMS, stdout) < 0)
179 return STATE_OUTFAILED;
180
181 if (tprm) {
182#if 0
183 l = fputc(',', stdout);
184 if (l < 0)
185 return STATE_OUTFAILED;
186#endif
187 }
188
189 dbg_puts(" \\\n \\\n\t/** param */ \\\n\t\t");
190
191 prm = 0;
192 prm_cmt = 1;
193 prm_first = 1;
194
195 while (1) {
196 if (prm_cmt) {
197 if (fputs("/* ", stdout) < 0)
198 return STATE_OUTFAILED;
199 prm_cmt = 0;
200 }
201
202 switch (c = fgetc(stdin)) {
203 case EOF:
204 return 1;
205
207 fputc(' ', stdout);
208PRM_CASE_BLANK:
209 prm_space_found = 1;
210 break;
211
212 default:
213 if (!isVarName(c)) {
214 PARAM[prm] = 0;
215 fputs(PARAM, stdout);
216 fputc(c, stdout);
217 prm = 0;
218 goto PRM_CASE_BLANK;
219 }
220
221 if (prm_first) {
222 prm_first = 0;
223 if (fputc(',', stdout) < 0)
224 return STATE_OUTFAILED;
225 }
226
227 if (prm_space_found) {
228 PARAM[prm] = 0;
229 fputs(PARAM, stdout);
230 prm = 0;
231 prm_space_found = 0;
232 }
233
234 PARAM[prm] = c;
235 if (prm == SZPARAM)
236 return STATE_OVERRUN;
237 prm++;
238 break;
239
240 case ',':
241 case ')':
242 PARAM[prm] = c;
243 PARAM[prm + 1] = 0;
244
245 if (fputs(" */ ", stdout) < 0)
246 return STATE_OUTFAILED;
247
248 prm_cmt = 1;
249
250#if CMT_REQUIRED
251 if (c == ')') {
252 PARAM[prm] = ' ';
253 PARAM[prm + 1] = '\\';
254 PARAM[prm + 2] = '\n';
255 PARAM[prm + 3] = c;
256 PARAM[prm + 4] = 0;
257 l = fputs(PARAM, stdout);
258 } else
259#endif
260 l = fputs(PARAM, stdout);
261
262 if (l < 0)
263 return STATE_OUTFAILED;
264
265 prm = 0;
266
267 if (',' == c) {
268 dbg_puts(" \\\n\t\t");
269 goto PRM_CASE_BLANK;
270 } else {
271 goto PRMED;
272 }
273 }
274 }
275PRMED:
276
277 while (1) {
278 switch (c = fgetc(stdin)) {
279 case EOF:
280 return 1;
281
283 ENDL[2] = c;
284 l = fputs(ENDL, stdout);
285 if (l < 0)
286 return STATE_OUTFAILED;
287
288 break;
289
290 default:
291 l = fputc(c, stdout);
292 if (l < 0)
293 return STATE_OUTFAILED;
294 break;
295
296 case '{':
297 if (!(++stack)) {
298 return STATE_OVERRUN;
299 }
300 l = fputc('{', stdout);
301 if (l < 0)
302 return STATE_OUTFAILED;
303 break;
304
305 case '}':
306 l = fputc('}', stdout);
307 if (l < 0)
308 return STATE_OUTFAILED;
309
310 if (!(--stack)) {
311 goto STACKED;
312 }
313 break;
314 }
315 }
316 /* stack */
317STACKED:;
318 }
319
320 else {
321 goto __REST;
322 }
323 }
324
325 puts("\n#undef\t__ae2f_MACRO_GENERATED\n"
326 "\n#define\t__ae2f_MACRO_GENERATED\t0\n");
327
328 return 0;
329
330STRING:
331 fputc('\"', stdout);
332 while((c = fgetc(stdin)) != EOF) {
333 switch(c) {
334 case '\"':
335 goto __REST;
336
337 case '\\':
338 fputc(c, stdout);
339 c = fgetc(stdin);
340
341 default:
342 fputc(c, stdout);
343 break;
344 }
345 }
346
347 return -1;
348}
#define ae2f_MAC_KEYWORD
Definition main.auto.h:1
#define SZPARAM
Definition main.auto.h:3
#define CMT_REQUIRED
Definition main.auto.h:2
#define SZTPARAM
Definition main.auto.h:4
#define prm_cmt
Definition main.c:26
#define STATE_OUTFAILED
Definition main.c:47
int tprm
Definition main.c:42
char SEE[sizeof(BOOK)]
Definition main.c:16
#define caseblanks
Definition main.c:55
#define isNewLine(c)
Definition main.c:64
int stack
Definition main.c:44
#define SZBOOK
Definition main.c:14
#define dbg_puts(s)
Definition main.c:8
#define STATE_OVERRUN
Definition main.c:49
int c
Definition main.c:40
int l
Definition main.c:41
#define BOOK
Definition main.c:4
int prm
Definition main.c:43
#define isNumber(c)
Definition main.c:62
char prmtype
Definition main.c:38
char TPARAMS[SZTPARAM+1]
Definition main.c:33
char ENDL[]
Definition main.c:37
#define prm_first
Definition main.c:28
#define casenewlines
Definition main.c:51
#define prm_space_found
Definition main.c:27
#define isAlph(c)
Definition main.c:60
#define isVarName(c)
Definition main.c:63
char PARAM[SZPARAM+2+(CMT_REQUIRED) *4]
Definition main.c:30
unsigned m_cmt
Definition main.c:21
unsigned m_space_found
Definition main.c:22
unsigned m_first
Definition main.c:23