]>
git.saurik.com Git - bison.git/blob - src/getargs.c
dadcbbb9108600a5b6899989f7232ac00186c1cf
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
26 #include <c-strcase.h>
27 #include <configmake.h>
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
38 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
39 # undef __GNU_LIBRARY__
40 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
48 #include "muscle-tab.h"
56 bool token_table_flag
;
57 bool yacc_flag
; /* for -y */
59 bool nondeterministic_parser
= false;
60 bool glr_parser
= false;
62 int report_flag
= report_none
;
63 int trace_flag
= trace_none
;
65 static struct bison_language
const valid_languages
[] = {
66 { "c", "c-skel.m4", ".c", ".h", true },
67 { "c++", "c++-skel.m4", ".cc", ".hh", true },
68 { "java", "java-skel.m4", ".java", ".java", false },
69 { "", "", "", "", false }
72 int skeleton_prio
= default_prio
;
73 const char *skeleton
= NULL
;
74 int language_prio
= default_prio
;
75 struct bison_language
const *language
= &valid_languages
[0];
76 const char *include
= NULL
;
79 /** Decode an option's set of keys.
81 * \param option option being decoded.
82 * \param keys array of valid subarguments.
83 * \param values array of corresponding (int) values.
84 * \param all the all value.
85 * \param flags the flags to update
86 * \param args comma separated list of effective subarguments to decode.
87 * If 0, then activate all the flags.
89 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
90 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
91 * flags from \c all. Thus no-none = all and no-all = none.
94 flags_argmatch (const char *option
,
95 const char * const keys
[], const int values
[],
96 int all
, int *flags
, char *args
)
100 args
= strtok (args
, ",");
103 int no
= STRPREFIX_LIT ("no-", args
) ? 3 : 0;
104 int value
= XARGMATCH (option
, args
+ no
, keys
, values
);
119 args
= strtok (NULL
, ",");
126 /** Decode a set of sub arguments.
128 * \param FlagName the flag familly to update.
129 * \param Args the effective sub arguments to decode.
130 * \param All the "all" value.
132 * \arg FlagName_args the list of keys.
133 * \arg FlagName_types the list of values.
134 * \arg FlagName_flag the flag to update.
136 #define FLAGS_ARGMATCH(FlagName, Args, All) \
137 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
138 All, &FlagName ## _flag, Args)
141 /*----------------------.
142 | --report's handling. |
143 `----------------------*/
145 static const char * const report_args
[] =
147 /* In a series of synonyms, present the most meaningful first, so
148 that argmatch_valid be more readable. */
151 "itemset", "itemsets",
152 "lookahead", "lookaheads", "look-ahead",
158 static const int report_types
[] =
161 report_states
, report_states
,
162 report_states
| report_itemsets
, report_states
| report_itemsets
,
163 report_states
| report_lookahead_tokens
,
164 report_states
| report_lookahead_tokens
,
165 report_states
| report_lookahead_tokens
,
166 report_states
| report_solved_conflicts
,
170 ARGMATCH_VERIFY (report_args
, report_types
);
173 /*---------------------.
174 | --trace's handling. |
175 `---------------------*/
177 static const char * const trace_args
[] =
179 /* In a series of synonyms, present the most meaningful first, so
180 that argmatch_valid be more readable. */
182 "scan - grammar scanner traces",
183 "parse - grammar parser traces",
184 "automaton - construction of the automaton",
185 "bitsets - use of bitsets",
186 "grammar - reading, reducing the grammar",
187 "resource - memory consumption (where available)",
188 "sets - grammar sets: firsts, nullable etc.",
189 "muscles - m4 definitions passed to the skeleton",
190 "tools - m4 invocation",
192 "skeleton - skeleton postprocessing",
193 "time - time consumption",
194 "ielr - IELR conversion",
195 "all - all of the above",
199 static const int trace_types
[] =
218 ARGMATCH_VERIFY (trace_args
, trace_types
);
221 /*------------------------.
222 | --warnings's handling. |
223 `------------------------*/
225 static const char * const warnings_args
[] =
227 /* In a series of synonyms, present the most meaningful first, so
228 that argmatch_valid be more readable. */
229 "none - no warnings",
230 "midrule-values - unset or unused midrule values",
231 "yacc - incompatibilities with POSIX Yacc",
232 "conflicts-sr - S/R conflicts",
233 "conflicts-rr - R/R conflicts",
234 "other - all other warnings",
235 "all - all of the above",
236 "error - warnings are errors",
240 static const int warnings_types
[] =
252 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
254 /*-------------------------------------------.
255 | Display the help message and exit STATUS. |
256 `-------------------------------------------*/
258 static void usage (int) ATTRIBUTE_NORETURN
;
264 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
268 /* For ../build-aux/cross-options.pl to work, use the format:
269 ^ -S, --long[=ARGS] (whitespace)
270 A --long option is required.
271 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
273 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
275 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
276 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
277 canonical LR(1) support is experimental.\n\
282 Mandatory arguments to long options are mandatory for short options too.\n\
285 The same is true for optional arguments.\n\
291 -h, --help display this help and exit\n\
292 -V, --version output version information and exit\n\
293 --print-localedir output directory containing locale-dependent data\n\
294 --print-datadir output directory containing skeletons and XSLT\n\
295 -y, --yacc emulate POSIX Yacc\n\
296 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
302 -L, --language=LANGUAGE specify the output programming language\n\
303 (this is an experimental feature)\n\
304 -S, --skeleton=FILE specify the skeleton to use\n\
305 -t, --debug instrument the parser for tracing\n\
306 same as `-Dparse.trace'\n\
307 --locations enable location support\n\
308 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
309 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
310 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
311 deprecated by '-Dapi.prefix=PREFIX'\n\
312 -l, --no-lines don't generate '#line' directives\n\
313 -k, --token-table include a table of token names\n\
317 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
318 * won't assume that -d also takes an argument. */
321 --defines[=FILE] also produce a header file\n\
322 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
323 -r, --report=THINGS also produce details on the automaton\n\
324 --report-file=FILE write report to FILE\n\
325 -v, --verbose same as `--report=state'\n\
326 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
327 -o, --output=FILE leave output to FILE\n\
328 -g, --graph[=FILE] also output a graph of the automaton\n\
329 -x, --xml[=FILE] also output an XML report of the automaton\n\
330 (the XML schema is experimental)\n\
335 Warning categories include:\n\
336 `midrule-values' unset or unused midrule values\n\
337 `yacc' incompatibilities with POSIX Yacc\n\
338 `conflicts-sr' S/R conflicts (enabled by default)\n\
339 `conflicts-rr' R/R conflicts (enabled by default)\n\
340 `other' all other warnings (enabled by default)\n\
341 `all' all the warnings\n\
342 `no-CATEGORY' turn off warnings in CATEGORY\n\
343 `none' turn off all the warnings\n\
344 `error' treat warnings as errors\n\
349 THINGS is a list of comma separated words that can include:\n\
350 `state' describe the states\n\
351 `itemset' complete the core item sets with their closure\n\
352 `lookahead' explicitly associate lookahead tokens to items\n\
353 `solved' describe shift/reduce conflicts solving\n\
354 `all' include all the above information\n\
355 `none' disable the report\n\
358 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
365 /*------------------------------.
366 | Display the version message. |
367 `------------------------------*/
372 /* Some efforts were made to ease the translators' task, please
374 printf (_("bison (GNU Bison) %s"), VERSION
);
376 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
380 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
381 PACKAGE_COPYRIGHT_YEAR
);
384 This is free software; see the source for copying conditions. There is NO\n\
385 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
391 /*-------------------------------------.
392 | --skeleton and --language handling. |
393 `--------------------------------------*/
396 skeleton_arg (char const *arg
, int prio
, location loc
)
398 if (prio
< skeleton_prio
)
400 skeleton_prio
= prio
;
403 else if (prio
== skeleton_prio
)
404 complain_at (loc
, complaint
,
405 _("multiple skeleton declarations are invalid"));
409 language_argmatch (char const *arg
, int prio
, location loc
)
413 if (prio
< language_prio
)
416 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
417 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
419 language_prio
= prio
;
420 language
= &valid_languages
[i
];
423 msg
= _("%s: invalid language");
425 else if (language_prio
== prio
)
426 msg
= _("multiple language declarations are invalid");
430 complain_at (loc
, complaint
, msg
, quotearg_colon (arg
));
433 /*----------------------.
434 | Process the options. |
435 `----------------------*/
438 Should be computed from long_options. */
439 static char const short_options
[] =
464 /* Values for long options that do not have single-letter equivalents. */
467 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
468 PRINT_LOCALEDIR_OPTION
,
469 PRINT_DATADIR_OPTION
,
473 static struct option
const long_options
[] =
475 /* Operation modes. */
476 { "help", no_argument
, 0, 'h' },
477 { "version", no_argument
, 0, 'V' },
478 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
479 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
480 { "warnings", optional_argument
, 0, 'W' },
483 { "name-prefix", required_argument
, 0, 'p' },
484 { "include", required_argument
, 0, 'I' },
487 { "file-prefix", required_argument
, 0, 'b' },
488 { "output", required_argument
, 0, 'o' },
489 { "output-file", required_argument
, 0, 'o' },
490 { "graph", optional_argument
, 0, 'g' },
491 { "xml", optional_argument
, 0, 'x' },
492 { "report", required_argument
, 0, 'r' },
493 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
494 { "verbose", no_argument
, 0, 'v' },
497 { "trace", optional_argument
, 0, 'T' },
500 { "defines", optional_argument
, 0, 'd' },
502 /* Operation modes. */
503 { "fixed-output-files", no_argument
, 0, 'y' },
504 { "yacc", no_argument
, 0, 'y' },
507 { "debug", no_argument
, 0, 't' },
508 { "define", required_argument
, 0, 'D' },
509 { "force-define", required_argument
, 0, 'F' },
510 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
511 { "no-lines", no_argument
, 0, 'l' },
512 { "raw", no_argument
, 0, 0 },
513 { "skeleton", required_argument
, 0, 'S' },
514 { "language", required_argument
, 0, 'L' },
515 { "token-table", no_argument
, 0, 'k' },
520 /* Under DOS, there is no difference on the case. This can be
521 troublesome when looking for `.tab' etc. */
523 # define AS_FILE_NAME(File) (strlwr (File), (File))
525 # define AS_FILE_NAME(File) (File)
528 /* Build a location for the current command line argument. */
531 command_line_location (void)
534 /* "<command line>" is used in GCC's messages about -D. */
535 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
, -1);
542 getargs (int argc
, char *argv
[])
546 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
550 /* ASCII Sorting for short options (i.e., upper case then
551 lower case), and then long-only options. */
554 /* Certain long options cause getopt_long to return 0. */
557 case 'D': /* -DNAME[=VALUE]. */
558 case 'F': /* -FNAME[=VALUE]. */
561 char* value
= strchr (optarg
, '=');
564 muscle_percent_define_insert (name
, command_line_location (),
566 c
== 'D' ? MUSCLE_PERCENT_DEFINE_D
567 : MUSCLE_PERCENT_DEFINE_F
);
572 include
= AS_FILE_NAME (optarg
);
576 language_argmatch (optarg
, command_line_prio
,
577 command_line_location ());
581 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
582 command_line_location ());
586 FLAGS_ARGMATCH (trace
, optarg
, trace_all
);
594 FLAGS_ARGMATCH (warnings
, optarg
, Wall
);
598 spec_file_prefix
= AS_FILE_NAME (optarg
);
602 /* Here, the -d and --defines options are differentiated. */
606 free (spec_defines_file
);
607 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
615 free (spec_graph_file
);
616 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
621 usage (EXIT_SUCCESS
);
624 token_table_flag
= true;
628 no_lines_flag
= true;
632 spec_outfile
= AS_FILE_NAME (optarg
);
636 spec_name_prefix
= optarg
;
640 FLAGS_ARGMATCH (report
, optarg
, report_all
);
644 muscle_percent_define_insert ("parse.trace",
645 command_line_location (), "",
646 MUSCLE_PERCENT_DEFINE_D
);
650 report_flag
|= report_states
;
657 free (spec_xml_file
);
658 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
666 case LOCATIONS_OPTION
:
667 muscle_percent_define_ensure ("locations",
668 command_line_location (), true);
671 case PRINT_LOCALEDIR_OPTION
:
672 printf ("%s\n", LOCALEDIR
);
675 case PRINT_DATADIR_OPTION
:
676 printf ("%s\n", pkgdatadir ());
679 case REPORT_FILE_OPTION
:
680 free (spec_verbose_file
);
681 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
685 usage (EXIT_FAILURE
);
688 if (argc
- optind
!= 1)
690 if (argc
- optind
< 1)
691 error (0, 0, _("%s: missing operand"), quotearg_colon (argv
[argc
- 1]));
693 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
694 usage (EXIT_FAILURE
);
697 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
698 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
702 tr (char *s
, char from
, char to
)