Spirit X3, two rules do not compile after being combined into one
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am currently learning how to use x3. As the title states, I have had success creating a grammar with a few simple rules, but upon combining two of these rules into one, the code no longer compiles. Here is the code for the AST portion:
namespace x3 = boost::spirit::x3;
struct Expression;
struct FunctionExpression {
std::string functionName;
std::vector<x3::forward_ast<Expression>> inputs;
};
struct Expression: x3::variant<int, double, bool, FunctionExpression> {
using base_type::base_type;
using base_type::operator=;
};
The rules I have created parse input formatted as {rangeMin, rangeMax}
:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct exp_pair_class, std::vector<ast::Expression>> const
exp_pair = "exp_pair";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const exp_pair_def = basic_expr >> ',' >> basic_expr;
auto const range_def = attr("computeRange") >> '{' >> exp_pair >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, exp_pair_def, range_def);
This code compiles fine. However, if I try to inline the exp_pair
rule into the range_def
rule, like so:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const range_def = attr("computeRange") >> '{' >> (
basic_exp >> ',' >> basic_exp
) >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, range_def);
The code fails to compile with a very long template error, ending with the line:
spirit/include/boost/spirit/home/x3/operator/detail/sequence.hpp:149:9: error: static assertion failed: Size of the passed attribute is less than expected.
static_assert(
^~~~~~~~~~~~~
The header file also includes this comment above the static_assert
:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
But I do not see why the code should fail. According to x3's compound attribute rules, the inlined portion in the parenthesis should have an attribute of type vector<ast::Expression>
, making the overall rule have the type tuple<string, vector<ast::Expression>
, so that it would be compatible with ast::FunctionExpression
. The same logic applies the more verbose three-rule version, the only difference being that I specifically declared a rule for the inner part and specifically stated its attribute needed to be of type vector<ast::Expression>
.
c++ boost-spirit boost-spirit-x3
add a comment |
I am currently learning how to use x3. As the title states, I have had success creating a grammar with a few simple rules, but upon combining two of these rules into one, the code no longer compiles. Here is the code for the AST portion:
namespace x3 = boost::spirit::x3;
struct Expression;
struct FunctionExpression {
std::string functionName;
std::vector<x3::forward_ast<Expression>> inputs;
};
struct Expression: x3::variant<int, double, bool, FunctionExpression> {
using base_type::base_type;
using base_type::operator=;
};
The rules I have created parse input formatted as {rangeMin, rangeMax}
:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct exp_pair_class, std::vector<ast::Expression>> const
exp_pair = "exp_pair";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const exp_pair_def = basic_expr >> ',' >> basic_expr;
auto const range_def = attr("computeRange") >> '{' >> exp_pair >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, exp_pair_def, range_def);
This code compiles fine. However, if I try to inline the exp_pair
rule into the range_def
rule, like so:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const range_def = attr("computeRange") >> '{' >> (
basic_exp >> ',' >> basic_exp
) >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, range_def);
The code fails to compile with a very long template error, ending with the line:
spirit/include/boost/spirit/home/x3/operator/detail/sequence.hpp:149:9: error: static assertion failed: Size of the passed attribute is less than expected.
static_assert(
^~~~~~~~~~~~~
The header file also includes this comment above the static_assert
:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
But I do not see why the code should fail. According to x3's compound attribute rules, the inlined portion in the parenthesis should have an attribute of type vector<ast::Expression>
, making the overall rule have the type tuple<string, vector<ast::Expression>
, so that it would be compatible with ast::FunctionExpression
. The same logic applies the more verbose three-rule version, the only difference being that I specifically declared a rule for the inner part and specifically stated its attribute needed to be of type vector<ast::Expression>
.
c++ boost-spirit boost-spirit-x3
2
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helperas
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.
– Bob Bills
Nov 22 '18 at 8:38
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
1
@BobBills this is exactly what I was looking for. By including theas
lambda, i can change the rule toauto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?
– CompileYourCake
Nov 22 '18 at 16:12
Why do you want to inline it? That's what rules for. If you pass a tuple (FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).
– Nikita Kniazev
Nov 23 '18 at 11:49
add a comment |
I am currently learning how to use x3. As the title states, I have had success creating a grammar with a few simple rules, but upon combining two of these rules into one, the code no longer compiles. Here is the code for the AST portion:
namespace x3 = boost::spirit::x3;
struct Expression;
struct FunctionExpression {
std::string functionName;
std::vector<x3::forward_ast<Expression>> inputs;
};
struct Expression: x3::variant<int, double, bool, FunctionExpression> {
using base_type::base_type;
using base_type::operator=;
};
The rules I have created parse input formatted as {rangeMin, rangeMax}
:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct exp_pair_class, std::vector<ast::Expression>> const
exp_pair = "exp_pair";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const exp_pair_def = basic_expr >> ',' >> basic_expr;
auto const range_def = attr("computeRange") >> '{' >> exp_pair >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, exp_pair_def, range_def);
This code compiles fine. However, if I try to inline the exp_pair
rule into the range_def
rule, like so:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const range_def = attr("computeRange") >> '{' >> (
basic_exp >> ',' >> basic_exp
) >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, range_def);
The code fails to compile with a very long template error, ending with the line:
spirit/include/boost/spirit/home/x3/operator/detail/sequence.hpp:149:9: error: static assertion failed: Size of the passed attribute is less than expected.
static_assert(
^~~~~~~~~~~~~
The header file also includes this comment above the static_assert
:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
But I do not see why the code should fail. According to x3's compound attribute rules, the inlined portion in the parenthesis should have an attribute of type vector<ast::Expression>
, making the overall rule have the type tuple<string, vector<ast::Expression>
, so that it would be compatible with ast::FunctionExpression
. The same logic applies the more verbose three-rule version, the only difference being that I specifically declared a rule for the inner part and specifically stated its attribute needed to be of type vector<ast::Expression>
.
c++ boost-spirit boost-spirit-x3
I am currently learning how to use x3. As the title states, I have had success creating a grammar with a few simple rules, but upon combining two of these rules into one, the code no longer compiles. Here is the code for the AST portion:
namespace x3 = boost::spirit::x3;
struct Expression;
struct FunctionExpression {
std::string functionName;
std::vector<x3::forward_ast<Expression>> inputs;
};
struct Expression: x3::variant<int, double, bool, FunctionExpression> {
using base_type::base_type;
using base_type::operator=;
};
The rules I have created parse input formatted as {rangeMin, rangeMax}
:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct exp_pair_class, std::vector<ast::Expression>> const
exp_pair = "exp_pair";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const exp_pair_def = basic_expr >> ',' >> basic_expr;
auto const range_def = attr("computeRange") >> '{' >> exp_pair >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, exp_pair_def, range_def);
This code compiles fine. However, if I try to inline the exp_pair
rule into the range_def
rule, like so:
rule<struct basic_exp_class, ast::Expression> const
basic_exp = "basic_exp";
rule<struct range_class, ast::FunctionExpression> const
range = "range";
auto const basic_exp_def = double_ | int_ | bool_;
auto const range_def = attr("computeRange") >> '{' >> (
basic_exp >> ',' >> basic_exp
) >> '}';
BOOST_SPIRIT_DEFINE(basic_expr, range_def);
The code fails to compile with a very long template error, ending with the line:
spirit/include/boost/spirit/home/x3/operator/detail/sequence.hpp:149:9: error: static assertion failed: Size of the passed attribute is less than expected.
static_assert(
^~~~~~~~~~~~~
The header file also includes this comment above the static_assert
:
// If you got an error here, then you are trying to pass
// a fusion sequence with the wrong number of elements
// as that expected by the (sequence) parser.
But I do not see why the code should fail. According to x3's compound attribute rules, the inlined portion in the parenthesis should have an attribute of type vector<ast::Expression>
, making the overall rule have the type tuple<string, vector<ast::Expression>
, so that it would be compatible with ast::FunctionExpression
. The same logic applies the more verbose three-rule version, the only difference being that I specifically declared a rule for the inner part and specifically stated its attribute needed to be of type vector<ast::Expression>
.
c++ boost-spirit boost-spirit-x3
c++ boost-spirit boost-spirit-x3
asked Nov 22 '18 at 6:52
CompileYourCakeCompileYourCake
133
133
2
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helperas
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.
– Bob Bills
Nov 22 '18 at 8:38
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
1
@BobBills this is exactly what I was looking for. By including theas
lambda, i can change the rule toauto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?
– CompileYourCake
Nov 22 '18 at 16:12
Why do you want to inline it? That's what rules for. If you pass a tuple (FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).
– Nikita Kniazev
Nov 23 '18 at 11:49
add a comment |
2
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helperas
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.
– Bob Bills
Nov 22 '18 at 8:38
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
1
@BobBills this is exactly what I was looking for. By including theas
lambda, i can change the rule toauto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?
– CompileYourCake
Nov 22 '18 at 16:12
Why do you want to inline it? That's what rules for. If you pass a tuple (FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).
– Nikita Kniazev
Nov 23 '18 at 11:49
2
2
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helper
as
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.– Bob Bills
Nov 22 '18 at 8:38
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helper
as
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.– Bob Bills
Nov 22 '18 at 8:38
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
1
1
@BobBills this is exactly what I was looking for. By including the
as
lambda, i can change the rule to auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?– CompileYourCake
Nov 22 '18 at 16:12
@BobBills this is exactly what I was looking for. By including the
as
lambda, i can change the rule to auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?– CompileYourCake
Nov 22 '18 at 16:12
Why do you want to inline it? That's what rules for. If you pass a tuple (
FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).– Nikita Kniazev
Nov 23 '18 at 11:49
Why do you want to inline it? That's what rules for. If you pass a tuple (
FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).– Nikita Kniazev
Nov 23 '18 at 11:49
add a comment |
1 Answer
1
active
oldest
votes
Spirit x3 is probably seeing the result of the inlined rule as two separate ast::Expression
instead of the std::vector<ast::Expression>
required by the ast::FunctionExpression
struct.
To solve it we can use a helper as
lambda as mentioned in another answer to specify the return type of a sub-rule.
And the modified range_def would become:
auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425339%2fspirit-x3-two-rules-do-not-compile-after-being-combined-into-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Spirit x3 is probably seeing the result of the inlined rule as two separate ast::Expression
instead of the std::vector<ast::Expression>
required by the ast::FunctionExpression
struct.
To solve it we can use a helper as
lambda as mentioned in another answer to specify the return type of a sub-rule.
And the modified range_def would become:
auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
add a comment |
Spirit x3 is probably seeing the result of the inlined rule as two separate ast::Expression
instead of the std::vector<ast::Expression>
required by the ast::FunctionExpression
struct.
To solve it we can use a helper as
lambda as mentioned in another answer to specify the return type of a sub-rule.
And the modified range_def would become:
auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
add a comment |
Spirit x3 is probably seeing the result of the inlined rule as two separate ast::Expression
instead of the std::vector<ast::Expression>
required by the ast::FunctionExpression
struct.
To solve it we can use a helper as
lambda as mentioned in another answer to specify the return type of a sub-rule.
And the modified range_def would become:
auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
Spirit x3 is probably seeing the result of the inlined rule as two separate ast::Expression
instead of the std::vector<ast::Expression>
required by the ast::FunctionExpression
struct.
To solve it we can use a helper as
lambda as mentioned in another answer to specify the return type of a sub-rule.
And the modified range_def would become:
auto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
answered Nov 23 '18 at 8:26
Bob BillsBob Bills
39436
39436
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425339%2fspirit-x3-two-rules-do-not-compile-after-being-combined-into-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It probably is seeing the inlined rule as two Expression instead of vector<Expected>, another answer has a helper
as
so that you can specify the type. stackoverflow.com/questions/49932608/… can't try it now so don't know if it works.– Bob Bills
Nov 22 '18 at 8:38
@BobBills Thanks, I was gonna post the same idea as a comment :)
– sehe
Nov 22 '18 at 13:20
1
@BobBills this is exactly what I was looking for. By including the
as
lambda, i can change the rule toauto const range_def = attr("computeRange") >> '{' >> as<std::vector<ast::Expression>>(basic_exp >> ',' >> basic_exp) >> '}';
, and it compiles sucessfully. Could you put your suggestion into a full answer so I can accept it?– CompileYourCake
Nov 22 '18 at 16:12
Why do you want to inline it? That's what rules for. If you pass a tuple (
FunctionExpression
) to a sequence parser (... >> ...
), Spirit will not do any magic (will not try to do a sequence collapse).– Nikita Kniazev
Nov 23 '18 at 11:49