string_view and c.insert(c.end(), std::move(val)) compile error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

string_view and c.insert(c.end(), std::move(val)) compile error

Olaf Peter
Hello,

for my x3 parser I replaced string with string_view, but failed in this
concrete case. For some reasons it want to insert into a container which
must fail in this case. I separated the raw rule, but no luck. Setting
x3::raw outside works, but the double quotes are embedded which isn't
intended. What do I overlook here? '"' is handled as literal, isn't it?
Explicit use of x3::lit('"') doesn't change it. How to fix the rule?
Note, I'm using an older compiler not fully C++17 aware :(


Thanks,

Olaf

---8<---

#include <iostream>
#include <vector>


#include <experimental/string_view>

namespace boost { namespace spirit { namespace x3 { namespace traits {

template <typename It>
void move_to(It b, It e, std::experimental::string_view& v)
{
     // Note storage is contiguous, requires a concept check for input
range, how to?
     v = std::experimental::string_view(&*b, e - b);
}

} } } }


//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>

#if 1
// XXX basic_string_view<char>« has no member named »insert« XXX
typedef std::experimental::string_view  attribute_type;
#else
typedef std::string                     attribute_type;
#endif

namespace parser {

     namespace x3 = boost::spirit::x3;
     namespace iso8859_1 = boost::spirit::x3::iso8859_1;

     using iso8859_1::char_;

     auto const string_literal_atom = x3::rule<struct _, attribute_type>
{ "string_literal" } =
         x3::raw[ x3::lexeme [
             *(  (char_ - '"')
              | "\"\""
              )
         ]]
         ;

     auto const string_literal = x3::rule<struct _, attribute_type> {
"string_literal" } =
         x3::no_skip[
            '"'
         >> string_literal_atom
         >> '"'
         ]
         ;
}


int main()
{
     namespace x3 = boost::spirit::x3;

     std::vector<std::string> const test_cases {
         R"("FooBar")",
         R"("FooBar"  )",
         R"(    "FooBar"  )",
         R"(" "" FooBarBaz "" ")"       // with ""inner escaped"" quotes
         };

     typedef std::string::const_iterator iterator_type;

     for(auto str: test_cases) {
         iterator_type iter = str.begin();
         iterator_type const end = str.end();

         attribute_type attr;

         auto& rule = parser::string_literal;

         std::cout << "parse '" << str << "': ";

         bool r = phrase_parse(iter, end, *x3::space >> rule, x3::space,
attr);

         if (r && iter == end) {
             std::cout << "succeeded: '" << attr << "'\n";
         } else {
             std::cout << "*** failed ***\n";
         }
     }

     return 0;
}

--->8---


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: string_view and c.insert(c.end(), std::move(val)) compile error

Olaf Peter
> for my x3 parser I replaced string with string_view, but failed in this
> concrete case. For some reasons it want to insert into a container which
> must fail in this case. I separated the raw rule, but no luck. Setting
> x3::raw outside works, but the double quotes are embedded which isn't
> intended. What do I overlook here? '"' is handled as literal, isn't it?
> Explicit use of x3::lit('"') doesn't change it. How to fix the rule?
> Note, I'm using an older compiler not fully C++17 aware :(

I reduced the example to:

---8<---
#include <iostream>
#include <vector>

#include <string_view>

namespace boost { namespace spirit { namespace x3 { namespace traits {

template <typename It>
void move_to(It b, It e, std::string_view& v)
{
     // Note requires storage to be contiguous
     v = std::string_view(&*b, e - b);
}

} } } }

//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>

#if 1
// XXX basic_string_view<char>« has no member named »insert« XXX
typedef std::string_view  attribute_type;
#else
typedef std::string                     attribute_type;
#endif

int main()
{
     namespace x3 = boost::spirit::x3;

     auto const string_literal = x3::rule<struct _, attribute_type> {} =
     x3::lexeme[
            '"'
         >> x3::raw[ *(x3::char_ - '"') ]
         >> '"'
     ];

     std::vector<std::string> const test_cases {
         R"("FooBar")",
         };

     /* expected output:
      * parse '"FooBar"': succeeded: 'FooBar'
      */

     typedef std::string::const_iterator iterator_type;

     for(auto str: test_cases) {
         iterator_type iter = str.begin();
         iterator_type const end = str.end();

         attribute_type attr;

         std::cout << "parse '" << str << "': ";

         bool r = phrase_parse(iter, end, string_literal, x3::space, attr);

         if (r && iter == end) {
             std::cout << "succeeded: '" << attr << "'\n";
         } else {
             std::cout << "*** failed ***\n";
         }
     }

     return 0;
}
--->8---

Is it a bug? Something more to specialize as move_to?

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: string_view and c.insert(c.end(), std::move(val)) compile error

Mike Gresens
This post was updated on .
Hello Olaf,

in my opinion it is no bug.
In my opinion it is a bad idea to try to parse into a string_view.

The string_view does not hold the parsed data (the string).
It is just a "pointer" to a constant string (+ size info).

So what the string_view points to if you parse a file? To a position in that file??!

In X3 any container you can parse into must hold the parsed data by itself (string, vector, set, map, etc.)
X3 uses container.insert() to append parsed data to the container.
So you see "basic_string_view<char>« has no member named »insert«" as error.

Best regards,
Mike...

PS: Might be interesting too:

http://stackoverflow.com/questions/35602390/spirit-x3-with-booststring-ref-construction-from-semantic-action
Loading...