2 * Test that libcdecl can parse its own output.
3 * Copyright © 2012, 2020, 2022-2024 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 #define PROGNAME "crossparse"
33 static const char sopts[] = "f:ECVH";
34 static const struct option lopts[] = {
35 { "cdecl", 0, NULL, 'C' },
36 { "english", 0, NULL, 'E' },
37 { "file", 2, NULL, 'f' },
38 { "version", 0, NULL, 'V' },
39 { "help", 0, NULL, 'H' },
43 static void print_usage(FILE *f)
45 fprintf(f, "Usage: %s [options]\n", progname);
48 static void print_help(void)
51 puts("Test that libcdecl can parse its own output.\n");
52 test_print_options(lopts);
60 typedef struct cdecl *parse_func(const char *);
61 typedef size_t render_func(char *, size_t, struct cdecl *);
63 char *rerender(const char *str, const char *parse_name, parse_func *parse,
64 const char *render_name, render_func *render)
66 struct cdecl *decl = NULL;
72 print_error("%s: failed to parse input: %s", parse_name,
73 cdecl_get_error()->str);
77 len = render(NULL, 0, decl);
78 buf = malloc_nofail(len+1);
79 if (render(buf, len+1, decl) != len) {
80 print_error("%s: inconsistent length returned", render_name);
90 print_error("the failed input was: %s", str);
93 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
95 static bool test_crossparse(const char *str, int mode)
97 char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
100 if (mode == MODE_ENGLISH) {
101 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
104 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
107 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
111 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
114 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
117 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
122 if (!strcmp(buf1, buf3))
130 print_error("failed cross-parse check of: %s", str);
136 int main(int argc, char **argv)
138 int opt, mode = MODE_CDECL;
139 int ret = EXIT_SUCCESS;
142 const char *filename = NULL;
147 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
159 test_print_version(PROGNAME);
170 if (filename && !freopen(filename, "r", stdin)) {
171 print_error("%s: %s", filename, strerror(errno));
173 } else if (filename) {
177 while (do_getline(&line, &n)) {
178 if (!test_crossparse(line, mode))
183 } else if (argv[optind]) {
184 for (i = optind; i < argc; i++) {
185 if (!test_crossparse(argv[i], mode))