Parser/compiler - Display errors on the console

This commit is contained in:
Emmanuel BENOîT 2017-11-16 08:34:13 +01:00
parent e0e7ab5441
commit 555105feb8
2 changed files with 33 additions and 32 deletions

32
demo.cc
View file

@ -65,38 +65,6 @@ void T_Demo::handleWheel(
{ {
} }
namespace {
/*============================================================================*/
void PrintStreamError(
char const* const prefix ,
T_String const& name ,
ebcl::X_StreamError const& error )
{
T_StringBuilder sb;
sb << prefix << " '" << name << "': " << error.what( );
if ( error.code( ) == ebcl::E_StreamError::SYSTEM_ERROR ) {
sb << " (error code " << error.systemError( ) << ")";
}
sb << '\n' << '\0';
fprintf( stderr , "%s" , sb.data( ) );
}
void WriteSRDError(
T_StringBuilder& sb ,
ebcl::T_SRDError const& error )
{
sb << error.location( ) << " - " << error.error( ) << "\n";
}
/*============================================================================*/
} // namespace
bool T_Demo::runInit( bool T_Demo::runInit(
ops::T_OpProgram& p ) ops::T_OpProgram& p )

View file

@ -8,6 +8,35 @@
#include <ebcl/SRDText.hh> #include <ebcl/SRDText.hh>
/*============================================================================*/
namespace {
void WriteSRDError(
T_StringBuilder& sb ,
ebcl::T_SRDError const& error )
{
sb << error.location( ) << " - " << error.error( ) << "\n";
}
void DumpSRDErrors(
T_String const& header ,
T_Array< ebcl::T_SRDError > const& errors )
{
T_StringBuilder sb;
sb << "=============================================================\n"
<< header << '\n'
<< "-------------------------------------------------------------\n";
for ( auto& error : errors ) {
WriteSRDError( sb , error );
}
sb << "=============================================================\n" << '\0';
printf( "%s" , sb.data( ) );
}
} // namespace
/*= T_ScriptManager ============================================================*/ /*= T_ScriptManager ============================================================*/
T_ScriptManager::T_ScriptManager( ) noexcept T_ScriptManager::T_ScriptManager( ) noexcept
@ -38,6 +67,7 @@ void T_ScriptManager::loadScript( ) noexcept
} }
errors_.addNew( std::move( sb ) , errors_.addNew( std::move( sb ) ,
T_SRDLocation{ inputName , 1 , 1 } ); T_SRDLocation{ inputName , 1 , 1 } );
DumpSRDErrors( "Script not found" , errors_ );
return; return;
} }
@ -56,6 +86,7 @@ void T_ScriptManager::loadScript( ) noexcept
} }
errors_.addNew( std::move( sb ) , errors_.addNew( std::move( sb ) ,
T_SRDLocation{ inputName , 1 , 1 } ); T_SRDLocation{ inputName , 1 , 1 } );
DumpSRDErrors( "Script not loaded" , errors_ );
return; return;
} catch ( X_SRDErrors const& e ) { } catch ( X_SRDErrors const& e ) {
@ -65,12 +96,14 @@ void T_ScriptManager::loadScript( ) noexcept
for ( auto i = 0u ; i < nErrors ; i ++ ) { for ( auto i = 0u ; i < nErrors ; i ++ ) {
errors_.add( e.errors[ i ] ); errors_.add( e.errors[ i ] );
} }
DumpSRDErrors( "Script not loaded" , errors_ );
return; return;
} }
// Parse the fuck // Parse the fuck
if ( !parser_.parse( srdOut.list( ) ) ) { if ( !parser_.parse( srdOut.list( ) ) ) {
errors_.addAll( parser_.errors( ) ); errors_.addAll( parser_.errors( ) );
DumpSRDErrors( "Parse errors" , errors_ );
return; return;
} }
output_ = compiler_.compile( *parser_.result( ) ); output_ = compiler_.compile( *parser_.result( ) );