Dynamic loop counter on x3
Takatoshi Kondo <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <CA+SaqLFP7_JEK4gnMp3o-Jo6Qvqn+eHJRHzXMA2PfztZkrCjcQ@mail.gmail.com> |
Hello,
I'm using spirit x3. I'd like to use `repeat(N)[a]` and want to
determin N dynamically. I found the document for qi. I tried to do the
same thing the last part of
http://www.boost.org/libs/spirit/doc/html/spirit/qi/reference/directive/repeat.html
on x3.
Here is the code I wrote:
http://melpon.org/wandbox/permlink/JfQQpSb2cS7an8jP
#include <iostream>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
int main() {
std::string str;
int n = 5;
std::string source = "\x0bHello World";
x3::parse(
source.begin(), source.end(),
x3::char_[([&n](auto& ctx){ n = _attr(ctx); })] >>
x3::repeat(n)[x3::char_][([&str](auto& ctx){ str = _attr(ctx);})]);
std::cout << n << ',' << str << std::endl; // will print "11,Hello World"
}
I got the following output:
11,Hello
It seems that the value 'n' is not updated.
Am I missing something or x3 doesn't support dynamic repeat count yet?
Thanks,
Takatoshi Kondo
------------------------------------------------------------------------------