且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有Boost Spirit的OBJ解析器-忽略评论

更新时间:2023-02-16 18:07:04

想到的最简单的方法是简单地使注释可跳过:

The simplest way that comes to mind is to simply make comments skippable:

bool ok = qi::phrase_parse(
        f,l,
         (
               ("v"  >> qi::double_ >> qi::double_ >> qi::double_) |
               ("vn" >> qi::double_ >> qi::double_ >> qi::double_)
          ) 
          % qi::eol,
        ('#' >> *(qi::char_ - qi::eol) >> qi::eol | qi::blank), b_vertices);

请注意,如果#出现在行内某处,这也会识别"注释.这可能很好(因为它将使解析失败,除非它是在其他有效输入行上尾随的注释).

Note that this also 'recognizes' comments if # appears somewhere inside the line. This is probably just fine (as it would make the parsing fail, unless it was a comment trailing on an otherwise valid input line).

查看 在Coliru上直播

See it Live on Coliru

或者,使用一些凤凰魔法来处理注释行",就像处理"vn"或"v"行一样.

Alternatively, use some phoenix magic to handle "comment lines" just as you handle a "vn" or "v" line.