#ifndef WONKY_ERROR_C
#define WONKY_ERROR_C WONKY_ERROR_C
#include <gcc_error.h>
struct Wonky_Message* get_wonky_message(enum Wonky_Message_Type type,enum Wonky_Message_Source source,ssize_t line,ssize_t column,char *filename,size_t filename_size,const char *msg_fmt,...)
{
va_list args;
struct Wonky_Message *ret;
va_start(args,msg_fmt);
ret=get_wonky_message_vargs(type,source,line,column,filename,filename_size,msg_fmt,args);
va_end(args);
return ret;
}
struct Wonky_Message* get_wonky_message_vargs(enum Wonky_Message_Type type,enum Wonky_Message_Source source,ssize_t line,ssize_t column,char *filename,size_t filename_size,const char *msg_fmt,va_list args)
{
struct Wonky_Message *ret;
ret=wonky_malloc(sizeof(struct Wonky_Message));
ret->type=type;
ret->source=source;
ret->message=wonky_malloc(sizeof(struct wonky_str));
*ret->message=wonky_string_make();
wonky_string_vprintf(ret->message,msg_fmt,args);
ret->line=line+1;
ret->column=column+1;
ret->filename=filename;
ret->filename_size=filename_size;
return ret;
}
void push_generic_error(struct Program *program,const char *err_fmt,...)
{
va_list args;
va_start(args,err_fmt);
push_generic_error_vargs(program,err_fmt,args);
va_end(args);
}
void push_generic_error_vargs(struct Program *program,const char *err_fmt,va_list args)
{
Queue_Push(program->errors,get_wonky_message_vargs(WONKY_MESSAGE_TYPE_ERROR,WONKY_MESSAGE_SOURCE_END,-1,-1,NULL,0,err_fmt,args));
}
void push_generic_note(struct Program *program,const char *note_fmt,...)
{
va_list args;
va_start(args,note_fmt);
push_generic_note_vargs(program,note_fmt,args);
va_end(args);
}
void push_generic_note_vargs(struct Program *program,const char *note_fmt,va_list args)
{
Queue_Push(program->errors,get_wonky_message_vargs(WONKY_MESSAGE_TYPE_NOTE,WONKY_MESSAGE_SOURCE_END,-1,-1,NULL,0,note_fmt,args));
}
void push_message_struct(struct Program *program,struct Wonky_Message *error)
{
Queue_Push(program->errors,error);
}
void push_token_pointer_error(struct Token_Pointer *ptr,const char *err_fmt,...)
{
va_list args;
va_start(args,err_fmt);
push_token_pointer_error_vargs(ptr,err_fmt,args);
va_end(args);
}
void push_token_pointer_error_vargs(struct Token_Pointer *ptr,const char *err_fmt,va_list args)
{
ssize_t line=-1;
ssize_t column=-1;
char *filename=NULL;
size_t filename_size=0;
struct Wonky_Message *hold;
if(ptr->context)
{
line=ptr->context->line;
column=ptr->context->column;
filename=ptr->context->filename;
filename_size=ptr->context->filename_size;
}
hold=get_wonky_message_vargs(WONKY_MESSAGE_TYPE_ERROR,
WONKY_MESSAGE_SOURCE_TRANSLATION,
line,column,filename,filename_size,err_fmt,args);
Queue_Push(ptr->program->errors,hold);
}
void push_translation_error(const char *fmt,struct Translation_Data *translation_data,...)
{
va_list args;
va_start(args,translation_data);
push_translation_error_vargs(fmt,translation_data,args);
va_end(args);
}
void push_translation_error_vargs(const char *fmt,struct Translation_Data *translation_data,va_list args)
{
ssize_t line=-1;
ssize_t column=-1;
char *filename=NULL;
size_t filename_size=0;
struct wonky_stream s;
struct Wonky_Message *hold;
if(translation_data->token_pointer->context)
{
line=translation_data->token_pointer->context->line;
column=translation_data->token_pointer->context->column;
filename=translation_data->token_pointer->context->filename;
filename_size=translation_data->token_pointer->context->filename_size;
}
hold=get_wonky_message_vargs(WONKY_MESSAGE_TYPE_ERROR,
WONKY_MESSAGE_SOURCE_TRANSLATION,
line,column,filename,filename_size,fmt,args);
s=wonky_string_stream(hold->message);
wonky_fseek(&s,0,SEEK_END);
wonky_fprintf(&s,"%WIC",translation_data->token_pointer);
Queue_Push(translation_data->program->errors,hold);
wonky_string_stream_delete(&s);
}
void push_translation_note(const char *fmt,struct Translation_Data *translation_data,...)
{
va_list args;
va_start(args,translation_data);
push_translation_note_vargs(fmt,translation_data,args);
va_end(args);
}
void push_translation_note_vargs(const char *fmt,struct Translation_Data *translation_data,va_list args)
{
ssize_t line=-1;
ssize_t column=-1;
char *filename=NULL;
size_t filename_size=0;
struct wonky_stream s;
struct Wonky_Message *hold;
if(translation_data->token_pointer->context)
{
line=translation_data->token_pointer->context->line;
column=translation_data->token_pointer->context->column;
filename=translation_data->token_pointer->context->filename;
filename_size=translation_data->token_pointer->context->filename_size;
}
hold=get_wonky_message_vargs(WONKY_MESSAGE_TYPE_NOTE,
WONKY_MESSAGE_SOURCE_TRANSLATION,
line,column,filename,filename_size,fmt,args);
s=wonky_string_stream(hold->message);
wonky_fprintf(&s,"%WIC",translation_data->token_pointer);
Queue_Push(translation_data->program->errors,hold);
wonky_string_stream_delete(&s);
}
void print_message(struct wonky_stream *out,struct Wonky_Message *msg)
{
wonky_assert(out && msg);
switch(msg->type)
{
case WONKY_MESSAGE_TYPE_ERROR:
wonky_fprintf(out,"[Error] ");
break;
case WONKY_MESSAGE_TYPE_NOTE:
wonky_fprintf(out,"[Note] ");
break;
}
if(msg->filename && msg->filename_size)
wonky_write(out,msg->filename,msg->filename_size);
if(msg->line>0)
{
wonky_fprintf(out,"%s%zu:%zu ",(msg->filename?":":" "),(size_t)msg->line,(size_t)msg->column);
}
wonky_fprintf(out,"%s\n",msg->message->cs);
}
void delete_error(struct Program *program,struct Wonky_Message *error)
{
/*currently all of wonky's memory is in a big heap an dropped after all use*/
}
#endif