added Sed::clearError() and Sed::retrieveError().
All checks were successful
continuous-integration/drone/push Build is passing

added a in-class error information buffer to Sed
This commit is contained in:
2025-10-01 21:17:51 +09:00
parent 99959cf35d
commit 9afed26820
3 changed files with 287 additions and 191 deletions

View File

@ -228,6 +228,8 @@ public:
/// initialized with the open() function. /// initialized with the open() function.
/// ///
hawk_loc_t getErrorLocation () const; hawk_loc_t getErrorLocation () const;
const hawk_uch_t* getErrorLocationFileU () const;
const hawk_bch_t* getErrorLocationFileB () const;
/// ///
/// The getErrorNumber() function gets the number of the last /// The getErrorNumber() function gets the number of the last
@ -240,7 +242,7 @@ public:
/// The setError() function sets information on an error occurred. /// The setError() function sets information on an error occurred.
/// ///
void setError ( void setError (
hawk_errnum_t num, ///< error number hawk_errnum_t code, ///< error number
const hawk_oocs_t* args = HAWK_NULL, ///< string array for formatting const hawk_oocs_t* args = HAWK_NULL, ///< string array for formatting
/// an error message /// an error message
const hawk_loc_t* loc = HAWK_NULL ///< error location const hawk_loc_t* loc = HAWK_NULL ///< error location
@ -260,6 +262,9 @@ public:
... ...
); );
void clearError ();
void retrieveError ();
const hawk_ooch_t* getCompileId () const; const hawk_ooch_t* getCompileId () const;
const hawk_ooch_t* setCompileId ( const hawk_ooch_t* setCompileId (
@ -301,6 +306,17 @@ protected:
hawk_sed_t* sed; hawk_sed_t* sed;
/// default error formatting string getter /// default error formatting string getter
hawk_errstr_t dflerrstr; hawk_errstr_t dflerrstr;
/// error information buffer
hawk_errinf_t errinf;
#if defined(HAWK_OOCH_IS_BCH)
mutable hawk_uch_t xerrmsg[HAWK_ERRMSG_CAPA];
mutable hawk_uch_t xerrlocfile[128];
#else
mutable hawk_bch_t xerrmsg[HAWK_ERRMSG_CAPA * 2];
mutable hawk_bch_t xerrlocfile[256];
#endif
/// Stream to read script from /// Stream to read script from
Stream* sstream; Stream* sstream;
/// I/O stream to read data from /// I/O stream to read data from

View File

@ -283,7 +283,7 @@ Hawk::Value::IntIndex::IntIndex (hawk_int_t x)
void* Hawk::Value::operator new (hawk_oow_t n, Run* run) throw () void* Hawk::Value::operator new (hawk_oow_t n, Run* run) throw ()
{ {
void* ptr = hawk_rtx_allocmem(run->rtx, HAWK_SIZEOF(run) + n); void* ptr = hawk_rtx_allocmem(run->rtx, HAWK_SIZEOF(run) + n);
if (ptr == HAWK_NULL) return HAWK_NULL; if (HAWK_UNLIKELY(!ptr)) return HAWK_NULL;
*(Run**)ptr = run; *(Run**)ptr = run;
return (char*)ptr + HAWK_SIZEOF(run); return (char*)ptr + HAWK_SIZEOF(run);
@ -292,7 +292,7 @@ void* Hawk::Value::operator new (hawk_oow_t n, Run* run) throw ()
void* Hawk::Value::operator new[] (hawk_oow_t n, Run* run) throw () void* Hawk::Value::operator new[] (hawk_oow_t n, Run* run) throw ()
{ {
void* ptr = hawk_rtx_allocmem(run->rtx, HAWK_SIZEOF(run) + n); void* ptr = hawk_rtx_allocmem(run->rtx, HAWK_SIZEOF(run) + n);
if (ptr == HAWK_NULL) return HAWK_NULL; if (HAWK_UNLIKELY(!ptr)) return HAWK_NULL;
*(Run**)ptr = run; *(Run**)ptr = run;
return (char*)ptr + HAWK_SIZEOF(run); return (char*)ptr + HAWK_SIZEOF(run);
@ -1739,7 +1739,7 @@ void Hawk::setError (hawk_errnum_t code, const hawk_loc_t* loc)
{ {
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf)); HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code; this->errinf.num = code;
if (loc != HAWK_NULL) this->errinf.loc = *loc; if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code)); hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
} }
} }
@ -1758,7 +1758,7 @@ void Hawk::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_bc
{ {
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf)); HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code; this->errinf.num = code;
if (loc != HAWK_NULL) this->errinf.loc = *loc; if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code)); hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
} }
} }
@ -1777,15 +1777,15 @@ void Hawk::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_uc
{ {
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf)); HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code; this->errinf.num = code;
if (loc != HAWK_NULL) this->errinf.loc = *loc; if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr (this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), HAWK_T("not ready to set an error message")); hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
} }
} }
void Hawk::clearError () void Hawk::clearError ()
{ {
HAWK_MEMSET (&errinf, 0, HAWK_SIZEOF(errinf)); HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
errinf.num = HAWK_ENOERR; this->errinf.num = HAWK_ENOERR;
} }
void Hawk::retrieveError() void Hawk::retrieveError()
@ -2374,7 +2374,7 @@ int Hawk::xstrs_t::add (hawk_t* hawk, const hawk_uch_t* arg, hawk_oow_t len)
capa += 64; capa += 64;
ptr = (hawk_oocs_t*)hawk_reallocmem(hawk, this->ptr, HAWK_SIZEOF(hawk_oocs_t)*(capa + 1)); ptr = (hawk_oocs_t*)hawk_reallocmem(hawk, this->ptr, HAWK_SIZEOF(hawk_oocs_t)*(capa + 1));
if (ptr == HAWK_NULL) return -1; if (HAWK_UNLIKELY(!ptr)) return -1;
this->ptr = ptr; this->ptr = ptr;
this->capa = capa; this->capa = capa;
@ -2405,7 +2405,7 @@ int Hawk::xstrs_t::add (hawk_t* hawk, const hawk_bch_t* arg, hawk_oow_t len)
capa += 64; capa += 64;
ptr = (hawk_oocs_t*)hawk_reallocmem(hawk, this->ptr, HAWK_SIZEOF(hawk_oocs_t)*(capa + 1)); ptr = (hawk_oocs_t*)hawk_reallocmem(hawk, this->ptr, HAWK_SIZEOF(hawk_oocs_t)*(capa + 1));
if (ptr == HAWK_NULL) return -1; if (HAWK_UNLIKELY(!ptr)) return -1;
this->ptr = ptr; this->ptr = ptr;
this->capa = capa; this->capa = capa;
@ -2611,7 +2611,7 @@ int Hawk::addFunction (
spec.trait = validOpts; spec.trait = validOpts;
hawk_fnc_t* fnc = hawk_addfncwithucstr(this->hawk, name, &spec); hawk_fnc_t* fnc = hawk_addfncwithucstr(this->hawk, name, &spec);
if (fnc == HAWK_NULL) if (HAWK_UNLIKELY(!fnc))
{ {
this->retrieveError(); this->retrieveError();
return -1; return -1;
@ -2625,7 +2625,7 @@ int Hawk::addFunction (
// the function name exists in the underlying function table. // the function name exists in the underlying function table.
// use the pointer to the name to maintain the hash table. // use the pointer to the name to maintain the hash table.
hawk_htb_pair_t* pair = hawk_htb_upsert(this->functionMap, (hawk_ooch_t*)fnc->name.ptr, fnc->name.len, &handler, HAWK_SIZEOF(handler)); hawk_htb_pair_t* pair = hawk_htb_upsert(this->functionMap, (hawk_ooch_t*)fnc->name.ptr, fnc->name.len, &handler, HAWK_SIZEOF(handler));
if (!pair) if (HAWK_UNLIKELY(!pair))
{ {
hawk_delfncwithucstr (this->hawk, name); hawk_delfncwithucstr (this->hawk, name);
this->retrieveError(); this->retrieveError();
@ -2635,7 +2635,7 @@ int Hawk::addFunction (
FunctionMap::Pair* pair; FunctionMap::Pair* pair;
try { pair = this->functionMap.upsert(Cstr(fnc->name.ptr, fnc->name.len), handler); } try { pair = this->functionMap.upsert(Cstr(fnc->name.ptr, fnc->name.len), handler); }
catch (...) { pair = HAWK_NULL; } catch (...) { pair = HAWK_NULL; }
if (!pair) if (HAWK_UNLIKELY(!pair))
{ {
hawk_delfncwithucstr (this->hawk, name); hawk_delfncwithucstr (this->hawk, name);
this->setError(HAWK_ENOMEM); this->setError(HAWK_ENOMEM);

View File

@ -26,6 +26,7 @@
#include <Hawk-Sed.hpp> #include <Hawk-Sed.hpp>
#include "sed-prv.h" #include "sed-prv.h"
#include "hawk-prv.h"
///////////////////////////////// /////////////////////////////////
HAWK_BEGIN_NAMESPACE(HAWK) HAWK_BEGIN_NAMESPACE(HAWK)
@ -44,6 +45,8 @@ static HAWK_INLINE xtn_t* GET_XTN(hawk_sed_t* sed) { return (xtn_t*)((hawk_uint8
Sed::Sed (Mmgr* mmgr): Mmged(mmgr), sed(HAWK_NULL), dflerrstr(HAWK_NULL) Sed::Sed (Mmgr* mmgr): Mmged(mmgr), sed(HAWK_NULL), dflerrstr(HAWK_NULL)
{ {
HAWK_MEMSET (&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = HAWK_ENOERR;
this->_cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8); this->_cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8);
} }
@ -61,14 +64,8 @@ void Sed::setCmgr (hawk_cmgr_t* cmgr)
int Sed::open () int Sed::open ()
{ {
// TODO: create this->errinf just like the Hawk class. // TODO: create this->errinf just like the Hawk class.
hawk_errinf_t errinf; this->sed = hawk_sed_open(this->getMmgr(), HAWK_SIZEOF(xtn_t), this->getCmgr(), &this->errinf);
this->sed = hawk_sed_open(this->getMmgr(), HAWK_SIZEOF(xtn_t), this->getCmgr(), &errinf); if (HAWK_UNLIKELY(!this->sed)) return -1;
if (HAWK_UNLIKELY(!this->sed))
{
// TODO: set error information like the Hawk class
// can not set error as this->sed is null until I update this class like Hawk.
return -1;
}
this->sed->_instsize += HAWK_SIZEOF(xtn_t); this->sed->_instsize += HAWK_SIZEOF(xtn_t);
@ -94,18 +91,20 @@ void Sed::close ()
int Sed::compile (Stream& sstream) int Sed::compile (Stream& sstream)
{ {
HAWK_ASSERT(this->sed != HAWK_NULL); HAWK_ASSERT(this->sed != HAWK_NULL);
this->sstream = &sstream; this->sstream = &sstream;
return hawk_sed_comp(this->sed, Sed::sin); int n = hawk_sed_comp(this->sed, Sed::sin);
if (n <= -1) this->retrieveError();
return n;
} }
int Sed::execute (Stream& istream, Stream& ostream) int Sed::execute (Stream& istream, Stream& ostream)
{ {
HAWK_ASSERT(this->sed != HAWK_NULL); HAWK_ASSERT(this->sed != HAWK_NULL);
this->istream = &istream; this->istream = &istream;
this->ostream = &ostream; this->ostream = &ostream;
return hawk_sed_exec(this->sed, Sed::xin, Sed::xout); int n = hawk_sed_exec(this->sed, Sed::xin, Sed::xout);
if (n <= -1) this->retrieveError();
return n;
} }
void Sed::halt () void Sed::halt ()
@ -136,58 +135,139 @@ void Sed::setTrait (int trait)
const hawk_ooch_t* Sed::getErrorMessage () const const hawk_ooch_t* Sed::getErrorMessage () const
{ {
return (this->sed == HAWK_NULL)? HAWK_T(""): hawk_sed_geterrmsg(this->sed); return this->errinf.msg;
} }
const hawk_uch_t* Sed::getErrorMessageU () const const hawk_uch_t* Sed::getErrorMessageU () const
{ {
return (this->sed == HAWK_NULL)? HAWK_UT(""): hawk_sed_geterrumsg(this->sed); #if defined(HAWK_OOCH_IS_UCH)
return this->errinf.msg;
#else
hawk_oow_t wcslen, mbslen;
wcslen = HAWK_COUNTOF(this->xerrmsg);
hawk_conv_bcstr_to_ucstr_with_cmgr(this->errinf.msg, &mbslen, this->xerrmsg, &wcslen, this->getCmgr(), 1);
return this->xerrmsg;
#endif
} }
const hawk_bch_t* Sed::getErrorMessageB () const const hawk_bch_t* Sed::getErrorMessageB () const
{ {
return (this->sed == HAWK_NULL)? HAWK_BT(""): hawk_sed_geterrbmsg(this->sed); #if defined(HAWK_OOCH_IS_UCH)
hawk_oow_t wcslen, mbslen;
mbslen = HAWK_COUNTOF(this->xerrmsg);
hawk_conv_ucstr_to_bcstr_with_cmgr(this->errinf.msg, &wcslen, this->xerrmsg, &mbslen, this->getCmgr());
return this->xerrmsg;
#else
return this->errinf.msg;
#endif
} }
hawk_loc_t Sed::getErrorLocation () const hawk_loc_t Sed::getErrorLocation () const
{ {
if (this->sed == HAWK_NULL) return this->errinf.loc;
{
hawk_loc_t loc;
loc.line = 0;
loc.colm = 0;
return loc;
} }
return *hawk_sed_geterrloc(this->sed);
const hawk_uch_t* Sed::getErrorLocationFileU () const
{
#if defined(HAWK_OOCH_IS_UCH)
return this->errinf.loc.file;
#else
if (!this->errinf.loc.file) return HAWK_NULL;
hawk_oow_t wcslen, mbslen;
wcslen = HAWK_COUNTOF(this->xerrlocfile);
hawk_conv_bcstr_to_ucstr_with_cmgr(this->errinf.loc.file, &mbslen, this->xerrlocfile, &wcslen, this->getCmgr(), 1);
return this->xerrlocfile;
#endif
}
const hawk_bch_t* Sed::getErrorLocationFileB () const
{
#if defined(HAWK_OOCH_IS_UCH)
if (!this->errinf.loc.file) return HAWK_NULL;
hawk_oow_t wcslen, mbslen;
mbslen = HAWK_COUNTOF(this->xerrlocfile);
hawk_conv_ucstr_to_bcstr_with_cmgr(this->errinf.loc.file, &wcslen, this->xerrlocfile, &mbslen, this->getCmgr());
return this->xerrlocfile;
#else
return this->errinf.loc.file;
#endif
} }
hawk_errnum_t Sed::getErrorNumber () const hawk_errnum_t Sed::getErrorNumber () const
{ {
return (this->sed == HAWK_NULL)? HAWK_ENOERR: hawk_sed_geterrnum(this->sed); return this->errinf.num;
} }
void Sed::setError (hawk_errnum_t err, const hawk_oocs_t* args, const hawk_loc_t* loc) void Sed::setError (hawk_errnum_t code, const hawk_oocs_t* args, const hawk_loc_t* loc)
{ {
HAWK_ASSERT(this->sed != HAWK_NULL); if (this->sed)
hawk_sed_seterror(this->sed, loc, err, args); {
hawk_sed_seterror(this->sed, loc, code, args);
this->retrieveError();
}
else
{
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code;
if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
}
} }
void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_bch_t* fmt, ...) void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_bch_t* fmt, ...)
{ {
HAWK_ASSERT(this->sed != HAWK_NULL); if (this->sed)
{
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
hawk_sed_seterrbvfmt(this->sed, loc, code, fmt, ap); hawk_sed_seterrbvfmt(this->sed, loc, code, fmt, ap);
va_end(ap); va_end(ap);
this->retrieveError();
}
else
{
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code;
if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
}
} }
void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_uch_t* fmt, ...) void Sed::formatError (hawk_errnum_t code, const hawk_loc_t* loc, const hawk_uch_t* fmt, ...)
{ {
HAWK_ASSERT(this->sed != HAWK_NULL); if (this->sed)
{
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
hawk_sed_seterruvfmt(this->sed, loc, code, fmt, ap); hawk_sed_seterruvfmt(this->sed, loc, code, fmt, ap);
va_end(ap); va_end(ap);
this->retrieveError();
}
else
{
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = code;
if (loc) this->errinf.loc = *loc;
hawk_copy_oocstr(this->errinf.msg, HAWK_COUNTOF(this->errinf.msg), hawk_dfl_errstr(code));
}
}
void Sed::clearError ()
{
HAWK_MEMSET(&this->errinf, 0, HAWK_SIZEOF(this->errinf));
this->errinf.num = HAWK_ENOERR;
}
void Sed::retrieveError()
{
if (this->sed == HAWK_NULL)
{
this->clearError();
}
else
{
hawk_sed_geterrinf(this->sed, &this->errinf);
}
} }
const hawk_ooch_t* Sed::getCompileId () const const hawk_ooch_t* Sed::getCompileId () const