<commit_before>/*!
  \copyright (c) RDO-Team, 2011
  \file      rdoparser.cpp
  \authors    
  \authors     (<EMAIL>)
  \date      
  \brief     
  \indent    4T
*/

// ---------------------------------------------------------------------------- PCH
#include "simulator/compiler/parser/pch.h"
// ----------------------------------------------------------------------- INCLUDES
#include <boost/foreach.hpp>
// ----------------------------------------------------------------------- SYNOPSIS
#include "simulator/compiler/parser/rdoparser.h"
#include "simulator/compiler/parser/rdoparser_rdo.h"
#include "simulator/compiler/parser/rdofun.h"
#include "simulator/compiler/parser/rdorss.h"
#include "simulator/compiler/parser/rdortp.h"
#include "simulator/compiler/parser/src/db/init_struct_db.h"
#include "simulator/runtime/calc/calc_process.h"
#include "simulator/runtime/calc/resource/calc_resource.h"
#include "simulator/runtime/calc/function/calc_function_system.h"
// --------------------------------------------------------------------------------

OPEN_RDO_PARSER_NAMESPACE

// --------------------------------------------------------------------------------
// -------------------- RDOParser
// --------------------------------------------------------------------------------
RDOParser::ParserList RDOParser::s_parserStack;

#define DECLARE_PARSER_OBJECT_CONTAINER_NONAME(NAME) \
void RDOParser::insert##NAME(LPRDO##NAME value) \
{ \
	howIsIt<LPRDO##NAME>(); \
	m_all##NAME.push_back(value); \
}

#define DECLARE_PARSER_OBJECT_CONTAINER(NAME) \
DECLARE_PARSER_OBJECT_CONTAINER_NONAME(NAME) \
const LPRDO##NAME RDOParser::find##NAME(CREF(tstring) name) const \
{ \
	NAME##List::const_iterator it = std::find_if(m_all##NAME.begin(), m_all##NAME.end(), compareName<RDO##NAME>(name)); \
	return it != m_all##NAME.end() ? *it : LPRDO##NAME(NULL); \
} \
rbool RDOParser::remove##NAME(const LPRDO##NAME item) \
{ \
	NAME##List::iterator it = std::find(m_all##NAME.begin(), m_all##NAME.end(), item); \
	if (it == m_all##NAME.end()) \
		return false; \
	m_all##NAME.erase(it); \
	return true; \
}

DECLARE_PARSER_OBJECT_CONTAINER(PATPattern );
DECLARE_PARSER_OBJECT_CONTAINER(RTPResType );
DECLARE_PARSER_OBJECT_CONTAINER(RSSResource   );
DECLARE_PARSER_OBJECT_CONTAINER(FRMFrame   );
DECLARE_PARSER_OBJECT_CONTAINER(FRMSprite     );
DECLARE_PARSER_OBJECT_CONTAINER(FRMCommandList);
DECLARE_PARSER_OBJECT_CONTAINER(FUNConstant   );
DECLARE_PARSER_OBJECT_CONTAINER(FUNFunction   );
DECLARE_PARSER_OBJECT_CONTAINER(FUNSequence   );
DECLARE_PARSER_OBJECT_CONTAINER(DPTSearch  );
DECLARE_PARSER_OBJECT_CONTAINER(DPTSome    );
DECLARE_PARSER_OBJECT_CONTAINER(DPTPrior   );
DECLARE_PARSER_OBJECT_CONTAINER(DPTActivity   );
DECLARE_PARSER_OBJECT_CONTAINER(Event      );
DECLARE_PARSER_OBJECT_CONTAINER(ResultGroup   );
DECLARE_PARSER_OBJECT_CONTAINER(PROCProcess   );

DECLARE_PARSER_OBJECT_CONTAINER_NONAME(FUNGroup);

rdoModelObjects::RDOFileType RDOParser::getFileToParse()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->m_type : rdoModelObjects::PAT;
}

ruint RDOParser::lexer_loc_line()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->lexer_loc_line() : ~0;
}

ruint RDOParser::lexer_loc_pos()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->lexer_loc_pos() : 0;
}

LPRDOParser RDOParser::s_parser()
{
	return !s_parserStack.empty() ? s_parserStack.back() : LPRDOParser(NULL);
}

RDOParser::RDOParser()
	: m_parser_item         (NULL )
	, m_have_kw_Resources   (false)
	, m_have_kw_ResourcesEnd(false)
	, m_pattern             (false)
{}

RDOParser::~RDOParser()
{}

void RDOParser::init()
{
	s_parserStack.push_back(this);

	m_pRuntime = rdo::Factory<rdo::runtime::RDORuntime>::create(new rdo::runtime::Error());
	ASSERT(m_pRuntime);
	m_pRuntime->memory_insert(sizeof(RDOParser));
	m_pRuntime->init();

	m_pContextStack = rdo::Factory<ContextStack>::create();
	ASSERT(m_pContextStack);

	m_pContextStack->push(this);

	LPRDOSMR pSMR = rdo::Factory<RDOSMR>::create();
	ASSERT(pSMR);
	setSMR(pSMR);

	m_resultGeneratorID.get(); //!  PMD   1
}

void RDOParser::deinit()
{
	m_pContextStack->pop();

	Context::deinit();

	m_allPATPattern .clear();
	m_allRTPResType .clear();
	m_allRSSResource.clear();
	m_allFRMFrame   .clear();
	m_allFRMSprite  .clear();
	m_allFRMCommandList.clear();
	m_allFUNConstant.clear();
	m_allFUNFunction.clear();
	m_allFUNSequence.clear();
	m_allDPTSearch  .clear();
	m_allDPTSome    .clear();
	m_allDPTPrior   .clear();
	m_allDPTActivity.clear();
	m_allEvent      .clear();
	m_allResultGroup.clear();
	m_allPROCProcess.clear();
	m_allFUNGroup   .clear();

	m_parser_item   = NULL;
	m_pSMR          = NULL;
	m_pContextStack = NULL;

	m_preCastTypeList   .clear();
	m_movementObjectList.clear();

	m_pRuntime->deinit();
	m_pRuntime = NULL;

	s_parserStack.remove(this);
}

LPContextStack RDOParser::contextStack()
{
	return m_pContextStack;
}

LPContext RDOParser::context() const
{
	return m_pContextStack->top();
}

Context::FindResult RDOParser::onFindContext(CREF(LPRDOValue) pValue) const
{
	ASSERT(pValue);

	if (pValue->value().getIdentificator() == _T("Time_now") || pValue->value().getIdentificator() == _T("time_now") || pValue->value().getIdentificator() == _T("_") || pValue->value().getIdentificator() == _T("_"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__real>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetTimeNow>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}
	else if (pValue->value().getIdentificator() == _T("Seconds") || pValue->value().getIdentificator() == _T("seconds"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__real>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetSeconds>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}
	else if (pValue->value().getIdentificator() == _T("Terminate_counter") || pValue->value().getIdentificator() == _T("terminate_counter"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__int>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetTermNow>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//!  
	LPRDORTPResType pResType = findRTPResType(pValue->value().getIdentificator());
	if (pResType)
	{
		//!      RTP
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::create(
				pResType,
				pValue->src_info()
			),
			rdo::runtime::LPRDOCalc(NULL),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue, pResType.object_static_cast<Context>());
	}

	//!     RSS
	LPRDORSSResource pResource = findRSSResource(pValue->value().getIdentificator());
	if (pResource)
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::create(
				pResource->getType(),
				pValue->src_info()
			),
			rdo::Factory<rdo::runtime::RDOCalcGetResourceByID>::create(pResource->getID()),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue, pResource.object_static_cast<Context>());
	}

	//! 
	LPRDOFUNConstant pConstant = findFUNConstant(pValue->value().getIdentificator());
	if (pConstant)
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			pConstant->getTypeInfo(),
			rdo::Factory<rdo::runtime::RDOCalcGetConst>::create(pConstant->getNumber()),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//! 
	LPRDOFUNSequence pSequence = findFUNSequence(pValue->value().getIdentificator());
	if (pSequence)
	{
		LPRDOFUNParams pParams = rdo::Factory<RDOFUNParams>::create(
			rdo::Factory<ArithmContainer>::create()
		);
		ASSERT(pParams);
		LPRDOFUNArithm pArithm = pParams->createSeqCall(pValue->value().getIdentificator());
		ASSERT(pArithm);
		pArithm->setSrcInfo(pValue->src_info());
		LPExpression pExpression = rdo::Factory<Expression>::create(
			pArithm->typeInfo(),
			pArithm->calc(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//! ,     ,         
	//!  ,       - 
	{ErrorBlockMonicker errorBlockMonicker;
		CREF(PreCastTypeList) typeList = getPreCastTypeList();
		STL_FOR_ALL_CONST(typeList, it)
		{
			LPRDOValue pTryCastValue = (*it)->value_cast(pValue);
			if (pTryCastValue && pTryCastValue->defined())
			{
				LPExpression pExpression = rdo::Factory<Expression>::create(
					rdo::Factory<TypeInfo>::delegate<RDOType__identificator>(pValue->src_info()),
					rdo::Factory<rdo::runtime::RDOCalcConst>::create(pValue->value()),
					pValue->src_info()
				);
				ASSERT(pExpression);
				return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
			}
		}
	}

	const_cast<PTR(RDOParser)>(this)->error().error(pValue->src_info(), rdo::format(_T(" : %s"), pValue->value().getIdentificator().c_str()));
	return Context::FindResult();
}

rbool RDOParser::isCurrentDPTSearch()
{
	return getLastDPTSearch() && !getLastDPTSearch()->closed() ? true : false;
}

rbool RDOParser::isCurrentDPTPrior()
{
	return getLastDPTPrior() ? true : false;
}

void RDOParser::insertChanges(CREF(tstring) name, CREF(tstring) value)
{
	m_changes.push_back(Changes(name, value));
}

tstring RDOParser::getChanges() const
{
	rdo::textstream stream;
	stream << _T("$Changes") << std::endl;
	ruint changes_max_length = 0;
	STL_FOR_ALL_CONST(m_changes, change_it)
	{
		if (change_it->m_name.length() > changes_max_length)
		{
			changes_max_length = change_it->m_name.length();
		}
	}
	STL_FOR_ALL_CONST(m_changes, change_it)
	{
		stream << _T("  ") << change_it->m_name;
		for (ruint i = change_it->m_name.length(); i < changes_max_length; i++)
		{
			stream << _T(" ");
		}
		stream << _T("  = ") << change_it->m_value << std::endl;
	}
	return stream.str();
}

tstring RDOParser::getModelStructure()
{
	rdo::textstream modelStructure;

	// $Changes
	modelStructure << getChanges();

	// RTP
	modelStructure << std::endl << std::endl << _T("$Resource_type") << std::endl;
	STL_FOR_ALL_CONST(m_allRTPResType, rtp_it)
	{
		(*rtp_it)->writeModelStructure(modelStructure);
	}

	// RSS
	modelStructure << std::endl << _T("$Resources") << std::endl;
	STL_FOR_ALL_CONST(m_allRSSResource, rss_it)
	{
		(*rss_it)->writeModelStructure(modelStructure);
	}

	// PAT
	modelStructure << std::endl << _T("$Pattern") << std::endl;
	STL_FOR_ALL_CONST(m_allPATPattern, pat_it)
	{
		(*pat_it)->writeModelStructure(modelStructure);
	}

	// OPR/DPT
	ruint counter = 1;
	modelStructure << std::endl << _T("$Activities") << std::endl;
	modelStructure << m_pRuntime->writeActivitiesStructure(counter);

	// DPT only
	for (ruint i = 0; i < m_allDPTSearch.size(); i++)
	{
		for (ruint j = 0; j < m_allDPTSearch.at(i)->getActivities().size(); j++)
		{
			LPRDODPTSearchActivity pSearchActivity = m_allDPTSearch.at(i)->getActivities().at(j);
			ASSERT(pSearchActivity);
			modelStructure << counter++ << _T(" ") << pSearchActivity->name() << _T(" ") << pSearchActivity->pattern()->getPatternId() << std::endl;
		}
	}

	// PMD
	modelStructure << std::endl << _T("$Watching") << std::endl;
	ruint watching_max_length = 0;
	STL_FOR_ALL_CONST(m_pRuntime->getResult(), watching_it)
	{
		LPITrace          trace     = *watching_it;
		LPIName           name      = trace;
		LPIModelStructure structure = trace;
		if (trace && name && structure)
		{
			if (trace->traceable() && name->name().length() > watching_max_length)
			{
				watching_max_length = name->name().length();
			}
		}
	}
	STL_FOR_ALL_CONST(m_pRuntime->getResult(), watching_it)
	{
		LPITrace          trace     = *watching_it;
		LPIName           name      = trace;
		LPIModelStructure structure = trace;
		if (trace && name && structure)
		{
			if (trace->traceable())
			{
				modelStructure << _T("  ") << name->name();
				for (ruint i = name->name().length(); i < watching_max_length + 2; i++)
					modelStructure << _T(" ");

				structure->writeModelStructure(modelStructure);
			}
		}
	}

	return modelStructure.str();
}

void RDOParser::parse()
{
	RDOParserContainer::Iterator it = begin();
	while (it != end())
	{
		m_parser_item = it->second;
		it->second->parse(this);
		m_parser_item = NULL;
		it++;
	}

	InitSructDB::dropDB();
	InitSructDB::createDB();
	InitSructDB db;

	STL_FOR_ALL_CONST(getRTPResTypes(), RTPResTypeIt)
	{
		LPRDORTPResType pResType = *RTPResTypeIt;
		pResType->serializeInDB(db);
	}
}

void RDOParser::parse(rdoModelObjects::RDOParseType file)
{<commit_msg> - замена stl_for_all_const на boostforeach<commit_after>/*!
  \copyright (c) RDO-Team, 2011
  \file      rdoparser.cpp
  \authors    
  \authors     (<EMAIL>)
  \date      
  \brief     
  \indent    4T
*/

// ---------------------------------------------------------------------------- PCH
#include "simulator/compiler/parser/pch.h"
// ----------------------------------------------------------------------- INCLUDES
#include <boost/foreach.hpp>
// ----------------------------------------------------------------------- SYNOPSIS
#include "simulator/compiler/parser/rdoparser.h"
#include "simulator/compiler/parser/rdoparser_rdo.h"
#include "simulator/compiler/parser/rdofun.h"
#include "simulator/compiler/parser/rdorss.h"
#include "simulator/compiler/parser/rdortp.h"
#include "simulator/compiler/parser/src/db/init_struct_db.h"
#include "simulator/runtime/calc/calc_process.h"
#include "simulator/runtime/calc/resource/calc_resource.h"
#include "simulator/runtime/calc/function/calc_function_system.h"
// --------------------------------------------------------------------------------

OPEN_RDO_PARSER_NAMESPACE

// --------------------------------------------------------------------------------
// -------------------- RDOParser
// --------------------------------------------------------------------------------
RDOParser::ParserList RDOParser::s_parserStack;

#define DECLARE_PARSER_OBJECT_CONTAINER_NONAME(NAME) \
void RDOParser::insert##NAME(LPRDO##NAME value) \
{ \
	howIsIt<LPRDO##NAME>(); \
	m_all##NAME.push_back(value); \
}

#define DECLARE_PARSER_OBJECT_CONTAINER(NAME) \
DECLARE_PARSER_OBJECT_CONTAINER_NONAME(NAME) \
const LPRDO##NAME RDOParser::find##NAME(CREF(tstring) name) const \
{ \
	NAME##List::const_iterator it = std::find_if(m_all##NAME.begin(), m_all##NAME.end(), compareName<RDO##NAME>(name)); \
	return it != m_all##NAME.end() ? *it : LPRDO##NAME(NULL); \
} \
rbool RDOParser::remove##NAME(const LPRDO##NAME item) \
{ \
	NAME##List::iterator it = std::find(m_all##NAME.begin(), m_all##NAME.end(), item); \
	if (it == m_all##NAME.end()) \
		return false; \
	m_all##NAME.erase(it); \
	return true; \
}

DECLARE_PARSER_OBJECT_CONTAINER(PATPattern );
DECLARE_PARSER_OBJECT_CONTAINER(RTPResType );
DECLARE_PARSER_OBJECT_CONTAINER(RSSResource   );
DECLARE_PARSER_OBJECT_CONTAINER(FRMFrame   );
DECLARE_PARSER_OBJECT_CONTAINER(FRMSprite     );
DECLARE_PARSER_OBJECT_CONTAINER(FRMCommandList);
DECLARE_PARSER_OBJECT_CONTAINER(FUNConstant   );
DECLARE_PARSER_OBJECT_CONTAINER(FUNFunction   );
DECLARE_PARSER_OBJECT_CONTAINER(FUNSequence   );
DECLARE_PARSER_OBJECT_CONTAINER(DPTSearch  );
DECLARE_PARSER_OBJECT_CONTAINER(DPTSome    );
DECLARE_PARSER_OBJECT_CONTAINER(DPTPrior   );
DECLARE_PARSER_OBJECT_CONTAINER(DPTActivity   );
DECLARE_PARSER_OBJECT_CONTAINER(Event      );
DECLARE_PARSER_OBJECT_CONTAINER(ResultGroup   );
DECLARE_PARSER_OBJECT_CONTAINER(PROCProcess   );

DECLARE_PARSER_OBJECT_CONTAINER_NONAME(FUNGroup);

rdoModelObjects::RDOFileType RDOParser::getFileToParse()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->m_type : rdoModelObjects::PAT;
}

ruint RDOParser::lexer_loc_line()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->lexer_loc_line() : ~0;
}

ruint RDOParser::lexer_loc_pos()
{
	return !s_parserStack.empty() && s_parserStack.back()->m_parser_item ? s_parserStack.back()->m_parser_item->lexer_loc_pos() : 0;
}

LPRDOParser RDOParser::s_parser()
{
	return !s_parserStack.empty() ? s_parserStack.back() : LPRDOParser(NULL);
}

RDOParser::RDOParser()
	: m_parser_item         (NULL )
	, m_have_kw_Resources   (false)
	, m_have_kw_ResourcesEnd(false)
	, m_pattern             (false)
{}

RDOParser::~RDOParser()
{}

void RDOParser::init()
{
	s_parserStack.push_back(this);

	m_pRuntime = rdo::Factory<rdo::runtime::RDORuntime>::create(new rdo::runtime::Error());
	ASSERT(m_pRuntime);
	m_pRuntime->memory_insert(sizeof(RDOParser));
	m_pRuntime->init();

	m_pContextStack = rdo::Factory<ContextStack>::create();
	ASSERT(m_pContextStack);

	m_pContextStack->push(this);

	LPRDOSMR pSMR = rdo::Factory<RDOSMR>::create();
	ASSERT(pSMR);
	setSMR(pSMR);

	m_resultGeneratorID.get(); //!  PMD   1
}

void RDOParser::deinit()
{
	m_pContextStack->pop();

	Context::deinit();

	m_allPATPattern .clear();
	m_allRTPResType .clear();
	m_allRSSResource.clear();
	m_allFRMFrame   .clear();
	m_allFRMSprite  .clear();
	m_allFRMCommandList.clear();
	m_allFUNConstant.clear();
	m_allFUNFunction.clear();
	m_allFUNSequence.clear();
	m_allDPTSearch  .clear();
	m_allDPTSome    .clear();
	m_allDPTPrior   .clear();
	m_allDPTActivity.clear();
	m_allEvent      .clear();
	m_allResultGroup.clear();
	m_allPROCProcess.clear();
	m_allFUNGroup   .clear();

	m_parser_item   = NULL;
	m_pSMR          = NULL;
	m_pContextStack = NULL;

	m_preCastTypeList   .clear();
	m_movementObjectList.clear();

	m_pRuntime->deinit();
	m_pRuntime = NULL;

	s_parserStack.remove(this);
}

LPContextStack RDOParser::contextStack()
{
	return m_pContextStack;
}

LPContext RDOParser::context() const
{
	return m_pContextStack->top();
}

Context::FindResult RDOParser::onFindContext(CREF(LPRDOValue) pValue) const
{
	ASSERT(pValue);

	if (pValue->value().getIdentificator() == _T("Time_now") || pValue->value().getIdentificator() == _T("time_now") || pValue->value().getIdentificator() == _T("_") || pValue->value().getIdentificator() == _T("_"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__real>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetTimeNow>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}
	else if (pValue->value().getIdentificator() == _T("Seconds") || pValue->value().getIdentificator() == _T("seconds"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__real>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetSeconds>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}
	else if (pValue->value().getIdentificator() == _T("Terminate_counter") || pValue->value().getIdentificator() == _T("terminate_counter"))
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::delegate<RDOType__int>(pValue->src_info()),
			rdo::Factory<rdo::runtime::RDOCalcGetTermNow>::create(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//!  
	LPRDORTPResType pResType = findRTPResType(pValue->value().getIdentificator());
	if (pResType)
	{
		//!      RTP
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::create(
				pResType,
				pValue->src_info()
			),
			rdo::runtime::LPRDOCalc(NULL),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue, pResType.object_static_cast<Context>());
	}

	//!     RSS
	LPRDORSSResource pResource = findRSSResource(pValue->value().getIdentificator());
	if (pResource)
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			rdo::Factory<TypeInfo>::create(
				pResource->getType(),
				pValue->src_info()
			),
			rdo::Factory<rdo::runtime::RDOCalcGetResourceByID>::create(pResource->getID()),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue, pResource.object_static_cast<Context>());
	}

	//! 
	LPRDOFUNConstant pConstant = findFUNConstant(pValue->value().getIdentificator());
	if (pConstant)
	{
		LPExpression pExpression = rdo::Factory<Expression>::create(
			pConstant->getTypeInfo(),
			rdo::Factory<rdo::runtime::RDOCalcGetConst>::create(pConstant->getNumber()),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//! 
	LPRDOFUNSequence pSequence = findFUNSequence(pValue->value().getIdentificator());
	if (pSequence)
	{
		LPRDOFUNParams pParams = rdo::Factory<RDOFUNParams>::create(
			rdo::Factory<ArithmContainer>::create()
		);
		ASSERT(pParams);
		LPRDOFUNArithm pArithm = pParams->createSeqCall(pValue->value().getIdentificator());
		ASSERT(pArithm);
		pArithm->setSrcInfo(pValue->src_info());
		LPExpression pExpression = rdo::Factory<Expression>::create(
			pArithm->typeInfo(),
			pArithm->calc(),
			pValue->src_info()
		);
		ASSERT(pExpression);
		return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
	}

	//! ,     ,         
	//!  ,       - 
	{ErrorBlockMonicker errorBlockMonicker;
		CREF(PreCastTypeList) typeList = getPreCastTypeList();
		STL_FOR_ALL_CONST(typeList, it)
		{
			LPRDOValue pTryCastValue = (*it)->value_cast(pValue);
			if (pTryCastValue && pTryCastValue->defined())
			{
				LPExpression pExpression = rdo::Factory<Expression>::create(
					rdo::Factory<TypeInfo>::delegate<RDOType__identificator>(pValue->src_info()),
					rdo::Factory<rdo::runtime::RDOCalcConst>::create(pValue->value()),
					pValue->src_info()
				);
				ASSERT(pExpression);
				return Context::FindResult(const_cast<PTR(RDOParser)>(this), pExpression, pValue);
			}
		}
	}

	const_cast<PTR(RDOParser)>(this)->error().error(pValue->src_info(), rdo::format(_T(" : %s"), pValue->value().getIdentificator().c_str()));
	return Context::FindResult();
}

rbool RDOParser::isCurrentDPTSearch()
{
	return getLastDPTSearch() && !getLastDPTSearch()->closed() ? true : false;
}

rbool RDOParser::isCurrentDPTPrior()
{
	return getLastDPTPrior() ? true : false;
}

void RDOParser::insertChanges(CREF(tstring) name, CREF(tstring) value)
{
	m_changes.push_back(Changes(name, value));
}

tstring RDOParser::getChanges() const
{
	rdo::textstream stream;
	stream << _T("$Changes") << std::endl;
	ruint changes_max_length = 0;
	STL_FOR_ALL_CONST(m_changes, change_it)
	{
		if (change_it->m_name.length() > changes_max_length)
		{
			changes_max_length = change_it->m_name.length();
		}
	}
	STL_FOR_ALL_CONST(m_changes, change_it)
	{
		stream << _T("  ") << change_it->m_name;
		for (ruint i = change_it->m_name.length(); i < changes_max_length; i++)
		{
			stream << _T(" ");
		}
		stream << _T("  = ") << change_it->m_value << std::endl;
	}
	return stream.str();
}

tstring RDOParser::getModelStructure()
{
	rdo::textstream modelStructure;

	// $Changes
	modelStructure << getChanges();

	// RTP
	modelStructure << std::endl << std::endl << _T("$Resource_type") << std::endl;
	STL_FOR_ALL_CONST(m_allRTPResType, rtp_it)
	{
		(*rtp_it)->writeModelStructure(modelStructure);
	}

	// RSS
	modelStructure << std::endl << _T("$Resources") << std::endl;
	STL_FOR_ALL_CONST(m_allRSSResource, rss_it)
	{
		(*rss_it)->writeModelStructure(modelStructure);
	}

	// PAT
	modelStructure << std::endl << _T("$Pattern") << std::endl;
	STL_FOR_ALL_CONST(m_allPATPattern, pat_it)
	{
		(*pat_it)->writeModelStructure(modelStructure);
	}

	// OPR/DPT
	ruint counter = 1;
	modelStructure << std::endl << _T("$Activities") << std::endl;
	modelStructure << m_pRuntime->writeActivitiesStructure(counter);

	// DPT only
	for (ruint i = 0; i < m_allDPTSearch.size(); i++)
	{
		for (ruint j = 0; j < m_allDPTSearch.at(i)->getActivities().size(); j++)
		{
			LPRDODPTSearchActivity pSearchActivity = m_allDPTSearch.at(i)->getActivities().at(j);
			ASSERT(pSearchActivity);
			modelStructure << counter++ << _T(" ") << pSearchActivity->name() << _T(" ") << pSearchActivity->pattern()->getPatternId() << std::endl;
		}
	}

	// PMD
	modelStructure << std::endl << _T("$Watching") << std::endl;
	ruint watching_max_length = 0;
	STL_FOR_ALL_CONST(m_pRuntime->getResult(), watching_it)
	{
		LPITrace          trace     = *watching_it;
		LPIName           name      = trace;
		LPIModelStructure structure = trace;
		if (trace && name && structure)
		{
			if (trace->traceable() && name->name().length() > watching_max_length)
			{
				watching_max_length = name->name().length();
			}
		}
	}
	STL_FOR_ALL_CONST(m_pRuntime->getResult(), watching_it)
	{
		LPITrace          trace     = *watching_it;
		LPIName           name      = trace;
		LPIModelStructure structure = trace;
		if (trace && name && structure)
		{
			if (trace->traceable())
			{
				modelStructure << _T("  ") << name->name();
				for (ruint i = name->name().length(); i < watching_max_length + 2; i++)
					modelStructure << _T(" ");

				structure->writeModelStructure(modelStructure);
			}
		}
	}

	return modelStructure.str();
}

void RDOParser::parse()
{
	RDOParserContainer::Iterator it = begin();
	while (it != end())
	{
		m_parser_item = it->second;
		it->second->parse(this);
		m_parser_item = NULL;
		it++;
	}

	InitSructDB::dropDB();
	InitSructDB::createDB();
	InitSructDB db;

	BOOST_FOREACH(const LPRDORTPResType& pResType, getRTPResTypes())
	{
		pResType->serializeInDB(db);
	}
}

void RDOParser::parse(rdoModelObjects::RDOParseType file)
{
{'fraction_non_alphanumeric': 0.19409908368503098, 'fraction_numerical': 0.001064598304246987, 'mean_word_length': 4.428689370485036, 'pattern_counts': {'":': 0, '<': 239, '<?xml version=': 0, '>': 265, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5009601', 'n_tokens_mistral': 10008, 'n_tokens_neox': 9330, 'n_words': 1199}