我正在尝试使用boost精神获取我正在解析的文件的当前行。我创建了一个语法类和我的结构来解析我的命令。我还想跟踪找到命令的哪一行,并将其解析到我的结构中。我已将我的istream文件迭代器包装在multi_pass迭代器中,然后将其包装在boost :: spirit :: classic :: position_iterator2中。在我的语法规则中,如何获得迭代器的当前位置,或者这是不可能的?
更新:它类似于那个问题,但我只需要能够保持所有已处理行的计数。我不需要在解决方案中完成所有额外的缓冲。
答案 0 :(得分:1)
更新:它类似于那个问题,但我只需要能够保持所有已处理行的计数。我不需要在解决方案中完成所有额外的缓冲。
保持所有处理线的计数与“获取当前线”几乎不相同。
如果这是您所需要的,请在解析后检查:
<强> Live On Wandbox 强>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <fstream>
#include <set>
namespace qi = boost::spirit::qi;
int main() {
using It = boost::spirit::istream_iterator;
std::ifstream ifs("main.cpp");
boost::spirit::line_pos_iterator<It> f(It(ifs >> std::noskipws)), l;
std::set<std::string> words;
if (qi::phrase_parse(f, l, *qi::lexeme[+qi::graph], qi::space, words)) {
std::cout << "Parsed " << words.size() << " words";
if (!words.empty())
std::cout << " (from '" << *words.begin() << "' to '" << *words.rbegin() << "')";
std::cout << "\nLast line processed: " << boost::spirit::get_line(f) << "\n";
}
}
打印
Parsed 50 words (from '"' to '}')
Last line processed: 22
如果你说“不,等等,我真的做了想要获得当前行/解析/”。真正的完整monty在这里:
以下是使用iter_pos
完全裁剪的版本:
<强> Live On Wandbox 强>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <boost/spirit/repository/include/qi_iter_pos.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <fstream>
#include <map>
namespace qi = boost::spirit::qi;
namespace qr = boost::spirit::repository::qi;
using LineNum = size_t;
struct line_number_f {
template <typename It> LineNum operator()(It it) const { return get_line(it); }
};
static boost::phoenix::function<line_number_f> line_number_;
int main() {
using Underlying = boost::spirit::istream_iterator;
using It = boost::spirit::line_pos_iterator<Underlying>;
qi::rule<It, LineNum()> line_no = qr::iter_pos [ qi::_val = line_number_(qi::_1) ];
std::ifstream ifs("main.cpp");
It f(Underlying{ifs >> std::noskipws}), l;
std::multimap<LineNum, std::string> words;
if (qi::phrase_parse(f, l, +(line_no >> qi::lexeme[+qi::graph]), qi::space, words)) {
std::cout << "Parsed " << words.size() << " words.\n";
if (!words.empty()) {
auto& first = *words.begin();
std::cout << "First word: '" << first.second << "' (in line " << first.first << ")\n";
auto& last = *words.rbegin();
std::cout << "Last word: '" << last.second << "' (in line " << last.first << ")\n";
}
std::cout << "Line 20 contains:\n";
auto p = words.equal_range(20);
for (auto it = p.first; it != p.second; ++it)
std::cout << " - '" << it->second << "'\n";
}
}
印刷:
Parsed 166 words.
First word: '#include' (in line 1)
Last word: '}' (in line 46)
Line 20 contains:
- 'int'
- 'main()'
- '{'