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 );
		}
	}
Beispiel #2
0
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);
	}
}