如何使错误处理对boost :: spirit起作用 [英] how to make error handling work for boost::spirit
问题描述
在boost :: spirit中,我添加了基于示例罗马字的错误处理代码.
in boost::spirit, I added error handling code based on example roman.
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
template <typename Iterator>
struct roman : qi::grammar<Iterator>
{
roman() : roman::base_type(start)
{
using qi::eps;
using qi::lit;
using qi::lexeme;
using qi::_val;
using qi::_1;
using ascii::char_;
// for on_error
using qi::on_error;
using qi::fail;
using phoenix::construct;
using phoenix::val;
start = +(lit('M') ) >> "</>";
on_error<fail>
(
start
, std::cout
<< val("Error! Expecting ")
// << _4 // what failed?
<< val(" here: \"")
// << construct<std::string>(_3, _2) // iterators to error-pos, end
<< val("\"")
<< std::endl
);
}
qi::rule<Iterator> start;
};
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "\t\tRoman Numerals Parser\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef roman<iterator_type> roman;
roman roman_parser; // Our grammar
std::string str;
unsigned result;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
//[tutorial_roman_grammar_parse
bool r = parse(iter, end, roman_parser, result);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "result = " << result << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \": " << rest << "\"\n";
std::cout << "-------------------------\n";
}
//]
}
std::cout << "Bye... :-) \n\n";
return 0;
}
我的问题是:
- 没有触发"on_error",为什么?
- 我评论<< _4",如果我想打印出故障部分,该怎么做?
推荐答案
三个步骤:
-
限定占位符:
Qualify the placeholders:
on_error<fail>(start,
std::cout
<< val("Error! Expecting ")
<< qi::_4
<< val(" here: \"")
<< construct<std::string>(qi::_3, qi::_2)
<< val("\"")
<< std::endl
);
您还需要确保您有期望点来触发错误处理程序.
You'll also need to make sure you have expectation points to trigger the error handler.
start = eps > +(lit('M') ) >> "</>";
例如参见 Boost.Spirit.Qi-错误发生规则的开始进行解释
(可选)为您的规则命名
start.name("start");
使用BOOST_SPIRIT_DEBUG_NODE(S)是隐式命名规则的另一种方法.
Using BOOST_SPIRIT_DEBUG_NODE(S) is another way to implicit name your rules.
查看它 在Coliru上生活 (已清理并在某些地方简化了
See it Live on Coliru (cleaned up and simplified in places)
现在打印(输入 iv
):
Now it prints (input iv
):
Error! Expecting <sequence>"M""</>" here: 'iv'
Parsing failed
stopped at: 'iv'
完整代码
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
#include <fstream>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
template <typename Iterator>
struct roman : qi::grammar<Iterator>
{
roman() : roman::base_type(start)
{
using namespace qi;
start = eps > +lit('M') >> "</>";
start.name("start");
on_error<fail>(start,
phx::ref(std::cout)
<< "Error! Expecting "
<< qi::_4
<< " here: '"
<< phx::construct<std::string>(qi::_3, qi::_2)
<< "'\n"
);
}
qi::rule<Iterator> start;
};
int main()
{
typedef std::string::const_iterator iterator_type;
roman<iterator_type> roman_parser; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
iterator_type iter = str.begin(), end = str.end();
unsigned result;
bool r = parse(iter, end, roman_parser, result);
if (r && iter == end)
{
std::cout << "Parsing succeeded\n";
std::cout << "result = " << result << std::endl;
}
else
{
std::string rest(iter, end);
std::cout << "Parsing failed\n";
std::cout << "stopped at: '" << rest << "'\n";
}
}
}
除了注释:这是我一直在测试的东西-尚未完全使其正常工作,但是错误处理程序正在被调用并按需使用输入.也许有帮助吗?
In addition to the comment: This is something I've been testing with - haven't exactly made it to work yet, but the error handler is getting invoked and eating input as it should. Maybe it could be of help?
static auto const at_eol = (*_1 == '\r') || (*_1 == '\n');
static auto const at_eoi = (_1 == _2);
on_error<retry>(start,
(
(phx::ref(std::cout) << "rule start: expecting " << _4 << " here: '" << escape_(_3, _2) << "'\n"),
phx::while_ (!at_eoi && !at_eol) [ ++_1, phx::ref(std::cout) << "\nadvance to newline\n" ],
phx::while_ (!at_eoi && at_eol) [ ++_1, phx::ref(std::cout) << "\neat newline\n" ],
phx::if_ (at_eoi) [ _pass = fail ]
)
);
另请参见multi_pass<>
See also the note under Important
in the documentation for multi_pass<>
这篇关于如何使错误处理对boost :: spirit起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!