demotool/parsercheck.cc

176 lines
4.4 KiB
C++

#include "externals.hh"
#include "opast.hh"
#include <ebcl/Files.hh>
#include <ebcl/SRDText.hh>
#include <ebcl/Algorithms.hh>
using namespace ebcl;
using namespace opast;
namespace {
/*============================================================================*/
void PrintStreamError(
char const* const prefix ,
T_String const& name ,
X_StreamError const& error )
{
T_StringBuilder sb;
sb << prefix << " '" << name << "': " << error.what( );
if ( error.code( ) == E_StreamError::SYSTEM_ERROR ) {
sb << " (error code " << error.systemError( ) << ")";
}
sb << '\n' << '\0';
fprintf( stderr , "%s" , sb.data( ) );
}
void WriteSRDError(
T_StringBuilder& sb ,
T_SRDError const& error )
{
sb << error.location( ) << " - " << error.error( ) << "\n";
}
/*============================================================================*/
T_Array< T_SRDError > parserPrototypes(
T_OwnPtr< T_RootNode > const& root )
{
T_Visitor< opast::A_Node > visitor{ ASTVisitorBrowser };
T_Array< T_SRDError > errors;
struct T_Decl_ {
E_DataType type;
T_SRDLocation location;
T_Decl_( const E_DataType dt ,
T_SRDLocation const& loc ) noexcept
: type( dt ) , location( loc )
{ }
T_Decl_( const E_DataType dt ) noexcept
: type( dt ) , location{}
{ }
};
T_KeyValueTable< T_String , T_Decl_ > type;
type.add( T_String::Pooled( "time" ) , T_Decl_{ E_DataType::BUILTIN } );
type.add( T_String::Pooled( "width" ) , T_Decl_{ E_DataType::BUILTIN } );
type.add( T_String::Pooled( "height" ) , T_Decl_{ E_DataType::BUILTIN } );
visitor.visit( *root , [&]( A_Node& node , bool exit ) -> bool {
if ( exit ) {
return false;
}
E_DataType dt{ E_DataType::UNKNOWN };
T_String id;
T_SRDLocation location;
if ( node.type( ) == A_Node::OP_SET ) {
dt = E_DataType::VARIABLE;
id = dynamic_cast< T_SetInstrNode& >( node ).id( );
location = dynamic_cast< T_SetInstrNode& >( node ).idLocation( );
} else {
auto* np{ dynamic_cast< A_ResourceDefInstrNode* >( &node ) };
if ( !np ) {
return !dynamic_cast< A_ExpressionNode* >( &node );
}
dt = np->dataType( );
id = np->id( );
location = np->idLocation( );
}
assert( dt != E_DataType::UNKNOWN );
T_Decl_ const* const existing( type.get( id ) );
if ( !existing ) {
type.add( id , T_Decl_{ dt , location } );
return false;
}
if ( existing->type == dt ) {
return false;
}
T_StringBuilder sb;
if ( existing->type == E_DataType::BUILTIN ) {
sb << "trying to redefine built-in variable " << id;
} else {
sb << "'" << id << "' redeclared as " << dt
<< "; previous declaration as "
<< existing->type << " at "
<< existing->location;
}
errors.addNew( std::move( sb ) , location );
return false;
} );
return errors;
}
/*============================================================================*/
} // namespace
int main( int argc , char** argv )
{
// Open file
const T_String inputName( argc >= 2 ? argv[ 1 ] : "demo.srd" );
T_File input( inputName , E_FileMode::READ_ONLY );
try {
input.open( );
} catch ( X_StreamError const& e ) {
PrintStreamError( "Could not open" , inputName , e );
return 1;
}
// Load SRD data
T_SRDMemoryTarget srdOut;
srdOut.clearComments( true ).clearFlushToken( true );
try {
T_SRDTextReader srdReader{ srdOut };
T_FileInputStream fis{ input };
srdReader.read( inputName , fis );
} catch ( X_StreamError const& e ) {
PrintStreamError( "Could not open" , inputName , e );
return 1;
} catch ( X_SRDErrors const& e ) {
T_StringBuilder sb;
const auto nErrors( e.errors.size( ) );
for ( auto i = 0u ; i < nErrors ; i ++ ) {
WriteSRDError( sb , e.errors[ i ] );
}
sb << "No parsing happened due to format errors\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 2;
}
// Parse the fuck
T_Parser parser;
if ( parser.parse( srdOut.list( ) ) ) {
printf( "Success - now running prototypes\n" );
T_Array< T_SRDError > errors{ parserPrototypes( parser.result( ) ) };
if ( errors.empty( ) ) {
printf( "Success!\n" );
return 0;
}
T_StringBuilder sb;
for ( auto const& err : errors ) {
WriteSRDError( sb , err );
}
sb << "Parser prototypes failed\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 4;
} else {
T_StringBuilder sb;
for ( auto const& err : parser.errors( ) ) {
WriteSRDError( sb , err );
}
sb << "Parser failed\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 3;
}
}