bool myparse(std::istream& input, const std::string filename, DataType &result) { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; namespace classic = boost::spirit::classic; typedef std::istreambuf_iterator<char> base_iterator_type; base_iterator_type in_begin(input); // convert input iterator to forward iterator, usable by spirit parser typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type; forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin); forward_iterator_type fwd_end; // wrap forward iterator with position iterator, to record the position typedef classic::position_iterator2<forward_iterator_type> pos_iterator_type; pos_iterator_type position_begin(fwd_begin, fwd_end, filename); pos_iterator_type position_end; typedef forward_iterator_type used_iterator_type; used_iterator_type s = fwd_begin; used_iterator_type e = fwd_end; // typedef pos_iterator_type used_iterator_type; // used_iterator_type s = position_begin; // used_iterator_type e = position_end; qi::rule<used_iterator_type> skipper = ascii::space | '#' >> *(ascii::char_ - qi::eol) >> qi::eol; // comment skipper, Grammar<used_iterator_type, qi::rule<used_iterator_type> > g; bool r = false; try { r = phrase_parse(s, e, g, skipper, result); } catch(const qi::expectation_failure<used_iterator_type>& e) { std::stringstream msg; // const classic::file_position_base<std::string>& pos = e.first.get_position(); // msg << // "parse error at file " << pos.file << // " line " << pos.line << " column " << pos.column << std::endl << // "'" << e.first.get_currentline() << "'" << std::endl << // std::setw(pos.column) << " " << "^- here"; msg << "parse error"; throw std::runtime_error(msg.str()); } return r; }
bool SurveyBuilder::buildQuestion(std::istream& input, Question*& output) { base_iterator_type in_begin(input); // convert input iterator to forward iterator, usable by spirit parser forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin); forward_iterator_type fwd_end; // wrap forward iterator with position iterator, to record the position pos_iterator_type position_begin(fwd_begin, fwd_end); pos_iterator_type position_end; bool ret = spirit::qi::phrase_parse(position_begin, position_end, grammar.question, spirit::ascii::blank, output); return ret && position_begin == position_end; }
// implementation std::vector<double> parse(std::istream& input, const std::string& filename) { // iterate over stream input typedef std::istreambuf_iterator<char> base_iterator_type; base_iterator_type in_begin(input); // convert input iterator to forward iterator, usable by spirit parser typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type; forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin); forward_iterator_type fwd_end; // wrap forward iterator with position iterator, to record the position typedef classic::position_iterator2<forward_iterator_type> pos_iterator_type; pos_iterator_type position_begin(fwd_begin, fwd_end, filename); pos_iterator_type position_end; // prepare output std::vector<double> output; // parse try { qi::phrase_parse( position_begin, position_end, // iterators over input qi::double_ > *(',' > qi::double_) >> qi::eoi, // recognize list of doubles ascii::space | '#' >> *(ascii::char_ - qi::eol) >> qi::eol, // comment skipper output); // doubles are stored into this object } catch(const qi::expectation_failure<pos_iterator_type>& e) { const classic::file_position_base<std::string>& pos = e.first.get_position(); std::stringstream msg; msg << "parse error at file " << pos.file << " line " << pos.line << " column " << pos.column << std::endl << "'" << e.first.get_currentline() << "'" << std::endl << std::setw(pos.column) << " " << "^- here"; throw std::runtime_error(msg.str()); } // return result return output; }
bool SurveyBuilder::buildSurvey(std::istream& input, Survey*& output) { base_iterator_type in_begin(input); // convert input iterator to forward iterator, usable by spirit parser forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin); forward_iterator_type fwd_end; // wrap forward iterator with position iterator, to record the position pos_iterator_type position_begin(fwd_begin, fwd_end); pos_iterator_type position_end; bool ret; try { ret = spirit::qi::phrase_parse(position_begin, position_end, grammar, spirit::ascii::blank, output); } catch(const qi::expectation_failure<pos_iterator_type>& e) { const classic::file_position_base<std::string>& pos = e.first.get_position(); std::stringstream msg; msg << "parse error at line " << pos.line << " column " << pos.column << std::endl; msg << "'" << e.first.get_currentline() << "'" << std::endl; throw std::runtime_error(msg.str()); } return ret && position_begin == position_end; }
int main (int argc, char **argv) { /* Where the magic happens */ argp_parse (&argp, argc, argv, 0, 0, &arguments); if (!arguments.scriptfile) { printf("you must specify a script file\n"); exit(1); } // open file, disable skipping of whitespace std::ifstream in(arguments.scriptfile); in.unsetf(std::ios::skipws); // wrap istream into iterator boost::spirit::istream_iterator begin(in); boost::spirit::istream_iterator end; // wrap forward iterator with position iterator, to record the position typedef classic::position_iterator2<boost::spirit::istream_iterator> pos_iterator_type; pos_iterator_type position_begin(begin, end, arguments.scriptfile); pos_iterator_type position_end; testscript<pos_iterator_type> p; // create instance of parser try { std::vector<std::string> s; qi::phrase_parse(position_begin, position_end, p, qi::space, s); if (position_begin != position_end) throw qi::expectation_failure<pos_iterator_type>(position_begin, position_end,boost::spirit::info("general error")); } catch(const qi::expectation_failure<pos_iterator_type> e) { const classic::file_position_base<std::string>& pos = e.first.get_position(); std::cout << "parse error at file " << pos.file << " line " << pos.line << " column " << pos.column << std::endl << e.what_ << std::endl << "'" << e.first.get_currentline() << "'" << std::endl << std::setw(pos.column) << " " << "^- here" << std::endl; return -1; } catch(std::string e) { std::cout << "logical error: " << e << std::endl; return -1; } if (arguments.beautify) { ast.beautify(0); } else { if (!arguments.host) { printf("you must specify a host name\n"); exit(2); } if (!arguments.port) { printf("you must specify a port\n"); exit(3); } tr::TcpRunner runner(arguments.verbose); std::ofstream myReportFile; if (arguments.xmlfile) { myReportFile.open(arguments.xmlfile); if (myReportFile.is_open()) runner.setReportFile(myReportFile); } tr::Action::setRunnerInterface(&runner); tr::Test::setRunnerInterface(&runner); runner.setCurrentSection(&ast); runner.connect(arguments.host, arguments.port); runner.run(); } return 0; }