我如何解析结束与boost :: spirit :: qi? [英] How do I parse end-of-line with boost::spirit::qi?

查看:159
本文介绍了我如何解析结束与boost :: spirit :: qi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不应该是一个简单的 eol 做什么?

  #include<算法> 
#include< boost / spirit / include / qi.hpp>
#include< iostream>
#include< string>
using boost :: spirit :: ascii :: space;
using boost :: spirit :: lit;
using boost :: spirit :: qi :: eol;
using boost :: spirit :: qi :: phrase_parse;

struct fix:std :: unary_function< char,void> {
fix(std :: string& result):result(result){}
void operator()(char c){
if(c =='\\\
') result + =\\\\
;
else if(c =='\r')result + =\\r;
else result + = c;
}
std :: string& result;
};

template< typename Parser>
void parse(const std :: string& s,const Parser& p){
std :: string :: const_iterator it = s.begin(),end = s.end
bool r = phrase_parse(it,end,p,space);
std :: string label;
fix f(label);
std :: for_each(s.begin(),s.end(),f);
std :: cout<< '''<< label<<\:\\\
< - ;
if(r&& it == end)std :: cout<< success!\\\
;
else std :: cout<< parse failed; r =< r<< '\\\
';
}

int main(){
parse(foo,lit(foo));
parse(foo\\\
,lit(foo)>>> eol);
parse(foo\r\\\
,lit(foo)>>> eol);
}

输出:

foo:
- success!
foo\\\

- parse failed; r = 0
foo\r\\\

- parse failed; r = 0



为什么后两个失败?





$ b b

相关问题:



使用boost :: spirit,如何要求记录的一部分在自己的行上?

解决方案

您正在使用 space 您对phrase_parse的调用。此解析器匹配 std :: isspace 返回true的任何字符(假设您正在进行基于ascii的解析)。因此,输入中的 \r\\\
会被您的船长吃掉,之后您的 eol parser。


Shouldn't a simple eol do the trick?

#include <algorithm>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
using boost::spirit::ascii::space;
using boost::spirit::lit;
using boost::spirit::qi::eol;
using boost::spirit::qi::phrase_parse;

struct fix : std::unary_function<char, void> {
  fix(std::string &result) : result(result) {}
  void operator() (char c) {
    if      (c == '\n') result += "\\n";
    else if (c == '\r') result += "\\r";
    else                result += c;
  }
  std::string &result;
};

template <typename Parser>
void parse(const std::string &s, const Parser &p) {
  std::string::const_iterator it = s.begin(), end = s.end();
  bool r = phrase_parse(it, end, p, space);
  std::string label;
  fix f(label);
  std::for_each(s.begin(), s.end(), f);
  std::cout << '"' << label << "\":\n" << "  - ";
  if (r && it == end) std::cout << "success!\n";
  else std::cout << "parse failed; r=" << r << '\n';
}

int main() {
  parse("foo",     lit("foo"));
  parse("foo\n",   lit("foo") >> eol);
  parse("foo\r\n", lit("foo") >> eol);
}

Output:

"foo":
  - success!
"foo\n":
  - parse failed; r=0
"foo\r\n":
  - parse failed; r=0

Why do the latter two fail?


Related question:

Using boost::spirit, how do I require part of a record to be on its own line?

解决方案

You are using space as the skipper for your calls to phrase_parse. This parser matches any character for which std::isspace returns true (assuming you're doing ascii based parsing). For this reason the \r\n in the input are eaten by your skipper before they can be seen by your eol parser.

这篇关于我如何解析结束与boost :: spirit :: qi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆