以下是boost spirit文档中的employee.cpp源文件。它是'结构员工',后面是一个宏,它告诉'结构员工'的聚合,其次是员工解析器。使用精神解析类?
我想适应这个为我的目的,而不是使用'结构员工',我有一些我想用来代替'结构员工'的类。
我正在试图替换类结构员工',但我没有看到宏融合做到这一点?而我不想把它放在结构体中的原因是因为我不得不将它从struct复制到我的类中,而这看起来没有必要,更不用说性能问题了。
经过多思考之后,我可能不会理解Fusion和元组的用途,因此,也许我必须这样使用它,然后将数据移动到我自己的类结构中。
任何指导?
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// Our employee struct
///////////////////////////////////////////////////////////////////////////
struct employee
{
int age;
std::string surname;
std::string forename;
double salary;
};
using boost::fusion::operator<<;
}}
// We need to tell fusion about our employee struct
// to make it a first-class fusion citizen. This has to
// be in global scope.
BOOST_FUSION_ADAPT_STRUCT(
client::ast::employee,
(int, age)
(std::string, surname)
(std::string, forename)
(double, salary)
)
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// Our employee parser
///////////////////////////////////////////////////////////////////////////////
namespace parser
{
namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
using x3::int_;
using x3::lit;
using x3::double_;
using x3::lexeme;
using ascii::char_;
x3::rule<class employee, ast::employee> const employee = "employee";
auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"'];
auto const employee_def =
lit("employee")
>> '{'
>> int_ >> ','
>> quoted_string >> ','
>> quoted_string >> ','
>> double_
>> '}'
;
BOOST_SPIRIT_DEFINE(employee);
}
}