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 ParseVRMLFile(MultiBody *aMB, std::string aFileName, vector<BodyGeometricalData> &aListOfURLs, bool ReadGeometry) { if (aFileName == std::string("")) { std::cerr << "SpiritVRMLReader: Filename is empty." << std::endl; return 0; } ifstream aif; SkipGrammar aSkipGrammar; SpiritOpenHRP< Actions > aSpiritOpenHRP; aif.open(aFileName.c_str(),ifstream::in|ifstream::binary); if (!aif.is_open()) { cout << "Unable to open file "<< aFileName<< endl; return 0; } else { /* if (aSpiritOpenHRP.getVerbose()>10) { cout << "Succeeded in opening " << aFileName <<endl; } */ } aif.open(aFileName.c_str(),ifstream::in|ifstream::binary); typedef multi_pass<istreambuf_iterator<char> > multi_pass_iterator_t; typedef istream::char_type char_t; multi_pass_iterator_t in_begin(make_multi_pass(istreambuf_iterator<char_t>(aif))), in_end(make_multi_pass(istreambuf_iterator<char_t>())); typedef position_iterator<multi_pass_iterator_t> iterator_t; iterator_t first(in_begin, in_end, aFileName), last; if (!parse(first,last,aSpiritOpenHRP,aSkipGrammar).full) { file_position fp_cur; // Store the current file position fp_cur = last.get_position(); ODEBUG("Display - Current file: " << fp_cur.file ); ODEBUG( "Line : " << fp_cur.line << " Column : " << fp_cur.column << endl); } aif.close(); if (ReadGeometry) { // Iterate over the included files if there is some. vector<BodyGeometricalData*> aLOU = aSpiritOpenHRP.actions.m_DataForParsing.m_ListOfURLs; string Path; unsigned int npos = aFileName.find_last_of('/'); Path = aFileName.substr(0,npos+1); ODEBUG( "Path: " << Path << " Size of m_ListOfURLs: " << aSpiritOpenHRP.actions.m_DataForParsing.m_ListOfURLs.size()); for(unsigned int iIndexBDG=0; iIndexBDG<aLOU.size(); iIndexBDG++) { const vector<string > URLs = aLOU[iIndexBDG]->getURLs(); ODEBUG(" i: " << iIndexBDG << " URLs.size():" << URLs.size()); for(unsigned int j=0;j<URLs.size();j++) { string GeomFileName = Path + URLs[j]; aif.open(GeomFileName.c_str(),ifstream::in|ifstream::binary); if (!aif.is_open()) { ODEBUG(" Unable to open :" << GeomFileName ); } else { ODEBUG3( " Open :" << GeomFileName ); multi_pass_iterator_t lin_begin(make_multi_pass(istreambuf_iterator<char_t>(aif))), lin_end(make_multi_pass(istreambuf_iterator<char_t>())); iterator_t lfirst(lin_begin, lin_end, URLs[j]), llast; *aSpiritOpenHRP.actions.m_DataForParsing.m_LOUIndex = iIndexBDG; if (!parse(lfirst,llast,aSpiritOpenHRP,aSkipGrammar).full) { file_position fp_cur; // Store the current file position fp_cur = lfirst.get_position(); ODEBUG("Display - Current file: " << fp_cur.file ); ODEBUG( "Line : " << fp_cur.line << " Column : " << fp_cur.column << endl); } ODEBUG3(" Finished"); } aif.close(); } } // Copy the list of URLS. aListOfURLs.resize(aLOU.size()); vector<BodyGeometricalData *>::iterator it_BGD = aLOU.begin(); unsigned int i=0; while (it_BGD!= aLOU.end()) { aListOfURLs[i] = *(*it_BGD); i++; it_BGD++; } }; // Copy multibody structure. *aMB = aSpiritOpenHRP.actions.m_DataForParsing.m_MultiBody; return 1; }