bison-patches
[Top][All Lists]
Advanced

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

[PATCH 2/5] tests: c++: use a custom string type


From: Akim Demaille
Subject: [PATCH 2/5] tests: c++: use a custom string type
Date: Sat, 22 Sep 2018 12:53:21 +0200

The forthcoming automove feature, to be properly checked, will require
that we can rely on the value of a moved-from string, which is not
something the C++ standard guarantees.  So introduce our own wrapper.

Suggested by Frank Heckenbach.
https://lists.gnu.org/archive/html/bison-patches/2018-09/msg00111.html

* tests/c++.at (Variants): Introduce and use a new 'string' class.
---
 tests/c++.at | 49 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/tests/c++.at b/tests/c++.at
index 2193f88f..d6cca694 100644
--- a/tests/c++.at
+++ b/tests/c++.at
@@ -222,8 +222,36 @@ AT_DATA_GRAMMAR([list.y],
 #include <sstream>
 #include <string>
 
+  class string
+  {
+    public:
+      string () {}
+
+      string (const std::string& s)
+        : val_(s)
+      {}
 
-  typedef std::vector<std::string> strings_type;
+      string (const string& s)
+        : val_(s.val_)
+      {}
+
+      string& operator= (const string& s)
+      {
+        val_ = s.val_;
+        return *this;
+      }
+
+      friend
+      std::ostream& operator<< (std::ostream& o, const string& s)
+      {
+        return o << s.val_;
+      }
+
+    private:
+      std::string val_;
+  };
+
+  typedef std::vector<string> strings_type;
 
   namespace yy
   {
@@ -255,31 +283,30 @@ AT_DATA_GRAMMAR([list.y],
     // Conversion to string.
     template <typename T>
       inline
-      std::string
+      string
       to_string (const T& t)
     {
       std::ostringstream o;
       o << t;
-      return o.str ();
+      return string (o.str ());
     }
   }
 }
 
-%token <::std::string> TEXT;
+%token <::string> TEXT;
 %token <int> NUMBER;
 %token END_OF_FILE 0;
 %token COMMA ","
 
-%type <::std::string> item;
-// Using the template type to exercize its parsing.
 // Starting with :: to ensure we don't output "<::" which starts by the
 // digraph for the left square bracket.
-%type <::std::vector<std::string>> list;
+%type <::string> item;
+// Using the template type to exercize its parsing.
+%type <::std::vector<string>> list;
 
-%printer { yyo << $$; }
-  <int> <::std::string> <::std::vector<std::string>>;
+%printer { yyo << $$; } <int> <::string> <::std::vector<string>>;
 %destructor { std::cerr << "Destroy: " << $$ << '\n'; } <*>;
-%destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::std::string>;
+%destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::string>;
 %%
 
 result:
@@ -343,7 +370,7 @@ namespace yy
        else
          {]AT_TOKEN_CTOR_IF([[
            return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, 
location ()])[);]], [[
-           yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
+           yylval->BUILD (string, to_string (stage));]AT_LOCATION_IF([
            *yylloc = location ();])[
            return parser::token::TEXT;]])[
          }
-- 
2.19.0




reply via email to

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