1

When I remove x3::eps in the below rule, the string result from the first partial match is still in the second match, resulting in a string with duplicated content.

If I add another case in between I still only get 1 duplicate instead of two.

  • Why do I need to use x3::eps, or, what am I misunderstanding in the evaluation of rules and synthesized attributes?
  • Should I use lookahead instead?
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace x3 = boost::spirit::x3;
namespace ascii = x3::ascii;

struct AstChannel {
    std::string label;
    bool complement;
};

x3::rule<class AstLabel, std::string> const astLabel = "astLabel";
auto const astLabel_def = ascii::lower >> *(ascii::alnum);
BOOST_SPIRIT_DEFINE(astLabel)

x3::rule<class AstChannel, AstChannel> const astChannel = "astChannel";
auto const astChannel_def = astLabel >> '!' >> x3::attr(true)
                          | astLabel >> x3::eps >> x3::attr(false) ;
BOOST_SPIRIT_DEFINE(astChannel) 

BOOST_FUSION_ADAPT_STRUCT(
    AstChannel,
    (std::string, label)
    (bool, complement)
)

int main() {
    std::string str("hello");
    auto iter = str.begin();
    auto end = str.end();
    AstChannel channel;
    bool r = phrase_parse(iter, end, astChannel, ascii::space, channel);
    if (r) {
        std::cout << channel.label << ',' << channel.complement << std::endl;
    }
    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
senevoldsen
  • 337
  • 3
  • 8

1 Answers1

1

The real answer is: force atomic attribute propagation of container attributes (e.g. with x3::hold or semantic actions).

The better answer is: use x3::matches:

Live On Coliru

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
using namespace std::string_literals;

struct AstChannel {
    std::string label;
    bool complement;
};

BOOST_FUSION_ADAPT_STRUCT(AstChannel, label, complement)

namespace Grammar {
    namespace x3 = boost::spirit::x3;
    namespace ascii = x3::ascii;

    auto const label   = x3::rule<struct labelRule, std::string> {"label" }
                       = x3::lexeme[ascii::lower >> *(ascii::alnum)];

    auto const channel = label >> x3::matches['!'];

    auto const entry   = x3::skip(ascii::space) [ channel ];
}

int main() {
    auto const str = "hello"s;

    AstChannel channel;
    if (parse(str.begin(), str.end(), Grammar::entry, channel)) {
        std::cout << channel.label << ',' << std::boolalpha << channel.complement << "\n";
    }
}

Prints

hello,false
sehe
  • 374,641
  • 47
  • 450
  • 633