#include "bod_parser_base.h"
#include "../common/strutils.h"
#include "../common/string_builder.h"

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
//---------------------------------------------------------------------------------
void bod_parser_base::error(const bod_text_parser::token *t, ErrorSeverity severity, const char *format, ...)
{
	va_list ap;
	va_start(ap, format);

	Error *e=new Error();
	e->makeCode(F_Compiler, severity, E_Error);
	if(t){
		e->line=t->line;
		e->col=t->col;
	}
	else{
		e->line=-1;
		e->col=-1;
	}
	_vsnprintf(e->text, sizeof(e->text), format, ap);
	va_end(ap);

	errors.push_back(e);
}
//---------------------------------------------------------------------------------
bod_text_parser::token* bod_parser_base::loadValue(token_stream& is, const char *context)
{
	bod_text_parser::token* t=is.tok();
	bod_text_parser::token* res;

	if(t==NULL || t->type!=token::t_text){
		error(t ? t : is.previous(), S_Error, "%sExpected value after '%s'", context, is.previous()->getText());
		return NULL;
	}
	res=t;

	t=(++is).tok();
	if(t==NULL || t->type!=token::t_semicolon){
		error(is.previous(), S_Error, "%sExpected ';' after '%s'", context, is.previous()->getText());
		return NULL;
	}
	++is;
	return res;
}
//---------------------------------------------------------------------------------
bool bod_parser_base::ungetValue(token_stream& s)
{
	if(s.fail()) return false;
	if(s.tell() >= 2){
		--s;
		if(s.tok()->type==bod_text_parser::token::t_semicolon){
			--s;
			if(s.tok()->type==bod_text_parser::token::t_text)
				return true;
		}
	}
	s.setstate(token_stream::failbit);
	return false;
}
//---------------------------------------------------------------------------------
bool bod_parser_base::loadIntValue(token_stream& is, int *i, const char *msg)
{
	const char *str;
	token *t=loadValue(is, msg);
	bool bRes;
	if(bRes=(t!=NULL)){	
		str=t->text;
		while(*str!=0 && *str==' ')
			str++;
		if(*str=='0' && ((str[1] | 0x20)=='x')){
			if(bRes=ishexa(t->text))
				*i=hextoi(t->text);
		}
		else{
			if(bRes=isinteger(t->text))
				*i=atoi(t->text);
		}
		if(bRes==false)
			error(t, S_Error, "%sExpected integer value", msg);
	}
	return bRes;
}
//---------------------------------------------------------------------------------
bool bod_parser_base::loadDoubleValue(token_stream& is, double *d, const char *msg)
{
	token *t=loadValue(is, msg);
	bool bRes;
	if(bRes=(t!=NULL)){
		if((bRes=isfloat(t->text))==false)
			error(t, S_Error, "%sExpected float value", msg);
		else
			*d=atof(t->text);
	}
	return bRes;
}
//---------------------------------------------------------------------------------
/*char * bod_parser_base::loadString(token_stream& is, const char *context)
{
	int pos=(int)is.tell();
	bod_text_parser::token *t, *prev=NULL;
	size_t size=0;
	while((t=is.tok())!=NULL && t->type==token::t_text || t->type==token::t_colon){
		size+=strlen(t->text) + 1;
		++is;
	}
	size++;
	
	if(t==NULL){
		error(is.previous(), S_Error, "%sUnexpected end of document found while looking for end of string (;)", context);
		return NULL;
	}
	if(t->type!=token::t_semicolon){
		error(t, S_Error, "%sUnexpected here '%s' while loading string", context, t->getText());
		return NULL;
	}

	char *str, *s;
	str=new char[size];
	s=str;

	// it can happen that the stream will unload some tokens as we iterate through it
	// whis will fix problem with pos being greater than current pos
	if((size_t)pos > is.tell())
		pos-=token_stream::tokenTreshold;

	is.advance(-((int)is.tell() - pos));
	while((t=is.tok())!=NULL && t->type==token::t_text || t->type==token::t_colon){
		if(prev!=NULL && prev->type==token::t_text && t->type==token::t_text){
			*s=' ';
			s++;
		}
		size=strlen(t->text);
		memcpy(s, t->text, size);
		s+=(int)size;
		
		prev=t;
		++is;
	}
	*s=0;

	++is;
	return str;
}*/

char * bod_parser_base::loadString(token_stream& is, const char *context)
{
	bod_text_parser::token *t, *prev=NULL;
	string_builder str="";
	
	while((t=is.tok())!=NULL && (t->type==token::t_text || t->type==token::t_colon)){
		if(prev!=NULL && prev->type==token::t_text && t->type==token::t_text){
			str << ' ';
		}
		str << t->getText();
		prev=t;
		++is;
	}
	if(t==NULL){
		error(is.previous(), S_Error, "%sUnexpected end of document found while looking for end of string (;)", context);
		return NULL;
	}
	if(t->type!=token::t_semicolon){
		error(t, S_Error, "%sUnexpected here '%s' while loading string", context, t->getText());
		return NULL;
	}
	++is;
	return is.fail() ? NULL : str.getbuffer();
}
//---------------------------------------------------------------------------------