好像');'
本来应该 ”);”?
另外,因为你有一个船长(x3::space
在通话中phrase_parse
)这没有多大意义:
- 还指定
space
在你的解析器表达式中(它永远不会匹配)
- 不包裹
nameRule
里面一个lexeme
or noskip
指示。也可以看看提升精神船长问题 https://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965
因此,首先要努力使其发挥作用:
std::string functionParser(const std::string& input) {
namespace x3 = boost::spirit::x3;
auto nameRule = x3::lexeme [x3::alpha >> *x3::alnum];
auto fcnPrototypeRule = nameRule >> *nameRule;
auto fcnRule = fcnPrototypeRule >> x3::char_('(') >> -(nameRule % ',') >> x3::char_(");");
std::string output;
x3::phrase_parse(input.begin(), input.end(), fcnRule, x3::space, output);
return output;
}
但是,您会注意到它会返回ndn()
(住在科里鲁 http://coliru.stacked-crooked.com/a/5343711408767b76).
我认为这基本上是由于您的 AS (std::string
) 与语法不太相符。我想说看起来你的意思是“匹配”而不是“解析”,我会使用x3::raw
暴露原始匹配:
Live On Colriu http://coliru.stacked-crooked.com/a/5bd37368dd5c7b3d
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <iomanip>
std::string functionParser(const std::string& input) {
namespace x3 = boost::spirit::x3;
auto nameRule = x3::lexeme [x3::alpha >> *x3::alnum];
auto fcnPrototypeRule = nameRule >> *nameRule;
auto fcnRule = x3::raw[ fcnPrototypeRule >> '(' >> -(nameRule % ',') >> ')' >> ';' ];
std::string output;
x3::phrase_parse(input.begin(), input.end(), fcnRule, x3::space, output);
return output;
}
int main() {
for (auto s : {
"extern void myFunction();",
})
{
std::cout << std::quoted(s) << " -> " << std::quoted(functionParser(s)) << "\n";
}
}