X3: How to parse "text" | "texture" into variant<text, texture> ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

X3: How to parse "text" | "texture" into variant<text, texture> ?

Mike Gresens
Hi,

some code here:

#include <boost/spirit/home/x3.hpp>
#include <boost/variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>
#include <iostream>

namespace x3 = boost::spirit::x3;

struct text_t {};
struct texture_t {};
typedef boost::variant<text_t, texture_t> object_t;
typedef std::string::const_iterator iterator;

BOOST_FUSION_ADAPT_STRUCT(text_t);
BOOST_FUSION_ADAPT_STRUCT(texture_t);

const x3::rule<class object_rule, object_t> object_rule("object");
const x3::rule<class text_rule, text_t> text_rule("text");
const x3::rule<class texture_rule, texture_t> texture_rule("texture");

const auto object_rule_def = text_rule | texture_rule;
const auto text_rule_def = x3::lit("text") > x3::lit('{') > x3::lit('}');
const auto texture_rule_def = x3::lit("texture") > x3::lit('{') > x3::lit('}');

BOOST_SPIRIT_DEFINE(object_rule, text_rule, texture_rule);

int main()
{
        const std::string text("text {}");
        const std::string texture("texture {}");
        object_t object;
        try
        {
                x3::phrase_parse(text.begin(), text.end(), object_rule, x3::space, object); // OK
                x3::phrase_parse(texture.begin(), texture.end(), object_rule, x3::space, object); // Failure
        }
        catch (const x3::expectation_failure<iterator>& failure)
        {
                std::cout << failure.what() << " : " << failure.which() << " : " << *failure.where() << std::endl;
        }
}

So how can I parse "text" | "texture" into the object variant?

Thanks,
Mike...
Reply | Threaded
Open this post in threaded view
|

Re: X3: How to parse "text" | "texture" into variant<text, texture> ?

Lee Clagett-2
On Sun, 14 Feb 2016 04:18:44 -0800 (PST)
Mike Gresens <[hidden email]> wrote:

> Hi,
>
> some code here:
>
> #include <boost/spirit/home/x3.hpp>
> #include <boost/variant.hpp>
> #include <boost/fusion/include/adapt_struct.hpp>
> #include <string>
> #include <iostream>
>
> namespace x3 = boost::spirit::x3;
>
> struct text_t {};
> struct texture_t {};
> typedef boost::variant<text_t, texture_t> object_t;
> typedef std::string::const_iterator iterator;
>
> BOOST_FUSION_ADAPT_STRUCT(text_t);
> BOOST_FUSION_ADAPT_STRUCT(texture_t);
>
> const x3::rule<class object_rule, object_t> object_rule("object");
> const x3::rule<class text_rule, text_t> text_rule("text");
> const x3::rule<class texture_rule, texture_t> texture_rule("texture");
>
> const auto object_rule_def = text_rule | texture_rule;
> const auto text_rule_def = x3::lit("text") > x3::lit('{') >
> x3::lit('}'); const auto texture_rule_def = x3::lit("texture") >
> x3::lit('{') > x3::lit('}');
>
> BOOST_SPIRIT_DEFINE(object_rule, text_rule, texture_rule);
>
> int main()
> {
> const std::string text("text {}");
> const std::string texture("texture {}");
> object_t object;
> try
> {
> x3::phrase_parse(text.begin(), text.end(),
> object_rule, x3::space, object); // OK
> x3::phrase_parse(texture.begin(), texture.end(),
> object_rule, x3::space, object); // Failure
> }
> catch (const x3::expectation_failure<iterator>& failure)
> {
> std::cout << failure.what() << " : " <<
> failure.which() << " : " << *failure.where() << std::endl;
> }
> }
>
> So how can I parse "text" | "texture" into the object variant?
>
> Thanks,
> Mike...
>

List texture first. The parser generated is exactly like a C++ `||`
expression in that the left side will be evaluated first.
Alternatively, you could do:
    `lit("text") >> ((lit("ure") >> ...) | (eps >> ... ))`
which is less readable, but eliminates backtracking.

Lee


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: How to parse "text" | "texture" into variant<text, texture> ?

Mike Gresens
Works! Thanks!