[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
- RFC: api.value.automove, Akim Demaille, 2018/09/19
- Re: RFC: api.value.automove, Hans Ã…berg, 2018/09/19
- Re: RFC: api.value.automove, Frank Heckenbach, 2018/09/20
- Re: RFC: api.value.automove, Akim Demaille, 2018/09/21
- Re: RFC: api.value.automove, Frank Heckenbach, 2018/09/21
- [PATCH 0/5] lalr1.cc: automove, Akim Demaille, 2018/09/22
- [PATCH 1/5] tests: prepare a test for automove, Akim Demaille, 2018/09/22
- [PATCH 2/5] tests: c++: use a custom string type,
Akim Demaille <=
- [PATCH 3/5] c++: introduce api.value.automove, Akim Demaille, 2018/09/22
- [PATCH 4/5] c++: issue a warning with a value is moved several times, Akim Demaille, 2018/09/22
- [PATCH 5/5] news: c++: move semantics, Akim Demaille, 2018/09/22