From cf88ac40ec0908280e7600e6589217c7f53169cb Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 17 Dec 2017 06:38:08 +0000 Subject: [PATCH] enhanced error message printing --- moo/lib/comp.c | 12 +++++++++--- moo/lib/logfmt.c | 4 ++-- moo/lib/main.c | 48 +++++++++++------------------------------------ moo/lib/moo-prv.h | 6 +++--- moo/lib/moo.h | 6 +++--- 5 files changed, 28 insertions(+), 48 deletions(-) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 907a41b..daab8a9 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -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; } diff --git a/moo/lib/logfmt.c b/moo/lib/logfmt.c index cbd6e4c..8a4b1a4 100644 --- a/moo/lib/logfmt.c +++ b/moo/lib/logfmt.c @@ -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; diff --git a/moo/lib/main.c b/moo/lib/main.c index 8349d8f..a4e5930 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -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 diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 04d63ce..ea2844c 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -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; /*-----------------------------------------------------------------*/ diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 313f161..f71b8e0 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -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;