Parse tab delimited file with Boost.Spirit where entries may contain whitespace in

我与影子孤独终老i 提交于 2019-12-25 04:31:06

问题


I want to parse a tab delimited file using Boost.Spirit (Qi). My file looks something like this:

John Doe\tAge 23\tMember
Jane Doe\tAge 25\tMember
...

Is it possible to parse this with a skip parser? The problem I have right now is, that boost::spirit::ascii:space also skips the whitespace within the name of the person. How would the phrase_parse(...) call look like?

I am also using the Boost.Fusion tuples for convient storing of the results in a struct:

struct Person
{
    string name;
    int age;
    string status;
};

This seems to work for the name:

String %= lexeme[+(char_-'\t')];

It matches everything char that is not a tab. It is then used as part of the bigger rule:

Start %= Name >> Age >> Status;  

回答1:


Q. Is it possible to parse this with a skip parser?

A. No, it's not possible to parse anything with the skip parser. Skippers achieve the opposite: they disregard certain input information.

However, what you seem to be looking for something like this hack: (I don't recommend it)

  • Read empty values with boost::spirit

Now, you could look at my other answers for proper ways to parse CSV/TSV dealing with embedded whitespace, quoted values, escaped quotes etc. (I believe one even shows line-continuation characters)

  • How to parse csv using boost::spirit
  • Parse quoted strings with boost::spirit
  • How to make my split work only on one real line and be capable to skip quoted parts of string?


来源:https://stackoverflow.com/questions/31536086/parse-tab-delimited-file-with-boost-spirit-where-entries-may-contain-whitespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!