Generator uses now pt::Log as a logger

- added method SetLogger(pt::Log & logger)
- removed SetCommentary(...) methods
- added logs when a model is not found

PatternParser uses only pt::Log
- removed SetCommentary(...) methods
This commit is contained in:
Tomasz Sowa 2021-06-19 20:18:30 +02:00
parent d76bb9349b
commit 9c4fe51790
3 changed files with 126 additions and 180 deletions

View File

@ -49,6 +49,7 @@
#include "outstreams.h"
#include "expressionparser.h"
#include "models.h"
#include "log/log.h"
#ifdef EZC_HAS_MORM_LIBRARY
#include "model.h"
@ -79,6 +80,8 @@ public:
void SetObjects(Objects<StreamType> & objects);
void SetVariables(Vars & variables); // [def] and [let]
void SetLogger(pt::Log & logger);
#ifdef EZC_HAS_MORM_LIBRARY
void SetModels(Models & models);
#endif
@ -131,19 +134,13 @@ public:
void Generate(OutStreams<StreamType> & out_streams);
void SetCommentary(const char * com_start, const char * com_stop);
void SetCommentary(const std::string & com_start, const std::string & com_stop);
void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop);
void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop);
private:
struct FindHelper
{
#ifdef EZC_HAS_MORM_LIBRARY
morm::ModelWrapper * model_wrapper;
#endif
std::wstring * fun_name;
BaseObj<StreamType> * base_obj;
int method_index;
@ -151,17 +148,23 @@ private:
Item * item_block;
Var * variable;
#ifdef EZC_HAS_MORM_LIBRARY
morm::ModelWrapper * model_wrapper;
#endif
FindHelper()
{
#ifdef EZC_HAS_MORM_LIBRARY
model_wrapper = nullptr;
#endif
fun_name = nullptr;
base_obj = nullptr;
method_index = -1;
function = nullptr;
item_block = nullptr;
variable = nullptr;
#ifdef EZC_HAS_MORM_LIBRARY
model_wrapper = nullptr;
#endif
}
};
@ -208,7 +211,9 @@ private:
#ifdef EZC_HAS_MORM_LIBRARY
Models * pmodels;
#endif
Vars * pvars;
Vars * pvars;
pt::Log * plog;
// pointer to the output streams map (can be null)
// output stream will be created when [ezc frame "stream_name"] statement is found
@ -262,8 +267,6 @@ private:
// a stack for [for] statements
std::vector<Stack> stack_tab;
std::wstring commentary_start, commentary_stop;
bool can_use_vars;
bool can_find_in_cache;
bool program_mode;
@ -298,16 +301,14 @@ private:
bool FindInCache(Item::Function & item_fun, FindHelper & find_helper);
#ifdef EZC_HAS_MORM_LIBRARY
bool FindInModels(const std::wstring & name, FindHelper & find_helper);
bool FindInModels(FindHelper & find_helper);
#endif
bool FindInFunctionsAndBlocks(const std::wstring & name, FindHelper & find_helper);
bool FindInVariables(const std::wstring & name, FindHelper & find_helper);
bool Find(Item::Function & item_fun,
std::wstring & fun_name,
FindHelper & find_helper);
bool Find(Item::Function & item_fun, FindHelper & find_helper);
void CallFunction(typename Functions<StreamType>::UserFunction & function, FunInfo<StreamType> & info);
@ -323,9 +324,9 @@ private:
void CallObject(BaseObj<StreamType> & base_obj, int method_index, FunInfo<StreamType> & info);
#ifdef EZC_HAS_MORM_LIBRARY
void CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
bool CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
CallModelHelper FindLastModelWrapper(morm::ModelWrapper & models_base, std::vector<std::wstring> & fields);
void CallModelWrapper(morm::ModelWrapper & models_base, std::vector<std::wstring> & fields, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
void CallModelWrapper(FindHelper & find_helper, std::vector<std::wstring> & fields, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream);
#endif
void CallObject(BaseObj<StreamType> & base_obj,
@ -361,12 +362,14 @@ private:
void CopyTmpStreamToOutputStreams(Item::Function & fun, StreamType & ezc_out_tmp_stream, StreamType & previous_stream);
void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0);
void CreateMsg(StreamType & stream, const wchar_t * type, const wchar_t * arg = 0);
void CreateMsg(const wchar_t * type, const wchar_t * arg = 0);
void CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector<std::wstring> & fields,
const wchar_t * arg = nullptr, const wchar_t * arg2 = nullptr, const wchar_t * arg3 = nullptr);
void CreateMsg(const wchar_t * type, const wchar_t * arg = nullptr);
void CreateMsg(const std::wstring & type, const std::wstring & arg);
void CreateMsg(const std::wstring & type);
void CreateUnknownMsg(const std::wstring & fun);
bool LimitAchieved();
void EvaluateProgramNode(Item & item);
@ -408,6 +411,7 @@ Generator<StreamType>::Generator() : empty_stream()
pfunctions = nullptr;
pobjects = nullptr;
pvars = nullptr;
plog = nullptr;
#ifdef EZC_HAS_MORM_LIBRARY
pmodels = nullptr;
@ -446,6 +450,7 @@ Generator<StreamType> & Generator<StreamType>::operator=(const Generator<StreamT
pfunctions = n.pfunctions;
pobjects = n.pobjects;
pvars = n.pvars;
plog = n.plog;
#ifdef EZC_HAS_MORM_LIBRARY
pmodels = n.pmodels;
@ -494,41 +499,6 @@ Generator<StreamType>::~Generator()
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const char * com_start, const char * com_stop)
{
pt::utf8_to_wide(com_start, commentary_start);
pt::utf8_to_wide(com_stop, commentary_stop);
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const std::string & com_start, const std::string & com_stop)
{
pt::utf8_to_wide(com_start, commentary_start);
pt::utf8_to_wide(com_stop, commentary_stop);
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
template<class StreamType>
void Generator<StreamType>::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
template<class StreamType>
void Generator<StreamType>::SetPattern(Pattern & pattern)
@ -567,6 +537,14 @@ void Generator<StreamType>::SetVariables(Vars & variables)
}
template<class StreamType>
void Generator<StreamType>::SetLogger(pt::Log & logger)
{
plog = &logger;
}
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType>
void Generator<StreamType>::SetModels(Models & models)
@ -942,11 +920,11 @@ return false;
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType>
bool Generator<StreamType>::FindInModels(const std::wstring & name, FindHelper & find_helper)
bool Generator<StreamType>::FindInModels(FindHelper & find_helper)
{
if( pmodels )
{
morm::ModelWrapper * m = pmodels->Find(name);
morm::ModelWrapper * m = pmodels->Find(*find_helper.fun_name);
if( m )
{
@ -1025,12 +1003,10 @@ bool Generator<StreamType>::FindInVariables(const std::wstring & name, FindHelpe
* and if not null then means: as a funcion name we are not using item_fun.name but fun_name
*/
template<class StreamType>
bool Generator<StreamType>::Find(Item::Function & item_fun,
std::wstring & fun_name,
FindHelper & find_helper)
bool Generator<StreamType>::Find(Item::Function & item_fun, FindHelper & find_helper)
{
#ifdef EZC_HAS_MORM_LIBRARY
if( FindInModels(fun_name, find_helper) )
if( FindInModels(find_helper) )
return true;
#endif
@ -1040,13 +1016,13 @@ bool Generator<StreamType>::Find(Item::Function & item_fun,
if( FindInCache(item_fun, find_helper) )
return true;
if( FindInFunctionsAndBlocks(fun_name, find_helper) )
if( FindInFunctionsAndBlocks(*find_helper.fun_name, find_helper) )
return true;
if( FindInVariables(fun_name, find_helper) )
if( FindInVariables(*find_helper.fun_name, find_helper) )
return true;
CreateUnknownMsg(fun_name);
CreateMsg(L"unknown function", find_helper.fun_name->c_str());
return false;
}
@ -1168,7 +1144,7 @@ bool Generator<StreamType>::ShouldEscapeValue(std::vector<Var> parameters)
#ifdef EZC_HAS_MORM_LIBRARY
template<class StreamType>
void Generator<StreamType>::CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream)
bool Generator<StreamType>::CallModel(morm::Model & model, const std::wstring & field, std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream)
{
/*
* if 'field' is a POD type then 'str' will be used in get_raw_value()
@ -1177,22 +1153,28 @@ void Generator<StreamType>::CallModel(morm::Model & model, const std::wstring &
*
*/
pt::TextStream str;
bool found = false;
if( parameters.empty() )
{
FunInfo<StreamType> info(out_stream, parameters, empty, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
found = model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
last_res = info.res;
}
else
{
FunInfo<StreamType> info(out_stream, parameters, parameters[0].str, in_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
found = model.get_raw_value(nullptr, field.c_str(), nullptr, info, str, false);
last_res = info.res;
}
bool should_escape = ShouldEscapeValue(parameters);
CopyStream(str, out_stream, should_escape);
if( found )
{
bool should_escape = ShouldEscapeValue(parameters);
CopyStream(str, out_stream, should_escape);
}
return found;
}
@ -1238,11 +1220,11 @@ typename Generator<StreamType>::CallModelHelper Generator<StreamType>::FindLastM
template<class StreamType>
void Generator<StreamType>::CallModelWrapper(morm::ModelWrapper & models_base_root, std::vector<std::wstring> & fields,
void Generator<StreamType>::CallModelWrapper(FindHelper & find_helper, std::vector<std::wstring> & fields,
std::vector<Var> parameters, StreamType & out_stream, const StreamType & in_stream)
{
last_res = false;
CallModelHelper helper = FindLastModelWrapper(models_base_root, fields);
CallModelHelper helper = FindLastModelWrapper(*find_helper.model_wrapper, fields);
// if:
// helper.field_index == fields.size() - all fields items are models or models containers
@ -1251,8 +1233,7 @@ void Generator<StreamType>::CallModelWrapper(morm::ModelWrapper & models_base_ro
if( helper.field_index + 1 < fields.size() )
{
// put some log
// at the end there are some fields which are not models nor models containers
CreateMsg(L"cannot find ", *find_helper.fun_name, fields, L", ", fields[helper.field_index].c_str(), L" is not a model nor a model container");
last_res = false;
return;
}
@ -1263,13 +1244,15 @@ void Generator<StreamType>::CallModelWrapper(morm::ModelWrapper & models_base_ro
if( helper.model )
{
CallModel(*helper.model, fields[helper.field_index], parameters, out_stream, in_stream);
if( !CallModel(*helper.model, fields[helper.field_index], parameters, out_stream, in_stream) )
{
CreateMsg(L"cannot find ", *find_helper.fun_name, fields, L", unknown ", fields[helper.field_index].c_str());
}
}
else
{
last_res = false;
// put log something like:
// model not set, have you forgotten to use [for ...] statement?
CreateMsg(L"model ", *find_helper.fun_name, fields, L" is not initialized, have you forgotten to use [for ...] statement?");
}
}
else
@ -1390,10 +1373,12 @@ std::vector<Var> parameters;
if( clear_out_stream )
ClearStream(out_stream);
if( !fun_name )
fun_name = &item_fun.name;
find_helper.fun_name = fun_name;
if( !Find(item_fun, *fun_name, find_helper) )
if( !find_helper.fun_name )
find_helper.fun_name = &item_fun.name;
if( !Find(item_fun, find_helper) )
return false;
parameters.resize(item_fun.parameters.size());
@ -1424,7 +1409,7 @@ std::vector<Var> parameters;
#ifdef EZC_HAS_MORM_LIBRARY
if( find_helper.model_wrapper )
CallModelWrapper(*find_helper.model_wrapper, item_fun.fields, parameters, out_stream, in_stream);
CallModelWrapper(find_helper, item_fun.fields, parameters, out_stream, in_stream);
else
#endif
if( find_helper.base_obj )
@ -1663,7 +1648,7 @@ void Generator<StreamType>::CopyTmpStreamToOutputStreams(Item::Function & fun, S
}
else
{
CreateMsg(previous_stream, L"limit of output streams in OutStreams<> has been reached");
CreateMsg(L"limit of output streams in OutStreams<> has been reached");
}
}
else
@ -1678,41 +1663,58 @@ void Generator<StreamType>::CopyTmpStreamToOutputStreams(Item::Function & fun, S
template<class StreamType>
void Generator<StreamType>::CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg)
{
out = commentary_start;
out += L"Ezc runtime error: ";
out += type;
if( arg )
{
out += ' ';
out += arg;
}
out += commentary_stop;
}
template<class StreamType>
void Generator<StreamType>::CreateMsg(StreamType & stream, const wchar_t * type, const wchar_t * arg)
{
CreateMsg(temp_msg, type, arg);
stream.write(temp_msg.c_str(), temp_msg.size());
temp_msg.clear();
}
template<class StreamType>
void Generator<StreamType>::CreateMsg(const wchar_t * type, const wchar_t * arg)
{
if( output_stream )
CreateMsg(*output_stream, type, arg);
if( plog )
{
(*plog) << pt::Log::log2 << L"Ezc: runtime error: " << type;
if( arg )
{
(*plog) << arg;
}
(*plog) << pt::Log::logend;
}
}
template<class StreamType>
void Generator<StreamType>::CreateMsg(const wchar_t * type, const std::wstring & model_name, std::vector<std::wstring> & fields,
const wchar_t * arg, const wchar_t * arg2, const wchar_t * arg3)
{
if( plog )
{
(*plog) << pt::Log::log2 << L"Ezc: " << type << model_name;
for(std::wstring & str : fields)
{
(*plog) << '.' << str;
}
if( arg )
{
(*plog) << arg;
}
if( arg2 )
{
(*plog) << arg2;
}
if( arg3 )
{
(*plog) << arg3;
}
(*plog) << pt::Log::logend;
}
}
template<class StreamType>
void Generator<StreamType>::CreateMsg(const std::wstring & type, const std::wstring & arg)
{
@ -1728,13 +1730,6 @@ void Generator<StreamType>::CreateMsg(const std::wstring & type)
}
template<class StreamType>
void Generator<StreamType>::CreateUnknownMsg(const std::wstring & fun)
{
CreateMsg(L"unknown function", fun.c_str());
}
template<class StreamType>
void Generator<StreamType>::EvaluateProgramNode(Item & item)

View File

@ -124,37 +124,6 @@ void PatternParser::SetBlocks(Blocks & blocks)
}
void PatternParser::SetCommentary(const char * com_start, const char * com_stop)
{
pt::utf8_to_wide(com_start, commentary_start);
pt::utf8_to_wide(com_stop, commentary_stop);
}
void PatternParser::SetCommentary(const std::string & com_start, const std::string & com_stop)
{
pt::utf8_to_wide(com_start, commentary_start);
pt::utf8_to_wide(com_stop, commentary_stop);
}
void PatternParser::SetCommentary(const wchar_t * com_start, const wchar_t * com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
void PatternParser::SetCommentary(const std::wstring & com_start, const std::wstring & com_stop)
{
commentary_start = com_start;
commentary_stop = com_stop;
}
void PatternParser::SetProgramMode(bool program_mode)
{
this->program_mode = program_mode;
@ -167,23 +136,6 @@ void PatternParser::SetLogger(pt::Log * log)
}
void PatternParser::CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg)
{
out = commentary_start;
out += L"Ezc: ";
out += type;
if( arg )
{
out += ' ';
out += arg;
}
out += commentary_stop;
}
void PatternParser::ParseFile(const std::string & file_name, Pattern & pattern)
{
ParseFile(file_name.c_str(), pattern);
@ -350,15 +302,25 @@ void PatternParser::ReadFile(const wchar_t * name, std::wstring & result)
{
if( !IsFileCorrect(name) )
{
CreateMsg(result, L"incorrect file name: ", name);
if( log )
{
(*log) << pt::Log::log2 << "Ezc: incorrect file name: " << name << pt::Log::logend;
}
}
else
{
result.clear();
if( !ReadFileFromDir(directory, name, result) )
{
if( !ReadFileFromDir(directory2, name, result) )
CreateMsg(result, L"can't open: ", name);
{
if( log )
{
(*log) << pt::Log::log2 << "Ezc: can't open file: " << name << pt::Log::logend;
}
}
}
}
}
@ -1161,7 +1123,7 @@ void PatternParser::CreateTreeReadIncludeSkipAllowFlag(Item & item)
{
if( log )
{
(*log) << pt::Log::log1 << "Ezc: \"include\" directive has reached the maximum level" << pt::Log::logend;
(*log) << pt::Log::log2 << "Ezc: \"include\" directive has reached the maximum level" << pt::Log::logend;
}
return;

View File

@ -79,16 +79,8 @@ public:
void AllowInclude(bool allow);
void DeleteWhiteTextItems(bool del);
void SetIncludeMax(int include_max);
void SetBlocks(Blocks & blocks);
void SetCommentary(const char * com_start, const char * com_stop);
void SetCommentary(const std::string & com_start, const std::string & com_stop);
void SetCommentary(const wchar_t * com_start, const wchar_t * com_stop);
void SetCommentary(const std::wstring & com_start, const std::wstring & com_stop);
void SetProgramMode(bool program_mode);
void SetLogger(pt::Log * log);
private:
@ -117,8 +109,6 @@ private:
std::wstring directory, directory2;
std::wstring commentary_start, commentary_stop;
int include_level, include_level_max;
@ -140,7 +130,6 @@ private:
pt::Log * log;
void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0);
void ReadFile(const std::wstring & name, std::wstring & result);
void ReadFile(const wchar_t * name, std::wstring & result);