]> git.saurik.com Git - bison.git/blob - src/getargs.c
muscle: enforce definition syntax for keyword variables
[bison.git] / src / getargs.c
1 /* Parse command line arguments for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 Free Software
4 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 #include <getopt.h>
30 #include <progname.h>
31
32 #include "complain.h"
33 #include "files.h"
34 #include "getargs.h"
35 #include "muscle-tab.h"
36 #include "quote.h"
37 #include "uniqstr.h"
38
39 bool defines_flag;
40 bool graph_flag;
41 bool xml_flag;
42 bool no_lines_flag;
43 bool token_table_flag;
44 bool yacc_flag; /* for -y */
45
46 bool nondeterministic_parser = false;
47 bool glr_parser = false;
48
49 int feature_flag = feature_caret;
50 int report_flag = report_none;
51 int trace_flag = trace_none;
52
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 }
58 };
59
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;
65
66 /** Decode an option's key.
67 *
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.
77 *
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.
81 */
82 static void
83 flag_argmatch (const char *option,
84 const char * const keys[], const int values[],
85 int all, int *flags, char *arg, size_t no)
86 {
87 int value = XARGMATCH (option, arg + no, keys, values);
88
89 /* -rnone == -rno-all, and -rno-none == -rall. */
90 if (!value)
91 {
92 value = all;
93 no = !no;
94 }
95
96 if (no)
97 *flags &= ~value;
98 else
99 *flags |= value;
100 }
101
102 /** Decode an option's set of keys.
103 *
104 * \param option option being decoded.
105 * \param keys array of valid subarguments.
106 * \param values array of corresponding (int) values.
107 * \param all the all value.
108 * \param flags the flags to update
109 * \param args comma separated list of effective subarguments to decode.
110 * If 0, then activate all the flags.
111 */
112 static void
113 flags_argmatch (const char *option,
114 const char * const keys[], const int values[],
115 int all, int *flags, char *args)
116 {
117 if (args)
118 for (args = strtok (args, ","); args; args = strtok (NULL, ","))
119 {
120 size_t no = STRPREFIX_LIT ("no-", args) ? 3 : 0;
121 flag_argmatch (option, keys,
122 values, all, flags, args, no);
123 }
124 else
125 *flags |= all;
126 }
127
128
129 /** Decode a set of sub arguments.
130 *
131 * \param FlagName the flag familly to update.
132 * \param Args the effective sub arguments to decode.
133 * \param All the "all" value.
134 *
135 * \arg FlagName_args the list of keys.
136 * \arg FlagName_types the list of values.
137 * \arg FlagName_flag the flag to update.
138 */
139 #define FLAGS_ARGMATCH(FlagName, Args, All) \
140 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
141 All, &FlagName ## _flag, Args)
142
143
144 /*----------------------.
145 | --report's handling. |
146 `----------------------*/
147
148 static const char * const report_args[] =
149 {
150 /* In a series of synonyms, present the most meaningful first, so
151 that argmatch_valid be more readable. */
152 "none",
153 "state", "states",
154 "itemset", "itemsets",
155 "lookahead", "lookaheads", "look-ahead",
156 "solved",
157 "all",
158 0
159 };
160
161 static const int report_types[] =
162 {
163 report_none,
164 report_states, report_states,
165 report_states | report_itemsets, report_states | report_itemsets,
166 report_states | report_lookahead_tokens,
167 report_states | report_lookahead_tokens,
168 report_states | report_lookahead_tokens,
169 report_states | report_solved_conflicts,
170 report_all
171 };
172
173 ARGMATCH_VERIFY (report_args, report_types);
174
175
176 /*---------------------.
177 | --trace's handling. |
178 `---------------------*/
179
180 static const char * const trace_args[] =
181 {
182 "none - no traces",
183 "scan - grammar scanner traces",
184 "parse - grammar parser traces",
185 "automaton - construction of the automaton",
186 "bitsets - use of bitsets",
187 "grammar - reading, reducing the grammar",
188 "resource - memory consumption (where available)",
189 "sets - grammar sets: firsts, nullable etc.",
190 "muscles - m4 definitions passed to the skeleton",
191 "tools - m4 invocation",
192 "m4 - m4 traces",
193 "skeleton - skeleton postprocessing",
194 "time - time consumption",
195 "ielr - IELR conversion",
196 "all - all of the above",
197 0
198 };
199
200 static const int trace_types[] =
201 {
202 trace_none,
203 trace_scan,
204 trace_parse,
205 trace_automaton,
206 trace_bitsets,
207 trace_grammar,
208 trace_resource,
209 trace_sets,
210 trace_muscles,
211 trace_tools,
212 trace_m4,
213 trace_skeleton,
214 trace_time,
215 trace_ielr,
216 trace_all
217 };
218
219 ARGMATCH_VERIFY (trace_args, trace_types);
220
221
222 /*-----------------------.
223 | --feature's handling. |
224 `-----------------------*/
225
226 static const char * const feature_args[] =
227 {
228 "none",
229 "caret", "diagnostics-show-caret",
230 "all",
231 0
232 };
233
234 static const int feature_types[] =
235 {
236 feature_none,
237 feature_caret, feature_caret,
238 feature_all
239 };
240
241 ARGMATCH_VERIFY (feature_args, feature_types);
242
243 /*-------------------------------------------.
244 | Display the help message and exit STATUS. |
245 `-------------------------------------------*/
246
247 static void usage (int) ATTRIBUTE_NORETURN;
248
249 static void
250 usage (int status)
251 {
252 if (status != 0)
253 fprintf (stderr, _("Try '%s --help' for more information.\n"),
254 program_name);
255 else
256 {
257 /* For ../build-aux/cross-options.pl to work, use the format:
258 ^ -S, --long[=ARGS] (whitespace)
259 A --long option is required.
260 Otherwise, add exceptions to ../build-aux/cross-options.pl. */
261
262 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
263 fputs (_("\
264 Generate a deterministic LR or generalized LR (GLR) parser employing\n\
265 LALR(1), IELR(1), or canonical LR(1) parser tables. IELR(1) and\n\
266 canonical LR(1) support is experimental.\n\
267 \n\
268 "), stdout);
269
270 fputs (_("\
271 Mandatory arguments to long options are mandatory for short options too.\n\
272 "), stdout);
273 fputs (_("\
274 The same is true for optional arguments.\n\
275 "), stdout);
276
277 fputs (_("\
278 \n\
279 Operation modes:\n\
280 -h, --help display this help and exit\n\
281 -V, --version output version information and exit\n\
282 --print-localedir output directory containing locale-dependent data\n\
283 --print-datadir output directory containing skeletons and XSLT\n\
284 -y, --yacc emulate POSIX Yacc\n\
285 -W, --warnings[=CATEGORY] report the warnings falling in CATEGORY\n\
286 -f, --feature[=FEATURE] activate miscellaneous features\n\
287 \n\
288 "), stdout);
289
290 fputs (_("\
291 Parser:\n\
292 -L, --language=LANGUAGE specify the output programming language\n\
293 -S, --skeleton=FILE specify the skeleton to use\n\
294 -t, --debug instrument the parser for tracing\n\
295 same as '-Dparse.trace'\n\
296 --locations enable location support\n\
297 -D, --define=NAME[=VALUE] similar to '%define NAME \"VALUE\"'\n\
298 -F, --force-define=NAME[=VALUE] override '%define NAME \"VALUE\"'\n\
299 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
300 deprecated by '-Dapi.prefix=PREFIX'\n\
301 -l, --no-lines don't generate '#line' directives\n\
302 -k, --token-table include a table of token names\n\
303 "), stdout);
304 putc ('\n', stdout);
305
306 /* Keep -d and --defines separate so that ../build-aux/cross-options.pl
307 * won't assume that -d also takes an argument. */
308 fputs (_("\
309 Output:\n\
310 --defines[=FILE] also produce a header file\n\
311 -d likewise but cannot specify FILE (for POSIX Yacc)\n\
312 -r, --report=THINGS also produce details on the automaton\n\
313 --report-file=FILE write report to FILE\n\
314 -v, --verbose same as '--report=state'\n\
315 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
316 -o, --output=FILE leave output to FILE\n\
317 -g, --graph[=FILE] also output a graph of the automaton\n\
318 -x, --xml[=FILE] also output an XML report of the automaton\n\
319 (the XML schema is experimental)\n\
320 "), stdout);
321 putc ('\n', stdout);
322
323 fputs (_("\
324 Warning categories include:\n\
325 'midrule-values' unset or unused midrule values\n\
326 'yacc' incompatibilities with POSIX Yacc\n\
327 'conflicts-sr' S/R conflicts (enabled by default)\n\
328 'conflicts-rr' R/R conflicts (enabled by default)\n\
329 'deprecated' obsolete constructs\n\
330 'empty-rule' empty rules without %empty\n\
331 'precedence' useless precedence and associativity\n\
332 'other' all other warnings (enabled by default)\n\
333 'all' all the warnings except 'yacc'\n\
334 'no-CATEGORY' turn off warnings in CATEGORY\n\
335 'none' turn off all the warnings\n\
336 'error[=CATEGORY]' treat warnings as errors\n\
337 "), stdout);
338 putc ('\n', stdout);
339
340 fputs (_("\
341 THINGS is a list of comma separated words that can include:\n\
342 'state' describe the states\n\
343 'itemset' complete the core item sets with their closure\n\
344 'lookahead' explicitly associate lookahead tokens to items\n\
345 'solved' describe shift/reduce conflicts solving\n\
346 'all' include all the above information\n\
347 'none' disable the report\n\
348 "), stdout);
349 putc ('\n', stdout);
350
351 fputs (_("\
352 FEATURE is a list of comma separated words that can include:\n\
353 'caret' show errors with carets\n\
354 'all' all of the above\n\
355 'none' disable all of the above\n\
356 "), stdout);
357
358 putc ('\n', stdout);
359 printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
360 printf (_("%s home page: <%s>.\n"), PACKAGE_NAME, PACKAGE_URL);
361 fputs (_("General help using GNU software: "
362 "<http://www.gnu.org/gethelp/>.\n"),
363 stdout);
364 /* Don't output this redundant message for English locales.
365 Note we still output for 'C' so that it gets included in the
366 man page. */
367 const char *lc_messages = setlocale (LC_MESSAGES, NULL);
368 if (lc_messages && !STREQ (lc_messages, "en_"))
369 /* TRANSLATORS: Replace LANG_CODE in this URL with your language
370 code <http://translationproject.org/team/LANG_CODE.html> to
371 form one of the URLs at http://translationproject.org/team/.
372 Otherwise, replace the entire URL with your translation team's
373 email address. */
374 fputs (_("Report translation bugs to "
375 "<http://translationproject.org/team/>.\n"), stdout);
376 fputs (_("For complete documentation, run: info bison.\n"), stdout);
377 }
378
379 exit (status);
380 }
381
382
383 /*------------------------------.
384 | Display the version message. |
385 `------------------------------*/
386
387 static void
388 version (void)
389 {
390 /* Some efforts were made to ease the translators' task, please
391 continue. */
392 printf (_("bison (GNU Bison) %s"), VERSION);
393 putc ('\n', stdout);
394 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
395 putc ('\n', stdout);
396
397 fprintf (stdout,
398 _("Copyright (C) %d Free Software Foundation, Inc.\n"),
399 PACKAGE_COPYRIGHT_YEAR);
400
401 fputs (_("\
402 This is free software; see the source for copying conditions. There is NO\n\
403 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
404 "),
405 stdout);
406 }
407
408
409 /*-------------------------------------.
410 | --skeleton and --language handling. |
411 `--------------------------------------*/
412
413 void
414 skeleton_arg (char const *arg, int prio, location loc)
415 {
416 if (prio < skeleton_prio)
417 {
418 skeleton_prio = prio;
419 skeleton = arg;
420 }
421 else if (prio == skeleton_prio)
422 complain (&loc, complaint,
423 _("multiple skeleton declarations are invalid"));
424 }
425
426 void
427 language_argmatch (char const *arg, int prio, location loc)
428 {
429 char const *msg;
430
431 if (prio < language_prio)
432 {
433 int i;
434 for (i = 0; valid_languages[i].language[0]; i++)
435 if (c_strcasecmp (arg, valid_languages[i].language) == 0)
436 {
437 language_prio = prio;
438 language = &valid_languages[i];
439 return;
440 }
441 msg = _("%s: invalid language");
442 }
443 else if (language_prio == prio)
444 msg = _("multiple language declarations are invalid");
445 else
446 return;
447
448 complain (&loc, complaint, msg, quotearg_colon (arg));
449 }
450
451 /*----------------------.
452 | Process the options. |
453 `----------------------*/
454
455 /* Shorts options.
456 Should be computed from long_options. */
457 static char const short_options[] =
458 "D:"
459 "F:"
460 "L:"
461 "S:"
462 "T::"
463 "V"
464 "W::"
465 "b:"
466 "d"
467 "f::"
468 "e"
469 "g::"
470 "h"
471 "k"
472 "l"
473 "n"
474 "o:"
475 "p:"
476 "r:"
477 "t"
478 "v"
479 "x::"
480 "y"
481 ;
482
483 /* Values for long options that do not have single-letter equivalents. */
484 enum
485 {
486 LOCATIONS_OPTION = CHAR_MAX + 1,
487 PRINT_LOCALEDIR_OPTION,
488 PRINT_DATADIR_OPTION,
489 REPORT_FILE_OPTION
490 };
491
492 static struct option const long_options[] =
493 {
494 /* Operation modes. */
495 { "help", no_argument, 0, 'h' },
496 { "version", no_argument, 0, 'V' },
497 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
498 { "print-datadir", no_argument, 0, PRINT_DATADIR_OPTION },
499 { "warnings", optional_argument, 0, 'W' },
500
501 /* Parser. */
502 { "name-prefix", required_argument, 0, 'p' },
503 { "include", required_argument, 0, 'I' },
504
505 /* Output. */
506 { "file-prefix", required_argument, 0, 'b' },
507 { "output", required_argument, 0, 'o' },
508 { "output-file", required_argument, 0, 'o' },
509 { "graph", optional_argument, 0, 'g' },
510 { "xml", optional_argument, 0, 'x' },
511 { "report", required_argument, 0, 'r' },
512 { "report-file", required_argument, 0, REPORT_FILE_OPTION },
513 { "verbose", no_argument, 0, 'v' },
514
515 /* Hidden. */
516 { "trace", optional_argument, 0, 'T' },
517
518 /* Output. */
519 { "defines", optional_argument, 0, 'd' },
520 { "feature", optional_argument, 0, 'f' },
521
522 /* Operation modes. */
523 { "fixed-output-files", no_argument, 0, 'y' },
524 { "yacc", no_argument, 0, 'y' },
525
526 /* Parser. */
527 { "debug", no_argument, 0, 't' },
528 { "define", required_argument, 0, 'D' },
529 { "force-define", required_argument, 0, 'F' },
530 { "locations", no_argument, 0, LOCATIONS_OPTION },
531 { "no-lines", no_argument, 0, 'l' },
532 { "raw", no_argument, 0, 0 },
533 { "skeleton", required_argument, 0, 'S' },
534 { "language", required_argument, 0, 'L' },
535 { "token-table", no_argument, 0, 'k' },
536
537 {0, 0, 0, 0}
538 };
539
540 /* Under DOS, there is no difference on the case. This can be
541 troublesome when looking for '.tab' etc. */
542 #ifdef MSDOS
543 # define AS_FILE_NAME(File) (strlwr (File), (File))
544 #else
545 # define AS_FILE_NAME(File) (File)
546 #endif
547
548 /* Build a location for the current command line argument. */
549 static
550 location
551 command_line_location (void)
552 {
553 location res;
554 /* "<command line>" is used in GCC's messages about -D. */
555 boundary_set (&res.start, uniqstr_new ("<command line>"), optind - 1, -1);
556 res.end = res.start;
557 return res;
558 }
559
560
561 void
562 getargs (int argc, char *argv[])
563 {
564 int c;
565
566 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
567 != -1)
568 switch (c)
569 {
570 /* ASCII Sorting for short options (i.e., upper case then
571 lower case), and then long-only options. */
572
573 case 0:
574 /* Certain long options cause getopt_long to return 0. */
575 break;
576
577 case 'D': /* -DNAME[=(VALUE|"VALUE"|{VALUE})]. */
578 case 'F': /* -FNAME[=(VALUE|"VALUE"|{VALUE})]. */
579 {
580 char *name = optarg;
581 char *value = strchr (optarg, '=');
582 muscle_kind kind = muscle_keyword;
583 if (value)
584 {
585 char *end = value + strlen (value) - 1;
586 *value++ = 0;
587 if (*value == '{' && *end == '}')
588 {
589 kind = muscle_code;
590 ++value;
591 *end = 0;
592 }
593 else if (*value == '"' && *end == '"')
594 {
595 kind = muscle_string;
596 ++value;
597 *end = 0;
598 }
599 }
600 muscle_percent_define_insert (name, command_line_location (),
601 kind, value ? value : "",
602 c == 'D' ? MUSCLE_PERCENT_DEFINE_D
603 : MUSCLE_PERCENT_DEFINE_F);
604 }
605 break;
606
607 case 'I':
608 include = AS_FILE_NAME (optarg);
609 break;
610
611 case 'L':
612 language_argmatch (optarg, command_line_prio,
613 command_line_location ());
614 break;
615
616 case 'S':
617 skeleton_arg (AS_FILE_NAME (optarg), command_line_prio,
618 command_line_location ());
619 break;
620
621 case 'T':
622 FLAGS_ARGMATCH (trace, optarg, trace_all);
623 break;
624
625 case 'V':
626 version ();
627 exit (EXIT_SUCCESS);
628
629 case 'f':
630 FLAGS_ARGMATCH (feature, optarg, feature_all);
631 break;
632
633 case 'W':
634 warnings_argmatch (optarg);
635 break;
636
637 case 'b':
638 spec_file_prefix = AS_FILE_NAME (optarg);
639 break;
640
641 case 'd':
642 /* Here, the -d and --defines options are differentiated. */
643 defines_flag = true;
644 if (optarg)
645 {
646 free (spec_defines_file);
647 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
648 }
649 break;
650
651 case 'g':
652 graph_flag = true;
653 if (optarg)
654 {
655 free (spec_graph_file);
656 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
657 }
658 break;
659
660 case 'h':
661 usage (EXIT_SUCCESS);
662
663 case 'k':
664 token_table_flag = true;
665 break;
666
667 case 'l':
668 no_lines_flag = true;
669 break;
670
671 case 'o':
672 spec_outfile = AS_FILE_NAME (optarg);
673 break;
674
675 case 'p':
676 spec_name_prefix = optarg;
677 break;
678
679 case 'r':
680 FLAGS_ARGMATCH (report, optarg, report_all);
681 break;
682
683 case 't':
684 muscle_percent_define_insert ("parse.trace",
685 command_line_location (),
686 muscle_keyword, "",
687 MUSCLE_PERCENT_DEFINE_D);
688 break;
689
690 case 'v':
691 report_flag |= report_states;
692 break;
693
694 case 'x':
695 xml_flag = true;
696 if (optarg)
697 {
698 free (spec_xml_file);
699 spec_xml_file = xstrdup (AS_FILE_NAME (optarg));
700 }
701 break;
702
703 case 'y':
704 warning_argmatch ("error=yacc", 0, 6);
705 yacc_flag = true;
706 break;
707
708 case LOCATIONS_OPTION:
709 muscle_percent_define_ensure ("locations",
710 command_line_location (), true);
711 break;
712
713 case PRINT_LOCALEDIR_OPTION:
714 printf ("%s\n", LOCALEDIR);
715 exit (EXIT_SUCCESS);
716
717 case PRINT_DATADIR_OPTION:
718 printf ("%s\n", pkgdatadir ());
719 exit (EXIT_SUCCESS);
720
721 case REPORT_FILE_OPTION:
722 free (spec_verbose_file);
723 spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
724 break;
725
726 default:
727 usage (EXIT_FAILURE);
728 }
729
730 if (argc - optind != 1)
731 {
732 if (argc - optind < 1)
733 error (0, 0, _("%s: missing operand"), quotearg_colon (argv[argc - 1]));
734 else
735 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
736 usage (EXIT_FAILURE);
737 }
738
739 current_file = grammar_file = uniqstr_new (argv[optind]);
740 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
741 }
742
743 void
744 tr (char *s, char from, char to)
745 {
746 for (; *s; s++)
747 if (*s == from)
748 *s = to;
749 }