Boost.Spirit.Qi: dynamically create a difference parser during parsing
The difference parser can be created by the binary operator -
(minus):
rule = qi::char_ - qi::lit("}}")
or even compound differences:
rule = qi::char_ - qi::lit("}}") - qi::lit("]]")
But how could I generate the entire result of the delta parser during parsing?
I am guessing it could be some kind of shape like below:
phoenix::function<difference_parser_impl> difference_parser;
rule = qi::lazy(difference_parser(qi::char_, {"}}", "]]"}));
The part here {..., ..., ...}
will actually be the stl container, but that's not the point; I can handle this part.
I found a template qi::difference<Left, Right>
- but I haven't been able to figure out how to use it.
source to share
It seems to me that you are not looking so much for a dynamic "difference" expression, but rather a dynamic "variadic alternative (a | b | c ...)" expression:
expr - a - b - c
equivalent to expr - (a|b|c)
Then you can easily achieve the difference using either:
expr - orCombine(alternatives)
or
!orCombine(alternatives) >> expr
Now, to do this, there are a lot of rough edges, which I will explain first. Fortunately, there is an easier way, using qi::symbols
which I will demonstrate right after that.
Difficult material
If you want, you can "generate" alternative parser expressions on demand, with a fair amount of magic. I showed you how to do it in this answer:
But
- it is fraught with traps (as a proto-expression can not be copied) 1
-
it is convenient to use variators to avoid intermediate storage (note the
deepcopy_
Undefined Behavior operand):template<typename ...Expr> void parse_one_of(Expr& ...expressions) { auto parser = boost::fusion::fold( boost::tie(expressions...), qi::eps(false), deepcopy_(arg2 | arg1) );
Having seen how you need a really dynamic composition of an alternative parser, I don't see how this can be adapted to your needs without an explosion of complexity and the possibility for a subtle error ( trust me, I've already tried ).
So, instead, I recommend the tried and true approach that "abuses" the existing "dynamic" parser:
Simplify use qi::symbols
This idea is borrowed from the famous "Nabialek Trick". It uses qi :: characters and therefore has excellent performance 2 .
Without further ado, this is an example of how you could use it, starting with a vector of string literals:
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, std::string(), Skipper>
{
parser() : parser::base_type(start)
{
static const std::vector<std::string> not_accepted { "}}", "]]" };
using namespace qi;
exclude = exclusions(not_accepted);
start = *(char_ - exclude);
BOOST_SPIRIT_DEBUG_NODE(start);
}
private:
qi::rule<It, std::string(), Skipper> start;
typedef qi::symbols<char, qi::unused_type> Exclude;
Exclude exclude;
template<typename Elements>
Exclude exclusions(Elements const& elements) {
Exclude result;
for(auto& el : elements)
result.add(el);
return result;
}
};
Full working example of this is here: http://coliru.stacked-crooked.com/view?id=ddbb2549674bfed90e3c8df33b048574-7616891f9fd25da6391c2728423de797 and it prints
parse success
data: 123
trailing unparsed: ']] 4'
Complete code
For future reference:
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, std::string(), Skipper>
{
parser() : parser::base_type(start)
{
static const std::vector<std::string> not_accepted { "}}", "]]" };
using namespace qi;
exclude = exclusions(not_accepted);
start = *(char_ - exclude);
BOOST_SPIRIT_DEBUG_NODE(start);
}
private:
qi::rule<It, std::string(), Skipper> start;
typedef qi::symbols<char, qi::unused_type> Exclude;
Exclude exclude;
template<typename Elements>
Exclude exclusions(Elements const& elements) {
Exclude result;
for(auto& el : elements)
result.add(el);
return result;
}
};
int main()
{
const std::string input = "1 2 3]] 4";
typedef std::string::const_iterator It;
It f(begin(input)), l(end(input));
parser<It> p;
std::string data;
bool ok = qi::phrase_parse(f,l,p,qi::space,data);
if (ok)
{
std::cout << "parse success\n";
std::cout << "data: " << data << "\n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
}
1 I believe this issue is about to be fixed in a new new version of Spirit (currently duplicated "Spirit X3" for the experimental version)
2 He uses Runs to find matches
source to share