#include "bod_parser.h"
#include "bod_text_parser.h"
#include "token_stream.h"
#include "bod_cut_parser.h"
//---------------------------------------------------------------------------------
bob_dom_document * bod_parser::parseBuffer(char *pszBuffer, size_t size, fileType type)
{
	bod_text_parser parser;
	token_stream is;
	bob_dom_document *doc=NULL;
	bob_dom_cut *cut=NULL;
	bob_dom_bob *bob=NULL;
	bod_parser_base *errorObj;
	bool bRes=false;
	
	is.rdbuffer(pszBuffer, size);
	
	if(type==bobFile){
		bod_bob_parser *bp=new bod_bob_parser(m_settings);
		errorObj=bp;
		bob=bp->loadBOB(is, false);
		bRes=bob!=NULL;
	}
	else if(type==cutFile){
		bod_cut_parser *cp=new bod_cut_parser(m_settings);
		cut=cp->loadCUT(is);
		errorObj=cp;
		bRes=cut!=NULL;
	}
	
	for(iterator &it=errorObj->errors.begin(); it!=errorObj->errors.end(); ++it){
		error(it);
	}
		
	if(bRes){
		doc=new bob_dom_document(m_settings);
		doc->bob=bob;
		doc->cut=cut;
	}
	return doc;
}
//---------------------------------------------------------------------------------
bool bod_parser::compile(char *pszBuffer, size_t size, fileType type, bob_dom_obinaryfilestream& os)
{
	bod_text_parser parser;
	token_stream is;
	bod_parser_base *errorObj;
	bool bRes=false;
	
	is.rdbuffer(pszBuffer, size);
	
	if(type==bobFile){
		bod_bob_parser *bp=new bod_bob_parser(m_settings);
		errorObj=bp;
		bRes=bp->compile(is, os, false);
	}
	else if(type==cutFile){
		bod_cut_parser *cp=new bod_cut_parser(m_settings);
		bRes=cp->compile(is, os);
		errorObj=cp;
	}
	
	for(iterator &it=errorObj->errors.begin(); it!=errorObj->errors.end(); ++it){
		error(it);
	}
	return bRes;
}
//---------------------------------------------------------------------------------
void bod_parser::error(iterator& it)
{
	Error *e=new Error();
	
	e->code=it->code;
	e->col=it->col;
	e->line=it->line;
	memcpy(e->text, it->text, sizeof(e->text));
	errors.push_back(e);
}
//---------------------------------------------------------------------------------
