/****************************************************************************
 * KONOHA COPYRIGHT, LICENSE NOTICE, AND DISCRIMER  
 * 
 * Copyright (c) 2005-2008, Kimio Kuramitsu <kimio at ynu.ac.jp>
 *           (c) 2008-      Konoha Software Foundation  
 * All rights reserved.
 * 
 * You may choose one of the following two licenses when you use konoha. 
 * See www.konohaware.org/license.html for further information.
 * 
 * (1) GNU General Public License 2.0      (with    KONOHA_UNDER_GPL2)
 * (2) Konoha Software Foundation License 1.0
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  
 ****************************************************************************/

/* ************************************************************************ */

#include"commons.h"

/* ************************************************************************ */

#ifdef __cplusplus 
extern "C" {
#endif

#ifndef KONOHAC_ERROR_BUFSIZ
#define KONOHAC_ERROR_BUFSIZ 512
#endif  

/* ======================================================================== */
/* [perrata] */

void
knh_perrata(Ctx *ctx, knh_fileid_t fileid, int line, char *oldt, char *newt)
{
	char buf[512];
	OutputStream *w = KNH_STDOUT;
	knh_snprintf(buf, sizeof(buf), "\t%s:%d (%s): ", FILEIDN(fileid), line, knh_message_text(KMSG_ERRATA));
	knh_print(ctx, w, B(buf));
	knh_write__s(ctx, w, oldt);
	knh_write__s(ctx, w, " ==> ");
	knh_write__s(ctx, w, newt);
	knh_write_EOL(ctx, w);
}

#define _KNH_PERRATA_(ctx, f, l, msgo, msgn)    knh_perrata(ctx, f, l, msgo, msgn)
#define _KNH_PERRATA(ctx, tk, msg)  knh_perrata(ctx, (tk)->fileid, (tk)->line, knh_Token_tochar(tk), msg)

/* ------------------------------------------------------------------------ */

void
knh_Token_perrata(Ctx *ctx, Token *o, char *newtoken)
{
	knh_perrata(ctx, DP(o)->fileid, DP(o)->line, knh_Token_tochar(o), newtoken);
}

/* ======================================================================== */
/* [perror] */

void
knh_perror(Ctx *ctx, knh_fileid_t fileid, int line, int pe, char *msg)
{
	char buf[512];
	OutputStream *w = KNH_STDOUT;
#ifdef KNH_DBGMODE
	knh_snprintf(buf, sizeof(buf), "%s:%d:%s(%d) ", FILEIDN(fileid), line, knh_message_tochar(pe), knh_message_type(pe));
#else
	knh_snprintf(buf, sizeof(buf), "%s:%d:(%d) ", FILEIDN(fileid), line, knh_message_type(pe));
#endif
	knh_write__s(ctx, w, buf);
	if(msg == NULL || msg[0] == 0) {
		knh_println(ctx, w, B(knh_message_text(pe)));
	}
	else {
		knh_print(ctx, w, B(knh_message_text(pe)));
		knh_write__s(ctx, w, ": ");
		knh_println(ctx, w, B(msg));
	}
}

/* ------------------------------------------------------------------------ */

void
knh_Token_perror(Ctx *ctx, Token *o, int pe)
{
	knh_perror(ctx, DP(o)->fileid, (int)DP(o)->line, pe, knh_Token_tochar(o));
}

/* ------------------------------------------------------------------------ */

void
knh_tokens_perror(Ctx *ctx, knh_tokens_t *tc, int pe)
{
	Token *tk = knh_tokens_curToken(tc);
	knh_perror(ctx, DP(tk)->fileid, (int)DP(tk)->line, pe, knh_Token_tochar(tk));
}


/* ------------------------------------------------------------------------ */

void knh_Stmt_perror(Ctx *ctx, Stmt *b, knh_tokens_t *tc, int pe)
{
	Token *tk = knh_tokens_curToken(tc);
	knh_perror(ctx, DP(tk)->fileid, (int)DP(tk)->line, pe, knh_Token_tochar(tk));
	knh_Stmt_add_ERR(ctx, b, tk);
	knh_tokens_nextStmt(tc);
}

/* ------------------------------------------------------------------------ */

Stmt *new_StmtERR(Ctx *ctx, knh_tokens_t *tc)
{
	Stmt *b = new_Stmt(ctx, 0, STT_ERR);
	knh_Stmt_add(ctx, b, knh_tokens_curToken(tc));
	knh_tokens_nextStmt(tc);
	return b;
}

/* ======================================================================== */
/* [STMT] */

void
knh_Compiler_perror(Ctx *ctx, Compiler *cpr, int pe, char *msg)
{
	knh_perror(ctx, DP(cpr)->fileid, (int)DP(cpr)->line, pe, msg);
}

/* ======================================================================== */
/* [STMT] */

void
knh_notice_TypeInf(Ctx *ctx, knh_fileid_t fileid, int line, char *msg, knh_type_t type)
{
	if(type != CLASS_Any || type != NNTYPE_Any) {
		char buf[512];
		OutputStream *w = KNH_STDOUT;
		knh_snprintf(buf, sizeof(buf), "%s:%d (%s): ", FILEIDN(fileid), line, knh_message_text(KMSG_TYPEINF));
		knh_print(ctx, w, B(buf));
		knh_write__s(ctx, w, msg);
		knh_write__s(ctx, w, " ..> ");
		knh_write__type(ctx, w, type);
		knh_write_EOL(ctx, w);
	}
}

/* ------------------------------------------------------------------------ */


#ifdef __cplusplus
}
#endif
