2 * Test that libcdecl can parse its own output.
3 * Copyright © 2012, 2020 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/>.
28 #define PROGNAME "crossparse"
29 static const char *progname = PROGNAME;
30 static const char sopts[] = "ECVH";
31 static const struct option lopts[] = {
32 { "cdecl", 0, NULL, 'C' },
33 { "english", 0, NULL, 'E' },
34 { "version", 0, NULL, 'V' },
35 { "help", 0, NULL, 'H' },
39 static void print_usage(FILE *f)
41 fprintf(f, "Usage: %s [options]\n", progname);
44 static void print_help(void)
46 const struct option *opt;
49 puts("Test that libcdecl can parse its own output.\n");
52 for (opt = lopts; opt->val; opt++) {
53 int w = print_option_start(opt, NULL);
65 typedef struct cdecl *parse_func(const char *);
66 typedef size_t render_func(char *, size_t, struct cdecl *);
68 char *rerender(const char *str, const char *parse_name, parse_func *parse,
69 const char *render_name, render_func *render)
71 struct cdecl *decl = NULL;
77 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
78 progname, parse_name, cdecl_get_error()->str);
82 len = render(NULL, 0, decl);
83 buf = malloc_nofail(len+1);
84 if (render(buf, len+1, decl) != len) {
85 fprintf(stderr, "%s: %s: inconsistent length returned\n",
86 progname, render_name);
96 fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
99 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
101 static bool test_crossparse(const char *str, int mode)
103 char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
106 if (mode == MODE_ENGLISH) {
107 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
110 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
113 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
117 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
120 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
123 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
128 if (!strcmp(buf1, buf3))
137 int main(int argc, char **argv)
139 int opt, mode = MODE_CDECL;
140 int ret = EXIT_FAILURE;
145 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
154 test_print_version(PROGNAME);
170 for (int i = optind; i < argc; i++) {
171 if (!test_crossparse(argv[i], mode)) {
172 fprintf(stderr, "%s: failed cross-parse check of: %s\n",