I'm migrating my code from Boost Qi to X3. I have non-terminal rule expression
that is compiling to structure ast::Expression
. For the minimal reproducing example I left 2 statements that forms expression
. value
creates new while '(' expression ')'
should allow use brackets and just propogate result of expression
to parent definition.
In the Qi I had %=
operator that copied attribute to value. So the question - how to get rid of lambda e2e
in the following code?
#include <iostream>
#include <tuple>
#include <string>
#include <variant>
#include <vector>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
namespace x3 = boost::spirit::x3;
namespace ast
{
enum class Cmd : int
{
const_val = 42
};
using data_t = x3::variant<int, double>;
struct Expression
{
Cmd _cmd;
std::string _args;
};
}//ast
BOOST_FUSION_ADAPT_STRUCT(ast::Expression, _cmd, _args)
inline std::ostream& operator << (std::ostream& os, const ast::Expression& val)
{
os << "{" << (int)val._cmd << ':' << val._args << "}";
return os;
}
namespace client
{
namespace x3 = boost::spirit::x3;
x3::rule<class expression, ast::Expression> const expression("expression_stmt");
x3::rule<class value, ast::data_t> const value("value_stmt");
auto const value_def =
x3::int_
| x3::double_
;
// construct expression form plain constant
auto val_to_expr = [](auto& ctx) {
auto& a1 = _attr(ctx);
_val(ctx) = ast::Expression{
ast::Cmd::const_val,
std::to_string(boost::get<int>(a1))
};
};
// copy attribute to result value
auto e2e = [](auto& ctx) {
auto& a1 = _attr(ctx);
_val(ctx) = a1;
};
auto const expression_def =
value[val_to_expr]
// !!!! why I need e2e to copy attr to value?
| ('(' > expression > ')') [e2e]
;
BOOST_SPIRIT_DEFINE(expression, value);
}//ns:client
auto parse_text(const std::string& s)
{
namespace x3 = boost::spirit::x3;
auto iter = s.cbegin();
auto end_iter = s.cend();
ast::Expression res;
x3::ascii::space_type space;
bool success = x3::phrase_parse(iter, end_iter,
client::expression, space,
res);
if (success && iter == end_iter)
{
std::cout << "Success\n";
}
else {
std::cout << "Parse failure\n{";
while (iter != end_iter)
std::cout << *iter++;
std::cout << "}\n";
}
return res;
}
int main()
{
auto test = +[](const std::string& s) {
std::cout << "parsing " << s << '\n';
auto res = parse_text(s);
std::cout << "'" << res << "'\n";
};
;
test("123");
test("(123)");
}
You can set the
force_attribute
template argument onx3::rule
:However, this means that all attribute assignments will be forced, so you have to facilitate
val_to_expr
. You easily can, using the relevant constructor:Also, in order for the constructor to be used, you should remove the Fusion adaptation, which you weren't using anyways. Now everything fits in roughly half the size of the code:
Live On Coliru
Printing
Bonus/Caveat
Ironically, at the end of that, you no longer need
force_attribute
to begin with, because you don't use any semantic actions (see Boost Spirit: "Semantic actions are evil"?).Also, the production
int_ | double_
will never match doubles (see e.g. Why doesn't this boost::spirit::qi rule successfully parse?).It looks as though you're crafting the classical expression parser. As such, you'd very much expect values to be expressions:
Now you can enjoy no-semantic-action Fusion adaptation bliss:
Printing Live On Coliru:
Having reached this point, it would become more obvious that it is probably NOT necessary to put
_args
as a string. I bet you'd rather recurse withstd::vector<Expression>
?And a minor change of the
Command
rule:Now you can parse arbitrarily nested expressions, which do not even flatten to strings in the AST:
Live On Coliru
Printing: