I try to parse
list<char> fldName
I used the nested structures. But I have a trouble with parsing when one structure nested in other. Look at the sollowing minimal sample code:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>
#include <string_view>
using namespace std::string_view_literals;
using namespace boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
namespace client::ast
{
struct ValidType
{
std::string Name;
std::string SubName1;
std::string SubName2;
};
struct StructField
{
ValidType Type;
std::string Name;
};
} // namespace client::ast
BOOST_FUSION_ADAPT_STRUCT(client::ast::ValidType,
Name, SubName1, SubName2
)
BOOST_FUSION_ADAPT_STRUCT(client::ast::StructField,
Type, Name
)
namespace client::parser
{
using ascii::char_;
template <typename T> static auto as = [](auto p) { return rule<struct tag, T> {"as"} = p; };
#define STRING(x) as<std::string>(x)
rule<class ValidType, ast::ValidType> const ValidType = "ValidType";
rule<class StructField, ast::StructField> const StructField = "StructField";
auto const ValidName = lexeme[(alpha | char_('_')) > *(alnum | char_('_'))];
auto const ValidType_SecondPart = char('<') > STRING(ValidName) > ('>' | ',' > STRING(ValidName) > '>');
auto const ValidType_def = STRING(ValidName) > -(ValidType_SecondPart);
auto const StructField_def = ValidType_def > STRING(ValidName);
BOOST_SPIRIT_DEFINE(ValidType);
BOOST_SPIRIT_DEFINE(StructField);
} // namespace client::parser
int main()
{
using boost::spirit::x3::ascii::space;
auto theData = R"(
list<char> fldName
)"sv;
using client::parser::StructField;
client::ast::StructField fld;
bool result = phrase_parse(theData.begin(), theData.end(), StructField, space, fld);
return result;
}
I receive following error:
Error C2338 Size of the passed attribute is less than expected
But I have no idea what is wrong. Its looks like boost::spirit::x3 have a bug with parsing nested structures.
Is there exists any way how to parse nested stuctures?
Answer - ValidType_def -> ValidType :
auto const StructField_def = ValidType_def > STRING(ValidName);
->
auto const StructField_def = ValidType > STRING(ValidName);