bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] -D, --define.


From: Akim Demaille
Subject: [PATCH] -D, --define.
Date: Fri, 07 Nov 2008 20:38:39 -0000

        * 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 89c35f5..3bcc555 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2008-11-07  Akim Demaille  <address@hidden>
 
+       -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  <address@hidden>
+
        Initialize the muscle table before parsing the command line.
        * src/getargs.c (quotearg.h, muscle_tab.h): Include.
        (getargs): Define file_name.
diff --git a/doc/bison.texinfo b/doc/bison.texinfo
index 6570c0c..09ca7ab 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}.
 
address@hidden -D @address@hidden
address@hidden address@hidden@var{value}]
+Same as running @samp{%define @var{name} "@var{value}"} (@pxref{Decl
+Summary, ,%define}).
+
 @item -L @var{language}
 @itemx address@hidden
 Specify the programming language for the generated parser, as if
diff --git a/src/getargs.c b/src/getargs.c
index cb90c36..34cb587 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;
+  /* "<command line>" is used in GCC's messages about -D. */
+  boundary_set (&res.start, uniqstr_new ("<command line>"), 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 8bf61fa..bb036c3 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], [],
+[[<command line>:4: warning: %define variable `FOO' redefined
+<command line>:3: warning: previous definition
+input.y:1.9-11: warning: %define variable `var' redefined
+<command line>:5: warning: previous definition
+<command line>:3: warning: %define variable `FOO' is not used
+<command line>:4: warning: %define variable `FOO' is not used
+<command line>: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.  ##
 ## --------------------------- ##
-- 
1.6.0.2.588.g3102





reply via email to

[Prev in Thread] Current Thread [Next in Thread]