void TdfParser::LoadBuffer( char const* buf, std::size_t size){ this->filename = "buffer"; std::list<std::string> junk_data; tdf_grammar grammar( &root_section, &junk_data ); std::string message; boost::spirit::position_iterator2<char const*> error_it( buf, buf + size, filename ); boost::spirit::parse_info<char const*> info; try { info = parse( buf , buf + size , grammar , space_p | comment_p("/*", "*/") // rule for C-comments | comment_p("//") ) ; }catch( boost::spirit::parser_error<tdf_grammar::Errors, char *> & e ) { // thrown by assertion parsers in tdf_grammar switch(e.descriptor) { case tdf_grammar::semicolon_expected: message = "semicolon expected"; break; case tdf_grammar::equals_sign_expected: message = "equals sign in name value pair expected"; break; case tdf_grammar::square_bracket_expected: message = "square bracket to close section name expected"; break; case tdf_grammar::brace_expected: message = "brace or further name value pairs expected"; break; }; for( size_t i = 0;i != size; ++i,++error_it ); } for(list<string>::const_iterator it = junk_data.begin(), e = junk_data.end(); it !=e ; ++it ){ std::string temp = boost::trim_copy( *it ); if( ! temp.empty() ) { G->L.eprint("Junk in "+ filename + " :" + temp); } } if(!message.empty()) G->L.eprint(message + " in " +filename); //throw parse_error( , error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename ); if(!info.full){ boost::spirit::position_iterator2<char const*> error_it( buf, buf + size, filename ); for( size_t i = 0; i != size; ++i,++error_it ); G->L.eprint("error in " +filename); // throw parse_error( error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename ); } }
std::vector<ParsedStateMachinePtr> StateGraphParser::parse( iter_t& begin, iter_t end ) { //skip_parser_t skip_parser = SKIP_PARSER; //iter_pol_t iter_policy( skip_parser ); //#define SKIP_PARSER skip_parser_t skip_parser = comment_p( "#" ) | comment_p( "//" ) | comment_p( "/*", "*/" ) | (space_p - eol_p) | commonparser->skipper; iter_pol_t iter_policy( skip_parser ); scanner_pol_t policies( iter_policy ); scanner_t scanner( begin, end, policies ); // reset the condition-transition priority. rank = 0; this->storeOffset(); try { if ( ! production.parse( scanner ) ) { // on error, we clear all remaining data, cause we can't // guarantee consistency... clear(); throw file_parse_exception( new parse_exception_syntactic_error( "Syntax error" ), mpositer.get_position().file, mpositer.get_position().line, mpositer.get_position().column ); } std::vector<ParsedStateMachinePtr> ret = values( rootmachines ); rootmachines.clear(); return ret; } catch( const parser_error<std::string, iter_t>& e ) { // on error, we clear all remaining data, cause we can't // guarantee consistency... clear(); throw file_parse_exception( new parse_exception_syntactic_error( e.descriptor ), mpositer.get_position().file, mpositer.get_position().line, mpositer.get_position().column ); } catch( const parser_error<GraphSyntaxErrors, iter_t>& ) { // on error, we clear all remaining data, cause we can't // guarantee consistency... clear(); throw file_parse_exception( new parse_exception_syntactic_error( "Expected one of: entry, handle, exit, transitions" ), mpositer.get_position().file, mpositer.get_position().line, mpositer.get_position().column ); } catch( const parse_exception& e ) { // on error, we clear all remaining data, cause we can't // guarantee consistency... clear(); throw file_parse_exception( e.copy(), mpositer.get_position().file, mpositer.get_position().line, mpositer.get_position().column ); } // catch( ... ) { // assert( false ); // } }
void TdfParser::parse_buffer(char const* buf, size_t size) { std::list<std::string> junk_data; tdf_grammar grammar(&root_section, &junk_data); parse_info<char const*> info; std::string message; typedef position_iterator2<char const*> iterator_t; iterator_t error_it(buf, buf + size); try { info = parse( buf , buf + size , grammar , space_p | comment_p("/*", "*/") // rule for C-comments | comment_p("//") ); } catch (parser_error<tdf_grammar::Errors, char const*> & e) { // thrown by assertion parsers in tdf_grammar switch(e.descriptor) { case tdf_grammar::semicolon_expected: message = "semicolon expected"; break; case tdf_grammar::equals_sign_expected: message = "equals sign in name value pair expected"; break; case tdf_grammar::square_bracket_expected: message = "square bracket to close section name expected"; break; case tdf_grammar::brace_expected: message = "brace or further name value pairs expected"; break; default: message = "unknown boost::spirit::parser_error exception"; break; }; std::ptrdiff_t target_pos = e.where - buf; for (int i = 1; i < target_pos; ++i) { ++error_it; if (error_it != (iterator_t(buf + i, buf + size))) { ++i; } } } for (std::list<std::string>::const_iterator it = junk_data.begin(), e = junk_data.end(); it !=e ; ++it) { std::string temp = StringTrim(*it); if (!temp.empty()) { ::logOutput.Print("TdfParser: Junk in "+ filename + ": " + temp); } } if (!message.empty()) { throw parse_error(message, error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename); } // a different error might have happened: if (!info.full) { std::ptrdiff_t target_pos = info.stop - buf; for (int i = 1; i < target_pos; ++i) { ++error_it; if (error_it != (iterator_t(buf + i, buf + size))) { ++i; } } throw parse_error(error_it.get_currentline(), error_it.get_position().line, error_it.get_position().column, filename); } }