From 58697c6d89f2db69aa2321fe92fc388f87bf2a3c Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 29 Jul 2008 12:52:19 +0200 Subject: [PATCH] -D, --define. * src/getargs.c (usage): Document -D. Fix help string for --locations. (command_line_location): New. (short_options, long_options, getargs): Support -D, --define. (getargs): Move -d support at the right place. * doc/bison.texinfo (Bison Options): Update. * tests/input.at (%define, --define): New. --- ChangeLog | 11 +++++++++++ doc/bison.texinfo | 5 +++++ src/getargs.c | 43 +++++++++++++++++++++++++++++++++++++------ tests/input.at | 28 +++++++++++++++++++++++++++- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 89c35f58..3bcc555e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-11-07 Akim Demaille + + -D, --define. + * src/getargs.c (usage): Document -D. + Fix help string for --locations. + (command_line_location): New. + (short_options, long_options, getargs): Support -D, --define. + (getargs): Move -d support at the right place. + * doc/bison.texinfo (Bison Options): Update. + * tests/input.at (%define, --define): New. + 2008-11-07 Akim Demaille Initialize the muscle table before parsing the command line. diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 6570c0cd..09ca7ab1 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -7833,6 +7833,11 @@ In the parser file, define the macro @code{YYDEBUG} to 1 if it is not already defined, so that the debugging facilities are compiled. @xref{Tracing, ,Tracing Your Parser}. +@item -D @var{name}[=@var{value}] +@itemx --define=@var{name}[=@var{value}] +Same as running @samp{%define @var{name} "@var{value}"} (@pxref{Decl +Summary, ,%define}). + @item -L @var{language} @itemx --language=@var{language} Specify the programming language for the generated parser, as if diff --git a/src/getargs.c b/src/getargs.c index cb90c362..34cb5874 100644 --- a/src/getargs.c +++ b/src/getargs.c @@ -277,7 +277,8 @@ Parser:\n\ (this is an experimental feature)\n\ -S, --skeleton=FILE specify the skeleton to use\n\ -t, --debug instrument the parser for debugging\n\ - --locations enable locations computation\n\ + --locations enable location support\n\ + -D, --define=NAME[=VALUE] same as `%define NAME \"VALUE\"'\n\ -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\ -l, --no-lines don't generate `#line' directives\n\ -k, --token-table include a table of token names\n\ @@ -413,6 +414,7 @@ language_argmatch (char const *arg, int prio, location const *loc) /* Shorts options. Should be computed from long_options. */ static char const short_options[] = + "D:" "L:" "S:" "T::" @@ -479,6 +481,7 @@ static struct option const long_options[] = /* Parser. */ { "debug", no_argument, 0, 't' }, + { "define", required_argument, 0, 'D' }, { "locations", no_argument, 0, LOCATIONS_OPTION }, { "no-lines", no_argument, 0, 'l' }, { "raw", no_argument, 0, 0 }, @@ -497,6 +500,19 @@ static struct option const long_options[] = # define AS_FILE_NAME(File) (File) #endif +/* Build a location for the current command line argument. */ +static +location +command_line_location() +{ + location res; + /* "" is used in GCC's messages about -D. */ + boundary_set (&res.start, uniqstr_new (""), optind, -1); + res.end = res.start; + return res; +} + + void getargs (int argc, char *argv[]) { @@ -506,15 +522,23 @@ getargs (int argc, char *argv[]) != -1) switch (c) { + /* ASCII Sorting for short options (i.e., upper case then + lower case), and then long-only options. */ + case 0: /* Certain long options cause getopt_long to return 0. */ break; - case 'd': - /* Here, the -d and --defines options are differentiated. */ - defines_flag = true; - if (optarg) - spec_defines_file = xstrdup (AS_FILE_NAME (optarg)); + case 'D': /* -DNAME[=VALUE]. */ + { + char* name = optarg; + char* value = strchr (optarg, '='); + if (value) + *value++ = 0; + else + value = ""; + muscle_percent_define_insert (name, command_line_location (), value); + } break; case 'I': @@ -548,6 +572,13 @@ getargs (int argc, char *argv[]) spec_file_prefix = AS_FILE_NAME (optarg); break; + case 'd': + /* Here, the -d and --defines options are differentiated. */ + defines_flag = true; + if (optarg) + spec_defines_file = xstrdup (AS_FILE_NAME (optarg)); + break; + case 'g': graph_flag = true; if (optarg) diff --git a/tests/input.at b/tests/input.at index 8bf61faa..bb036c38 100644 --- a/tests/input.at +++ b/tests/input.at @@ -1,5 +1,5 @@ # Checking the Bison scanner. -*- Autotest -*- -# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, +# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, # Inc. # This program is free software: you can redistribute it and/or modify @@ -821,6 +821,32 @@ input.y:5.9-16: warning: %define variable `special2' is not used AT_CLEANUP + +## ------------------- ## +## %define, --define. ## +## ------------------- ## + +AT_SETUP([%define, --define]) + +AT_DATA([input.y], +[[%define var "value1" +%% +start: ; +]]) + +AT_BISON_CHECK([[input.y -DFOO -DFOO -Dvar=value]], [0], [], +[[:4: warning: %define variable `FOO' redefined +:3: warning: previous definition +input.y:1.9-11: warning: %define variable `var' redefined +:5: warning: previous definition +:3: warning: %define variable `FOO' is not used +:4: warning: %define variable `FOO' is not used +:5: warning: %define variable `var' is not used +input.y:1.9-11: warning: %define variable `var' is not used +]]) + +AT_CLEANUP + ## --------------------------- ## ## %define Boolean variables. ## ## --------------------------- ## -- 2.45.2