]> git.saurik.com Git - bison.git/blame_incremental - src/getargs.c
bench: More use of the verbosity level.
[bison.git] / src / getargs.c
... / ...
CommitLineData
1/* Parse command line arguments for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include <config.h>
22#include "system.h"
23#include "output.h"
24
25#include <argmatch.h>
26#include <c-strcase.h>
27#include <configmake.h>
28#include <error.h>
29
30/* Hack to get <getopt.h> to declare getopt with a prototype. */
31#if lint && ! defined __GNU_LIBRARY__
32# define __GNU_LIBRARY__
33# define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1
34#endif
35
36#include <getopt.h>
37
38#ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39# undef __GNU_LIBRARY__
40# undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
41#endif
42
43#include "complain.h"
44#include "files.h"
45#include "getargs.h"
46#include "uniqstr.h"
47
48bool debug_flag;
49bool defines_flag;
50bool graph_flag;
51bool xml_flag;
52bool locations_flag;
53bool no_lines_flag;
54bool token_table_flag;
55bool yacc_flag; /* for -y */
56
57bool error_verbose = false;
58
59bool nondeterministic_parser = false;
60bool glr_parser = false;
61
62int report_flag = report_none;
63int trace_flag = trace_none;
64int warnings_flag = warnings_none;
65
66static struct bison_language const valid_languages[] = {
67 { "c", "c-skel.m4", ".c", ".h", true },
68 { "c++", "c++-skel.m4", ".cc", ".hh", true },
69 { "java", "java-skel.m4", ".java", ".java", false },
70 { "", "", "", "", false }
71};
72
73static int skeleton_prio = 2;
74const char *skeleton = NULL;
75static int language_prio = 2;
76struct bison_language const *language = &valid_languages[0];
77const char *include = NULL;
78
79char *program_name;
80
81
82/** Decode an option's set of keys.
83 *
84 * \param option option being decoded.
85 * \param keys array of valid subarguments.
86 * \param values array of corresponding (int) values.
87 * \param flags the flags to update
88 * \param args colon separated list of effective subarguments to decode.
89 * If 0, then activate all the flags.
90 *
91 * The special value 0 resets the flags to 0.
92 */
93static void
94flags_argmatch (const char *option,
95 const char * const keys[], const int values[],
96 int *flags, char *args)
97{
98 if (args)
99 {
100 args = strtok (args, ",");
101 while (args)
102 {
103 int value = XARGMATCH (option, args, keys, values);
104 if (value == 0)
105 *flags = 0;
106 else
107 *flags |= value;
108 args = strtok (NULL, ",");
109 }
110 }
111 else
112 *flags = ~0;
113}
114
115/** Decode a set of sub arguments.
116 *
117 * \param FlagName the flag familly to update.
118 * \param Args the effective sub arguments to decode.
119 *
120 * \arg FlagName_args the list of keys.
121 * \arg FlagName_types the list of values.
122 * \arg FlagName_flag the flag to update.
123 */
124#define FLAGS_ARGMATCH(FlagName, Args) \
125 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
126 &FlagName ## _flag, Args)
127
128
129/*----------------------.
130| --report's handling. |
131`----------------------*/
132
133static const char * const report_args[] =
134{
135 /* In a series of synonyms, present the most meaningful first, so
136 that argmatch_valid be more readable. */
137 "none",
138 "state", "states",
139 "itemset", "itemsets",
140 "lookahead", "lookaheads", "look-ahead",
141 "solved",
142 "all",
143 0
144};
145
146static const int report_types[] =
147{
148 report_none,
149 report_states, report_states,
150 report_states | report_itemsets, report_states | report_itemsets,
151 report_states | report_lookahead_tokens,
152 report_states | report_lookahead_tokens,
153 report_states | report_lookahead_tokens,
154 report_states | report_solved_conflicts,
155 report_all
156};
157
158ARGMATCH_VERIFY (report_args, report_types);
159
160
161/*---------------------.
162| --trace's handling. |
163`---------------------*/
164
165static const char * const trace_args[] =
166{
167 /* In a series of synonyms, present the most meaningful first, so
168 that argmatch_valid be more readable. */
169 "none - no traces",
170 "scan - grammar scanner traces",
171 "parse - grammar parser traces",
172 "automaton - construction of the automaton",
173 "bitsets - use of bitsets",
174 "grammar - reading, reducing the grammar",
175 "resource - memory consumption (where available)",
176 "sets - grammar sets: firsts, nullable etc.",
177 "tools - m4 invocation",
178 "m4 - m4 traces",
179 "skeleton - skeleton postprocessing",
180 "time - time consumption",
181 "all - all of the above",
182 0
183};
184
185static const int trace_types[] =
186{
187 trace_none,
188 trace_scan,
189 trace_parse,
190 trace_automaton,
191 trace_bitsets,
192 trace_grammar,
193 trace_resource,
194 trace_sets,
195 trace_tools,
196 trace_m4,
197 trace_skeleton,
198 trace_time,
199 trace_all
200};
201
202ARGMATCH_VERIFY (trace_args, trace_types);
203
204
205/*------------------------.
206| --warnings's handling. |
207`------------------------*/
208
209static const char * const warnings_args[] =
210{
211 /* In a series of synonyms, present the most meaningful first, so
212 that argmatch_valid be more readable. */
213 "none - no warnings",
214 "midrule-values - unset or unused midrule values",
215 "yacc - incompatibilities with POSIX YACC",
216 "all - all of the above",
217 "error - warnings are errors",
218 0
219};
220
221static const int warnings_types[] =
222{
223 warnings_none,
224 warnings_midrule_values,
225 warnings_yacc,
226 warnings_all,
227 warnings_error
228};
229
230ARGMATCH_VERIFY (warnings_args, warnings_types);
231
232
233/*-------------------------------------------.
234| Display the help message and exit STATUS. |
235`-------------------------------------------*/
236
237static void usage (int) ATTRIBUTE_NORETURN;
238
239static void
240usage (int status)
241{
242 if (status != 0)
243 fprintf (stderr, _("Try `%s --help' for more information.\n"),
244 program_name);
245 else
246 {
247 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
248 fputs (_("\
249Generate LALR(1) and GLR parsers.\n\
250\n\
251"), stdout);
252
253 fputs (_("\
254Mandatory arguments to long options are mandatory for short options too.\n\
255"), stdout);
256 fputs (_("\
257The same is true for optional arguments.\n\
258"), stdout);
259
260 fputs (_("\
261\n\
262Operation modes:\n\
263 -h, --help display this help and exit\n\
264 -V, --version output version information and exit\n\
265 --print-localedir output directory containing locale-dependent data\n\
266 --print-datadir output directory containing skeletons and XSLT\n\
267 -y, --yacc emulate POSIX Yacc\n\
268 -W, --warnings=[CATEGORY] report the warnings falling in CATEGORY\n\
269\n\
270"), stdout);
271
272 fputs (_("\
273Parser:\n\
274 -L, --language=LANGUAGE specify the output programming language\n\
275 (this is an experimental feature)\n\
276 -S, --skeleton=FILE specify the skeleton to use\n\
277 -t, --debug instrument the parser for debugging\n\
278 --locations enable locations computation\n\
279 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
280 -l, --no-lines don't generate `#line' directives\n\
281 -k, --token-table include a table of token names\n\
282\n\
283"), stdout);
284
285 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
286 * won't assume that -d also takes an argument. */
287 fputs (_("\
288Output:\n\
289 --defines[=FILE] also produce a header file\n\
290 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
291 -r, --report=THINGS also produce details on the automaton\n\
292 --report-file=FILE write report to FILE\n\
293 -v, --verbose same as `--report=state'\n\
294 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
295 -o, --output=FILE leave output to FILE\n\
296 -g, --graph[=FILE] also output a graph of the automaton\n\
297 -x, --xml[=FILE] also output an XML report of the automaton\n\
298 (the XML schema is experimental)\n\
299\n\
300"), stdout);
301
302 fputs (_("\
303Warning categories include:\n\
304 `midrule-values' unset or unused midrule values\n\
305 `yacc' incompatibilities with POSIX YACC\n\
306 `all' all the warnings\n\
307 `no-CATEGORY' turn off warnings in CATEGORY\n\
308 `none' turn off all the warnings\n\
309 `error' treat warnings as errors\n\
310\n\
311"), stdout);
312
313 fputs (_("\
314THINGS is a list of comma separated words that can include:\n\
315 `state' describe the states\n\
316 `itemset' complete the core item sets with their closure\n\
317 `lookahead' explicitly associate lookahead tokens to items\n\
318 `solved' describe shift/reduce conflicts solving\n\
319 `all' include all the above information\n\
320 `none' disable the report\n\
321"), stdout);
322
323 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
324 }
325
326 exit (status);
327}
328
329
330/*------------------------------.
331| Display the version message. |
332`------------------------------*/
333
334static void
335version (void)
336{
337 /* Some efforts were made to ease the translators' task, please
338 continue. */
339 printf (_("bison (GNU Bison) %s"), VERSION);
340 putc ('\n', stdout);
341 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
342 putc ('\n', stdout);
343
344 fprintf (stdout,
345 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
346 PACKAGE_COPYRIGHT_YEAR);
347
348 fputs (_("\
349This is free software; see the source for copying conditions. There is NO\n\
350warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
351"),
352 stdout);
353}
354
355
356/*-------------------------------------.
357| --skeleton and --language handling. |
358`--------------------------------------*/
359
360void
361skeleton_arg (char const *arg, int prio, location const *loc)
362{
363 if (prio < skeleton_prio)
364 {
365 skeleton_prio = prio;
366 skeleton = arg;
367 }
368 else if (prio == skeleton_prio)
369 {
370 char const *msg =
371 _("multiple skeleton declarations are invalid");
372 if (loc)
373 complain_at (*loc, msg);
374 else
375 complain (msg);
376 }
377}
378
379void
380language_argmatch (char const *arg, int prio, location const *loc)
381{
382 char const *msg;
383
384 if (prio < language_prio)
385 {
386 int i;
387 for (i = 0; valid_languages[i].language[0]; i++)
388 if (c_strcasecmp (arg, valid_languages[i].language) == 0)
389 {
390 language_prio = prio;
391 language = &valid_languages[i];
392 return;
393 }
394 msg = _("invalid language `%s'");
395 }
396 else if (language_prio == prio)
397 msg = _("multiple language declarations are invalid");
398 else
399 return;
400
401 if (loc)
402 complain_at (*loc, msg, arg);
403 else
404 complain (msg, arg);
405}
406
407/*----------------------.
408| Process the options. |
409`----------------------*/
410
411/* Shorts options.
412 Should be computed from long_options. */
413static char const short_options[] =
414 "L:"
415 "S:"
416 "T::"
417 "V"
418 "W::"
419 "b:"
420 "d"
421 "e"
422 "g::"
423 "h"
424 "k"
425 "l"
426 "n"
427 "o:"
428 "p:"
429 "r:"
430 "t"
431 "v"
432 "x::"
433 "y"
434 ;
435
436/* Values for long options that do not have single-letter equivalents. */
437enum
438{
439 LOCATIONS_OPTION = CHAR_MAX + 1,
440 PRINT_LOCALEDIR_OPTION,
441 PRINT_DATADIR_OPTION,
442 REPORT_FILE_OPTION
443};
444
445static struct option const long_options[] =
446{
447 /* Operation modes. */
448 { "help", no_argument, 0, 'h' },
449 { "version", no_argument, 0, 'V' },
450 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
451 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
452 { "warnings", optional_argument, 0, 'W' },
453
454 /* Parser. */
455 { "name-prefix", required_argument, 0, 'p' },
456 { "include", required_argument, 0, 'I' },
457
458 /* Output. */
459 { "file-prefix", required_argument, 0, 'b' },
460 { "output", required_argument, 0, 'o' },
461 { "output-file", required_argument, 0, 'o' },
462 { "graph", optional_argument, 0, 'g' },
463 { "xml", optional_argument, 0, 'x' },
464 { "report", required_argument, 0, 'r' },
465 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
466 { "verbose", no_argument, 0, 'v' },
467
468 /* Hidden. */
469 { "trace", optional_argument, 0, 'T' },
470
471 /* Output. */
472 { "defines", optional_argument, 0, 'd' },
473
474 /* Operation modes. */
475 { "fixed-output-files", no_argument, 0, 'y' },
476 { "yacc", no_argument, 0, 'y' },
477
478 /* Parser. */
479 { "debug", no_argument, 0, 't' },
480 { "locations", no_argument, 0, LOCATIONS_OPTION },
481 { "no-lines", no_argument, 0, 'l' },
482 { "raw", no_argument, 0, 0 },
483 { "skeleton", required_argument, 0, 'S' },
484 { "language", required_argument, 0, 'L' },
485 { "token-table", no_argument, 0, 'k' },
486
487 {0, 0, 0, 0}
488};
489
490/* Under DOS, there is no difference on the case. This can be
491 troublesome when looking for `.tab' etc. */
492#ifdef MSDOS
493# define AS_FILE_NAME(File) (strlwr (File), (File))
494#else
495# define AS_FILE_NAME(File) (File)
496#endif
497
498void
499getargs (int argc, char *argv[])
500{
501 int c;
502
503 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
504 != -1)
505 switch (c)
506 {
507 case 0:
508 /* Certain long options cause getopt_long to return 0. */
509 break;
510
511 case 'd':
512 /* Here, the -d and --defines options are differentiated. */
513 defines_flag = true;
514 if (optarg)
515 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
516 break;
517
518 case 'I':
519 include = AS_FILE_NAME (optarg);
520 break;
521
522 case 'L':
523 language_argmatch (optarg, 0, NULL);
524 break;
525
526 case 'S':
527 skeleton_arg (AS_FILE_NAME (optarg), 0, NULL);
528 break;
529
530 case 'T':
531 FLAGS_ARGMATCH (trace, optarg);
532 break;
533
534 case 'V':
535 version ();
536 exit (EXIT_SUCCESS);
537
538 case 'W':
539 if (optarg)
540 FLAGS_ARGMATCH (warnings, optarg);
541 else
542 warnings_flag |= warnings_all;
543 break;
544
545 case 'b':
546 spec_file_prefix = AS_FILE_NAME (optarg);
547 break;
548
549 case 'g':
550 graph_flag = true;
551 if (optarg)
552 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
553 break;
554
555 case 'h':
556 usage (EXIT_SUCCESS);
557
558 case 'k':
559 token_table_flag = true;
560 break;
561
562 case 'l':
563 no_lines_flag = true;
564 break;
565
566 case 'o':
567 spec_outfile = AS_FILE_NAME (optarg);
568 break;
569
570 case 'p':
571 spec_name_prefix = optarg;
572 break;
573
574 case 'r':
575 FLAGS_ARGMATCH (report, optarg);
576 break;
577
578 case 't':
579 debug_flag = true;
580 break;
581
582 case 'v':
583 report_flag |= report_states;
584 break;
585
586 case 'x':
587 xml_flag = true;
588 if (optarg)
589 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
590 break;
591
592 case 'y':
593 yacc_flag = true;
594 break;
595
596 case LOCATIONS_OPTION:
597 locations_flag = true;
598 break;
599
600 case PRINT_LOCALEDIR_OPTION:
601 printf ("%s\n", LOCALEDIR);
602 exit (EXIT_SUCCESS);
603
604 case PRINT_DATADIR_OPTION:
605 printf ("%s\n", compute_pkgdatadir ());
606 exit (EXIT_SUCCESS);
607
608 case REPORT_FILE_OPTION:
609 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
610 break;
611
612 default:
613 usage (EXIT_FAILURE);
614 }
615
616 if (argc - optind != 1)
617 {
618 if (argc - optind < 1)
619 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
620 else
621 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
622 usage (EXIT_FAILURE);
623 }
624
625 current_file = grammar_file = uniqstr_new (argv[optind]);
626}