enhanced error message printing

This commit is contained in:
hyunghwan.chung 2017-12-17 06:38:08 +00:00
parent 5132238bfd
commit cf88ac40ec
5 changed files with 28 additions and 48 deletions

View File

@ -411,9 +411,10 @@ static int is_restricted_word (const moo_oocs_t* ucs)
static int begin_include (moo_t* moo);
static int end_include (moo_t* moo);
static void set_syntax_error (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt)
static void set_syntax_errbmsg (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt, const moo_bch_t* msg)
{
moo_seterrnum (moo, MOO_ESYNTAX);
if (msg) moo_seterrbfmt (moo, MOO_ESYNERR, "%s", msg);
else moo_seterrnum (moo, MOO_ESYNERR);
moo->c->synerr.num = num;
/* The SCO compiler complains of this ternary operation saying:
@ -434,6 +435,11 @@ static void set_syntax_error (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t
}
}
static void set_syntax_error (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt)
{
set_syntax_errbmsg (moo, num, loc, tgt, MOO_NULL);
}
static int copy_string_to (moo_t* moo, const moo_oocs_t* src, moo_oocs_t* dst, moo_oow_t* dst_capa, int append, moo_ooch_t delim_char)
{
moo_oow_t len, pos;
@ -7243,7 +7249,7 @@ static int __compile_class_definition (moo_t* moo, int extend)
if (TOKEN_TYPE(moo) != MOO_IOTOK_LPAREN)
{
set_syntax_error (moo, MOO_SYNERR_LPAREN, TOKEN_LOC(moo), TOKEN_NAME(moo));
set_syntax_errbmsg (moo, MOO_SYNERR_LPAREN, TOKEN_LOC(moo), TOKEN_NAME(moo), "superclass must be specified");
return -1;
}

View File

@ -160,7 +160,7 @@ struct moo_fmtout_t
* The buffer pointed to by `nbuf' must have length >= MAXNBUF.
*/
static moo_bch_t* sprintn_lower (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t *lenp)
static moo_bch_t* sprintn_lower (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t* lenp)
{
moo_bch_t* p;
@ -172,7 +172,7 @@ static moo_bch_t* sprintn_lower (moo_bch_t* nbuf, moo_uintmax_t num, int base, m
return p; /* returns the end */
}
static moo_bch_t* sprintn_upper (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t *lenp)
static moo_bch_t* sprintn_upper (moo_bch_t* nbuf, moo_uintmax_t num, int base, moo_ooi_t* lenp)
{
moo_bch_t* p;

View File

@ -2328,60 +2328,34 @@ int main (int argc, char* argv[])
compile:
if (moo_compile (moo, input_handler) <= -1)
{
if (moo->errnum == MOO_ESYNTAX)
if (moo->errnum == MOO_ESYNERR)
{
moo_synerr_t synerr;
moo_bch_t bcs[1024]; /* TODO: right buffer size */
moo_oow_t bcslen, ucslen;
moo_getsynerr (moo, &synerr);
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "ERROR: ");
if (synerr.loc.file)
{
#if defined(MOO_OOCH_IS_UCH)
bcslen = MOO_COUNTOF(bcs);
if (moo_convootobcstr (moo, synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "%.*s ", (int)bcslen, bcs);
}
#else
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "%s ", synerr.loc.file);
#endif
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "%js", synerr.loc.file);
}
else
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "%s ", xtn->source_path);
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "%s", xtn->source_path);
}
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "syntax error at line %lu column %lu - ",
(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm);
bcslen = MOO_COUNTOF(bcs);
#if defined(MOO_OOCH_IS_UCH)
if (moo_convootobcstr (moo, moo_synerrnum_to_errstr(synerr.num), &ucslen, bcs, &bcslen) >= 0)
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " [%.*s]", (int)bcslen, bcs);
}
#else
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " [%s]", moo_synerrnum_to_errstr(synerr.num));
#endif
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "[%zu,%zu] syntax error - %js", synerr.loc.line, synerr.loc.colm, moo_synerrnum_to_errstr(synerr.num));
if (synerr.tgt.len > 0)
{
bcslen = MOO_COUNTOF(bcs);
ucslen = synerr.tgt.len;
#if defined(MOO_OOCH_IS_UCH)
if (moo_convootobchars (moo, synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " [%.*s]", (int)bcslen, bcs);
}
#else
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " [%.*s]", (int)synerr.tgt.len, synerr.tgt.ptr);
#endif
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " in place of %.*js", synerr.tgt.len, synerr.tgt.ptr);
}
if (moo_geterrmsg(moo) != moo_geterrstr(moo))
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " - %js", moo_geterrmsg(moo));
}
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, "\n");
}
else

View File

@ -307,9 +307,9 @@ struct moo_ioarg_t
int pos, len, state;
} b;
unsigned long int line;
unsigned long int colm;
moo_ooci_t nl;
moo_oow_t line;
moo_oow_t colm;
moo_ooci_t nl;
moo_iolxc_t lxc;
/*-----------------------------------------------------------------*/

View File

@ -83,7 +83,7 @@ enum moo_errnum_t
MOO_EBUFFULL, /**< buffer full */
#if defined(MOO_INCLUDE_COMPILER)
MOO_ESYNTAX /** < syntax error */
MOO_ESYNERR /** < syntax error */
#endif
};
typedef enum moo_errnum_t moo_errnum_t;
@ -1440,8 +1440,8 @@ typedef enum moo_iocmd_t moo_iocmd_t;
struct moo_ioloc_t
{
unsigned long int line; /**< line */
unsigned long int colm; /**< column */
moo_oow_t line; /**< line */
moo_oow_t colm; /**< column */
const moo_ooch_t* file; /**< file specified in #include */
};
typedef struct moo_ioloc_t moo_ioloc_t;