質問

シンプルではいけない eol トリックをしますか?

#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);
}

出力:

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

なぜ後者の 2 つは失敗するのでしょうか?


関連する質問:

boost::spirit を使用して、レコードの一部を単独の行に配置するにはどうすればよいですか?

役に立ちましたか?

解決

あなたが使っているのは space phrase_parse への呼び出しのスキッパーとして。このパーサーは、次のいずれかの文字に一致します。 std::isspace true を返します (ASCII ベースの解析を行っていると仮定します)。このため、 \r\n 入力にあるものは、船長が見る前に船長に食べられてしまいます。 eol パーサー。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top