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