1
0
forked from Alepha/Alepha

Fix some bugs in iostream exception handling for expansions.

This commit is contained in:
2023-12-02 04:25:32 -05:00
parent b9de163083
commit bf207244b6
2 changed files with 21 additions and 4 deletions

View File

@ -6,6 +6,8 @@ static_assert( __cplusplus > 2020'99 );
#include <algorithm>
#include <exception>
#include <Alepha/IOStreams/EnableExceptions.h>
#include "error.h"
#include "AutoRAII.h"
@ -39,22 +41,31 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m
void
writeChar( const char ch ) override
{
if( C::debugExpansion ) error() << "Entry to write and got `" << ch << '`' << std::endl;
std::ostream current{ underlying };
if( mode == Normal and ch == sigil )
{
if( C::debugExpansion ) error() << "Normal and got `" << ch << '`' << std::endl;
mode= Symbol;
varName.str( "" );
return;
}
if( mode == Symbol and ch == sigil )
{
if( C::debugExpansion ) error() << "Closed symbol and got `" << ch << '`' << std::endl;
mode= Normal;
const std::string name( varName.view() );
if( C::debugExpansion )
{
error() << "Asked to expand `" << name << '`' << std::endl;
}
if( not name.empty() )
{
if( C::debugExpansion ) error() << "Name wasn't empty" << std::endl;
if( not substitutions.contains( name ) )
{
if( C::debugExpansion ) error() << "Throwing" << std::endl;
throw std::runtime_error{ "No such variable: `" + name +"`" };
}
if( C::debugExpansion ) error() << "Expanding variable with name `" << name << "`" << std::endl;
@ -64,6 +75,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m
return;
}
if( C::debugExpansion ) error() << "Stashing `" << ch << '`' << std::endl;
if( mode == Symbol ) current.rdbuf( &varName );
current << ch;
}
@ -76,7 +88,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m
{
if( C::debugIOStreamLifecycle ) error() << "Mode not being normal, we're throwing (" << ++throws << " times now)..." << std::endl;
mode= Normal;
throw std::runtime_error{ "Unterminated variable `" + varName.str() + " in expansion." };
throw std::runtime_error{ "Unterminated variable `" + varName.str() + "` in expansion." };
}
}
};
@ -92,6 +104,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m
exports::expandVariables( const std::string &text, const VarMap &vars, const char sigil )
{
std::ostringstream oss;
IOStreams::EnableExceptions exc{ oss };
oss << StartSubstitutions{ sigil, vars };
oss << text;

View File

@ -38,7 +38,7 @@ static auto init= enroll <=[]
},
};
"An exception should be thrown when there is a trailing unenclosed variable."_test <=TableTest< Alepha::expandVariables >::ExceptionCases
"An exception should be thrown when there is a trailing unenclosed variable."_test <=TableTest< Alepha::expandVariables >::Cases
{
{ "Complete var",
{ "$H$ $W$", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' },
@ -46,11 +46,15 @@ static auto init= enroll <=[]
},
{ "Complete var",
{ "$H$ $W$", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' },
std::type_identity< void >{}
"Hello World"
},
{ "Incomplete var",
{ "$H$ $W", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' },
std::type_identity< std::exception >{}
std::runtime_error{ "Unterminated variable `W` in expansion." }
},
{ "Missing var",
{ "$H$ $W$", { { "W", lambaste<="World" } }, '$' },
std::runtime_error{ "No such variable: `H`" }
},
};