]>
git.saurik.com Git - bison.git/blob - src/getargs.c
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 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>
35 #include "muscle-tab.h"
43 bool token_table_flag
;
44 bool yacc_flag
; /* for -y */
46 bool nondeterministic_parser
= false;
47 bool glr_parser
= false;
49 int feature_flag
= feature_caret
;
50 int report_flag
= report_none
;
51 int trace_flag
= trace_none
;
53 static struct bison_language
const valid_languages
[] = {
54 { "c", "c-skel.m4", ".c", ".h", true },
55 { "c++", "c++-skel.m4", ".cc", ".hh", true },
56 { "java", "java-skel.m4", ".java", ".java", false },
57 { "", "", "", "", false }
60 int skeleton_prio
= default_prio
;
61 const char *skeleton
= NULL
;
62 int language_prio
= default_prio
;
63 struct bison_language
const *language
= &valid_languages
[0];
64 const char *include
= NULL
;
66 /** Decode an option's key.
68 * \param option option being decoded.
69 * \param keys array of valid subarguments.
70 * \param values array of corresponding (int) values.
71 * \param all the all value.
72 * \param flags the flags to update
73 * \param arg the subarguments to decode.
74 * If null, then activate all the flags.
75 * \param no length of the potential "no-" prefix.
76 * Can be 0 or 3. If 3, negate the action of the subargument.
78 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
79 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
80 * flags from \c all. Thus no-none = all and no-all = none.
83 flag_argmatch (const char *option
,
84 const char * const keys
[], const int values
[],
85 int all
, int *flags
, char *arg
, size_t no
)
88 value
= XARGMATCH (option
, arg
+ no
, keys
, values
);
106 /** Decode an option's set of keys.
108 * \param option option being decoded.
109 * \param keys array of valid subarguments.
110 * \param values array of corresponding (int) values.
111 * \param all the all value.
112 * \param flags the flags to update
113 * \param args comma separated list of effective subarguments to decode.
114 * If 0, then activate all the flags.
117 flags_argmatch (const char *option
,
118 const char * const keys
[], const int values
[],
119 int all
, int *flags
, char *args
)
122 for (args
= strtok (args
, ","); args
; args
= strtok (NULL
, ","))
124 size_t no
= STRPREFIX_LIT ("no-", args
) ? 3 : 0;
125 flag_argmatch (option
, keys
,
126 values
, all
, flags
, args
, no
);
133 /** Decode a set of sub arguments.
135 * \param FlagName the flag familly to update.
136 * \param Args the effective sub arguments to decode.
137 * \param All the "all" value.
139 * \arg FlagName_args the list of keys.
140 * \arg FlagName_types the list of values.
141 * \arg FlagName_flag the flag to update.
143 #define FLAGS_ARGMATCH(FlagName, Args, All) \
144 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
145 All, &FlagName ## _flag, Args)
148 /*----------------------.
149 | --report's handling. |
150 `----------------------*/
152 static const char * const report_args
[] =
154 /* In a series of synonyms, present the most meaningful first, so
155 that argmatch_valid be more readable. */
158 "itemset", "itemsets",
159 "lookahead", "lookaheads", "look-ahead",
165 static const int report_types
[] =
168 report_states
, report_states
,
169 report_states
| report_itemsets
, report_states
| report_itemsets
,
170 report_states
| report_lookahead_tokens
,
171 report_states
| report_lookahead_tokens
,
172 report_states
| report_lookahead_tokens
,
173 report_states
| report_solved_conflicts
,
177 ARGMATCH_VERIFY (report_args
, report_types
);
180 /*---------------------.
181 | --trace's handling. |
182 `---------------------*/
184 static const char * const trace_args
[] =
187 "scan - grammar scanner traces",
188 "parse - grammar parser traces",
189 "automaton - construction of the automaton",
190 "bitsets - use of bitsets",
191 "grammar - reading, reducing the grammar",
192 "resource - memory consumption (where available)",
193 "sets - grammar sets: firsts, nullable etc.",
194 "muscles - m4 definitions passed to the skeleton",
195 "tools - m4 invocation",
197 "skeleton - skeleton postprocessing",
198 "time - time consumption",
199 "ielr - IELR conversion",
200 "all - all of the above",
204 static const int trace_types
[] =
223 ARGMATCH_VERIFY (trace_args
, trace_types
);
226 /*------------------------.
227 | --warnings's handling. |
228 `------------------------*/
230 /** Decode a single argument from -W.
232 * \param arg the subarguments to decode.
233 * If null, then activate all the flags.
234 * \param no length of the potential "no-" prefix.
235 * Can be 0 or 3. If 3, negate the action of the subargument.
236 * \param err length of a potential "error=".
237 * Can be 0 or 6. If 6, treat the subargument as a CATEGORY.
239 * If VALUE != 0 then KEY sets flags and no-KEY clears them.
240 * If VALUE == 0 then KEY clears all flags from \c all and no-KEY sets all
241 * flags from \c all. Thus no-none = all and no-all = none.
244 warning_argmatch (char const *arg
, size_t no
, size_t err
)
246 int value
= XARGMATCH ("--warning", arg
+ no
+ err
,
247 warnings_args
, warnings_types
);
249 /* -Wnone == -Wno-all, and -Wno-none == -Wall. */
259 for (b
= 0; b
< warnings_size
; ++b
)
264 /* -Wno-error=foo: if foo enabled as an error,
265 make it a warning. */
266 if (warnings_flag
[b
] == severity_error
)
267 warnings_flag
[b
] = severity_warning
;
271 warnings_flag
[b
] = severity_disabled
;
277 for (b
= 0; b
< warnings_size
; ++b
)
279 /* -Wfoo and -Werror=foo. */
280 warnings_flag
[b
] = err
? severity_error
: severity_warning
;
284 /** Decode a comma-separated list of arguments from -W.
286 * \param args comma separated list of effective subarguments to decode.
287 * If 0, then activate all the flags.
290 warnings_argmatch (char *args
)
293 for (args
= strtok (args
, ","); args
; args
= strtok (NULL
, ","))
294 if (STREQ (args
, "error"))
295 warnings_are_errors
= true;
296 else if (STREQ (args
, "no-error"))
298 warnings_are_errors
= false;
299 warning_argmatch ("no-error=all", 3, 6);
303 size_t no
= STRPREFIX_LIT ("no-", args
) ? 3 : 0;
304 size_t err
= STRPREFIX_LIT ("error=", args
+ no
) ? 6 : 0;
306 warning_argmatch (args
, no
, err
);
309 warning_argmatch ("all", 0, 0);
312 const char * const warnings_args
[] =
327 const int warnings_types
[] =
341 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
343 /*-----------------------.
344 | --feature's handling. |
345 `-----------------------*/
347 static const char * const feature_args
[] =
350 "caret", "diagnostics-show-caret",
355 static const int feature_types
[] =
358 feature_caret
, feature_caret
,
362 ARGMATCH_VERIFY (feature_args
, feature_types
);
364 /*-------------------------------------------.
365 | Display the help message and exit STATUS. |
366 `-------------------------------------------*/
368 static void usage (int) ATTRIBUTE_NORETURN
;
374 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
378 /* For ../build-aux/cross-options.pl to work, use the format:
379 ^ -S, --long[=ARGS] (whitespace)
380 A --long option is required.
381 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
383 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
385 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
386 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
387 canonical LR(1) support is experimental.\n\
392 Mandatory arguments to long options are mandatory for short options too.\n\
395 The same is true for optional arguments.\n\
401 -h, --help display this help and exit\n\
402 -V, --version output version information and exit\n\
403 --print-localedir output directory containing locale-dependent data\n\
404 --print-datadir output directory containing skeletons and XSLT\n\
405 -y, --yacc emulate POSIX Yacc\n\
406 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
407 -f, --feature[=FEATURE] activate miscellaneous features\n\
413 -L, --language=LANGUAGE specify the output programming language\n\
414 -S, --skeleton=FILE specify the skeleton to use\n\
415 -t, --debug instrument the parser for tracing\n\
416 same as `-Dparse.trace'\n\
417 --locations enable location support\n\
418 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
419 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
420 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
421 deprecated by '-Dapi.prefix=PREFIX'\n\
422 -l, --no-lines don't generate '#line' directives\n\
423 -k, --token-table include a table of token names\n\
427 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
428 * won't assume that -d also takes an argument. */
431 --defines[=FILE] also produce a header file\n\
432 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
433 -r, --report=THINGS also produce details on the automaton\n\
434 --report-file=FILE write report to FILE\n\
435 -v, --verbose same as `--report=state'\n\
436 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
437 -o, --output=FILE leave output to FILE\n\
438 -g, --graph[=FILE] also output a graph of the automaton\n\
439 -x, --xml[=FILE] also output an XML report of the automaton\n\
440 (the XML schema is experimental)\n\
445 Warning categories include:\n\
446 `midrule-values' unset or unused midrule values\n\
447 `yacc' incompatibilities with POSIX Yacc\n\
448 `conflicts-sr' S/R conflicts (enabled by default)\n\
449 `conflicts-rr' R/R conflicts (enabled by default)\n\
450 `deprecated' obsolete constructs\n\
451 `precedence' useless precedence and associativity\n\
452 `other' all other warnings (enabled by default)\n\
453 `all' all the warnings\n\
454 `no-CATEGORY' turn off warnings in CATEGORY\n\
455 `none' turn off all the warnings\n\
456 `error[=CATEGORY]' treat warnings as errors\n\
461 THINGS is a list of comma separated words that can include:\n\
462 `state' describe the states\n\
463 `itemset' complete the core item sets with their closure\n\
464 `lookahead' explicitly associate lookahead tokens to items\n\
465 `solved' describe shift/reduce conflicts solving\n\
466 `all' include all the above information\n\
467 `none' disable the report\n\
472 FEATURE is a list of comma separated words that can include:\n\
473 `caret' show errors with carets\n\
474 `all' all of the above\n\
475 `none' disable all of the above\n\
479 printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
480 printf (_("%s home page: <%s>.\n"), PACKAGE_NAME
, PACKAGE_URL
);
481 fputs (_("General help using GNU software: "
482 "<http://www.gnu.org/gethelp/>.\n"),
484 /* Don't output this redundant message for English locales.
485 Note we still output for 'C' so that it gets included in the
487 const char *lc_messages
= setlocale (LC_MESSAGES
, NULL
);
488 if (lc_messages
&& !STREQ (lc_messages
, "en_"))
489 /* TRANSLATORS: Replace LANG_CODE in this URL with your language
490 code <http://translationproject.org/team/LANG_CODE.html> to
491 form one of the URLs at http://translationproject.org/team/.
492 Otherwise, replace the entire URL with your translation team's
494 fputs (_("Report translation bugs to "
495 "<http://translationproject.org/team/>.\n"), stdout
);
496 fputs (_("For complete documentation, run: info bison.\n"), stdout
);
503 /*------------------------------.
504 | Display the version message. |
505 `------------------------------*/
510 /* Some efforts were made to ease the translators' task, please
512 printf (_("bison (GNU Bison) %s"), VERSION
);
514 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
518 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
519 PACKAGE_COPYRIGHT_YEAR
);
522 This is free software; see the source for copying conditions. There is NO\n\
523 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
529 /*-------------------------------------.
530 | --skeleton and --language handling. |
531 `--------------------------------------*/
534 skeleton_arg (char const *arg
, int prio
, location loc
)
536 if (prio
< skeleton_prio
)
538 skeleton_prio
= prio
;
541 else if (prio
== skeleton_prio
)
542 complain (&loc
, complaint
,
543 _("multiple skeleton declarations are invalid"));
547 language_argmatch (char const *arg
, int prio
, location loc
)
551 if (prio
< language_prio
)
554 for (i
= 0; valid_languages
[i
].language
[0]; i
++)
555 if (c_strcasecmp (arg
, valid_languages
[i
].language
) == 0)
557 language_prio
= prio
;
558 language
= &valid_languages
[i
];
561 msg
= _("%s: invalid language");
563 else if (language_prio
== prio
)
564 msg
= _("multiple language declarations are invalid");
568 complain (&loc
, complaint
, msg
, quotearg_colon (arg
));
571 /*----------------------.
572 | Process the options. |
573 `----------------------*/
576 Should be computed from long_options. */
577 static char const short_options
[] =
603 /* Values for long options that do not have single-letter equivalents. */
606 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
607 PRINT_LOCALEDIR_OPTION
,
608 PRINT_DATADIR_OPTION
,
612 static struct option
const long_options
[] =
614 /* Operation modes. */
615 { "help", no_argument
, 0, 'h' },
616 { "version", no_argument
, 0, 'V' },
617 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
618 { "print-datadir", no_argument
, 0, PRINT_DATADIR_OPTION
},
619 { "warnings", optional_argument
, 0, 'W' },
622 { "name-prefix", required_argument
, 0, 'p' },
623 { "include", required_argument
, 0, 'I' },
626 { "file-prefix", required_argument
, 0, 'b' },
627 { "output", required_argument
, 0, 'o' },
628 { "output-file", required_argument
, 0, 'o' },
629 { "graph", optional_argument
, 0, 'g' },
630 { "xml", optional_argument
, 0, 'x' },
631 { "report", required_argument
, 0, 'r' },
632 { "report-file", required_argument
, 0, REPORT_FILE_OPTION
},
633 { "verbose", no_argument
, 0, 'v' },
636 { "trace", optional_argument
, 0, 'T' },
639 { "defines", optional_argument
, 0, 'd' },
640 { "feature", optional_argument
, 0, 'f' },
642 /* Operation modes. */
643 { "fixed-output-files", no_argument
, 0, 'y' },
644 { "yacc", no_argument
, 0, 'y' },
647 { "debug", no_argument
, 0, 't' },
648 { "define", required_argument
, 0, 'D' },
649 { "force-define", required_argument
, 0, 'F' },
650 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
651 { "no-lines", no_argument
, 0, 'l' },
652 { "raw", no_argument
, 0, 0 },
653 { "skeleton", required_argument
, 0, 'S' },
654 { "language", required_argument
, 0, 'L' },
655 { "token-table", no_argument
, 0, 'k' },
660 /* Under DOS, there is no difference on the case. This can be
661 troublesome when looking for `.tab' etc. */
663 # define AS_FILE_NAME(File) (strlwr (File), (File))
665 # define AS_FILE_NAME(File) (File)
668 /* Build a location for the current command line argument. */
671 command_line_location (void)
674 /* "<command line>" is used in GCC's messages about -D. */
675 boundary_set (&res
.start
, uniqstr_new ("<command line>"), optind
- 1, -1);
682 getargs (int argc
, char *argv
[])
686 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
690 /* ASCII Sorting for short options (i.e., upper case then
691 lower case), and then long-only options. */
694 /* Certain long options cause getopt_long to return 0. */
697 case 'D': /* -DNAME[=VALUE]. */
698 case 'F': /* -FNAME[=VALUE]. */
701 char* value
= strchr (optarg
, '=');
704 muscle_percent_define_insert (name
, command_line_location (),
706 c
== 'D' ? MUSCLE_PERCENT_DEFINE_D
707 : MUSCLE_PERCENT_DEFINE_F
);
712 include
= AS_FILE_NAME (optarg
);
716 language_argmatch (optarg
, command_line_prio
,
717 command_line_location ());
721 skeleton_arg (AS_FILE_NAME (optarg
), command_line_prio
,
722 command_line_location ());
726 FLAGS_ARGMATCH (trace
, optarg
, trace_all
);
734 FLAGS_ARGMATCH (feature
, optarg
, feature_all
);
738 warnings_argmatch (optarg
);
742 spec_file_prefix
= AS_FILE_NAME (optarg
);
746 /* Here, the -d and --defines options are differentiated. */
750 free (spec_defines_file
);
751 spec_defines_file
= xstrdup (AS_FILE_NAME (optarg
));
759 free (spec_graph_file
);
760 spec_graph_file
= xstrdup (AS_FILE_NAME (optarg
));
765 usage (EXIT_SUCCESS
);
768 token_table_flag
= true;
772 no_lines_flag
= true;
776 spec_outfile
= AS_FILE_NAME (optarg
);
780 spec_name_prefix
= optarg
;
784 FLAGS_ARGMATCH (report
, optarg
, report_all
);
788 muscle_percent_define_insert ("parse.trace",
789 command_line_location (), "",
790 MUSCLE_PERCENT_DEFINE_D
);
794 report_flag
|= report_states
;
801 free (spec_xml_file
);
802 spec_xml_file
= xstrdup (AS_FILE_NAME (optarg
));
807 warnings_flag
[warning_yacc
] = severity_error
;
811 case LOCATIONS_OPTION
:
812 muscle_percent_define_ensure ("locations",
813 command_line_location (), true);
816 case PRINT_LOCALEDIR_OPTION
:
817 printf ("%s\n", LOCALEDIR
);
820 case PRINT_DATADIR_OPTION
:
821 printf ("%s\n", pkgdatadir ());
824 case REPORT_FILE_OPTION
:
825 free (spec_verbose_file
);
826 spec_verbose_file
= xstrdup (AS_FILE_NAME (optarg
));
830 usage (EXIT_FAILURE
);
833 if (argc
- optind
!= 1)
835 if (argc
- optind
< 1)
836 error (0, 0, _("%s: missing operand"), quotearg_colon (argv
[argc
- 1]));
838 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 1]));
839 usage (EXIT_FAILURE
);
842 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);
843 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
847 tr (char *s
, char from
, char to
)