Make objectFromStream always throw parse_error_exception when throwing

This commit is contained in:
Vikram Rajkumar 2014-05-17 03:00:07 -04:00
parent cde67a202c
commit e8326ca66c

View file

@ -43,9 +43,8 @@ namespace fc
FC_THROW_EXCEPTION( parse_error_exception, "Expected '\\'" ); FC_THROW_EXCEPTION( parse_error_exception, "Expected '\\'" );
} }
template<typename T> template<typename T>
void skip_white_space( T& in ) void skip_white_space( T& in )
{ {
while( true ) while( true )
{ {
@ -67,18 +66,18 @@ namespace fc
fc::string stringFromStream( T& in ) fc::string stringFromStream( T& in )
{ {
fc::stringstream token; fc::stringstream token;
try try
{ {
char c = in.peek(); char c = in.peek();
if( c != '"' ) if( c != '"' )
FC_THROW_EXCEPTION( parse_error_exception, FC_THROW_EXCEPTION( parse_error_exception,
"Expected '\"' but read '${char}'", "Expected '\"' but read '${char}'",
("char", string(&c, (&c) + 1) ) ); ("char", string(&c, (&c) + 1) ) );
in.get(); in.get();
while( true ) while( true )
{ {
switch( c = in.peek() ) switch( c = in.peek() )
{ {
case '\\': case '\\':
@ -94,14 +93,14 @@ namespace fc
} }
FC_THROW_EXCEPTION( parse_error_exception, "EOF before closing '\"' in string '${token}'", FC_THROW_EXCEPTION( parse_error_exception, "EOF before closing '\"' in string '${token}'",
("token", token.str() ) ); ("token", token.str() ) );
} FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", } FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'",
("token", token.str() ) ); ("token", token.str() ) );
} }
template<typename T> template<typename T>
fc::string stringFromToken( T& in ) fc::string stringFromToken( T& in )
{ {
fc::stringstream token; fc::stringstream token;
try try
{ {
char c = in.peek(); char c = in.peek();
@ -128,12 +127,12 @@ namespace fc
} }
} }
return token.str(); return token.str();
} }
catch( const fc::eof_exception& eof ) catch( const fc::eof_exception& eof )
{ {
return token.str(); return token.str();
} }
FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'",
("token", token.str() ) ); ("token", token.str() ) );
} }
@ -143,10 +142,10 @@ namespace fc
mutable_variant_object obj; mutable_variant_object obj;
try try
{ {
char c = in.peek(); char c = in.peek();
if( c != '{' ) if( c != '{' )
FC_THROW_EXCEPTION( parse_error_exception, FC_THROW_EXCEPTION( parse_error_exception,
"Expected '{', but read '${char}'", "Expected '{', but read '${char}'",
("char",string(&c, &c + 1)) ); ("char",string(&c, &c + 1)) );
in.get(); in.get();
skip_white_space(in); skip_white_space(in);
@ -176,8 +175,10 @@ namespace fc
return obj; return obj;
} }
FC_THROW_EXCEPTION( parse_error_exception, "Expected '}' after ${variant}", ("variant", obj ) ); FC_THROW_EXCEPTION( parse_error_exception, "Expected '}' after ${variant}", ("variant", obj ) );
}
catch( const fc::eof_exception& e )
{
FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF: ${e}", ("e", e.to_detail_string() ) );
} FC_RETHROW_EXCEPTIONS( warn, "Error parsing object" ); } FC_RETHROW_EXCEPTIONS( warn, "Error parsing object" );
} }
@ -194,7 +195,7 @@ namespace fc
while( in.peek() != ']' ) while( in.peek() != ']' )
{ {
while( in.peek() == ',' ) while( in.peek() == ',' )
in.get(); in.get();
ar.push_back( variant_from_stream(in) ); ar.push_back( variant_from_stream(in) );
skip_white_space(in); skip_white_space(in);
@ -204,7 +205,7 @@ namespace fc
("variant", ar) ); ("variant", ar) );
in.get(); in.get();
} FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}", } FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}",
("array", ar ) ); ("array", ar ) );
return ar; return ar;
} }
@ -228,11 +229,11 @@ namespace fc
char c; char c;
while((c = in.peek()) && !done) while((c = in.peek()) && !done)
{ {
switch( c ) switch( c )
{ {
case '.': case '.':
if (dot) if (dot)
FC_THROW_EXCEPTION(parse_error_exception, "Can't parse a number with two decimal places"); FC_THROW_EXCEPTION(parse_error_exception, "Can't parse a number with two decimal places");
dot = true; dot = true;
case '0': case '0':
@ -265,7 +266,7 @@ namespace fc
FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str)); FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str));
if( dot ) if( dot )
return to_double(str); return to_double(str);
if( neg ) if( neg )
return to_int64(str); return to_int64(str);
return to_uint64(str); return to_uint64(str);
} }
@ -309,7 +310,7 @@ namespace fc
if( str == "null" ) return variant(); if( str == "null" ) return variant();
if( str == "true" ) return true; if( str == "true" ) return true;
if( str == "false" ) return false; if( str == "false" ) return false;
else else
{ {
if (received_eof) if (received_eof)
FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" ); FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" );
@ -372,7 +373,7 @@ namespace fc
default: default:
// ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); // ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) );
return stringFromToken(in); return stringFromToken(in);
in.get(); // in.get(); //
ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) );
return variant(); return variant();
} }
@ -395,7 +396,7 @@ namespace fc
void toUTF8( const wchar_t c, ostream& os ) void toUTF8( const wchar_t c, ostream& os )
{ {
utf8::utf16to8( &c, (&c)+1, ostream_iterator<char>(os) ); utf8::utf16to8( &c, (&c)+1, ostream_iterator<char>(os) );
} }
*/ */
@ -462,7 +463,7 @@ namespace fc
{ {
os << '{'; os << '{';
auto itr = o.begin(); auto itr = o.begin();
while( itr != o.end() ) while( itr != o.end() )
{ {
escape_string( itr->key(), os ); escape_string( itr->key(), os );
@ -480,13 +481,13 @@ namespace fc
{ {
switch( v.get_type() ) switch( v.get_type() )
{ {
case variant::null_type: case variant::null_type:
os << "null"; os << "null";
return; return;
case variant::int64_type: case variant::int64_type:
os << v.as_int64(); os << v.as_int64();
return; return;
case variant::uint64_type: case variant::uint64_type:
os << v.as_uint64(); os << v.as_uint64();
return; return;
case variant::double_type: case variant::double_type:
@ -531,7 +532,7 @@ namespace fc
switch( v[i] ) { switch( v[i] ) {
case '\\': case '\\':
if( !escape ) { if( !escape ) {
if( quote ) if( quote )
escape = true; escape = true;
} else { escape = false; } } else { escape = false; }
ss<<v[i]; ss<<v[i];
@ -551,7 +552,7 @@ namespace fc
} }
if( !escape ) { if( !escape ) {
quote = !quote; quote = !quote;
} }
escape = false; escape = false;
ss<<'"'; ss<<'"';
break; break;
@ -629,7 +630,7 @@ namespace fc
{ {
//auto tmp = std::make_shared<fc::ifstream>( p, ifstream::binary ); //auto tmp = std::make_shared<fc::ifstream>( p, ifstream::binary );
//auto tmp = std::make_shared<std::ifstream>( p.generic_string().c_str(), std::ios::binary ); //auto tmp = std::make_shared<std::ifstream>( p.generic_string().c_str(), std::ios::binary );
//buffered_istream bi( tmp ); //buffered_istream bi( tmp );
std::ifstream bi( p.generic_string().c_str(), std::ios::binary ); std::ifstream bi( p.generic_string().c_str(), std::ios::binary );
return variant_from_stream( bi ); return variant_from_stream( bi );
} }