/****************************************************************************
	Kalkulon - A programmable calculator for programmers

	This file is part of the Kalkulon core.
	
	If you find this source code useful, your are encouraged to use it in your own projects.
	Please give me credit!

	COPYRIGHT by Juergen Holetzeck 2003 - 2008
	e-mail:		contact@kalkulon.de
	homepage:	www.kalkulon.de
****************************************************************************/


// ErrBase.cpp: implementation of the ErrBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include <string.h>

#include "DataObject.h"
#include "Token.h"
#include "ErrBase.h"


// error messages for GetErrorMessage
#define		IDS_ERRWRONGTOKEN       "error: 'wrong token' at pos %d \"%s\""
#define		IDS_ERRPRIMEXPR         "error: 'primary expression' expected at pos %i \"%s\""
#define		IDS_ERRLPEXPR           "error: 'left parenthesis expected' at pos %i"
#define		IDS_ERRRPEXPR           "error: 'right parenthesis expected' at pos %i"
#define		IDS_ERRRCBEXPR          "error: 'right curly braces expected' at pos %i"
#define		IDS_ERRRBEXPR           "error: 'right brackets expected' at pos %i"
#define		IDS_ERRRQEXPR           "error: 'right quotes expected' at pos %i"
#define		IDS_ERRSTATEMENT        "error: 'syntax error (statement)' \"%s\" ' at pos %i"
#define		IDS_ERRENDEXPR          "error: 'unexpected token' at pos %i"
#define		IDS_ERRNUMOFARG         "error: 'wrong number of arguments' for \"%s()\" at pos %i"
#define		IDS_ERRUNDEFSYM         "error: 'undefined symbol' \"%s\" at pos %i"
#define		IDS_ERRUNKNFUNC         "error: 'unknown function' \"%s@%i\" at pos %i"
#define		IDS_ERRWRONGDEF         "error: 'wrong function definition' \"%s\" at pos %i"
#define		IDS_ERRMATH             "error: '%s' in function \"%s\" at pos %i"
#define   IDS_ERRUSERDEF          "error: 'user defined' %s"
#define   IDS_ERRIDEXPECTED       "error: 'identifier expected' \"%s\" at pos %i"

#define		IDS_ERRINTERNAL         "error: 'internal error' at line %i in module %s"

#define   IDS_ERRBREAKSIG         "info: 'break by user'"

const char* Kalkulon::ErrWrongToken::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRWRONGTOKEN, tok.pos, ((std::string) tok).c_str());
	return strbuf;
}

const char* Kalkulon::ErrPrimExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRPRIMEXPR, tok.pos, ((std::string) tok).c_str());
	return strbuf;
}

const char* Kalkulon::ErrLPExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRLPEXPR, tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrRPExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRRPEXPR, tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrRCBExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRRCBEXPR, tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrRBExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRRBEXPR, tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrRQExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRRQEXPR, tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrStatement::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRSTATEMENT, ((std::string) tok).c_str(), tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrENDExpr::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRENDEXPR, tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrIdExpected::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRIDEXPECTED, ((std::string) tok).c_str(),tok.pos);
	return strbuf;
}


const char* Kalkulon::ErrNumOfArg::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRNUMOFARG, ((std::string) tok).c_str(), tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrUndefSym::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRUNDEFSYM, ((std::string) tok).c_str(), tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrUnknFunc::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRUNKNFUNC, ((std::string) tok).c_str(), tok.token_int, tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrWrongDef::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRWRONGDEF, ((std::string) tok).c_str(), tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrInDefFunc::GetErrorMessage()
{
  if(fn.length()+err_str.length()+2 > strbuf_size)
	{
    const size_t max_error_size=strbuf_size-32;
    if(err_str.length() > max_error_size)
      err_str = err_str.substr(0, max_error_size/2-3) + "..." + err_str.substr(err_str.length()-max_error_size/2, max_error_size/2);
    if(fn.length()+err_str.length()+2 > strbuf_size)
      fn = fn.substr(0, strbuf_size-max_error_size-3-2) + "...";
	}
  strncpy(strbuf, (fn+"::"+err_str).c_str(), strbuf_size);
	return strbuf;
}

const char* Kalkulon::ErrMath::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRMATH, reason.c_str(), ((std::string) tok).c_str(), tok.pos);
	return strbuf;
}

const char* Kalkulon::ErrUserDef::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRUSERDEF, reason.c_str());
	return strbuf;
}

const char* Kalkulon::ErrInternal::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRINTERNAL, line, module.c_str());
	return strbuf;
}

const char* Kalkulon::ErrBreakSig::GetErrorMessage()
{
	SNPRINTF(strbuf, strbuf_size, IDS_ERRBREAKSIG);
	return strbuf;
}
