2010-11-22 02:23:32 +01:00
|
|
|
/*
|
2014-10-17 23:48:11 +02:00
|
|
|
* This file is a part of EZC -- Easy templating in C++ library
|
|
|
|
* and is distributed under the BSD 3-Clause licence.
|
2010-11-22 02:23:32 +01:00
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-10-11 21:56:48 +02:00
|
|
|
* Copyright (c) 2007-2014, Tomasz Sowa
|
2010-11-22 02:23:32 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef headerfile_ezc_generator
|
|
|
|
#define headerfile_ezc_generator
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
#include "vars.h"
|
|
|
|
#include "blocks.h"
|
2010-11-22 02:23:32 +01:00
|
|
|
#include "pattern.h"
|
|
|
|
#include "functions.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
2011-01-26 13:42:49 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
namespace Ezc
|
|
|
|
{
|
2012-02-27 18:39:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 02:55:32 +01:00
|
|
|
/*
|
|
|
|
StreamType
|
2010-11-25 23:39:58 +01:00
|
|
|
we use only method write(const wchar_t * str, size_t len) from the stream
|
2010-11-25 02:55:32 +01:00
|
|
|
*/
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
|
|
|
class Generator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Generator();
|
2011-01-26 13:42:49 +01:00
|
|
|
Generator(const Generator & n);
|
|
|
|
Generator & operator=(const Generator & n);
|
|
|
|
~Generator();
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
void SetPattern(Pattern & pattern);
|
|
|
|
void SetBlocks(Blocks & blocks);
|
|
|
|
void SetFunctions(Functions<StreamType> & functions);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
void SetMax(size_t max_items_, size_t max_for_items_);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
// recognizing some special characters in text patterns (item_text in Patterns)
|
|
|
|
// \r will be a carriage return (13)
|
|
|
|
// \n will be a new line (10)
|
|
|
|
// \t will be a tabulator (9)
|
|
|
|
// \s will be a space
|
|
|
|
// \\ will be one '\'
|
2010-11-25 02:55:32 +01:00
|
|
|
// if the second character is different from those shown above the first slash will be printed
|
|
|
|
// so "\x" gives "\x"
|
2010-11-22 02:23:32 +01:00
|
|
|
// default: false
|
|
|
|
void RecognizeSpecialChars(bool spec);
|
|
|
|
|
|
|
|
// trimming white characters (at the beginning and at the end of an item_text)
|
|
|
|
// (special char \s if enabled is not treated as a white character here)
|
|
|
|
// default: false
|
|
|
|
void TrimWhite(bool trim);
|
|
|
|
|
|
|
|
// skipping new line characters (from the whole string in an item_text)
|
|
|
|
// but you can use a new line character written as "\n" (if special chars are turn on)
|
|
|
|
// default: false
|
|
|
|
void SkipNewLine(bool skip);
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
// default: 20
|
|
|
|
void SetMaxFilters(size_t new_len);
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
// default stack size: 300
|
|
|
|
void SetStackSize(size_t new_stack_size);
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// do not print anything to stream stream_index
|
|
|
|
// useful when using Generate() method with StreamType tables
|
|
|
|
// this is a small optimization if we know that a stream with stream_index will not be used later
|
|
|
|
// set stream_index to -1 to turn off (default)
|
|
|
|
void SkipStream(int stream_index);
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
// set whether or not we can use cache for functions or blocks
|
|
|
|
// true by default
|
|
|
|
void CanUseCache(bool can_use_cache);
|
|
|
|
|
|
|
|
// set whether or not we can use variables: [def ...] statement
|
|
|
|
// true by default
|
|
|
|
void CanUseVars(bool can_use_variables);
|
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
// the main methods for generating
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generate(StreamType & o);
|
|
|
|
void Generate(std::vector<StreamType> & o);
|
|
|
|
void Generate(std::vector<StreamType*> & o);
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2014-10-19 07:42:25 +02:00
|
|
|
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);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
struct BlockStack
|
|
|
|
{
|
|
|
|
std::vector<std::wstring> args;
|
|
|
|
StreamType * out_stream;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<BlockStack> block_stack_tab;
|
|
|
|
size_t block_stack_index;
|
|
|
|
size_t block_stack_size;
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// current output stream
|
2014-10-17 23:36:55 +02:00
|
|
|
// if we are printing only to one stream e.g. [ezc output "3"]
|
2013-11-07 11:16:10 +01:00
|
|
|
// then output_stream is pointing directly to that stream
|
2014-10-17 23:36:55 +02:00
|
|
|
// but if we are printing to more than one stream e.g. [ezc output "0" "2" "5"]
|
2013-11-17 22:53:53 +01:00
|
|
|
// then output_stream is pointing to output_tmp_stream and at the end
|
|
|
|
// output_tmp_stream is copied to the correct streams
|
2014-10-17 23:36:55 +02:00
|
|
|
// output_stream can be null e.g. [ezc output] without arguments
|
|
|
|
// in such a case we do not print anything (but any functions are evaluated)
|
2010-11-22 02:23:32 +01:00
|
|
|
StreamType * output_stream;
|
2014-10-17 23:36:55 +02:00
|
|
|
|
|
|
|
Pattern * ppattern;
|
|
|
|
Blocks * pblocks;
|
|
|
|
Functions<StreamType> * pfunctions;
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
// variables set
|
|
|
|
Vars vars;
|
|
|
|
|
2013-11-17 22:53:53 +01:00
|
|
|
// output_tmp_stream is used when outputting to more than one stream
|
|
|
|
// (first we output to output_tmp_stream and then the content is copied
|
2013-11-07 11:16:10 +01:00
|
|
|
// to the correct streams)
|
2013-11-17 22:53:53 +01:00
|
|
|
StreamType output_tmp_stream;
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
// local_temp_stream is used when there are nested function calls
|
|
|
|
// e.g.: [function1 [function2]]
|
|
|
|
// function2 will be redirected to local_temp_stream and the string
|
|
|
|
// will be copied to Item::function.par std::wstring object
|
|
|
|
StreamType local_temp_stream;
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::wstring> parameters;
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// pointers to the output streams
|
|
|
|
std::vector<StreamType*> output_stream_tab;
|
|
|
|
|
|
|
|
// indices to output_stream_tab
|
|
|
|
// they are set by [put stream ...] keyword
|
|
|
|
// at the beginning there is only one index created automatically - "0"
|
|
|
|
std::vector<size_t> output_stream_index;
|
|
|
|
|
|
|
|
// if has_skip_stream_index is true then when reading stream indices from [put stream ...]
|
|
|
|
// a stream with skip_stream_index index will be ignored
|
|
|
|
bool has_skip_stream_index;
|
|
|
|
size_t skip_stream_index;
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
// temporary error messages
|
|
|
|
std::wstring temp_msg;
|
|
|
|
|
|
|
|
bool break_generating;
|
2012-02-24 13:04:36 +01:00
|
|
|
size_t current_item;
|
|
|
|
size_t max_items;
|
|
|
|
size_t max_for_items;
|
2010-11-22 02:23:32 +01:00
|
|
|
bool special_chars;
|
|
|
|
bool trim_white;
|
|
|
|
bool skip_new_line;
|
2012-02-27 18:39:46 +01:00
|
|
|
|
|
|
|
size_t stack_index;
|
|
|
|
size_t stack_size;
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
size_t filter_index;
|
|
|
|
size_t filter_size;
|
|
|
|
// we have to use a pointers table because standard streams such
|
|
|
|
// as std::wostringstream are not copyable
|
|
|
|
std::vector<StreamType*> filter_tab;
|
|
|
|
const StreamType empty_stream;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
// used in [0] [1] [2] when there is no such argument defined
|
|
|
|
std::wstring empty_argument;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// temporary streams used in [if..] [is...] or [for...]
|
2013-11-07 11:16:10 +01:00
|
|
|
// or if output_stream is null and an ezc function should be called
|
2014-10-17 23:36:55 +02:00
|
|
|
StreamType stream_temp1, stream_temp2, stream_temp_define;
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// last result from a user function (FunInfo::res)
|
|
|
|
bool last_res;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// true if this Generator is working now
|
|
|
|
bool is_generator_working;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
// true if the Generator is working with [for ...], [if ...] etc.
|
2012-01-17 23:57:06 +01:00
|
|
|
bool is_generating_for;
|
2012-02-24 13:04:36 +01:00
|
|
|
bool is_generating_if;
|
|
|
|
bool is_generating_is;
|
|
|
|
bool is_generating_normal;
|
|
|
|
bool is_generating_filter;
|
2012-01-17 23:57:06 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// an empty string for FunInfo objects
|
2010-11-22 02:23:32 +01:00
|
|
|
// when there is no any parameters
|
|
|
|
const std::wstring empty;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// a stack for [for] statements
|
2012-02-27 18:39:46 +01:00
|
|
|
std::vector<Stack> stack_tab;
|
2011-01-15 00:59:30 +01:00
|
|
|
|
2014-10-19 07:42:25 +02:00
|
|
|
std::wstring commentary_start, commentary_stop;
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
bool can_use_vars;
|
|
|
|
bool can_find_in_cache;
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
void ResizeFilterTab();
|
2012-02-27 18:39:46 +01:00
|
|
|
void ResizeStack();
|
2014-10-17 23:36:55 +02:00
|
|
|
void ResizeBlockStack();
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
void ClearFilterTab();
|
2014-10-17 23:36:55 +02:00
|
|
|
void ClearForStack();
|
2014-10-21 09:21:20 +02:00
|
|
|
void ClearBlockStack();
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
void ClearStream(StreamType & str);
|
2012-02-27 18:39:46 +01:00
|
|
|
void RemoveStackFunData(Stack & sitem);
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
template<class CharType>
|
|
|
|
CharType ToLower(CharType c);
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
bool AreStreamsEqual(StreamType & str1, StreamType & str2);
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
bool FindInCache(const Item::Function & item_fun,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable);
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
bool FindInFunctionsAndBlocks(const std::wstring & name,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block);
|
|
|
|
|
|
|
|
bool FindInVariables(const std::wstring & name,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable);
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Find(const Item::Function & item_fun,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable);
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
void CallFunction(typename Functions<StreamType>::UserFunction * function,
|
|
|
|
FunInfo<StreamType> & info);
|
|
|
|
|
|
|
|
void CallFunction(typename Functions<StreamType>::UserFunction * function,
|
|
|
|
std::vector<std::wstring> & params,
|
|
|
|
StreamType & out_stream,
|
|
|
|
const StreamType & in_stream);
|
|
|
|
|
|
|
|
bool CallBlock(Item & item_block,
|
|
|
|
std::vector<std::wstring> & params,
|
|
|
|
StreamType & out_stream);
|
|
|
|
|
|
|
|
bool Call(Item::Function & item_fun,
|
|
|
|
StreamType & out_stream,
|
|
|
|
bool clear_out_stream,
|
|
|
|
const StreamType & in_stream);
|
|
|
|
|
|
|
|
bool Call(Item::Function & item_fun);
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
wchar_t CreateSpecialChar(wchar_t c);
|
2010-11-25 02:55:32 +01:00
|
|
|
const wchar_t * PrintSpecialChar(const wchar_t * start, const wchar_t * end);
|
2010-11-22 02:23:32 +01:00
|
|
|
void PrintSpecialText(const wchar_t * start, const wchar_t * end);
|
|
|
|
void PrintNormalText(const wchar_t * start, const wchar_t * end);
|
2014-10-11 21:56:48 +02:00
|
|
|
bool IsWhite(wchar_t c);
|
2010-11-22 02:23:32 +01:00
|
|
|
void TrimWhite(const wchar_t *& start, const wchar_t *& end);
|
|
|
|
void SkipWhite(const wchar_t *& str);
|
2013-11-07 11:16:10 +01:00
|
|
|
size_t StrToSize(const wchar_t * str, const wchar_t ** str_end = 0);
|
2012-03-03 21:58:55 +01:00
|
|
|
bool LookForLastForStatement(size_t & last_for_index);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
void WriteTmpStreamToStreams();
|
2014-10-19 07:42:25 +02:00
|
|
|
void CreateMsg(std::wstring & out, const wchar_t * type, const wchar_t * arg = 0);
|
2010-11-25 23:39:58 +01:00
|
|
|
void CreateMsg(const wchar_t * type, const wchar_t * arg = 0);
|
|
|
|
void CreateMsg(const std::wstring & type, const std::wstring & arg);
|
|
|
|
void CreateMsg(const std::wstring & type);
|
|
|
|
void CreateUnknownMsg(const std::wstring & fun);
|
2012-02-27 18:39:46 +01:00
|
|
|
bool LimitAchieved();
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
void MakeTextIf_go(Item & item, bool result);
|
2012-03-03 21:58:55 +01:00
|
|
|
bool MakeTextIfindexnumber(Item & item, size_t for_index, bool & result);
|
2010-11-22 02:23:32 +01:00
|
|
|
void MakeTextIf(Item & item);
|
|
|
|
void MakeTextIfno(Item & item);
|
|
|
|
void MakeTextIfany(Item & item);
|
|
|
|
void MakeTextIfone(Item & item);
|
|
|
|
void MakeTextIfanyno(Item & item);
|
|
|
|
void MakeTextIfoneno(Item & item);
|
|
|
|
void MakeTextIfindex(Item & item);
|
|
|
|
void MakeTextFor(Item & item);
|
|
|
|
void MakeItemText(Item & item);
|
|
|
|
void MakeTextContainer(Item & item);
|
|
|
|
void MakeTextNormal(Item & item);
|
2011-04-26 19:17:06 +02:00
|
|
|
void MakeTextIs(Item & item, bool equal);
|
2010-11-22 02:23:32 +01:00
|
|
|
void MakeTextIs(Item & item);
|
|
|
|
void MakeTextIsno(Item & item);
|
|
|
|
void MakeTextDefine(Item & item);
|
2011-01-26 13:42:49 +01:00
|
|
|
void MakeTextFilter(Item & item);
|
2013-11-07 11:16:10 +01:00
|
|
|
void MakeTextEzc(Item & item);
|
2010-11-22 02:23:32 +01:00
|
|
|
void MakeText(Item & item);
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
void MakeEzcStream(Item::Function & fun);
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generate();
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
}; // class Generator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2012-07-01 01:24:09 +02:00
|
|
|
Generator<StreamType>::Generator() : empty_stream()
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
|
|
|
output_stream = 0;
|
2014-10-17 23:36:55 +02:00
|
|
|
ppattern = 0;
|
|
|
|
pblocks = 0;
|
|
|
|
pfunctions = 0;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
max_items = 50000;
|
|
|
|
max_for_items = 5000;
|
2011-01-26 13:42:49 +01:00
|
|
|
filter_size = 20;
|
2010-11-22 02:23:32 +01:00
|
|
|
special_chars = false;
|
|
|
|
trim_white = false;
|
|
|
|
skip_new_line = false;
|
2011-04-26 19:17:06 +02:00
|
|
|
is_generator_working = false;
|
2012-02-27 18:39:46 +01:00
|
|
|
stack_size = 300;
|
2014-10-17 23:36:55 +02:00
|
|
|
block_stack_size = 64;
|
2013-11-07 11:16:10 +01:00
|
|
|
has_skip_stream_index = false;
|
2014-10-19 23:09:34 +02:00
|
|
|
can_find_in_cache = true;
|
|
|
|
can_use_vars = true;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
|
|
|
Generator<StreamType>::Generator(const Generator<StreamType> & n)
|
|
|
|
{
|
|
|
|
operator=(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
Generator<StreamType> & Generator<StreamType>::operator=(const Generator<StreamType> & n)
|
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
// !! CHECK ME may output_streams should not be copied?
|
|
|
|
output_stream_tab = n.output_stream_tab;
|
|
|
|
output_stream_index = n.output_stream_index;
|
|
|
|
output_tmp_stream = n.output_tmp_stream;
|
|
|
|
output_stream = n.output_stream;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
ppattern = n.ppattern;
|
|
|
|
pblocks = n.pblocks;
|
|
|
|
pfunctions = n.pfunctions;
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
max_items = n.max_items;
|
|
|
|
max_for_items = n.max_for_items;
|
|
|
|
special_chars = n.special_chars;
|
|
|
|
trim_white = n.trim_white;
|
|
|
|
skip_new_line = n.skip_new_line;
|
2013-11-07 11:16:10 +01:00
|
|
|
has_skip_stream_index = n.has_skip_stream_index;
|
2014-10-19 23:09:34 +02:00
|
|
|
can_find_in_cache = n.can_find_in_cache;
|
|
|
|
can_use_vars = n.can_use_vars;
|
2012-02-27 18:39:46 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
// filter, stack and block_stack will be auto resized when calling Generate() method
|
|
|
|
filter_size = n.filter_size;
|
|
|
|
stack_size = n.stack_size;
|
|
|
|
block_stack_size = n.block_stack_size;
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
// vars doesn't have to be copied
|
2012-02-27 18:39:46 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
// don't copy filter tab
|
2012-02-27 18:39:46 +01:00
|
|
|
// don't copy stack
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2012-02-25 04:29:44 +01:00
|
|
|
// !! CHECK ME
|
|
|
|
// may copying should be denied when generator is working?
|
2011-04-26 19:17:06 +02:00
|
|
|
// don't copy 'is_generator_working' flag
|
|
|
|
is_generator_working = false;
|
2011-01-26 13:42:49 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
Generator<StreamType>::~Generator()
|
|
|
|
{
|
|
|
|
ClearFilterTab();
|
2012-02-24 13:04:36 +01:00
|
|
|
ClearForStack();
|
2014-10-21 09:21:20 +02:00
|
|
|
ClearBlockStack();
|
2011-01-26 13:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-19 07:42:25 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetCommentary(const char * com_start, const char * com_stop)
|
|
|
|
{
|
|
|
|
PT::UTF8ToWide(com_start, commentary_start);
|
|
|
|
PT::UTF8ToWide(com_stop, commentary_stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetCommentary(const std::string & com_start, const std::string & com_stop)
|
|
|
|
{
|
|
|
|
PT::UTF8ToWide(com_start, commentary_start);
|
|
|
|
PT::UTF8ToWide(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetPattern(Pattern & pattern)
|
|
|
|
{
|
|
|
|
ppattern = &pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetBlocks(Blocks & blocks)
|
|
|
|
{
|
|
|
|
pblocks = &blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetFunctions(Functions<StreamType> & functions)
|
|
|
|
{
|
|
|
|
pfunctions = &functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::CanUseCache(bool can_use_cache)
|
|
|
|
{
|
|
|
|
can_find_in_cache = can_use_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::CanUseVars(bool can_use_variables)
|
|
|
|
{
|
|
|
|
can_use_vars = can_use_variables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ResizeFilterTab()
|
|
|
|
{
|
|
|
|
if( filter_tab.size() != filter_size )
|
|
|
|
{
|
|
|
|
if( filter_tab.size() < filter_size )
|
|
|
|
{
|
|
|
|
for(size_t i=filter_tab.size() ; i<filter_size ; ++i)
|
|
|
|
filter_tab.push_back( new StreamType() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(size_t i=filter_size ; i<filter_tab.size() ; ++i)
|
|
|
|
delete filter_tab[i];
|
|
|
|
|
|
|
|
filter_tab.resize(filter_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ResizeStack()
|
|
|
|
{
|
|
|
|
if( stack_tab.size() != stack_size )
|
|
|
|
{
|
|
|
|
if( stack_tab.size() > stack_size )
|
|
|
|
{
|
|
|
|
for(size_t i=stack_size ; i<stack_tab.size() ; ++i)
|
|
|
|
RemoveStackFunData(stack_tab[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
stack_tab.resize(stack_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ResizeBlockStack()
|
|
|
|
{
|
|
|
|
if( block_stack_tab.size() != block_stack_size )
|
|
|
|
{
|
|
|
|
if( block_stack_tab.size() < block_stack_size )
|
|
|
|
{
|
|
|
|
size_t i = block_stack_tab.size();
|
|
|
|
block_stack_tab.resize(block_stack_size);
|
|
|
|
|
|
|
|
for( ; i<block_stack_tab.size() ; ++i)
|
|
|
|
block_stack_tab[i].out_stream = new StreamType();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(size_t i=block_stack_size ; i<block_stack_tab.size() ; ++i)
|
|
|
|
delete block_stack_tab[i].out_stream;
|
|
|
|
|
|
|
|
block_stack_tab.resize(block_stack_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ClearFilterTab()
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<filter_tab.size() ; ++i)
|
|
|
|
delete filter_tab[i];
|
|
|
|
|
|
|
|
filter_tab.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-21 09:21:20 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ClearBlockStack()
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<block_stack_tab.size() ; ++i)
|
|
|
|
delete block_stack_tab[i].out_stream;
|
|
|
|
|
|
|
|
block_stack_tab.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::ClearForStack()
|
2012-02-24 13:04:36 +01:00
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<stack_tab.size() ; ++i)
|
2012-02-25 04:29:44 +01:00
|
|
|
RemoveStackFunData(stack_tab[i]);
|
2012-02-24 13:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::ClearStream(StreamType & str)
|
|
|
|
{
|
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
str.Clear();
|
|
|
|
#else
|
|
|
|
str.str(L"");
|
|
|
|
#endif
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
|
2012-02-25 04:29:44 +01:00
|
|
|
template<class StreamType>
|
2012-02-27 18:39:46 +01:00
|
|
|
void Generator<StreamType>::RemoveStackFunData(Stack & s)
|
2012-02-25 04:29:44 +01:00
|
|
|
{
|
2014-10-19 07:42:25 +02:00
|
|
|
if( s.fun_data && s.auto_remove )
|
2012-02-25 04:29:44 +01:00
|
|
|
{
|
2012-02-27 18:39:46 +01:00
|
|
|
delete s.fun_data;
|
|
|
|
s.fun_data = 0;
|
2012-02-25 04:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SkipStream(int stream_index)
|
|
|
|
{
|
|
|
|
if( stream_index < 0 )
|
|
|
|
{
|
|
|
|
has_skip_stream_index = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
has_skip_stream_index = true;
|
|
|
|
skip_stream_index = stream_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-25 04:29:44 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
template<class StreamType>
|
|
|
|
template<class CharType>
|
|
|
|
CharType Generator<StreamType>::ToLower(CharType c)
|
|
|
|
{
|
|
|
|
if( c>='A' && c<='Z' )
|
|
|
|
return c - 'A' + 'a';
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Generator<StreamType>::AreStreamsEqual(StreamType & str1, StreamType & str2)
|
2011-04-26 19:17:06 +02:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
return str1.Str() == str2.Str();
|
|
|
|
#else
|
|
|
|
return str1.str() == str2.str();
|
|
|
|
#endif
|
2011-04-26 19:17:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2012-02-24 13:04:36 +01:00
|
|
|
void Generator<StreamType>::SetMax(size_t max_items_, size_t max_for_items_)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
|
|
|
max_items = max_items_;
|
|
|
|
max_for_items = max_for_items_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::RecognizeSpecialChars(bool spec)
|
|
|
|
{
|
|
|
|
special_chars = spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::TrimWhite(bool trim)
|
|
|
|
{
|
|
|
|
trim_white = trim;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SkipNewLine(bool skip)
|
|
|
|
{
|
|
|
|
skip_new_line = skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2011-01-26 13:42:49 +01:00
|
|
|
void Generator<StreamType>::SetMaxFilters(size_t new_len)
|
|
|
|
{
|
|
|
|
// the table will be resized when Generate() method is called
|
|
|
|
filter_size = new_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SetStackSize(size_t new_stack_size)
|
|
|
|
{
|
|
|
|
// the stack will be resized when Generate() method is called
|
|
|
|
stack_size = new_stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::Generate()
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( is_generator_working )
|
2011-01-26 13:42:49 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
CreateMsg(L"generator busy");
|
2011-01-26 13:42:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
output_stream = 0;
|
|
|
|
output_stream_index.clear();
|
2013-11-17 22:53:53 +01:00
|
|
|
ClearStream(output_tmp_stream);
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
if( !output_stream_tab.empty() )
|
|
|
|
{
|
|
|
|
if( !has_skip_stream_index || skip_stream_index != 0 )
|
|
|
|
{
|
|
|
|
output_stream_index.push_back(0);
|
|
|
|
output_stream = output_stream_tab[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
break_generating = false;
|
|
|
|
current_item = 0;
|
2012-02-27 18:39:46 +01:00
|
|
|
|
|
|
|
// in the case that there something has left on the stack
|
|
|
|
// from previous call to Generate()
|
|
|
|
// (an exception could have been thrown)
|
2014-10-17 23:36:55 +02:00
|
|
|
ClearForStack();
|
2012-02-27 18:39:46 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
ResizeFilterTab();
|
2012-02-27 18:39:46 +01:00
|
|
|
ResizeStack();
|
2014-10-17 23:36:55 +02:00
|
|
|
ResizeBlockStack();
|
2011-01-26 13:42:49 +01:00
|
|
|
filter_index = 0;
|
2012-02-27 18:39:46 +01:00
|
|
|
stack_index = 0;
|
2014-10-17 23:36:55 +02:00
|
|
|
block_stack_index = 0;
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Clear();
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( ppattern )
|
2011-01-26 13:42:49 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
is_generator_working = true;
|
2014-10-17 23:36:55 +02:00
|
|
|
MakeText( ppattern->item_root );
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
if( output_stream_index.size() > 1 )
|
|
|
|
WriteTmpStreamToStreams();
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Clear();
|
2011-04-26 19:17:06 +02:00
|
|
|
is_generator_working = false;
|
2011-01-26 13:42:49 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
is_generator_working = false;
|
2011-01-26 13:42:49 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2010-11-25 23:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::Generate(StreamType & o)
|
2010-11-25 23:39:58 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
output_stream_tab.resize(1);
|
|
|
|
output_stream_tab[0] = &o;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
Generate();
|
2011-01-26 13:42:49 +01:00
|
|
|
}
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::Generate(std::vector<StreamType> & o)
|
2013-11-07 11:16:10 +01:00
|
|
|
{
|
|
|
|
output_stream_tab.resize(o.size());
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<o.size() ; ++i)
|
|
|
|
output_stream_tab[i] = &o[i];
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
Generate();
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::Generate(std::vector<StreamType*> & o)
|
2013-11-07 11:16:10 +01:00
|
|
|
{
|
|
|
|
output_stream_tab.resize(o.size());
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<o.size() ; ++i)
|
|
|
|
output_stream_tab[i] = o[i];
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
Generate();
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Generator<StreamType>::FindInCache(const Item::Function & item_fun,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable)
|
2011-01-26 13:42:49 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
if( item_fun.arg >=0 )
|
|
|
|
{
|
|
|
|
empty_argument.clear();
|
|
|
|
*variable = &empty_argument;
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
// it's a numeric function -- an argument to a block e.g.: [1]
|
|
|
|
if( block_stack_index > 0 )
|
|
|
|
{
|
|
|
|
BlockStack & block_stack = block_stack_tab[block_stack_index-1];
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( size_t(item_fun.arg) < block_stack.args.size() )
|
|
|
|
*variable = &block_stack.args[item_fun.arg];
|
|
|
|
}
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
if( can_find_in_cache )
|
2014-10-17 23:36:55 +02:00
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
if( item_fun.fun_cache )
|
|
|
|
{
|
|
|
|
*function = reinterpret_cast<typename Functions<StreamType>::UserFunction*>(item_fun.fun_cache);
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
if( item_fun.item_block )
|
|
|
|
{
|
|
|
|
*item_block = item_fun.item_block;
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-17 23:36:55 +02:00
|
|
|
}
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
return false;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Generator<StreamType>::FindInFunctionsAndBlocks(const std::wstring & name,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block)
|
2013-11-07 11:16:10 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
if( pfunctions )
|
|
|
|
{
|
2014-10-18 00:14:28 +02:00
|
|
|
typename Functions<StreamType>::Iterator i = pfunctions->Find(name);
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( i != pfunctions->End() )
|
|
|
|
{
|
|
|
|
*function = &i->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( pblocks )
|
|
|
|
{
|
|
|
|
Blocks::Iterator i = pblocks->Find(name);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( i != pblocks->End() )
|
|
|
|
{
|
|
|
|
*item_block = &i->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Generator<StreamType>::FindInVariables(const std::wstring & name,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
Vars::Iterator i = vars.Find(name);
|
|
|
|
|
|
|
|
if( i != vars.End() )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
Vars::Variable & var = i->second;
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
if( var.type == Vars::variable_string )
|
2010-11-25 23:39:58 +01:00
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
*variable = &var.val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is an alias to a function or to a block
|
|
|
|
return FindInFunctionsAndBlocks(var.val, function, item_block);
|
2010-11-25 23:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
bool Generator<StreamType>::Find(const Item::Function & item_fun,
|
|
|
|
typename Functions<StreamType>::UserFunction ** function,
|
|
|
|
Item ** item_block,
|
|
|
|
std::wstring ** variable)
|
|
|
|
{
|
|
|
|
*function = 0;
|
|
|
|
*item_block = 0;
|
|
|
|
*variable = 0;
|
|
|
|
|
|
|
|
if( FindInCache(item_fun, function, item_block, variable) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( FindInFunctionsAndBlocks(item_fun.name, function, item_block) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( FindInVariables(item_fun.name, function, item_block, variable) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
CreateUnknownMsg(item_fun.name);
|
|
|
|
|
|
|
|
return false;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction * function, FunInfo<StreamType> & info)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
|
|
|
info.Clear();
|
2011-01-15 00:59:30 +01:00
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
info.is_for = is_generating_for;
|
|
|
|
info.is_if = is_generating_if;
|
|
|
|
info.is_is = is_generating_is;
|
|
|
|
info.is_normal = is_generating_normal;
|
|
|
|
info.is_filter = is_generating_filter;
|
2014-10-17 23:36:55 +02:00
|
|
|
info.iter = info.stack.iter;
|
2012-02-24 13:04:36 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
(*function)(info);
|
2011-04-26 19:17:06 +02:00
|
|
|
|
|
|
|
last_res = info.res;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::CallFunction(typename Functions<StreamType>::UserFunction * function,
|
|
|
|
std::vector<std::wstring> & params,
|
|
|
|
StreamType & out_stream,
|
|
|
|
const StreamType & in_stream)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2011-01-26 13:42:49 +01:00
|
|
|
if( params.empty() )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
FunInfo<StreamType> info(out_stream, params, empty, in_stream, stack_tab[stack_index-1]);
|
|
|
|
CallFunction(function, info);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
FunInfo<StreamType> info(out_stream, params, params[0], in_stream, stack_tab[stack_index-1]);
|
|
|
|
CallFunction(function, info);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
2011-01-26 13:42:49 +01:00
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
template<class StreamType>
|
|
|
|
bool Generator<StreamType>::CallBlock(Item & item_block,
|
|
|
|
std::vector<std::wstring> & params,
|
|
|
|
StreamType & out_stream)
|
|
|
|
{
|
|
|
|
if( block_stack_index >= block_stack_tab.size() )
|
|
|
|
{
|
|
|
|
CreateMsg(L"Generator exceeded allowed number of blocks");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamType * old_stream = output_stream;
|
|
|
|
BlockStack & block_stack = block_stack_tab[block_stack_index];
|
|
|
|
|
|
|
|
block_stack.args.resize(params.size());
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<params.size() ; ++i)
|
|
|
|
block_stack.args[i] = params[i]; // or may std::move in the future? (params will not be used anymore)
|
|
|
|
|
|
|
|
output_stream = block_stack.out_stream;
|
|
|
|
ClearStream(*output_stream);
|
|
|
|
block_stack_index += 1;
|
|
|
|
|
|
|
|
MakeText(item_block);
|
|
|
|
|
2014-10-18 00:14:28 +02:00
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
const std::wstring & str = output_stream->Str();
|
|
|
|
#else
|
|
|
|
const std::wstring & str = output_stream->str();
|
|
|
|
#endif
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
out_stream.write(str.c_str(), str.size());
|
2014-10-18 00:14:28 +02:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
last_res = !str.empty();
|
|
|
|
|
|
|
|
ClearStream(*output_stream);
|
|
|
|
output_stream = old_stream;
|
|
|
|
block_stack_index -= 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
// return: true if a function or variable was found and called
|
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
bool Generator<StreamType>::Call(Item::Function & item_fun,
|
|
|
|
StreamType & out_stream,
|
|
|
|
bool clear_out_stream,
|
|
|
|
const StreamType & in_stream)
|
2011-01-26 13:42:49 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
typename Functions<StreamType>::UserFunction * fun;
|
|
|
|
Item * item_block;
|
|
|
|
std::wstring * variable;
|
|
|
|
|
|
|
|
if( clear_out_stream )
|
|
|
|
ClearStream(out_stream);
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( !Find(item_fun, &fun, &item_block, &variable) )
|
2011-01-26 13:42:49 +01:00
|
|
|
return false;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
for(size_t i=0 ; i<item_fun.parameters.size() ; ++i)
|
|
|
|
{
|
|
|
|
Item::Function & fun_child = *item_fun.parameters[i];
|
|
|
|
|
|
|
|
if( !fun_child.name.empty() )
|
|
|
|
{
|
|
|
|
Call(fun_child, local_temp_stream, true, empty_stream);
|
|
|
|
|
|
|
|
if( fun || item_block )
|
2014-10-18 00:14:28 +02:00
|
|
|
{
|
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
fun_child.par = local_temp_stream.Str();
|
|
|
|
#else
|
|
|
|
fun_child.par = local_temp_stream.str();
|
|
|
|
#endif
|
|
|
|
}
|
2014-10-17 23:36:55 +02:00
|
|
|
|
|
|
|
// the stream has to be cleared when we finished
|
|
|
|
ClearStream(local_temp_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fun || item_block )
|
|
|
|
{
|
|
|
|
parameters.resize(item_fun.parameters.size());
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<item_fun.parameters.size() ; ++i)
|
|
|
|
{
|
|
|
|
// in c++11 we can use std::move here
|
|
|
|
parameters[i] = item_fun.parameters[i]->par;
|
2014-10-19 23:09:34 +02:00
|
|
|
|
|
|
|
if( !item_fun.parameters[i]->name.empty() )
|
|
|
|
item_fun.parameters[i]->par.clear();
|
2014-10-17 23:36:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fun )
|
|
|
|
{
|
|
|
|
CallFunction(fun, parameters, out_stream, in_stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( item_block )
|
|
|
|
{
|
|
|
|
return CallBlock(*item_block, parameters, out_stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( variable )
|
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
out_stream.write(variable->c_str(), variable->size());
|
2014-10-17 23:36:55 +02:00
|
|
|
last_res = !variable->empty();
|
|
|
|
}
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
return true;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
// return: true if a function or variable was found and called
|
2011-01-15 00:59:30 +01:00
|
|
|
template<class StreamType>
|
2011-04-26 19:17:06 +02:00
|
|
|
bool Generator<StreamType>::Call(Item::Function & item_fun)
|
2011-01-15 00:59:30 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
return Call(item_fun, stream_temp1, true, empty_stream);
|
2011-01-15 00:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
|
|
|
wchar_t Generator<StreamType>::CreateSpecialChar(wchar_t c)
|
|
|
|
{
|
|
|
|
wchar_t res = 0;
|
|
|
|
|
|
|
|
if( c == 'r' )
|
|
|
|
res = '\r';
|
|
|
|
else
|
|
|
|
if( c == 'n' )
|
|
|
|
res = '\n';
|
|
|
|
else
|
|
|
|
if( c == 't' )
|
|
|
|
res = '\t';
|
|
|
|
else
|
|
|
|
if( c == 's' )
|
|
|
|
res = ' ';
|
|
|
|
else
|
|
|
|
if( c == '\\' )
|
|
|
|
res = '\\';
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-25 02:55:32 +01:00
|
|
|
|
|
|
|
// a special character is precedeed by a slash '\'
|
|
|
|
// if the special character is unknown the first slash is printing
|
|
|
|
// so "\t" gives one character of code 9
|
|
|
|
// and "\x" gives "\x"
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2010-11-25 02:55:32 +01:00
|
|
|
const wchar_t * Generator<StreamType>::PrintSpecialChar(const wchar_t * start, const wchar_t * end)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2010-11-25 02:55:32 +01:00
|
|
|
wchar_t special = 0;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2010-11-25 02:55:32 +01:00
|
|
|
if( *start == '\\' && (start+1) != end )
|
|
|
|
special = CreateSpecialChar(*(start+1));
|
|
|
|
|
|
|
|
if( special )
|
|
|
|
{
|
|
|
|
output_stream->write(&special, 1);
|
|
|
|
start += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !skip_new_line || *start != 10 )
|
|
|
|
output_stream->write(start, 1);
|
|
|
|
|
|
|
|
start += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::PrintSpecialText(const wchar_t * start, const wchar_t * end)
|
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
if( output_stream )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
while( start != end )
|
|
|
|
{
|
|
|
|
const wchar_t * end2 = start;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// looking for a first new line character or a special char
|
|
|
|
// (for new line only if skip_new_line is true)
|
|
|
|
while( end2 != end && *end2 != '\\' && (!skip_new_line || *end2 != 10) )
|
|
|
|
end2 += 1;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// printing the first part of the text
|
|
|
|
if( start != end2 )
|
|
|
|
output_stream->write(start, end2 - start);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
start = end2;
|
|
|
|
|
|
|
|
// skipping one or more new line characters or special chars
|
|
|
|
// (new lines only if skip_new_line is true)
|
|
|
|
while( start != end && (*start == '\\' || (skip_new_line && *start == 10)) )
|
|
|
|
start = PrintSpecialChar(start, end);
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::PrintNormalText(const wchar_t * start, const wchar_t * end)
|
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
if( output_stream )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
if( skip_new_line )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
while( start != end )
|
|
|
|
{
|
|
|
|
const wchar_t * end2 = start;
|
2010-11-25 02:55:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// looking for a first new line character
|
|
|
|
while( end2 != end && *end2 != 10 )
|
|
|
|
end2 += 1;
|
2010-11-25 02:55:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
// printing the first part of the text (until the new line)
|
|
|
|
if( start != end2 )
|
|
|
|
output_stream->write(start, end2 - start);
|
2010-11-25 02:55:32 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
start = end2;
|
|
|
|
|
|
|
|
// skipping one or more new line characters
|
|
|
|
while( start != end && *start == 10 )
|
|
|
|
start += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( start != end )
|
|
|
|
output_stream->write(start, end - start);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-11 21:56:48 +02:00
|
|
|
template<class StreamType>
|
|
|
|
bool Generator<StreamType>::IsWhite(wchar_t c)
|
|
|
|
{
|
|
|
|
// 13 (\r) is from a dos file at the end of a line (\r\n)
|
|
|
|
// 160 is a non-breaking space
|
|
|
|
|
|
|
|
if( c==' ' || c=='\t' || c==13 || c==160 || c==10 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::TrimWhite(const wchar_t *& start, const wchar_t *& end)
|
|
|
|
{
|
2014-10-11 21:56:48 +02:00
|
|
|
while( start != end && IsWhite(*start) )
|
2010-11-22 02:23:32 +01:00
|
|
|
++start;
|
|
|
|
|
2014-10-11 21:56:48 +02:00
|
|
|
while( start != end && IsWhite(*(end-1)) )
|
2010-11-22 02:23:32 +01:00
|
|
|
--end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::SkipWhite(const wchar_t *& str)
|
|
|
|
{
|
2014-10-11 21:56:48 +02:00
|
|
|
while( IsWhite(*str) )
|
2010-11-22 02:23:32 +01:00
|
|
|
str += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2012-02-24 13:04:36 +01:00
|
|
|
size_t Generator<StreamType>::StrToSize(const wchar_t * str, const wchar_t ** str_end)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2012-02-24 13:04:36 +01:00
|
|
|
size_t res = 0;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
SkipWhite(str);
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
// !! IMPROVE ME
|
2010-11-22 02:23:32 +01:00
|
|
|
// overflow is not checked
|
|
|
|
|
|
|
|
while( *str>='0' && *str<='9' )
|
|
|
|
{
|
|
|
|
res *= 10;
|
|
|
|
res += *str - '0';
|
|
|
|
str += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkipWhite(str);
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
if( str_end )
|
|
|
|
*str_end = str;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
2013-11-07 11:16:10 +01:00
|
|
|
void Generator<StreamType>::WriteTmpStreamToStreams()
|
2011-01-26 13:42:49 +01:00
|
|
|
{
|
2014-10-18 00:14:28 +02:00
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
const std::wstring & str = output_tmp_stream.Str();
|
|
|
|
#else
|
|
|
|
const std::wstring & str = output_tmp_stream.str();
|
|
|
|
#endif
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
if( !str.empty() )
|
|
|
|
{
|
|
|
|
for(size_t s = 0 ; s < output_stream_index.size() ; ++s)
|
|
|
|
{
|
|
|
|
size_t i = output_stream_index[s];
|
|
|
|
|
|
|
|
if( i < output_stream_tab.size() )
|
|
|
|
output_stream_tab[i]->write(str.c_str(), str.size());
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:53:53 +01:00
|
|
|
ClearStream(output_tmp_stream);
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
2011-01-26 13:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-19 07:42:25 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
template<class StreamType>
|
2010-11-25 23:39:58 +01:00
|
|
|
void Generator<StreamType>::CreateMsg(const wchar_t * type, const wchar_t * arg)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2013-11-07 11:16:10 +01:00
|
|
|
if( output_stream )
|
|
|
|
{
|
2014-10-19 07:42:25 +02:00
|
|
|
CreateMsg(temp_msg, type, arg);
|
2013-11-07 11:16:10 +01:00
|
|
|
output_stream->write(temp_msg.c_str(), temp_msg.size());
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2010-11-25 23:39:58 +01:00
|
|
|
void Generator<StreamType>::CreateMsg(const std::wstring & type, const std::wstring & arg)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(type.c_str(), arg.c_str());
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2010-11-25 23:39:58 +01:00
|
|
|
void Generator<StreamType>::CreateMsg(const std::wstring & type)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(type.c_str());
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-25 23:39:58 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::CreateUnknownMsg(const std::wstring & fun)
|
|
|
|
{
|
|
|
|
CreateMsg(L"unknown function", fun.c_str());
|
|
|
|
}
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeItemText(Item & item)
|
|
|
|
{
|
|
|
|
const wchar_t * start = item.text.c_str();
|
|
|
|
const wchar_t * end = item.text.c_str() + item.text.size();
|
|
|
|
|
|
|
|
if( trim_white )
|
|
|
|
TrimWhite(start, end);
|
|
|
|
|
|
|
|
if( special_chars )
|
|
|
|
PrintSpecialText(start, end);
|
|
|
|
else
|
|
|
|
PrintNormalText(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextContainer(Item & item)
|
|
|
|
{
|
|
|
|
std::vector<Item*>::iterator i = item.item_tab.begin();
|
|
|
|
|
|
|
|
for( ; i != item.item_tab.end() && !break_generating ; ++i )
|
2011-07-15 04:09:40 +02:00
|
|
|
MakeText(**i);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextNormal(Item & item)
|
|
|
|
{
|
|
|
|
if( item.functions.size() != 1 )
|
|
|
|
return;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_normal = true;
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
if( output_stream )
|
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
Call(item.functions[0], *output_stream, false, empty_stream);
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
Call(item.functions[0], stream_temp1, false, empty_stream);
|
2013-11-07 11:16:10 +01:00
|
|
|
ClearStream(stream_temp1);
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIf_go(Item & item, bool result)
|
|
|
|
{
|
|
|
|
if( result )
|
|
|
|
{
|
|
|
|
if( item.item_tab.size() > 0 )
|
|
|
|
MakeText( *item.item_tab[0] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// second element can be (or not -- it's from [else])
|
|
|
|
if( item.item_tab.size() > 1 )
|
|
|
|
MakeText( *item.item_tab[1] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIf(Item & item)
|
|
|
|
{
|
|
|
|
if( item.functions.size() != 1 )
|
|
|
|
return;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
if( !Call(item.functions[0]) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
MakeTextIf_go(item, last_res);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfno(Item & item)
|
|
|
|
{
|
|
|
|
if( item.functions.size() != 1 )
|
|
|
|
return;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
if( !Call(item.functions[0]) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
MakeTextIf_go(item, !last_res);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfany(Item & item)
|
|
|
|
{
|
|
|
|
std::vector<typename Item::Function>::iterator d = item.functions.begin();
|
|
|
|
unsigned how_many_true = 0;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
for( ; d != item.functions.end() ; ++d )
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( !Call(*d) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
if( last_res )
|
2010-11-22 02:23:32 +01:00
|
|
|
++how_many_true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeTextIf_go(item, how_many_true == item.functions.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfone(Item & item)
|
|
|
|
{
|
|
|
|
std::vector<typename Item::Function>::iterator d = item.functions.begin();
|
|
|
|
unsigned how_many_true = 0;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
for( ; d != item.functions.end() ; ++d )
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( Call(*d) )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( last_res )
|
|
|
|
{
|
|
|
|
++how_many_true;
|
|
|
|
break; // there is no sense to go through all functions
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeTextIf_go(item, how_many_true > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfanyno(Item & item)
|
|
|
|
{
|
|
|
|
std::vector<typename Item::Function>::iterator d = item.functions.begin();
|
|
|
|
unsigned how_many_true = 0;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
for( ; d != item.functions.end() ; ++d )
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( !Call(*d) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
if( last_res )
|
2010-11-22 02:23:32 +01:00
|
|
|
++how_many_true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeTextIf_go(item, how_many_true == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfoneno(Item & item)
|
|
|
|
{
|
|
|
|
std::vector<typename Item::Function>::iterator d = item.functions.begin();
|
|
|
|
unsigned how_many_false = 0;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_if = true;
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
for( ; d != item.functions.end() ; ++d )
|
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
if( Call(*d) )
|
|
|
|
{
|
|
|
|
if( !last_res )
|
|
|
|
{
|
|
|
|
++how_many_false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MakeTextIf_go(item, how_many_false > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2011-04-26 19:17:06 +02:00
|
|
|
void Generator<StreamType>::MakeTextIs(Item & item, bool equal)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
|
|
|
if( item.functions.size() != 2 )
|
|
|
|
return;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_is = true;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( !Call(item.functions[0], stream_temp1, true, empty_stream) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( !Call(item.functions[1], stream_temp2, true, empty_stream) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
bool res = AreStreamsEqual(stream_temp1, stream_temp2);
|
2011-04-26 19:17:06 +02:00
|
|
|
|
|
|
|
if( !equal )
|
|
|
|
res = !res;
|
|
|
|
|
|
|
|
MakeTextIf_go(item, res);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2011-04-26 19:17:06 +02:00
|
|
|
void Generator<StreamType>::MakeTextIs(Item & item)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2011-04-26 19:17:06 +02:00
|
|
|
MakeTextIs(item, true);
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIsno(Item & item)
|
|
|
|
{
|
|
|
|
MakeTextIs(item, false);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-03 21:58:55 +01:00
|
|
|
template<class StreamType>
|
|
|
|
bool Generator<StreamType>::LookForLastForStatement(size_t & last_for_index)
|
|
|
|
{
|
|
|
|
last_for_index = stack_index;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2012-03-03 21:58:55 +01:00
|
|
|
while( last_for_index-- > 0 )
|
|
|
|
{
|
|
|
|
if( stack_tab[last_for_index].is_for )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2012-03-03 21:58:55 +01:00
|
|
|
bool Generator<StreamType>::MakeTextIfindexnumber(Item & item, size_t for_index, bool & result)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2012-02-27 18:39:46 +01:00
|
|
|
if( item.functions.size() != 1 )
|
2010-11-22 02:23:32 +01:00
|
|
|
return false;
|
|
|
|
|
2011-01-15 00:59:30 +01:00
|
|
|
const wchar_t * number_text = item.functions[0].name.c_str();
|
2010-11-22 02:23:32 +01:00
|
|
|
const wchar_t * last_char;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
size_t number = StrToSize(number_text, &last_char);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
if( *last_char != '\0' )
|
|
|
|
{
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(L"if-index: syntax error");
|
2010-11-22 02:23:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-03 21:58:55 +01:00
|
|
|
result = (stack_tab[for_index].iter == number);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextIfindex(Item & item)
|
|
|
|
{
|
2012-03-03 21:58:55 +01:00
|
|
|
size_t for_index;
|
|
|
|
|
|
|
|
if( item.functions.size() != 1 || !LookForLastForStatement(for_index) )
|
2010-11-22 02:23:32 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
if( item.functions[0].name == L"odd" )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2012-03-03 21:58:55 +01:00
|
|
|
result = (stack_tab[for_index].iter & 1) == 1;
|
2012-02-27 18:39:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( item.functions[0].name == L"even" )
|
|
|
|
{
|
2012-03-03 21:58:55 +01:00
|
|
|
result = (stack_tab[for_index].iter & 1) == 0;
|
2012-02-27 18:39:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( item.functions[0].name == L"first" )
|
|
|
|
{
|
2012-03-03 21:58:55 +01:00
|
|
|
result = stack_tab[for_index].iter == 0;
|
2012-02-27 18:39:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-03 21:58:55 +01:00
|
|
|
if( !MakeTextIfindexnumber(item, for_index, result) )
|
2012-02-27 18:39:46 +01:00
|
|
|
return;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MakeTextIf_go(item, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
2014-10-17 23:36:55 +02:00
|
|
|
void Generator<StreamType>::MakeTextFor(Item & item)
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
if( item.functions.size() != 1 )
|
|
|
|
return;
|
|
|
|
|
2012-03-03 21:58:55 +01:00
|
|
|
stack_tab[stack_index-1].is_for = true;
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
for( ; !break_generating ; stack_tab[stack_index-1].iter += 1 )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2012-02-27 18:39:46 +01:00
|
|
|
if( stack_tab[stack_index-1].iter >= max_for_items )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(item.functions[0].name.c_str(),
|
2010-11-22 02:23:32 +01:00
|
|
|
L"function exceeded a limit for a [for] statement");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-17 23:57:06 +01:00
|
|
|
// is_generating_for will be changed by next call to MakeText()
|
|
|
|
// so we should set it in each iterations
|
|
|
|
is_generating_for = true;
|
2014-10-17 23:36:55 +02:00
|
|
|
//Call(function, item.functions[0].params, stream_temp1, true, empty_stream);
|
|
|
|
Call(item.functions[0], stream_temp1, true, empty_stream);
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2011-04-26 19:17:06 +02:00
|
|
|
if( !last_res )
|
2010-11-22 02:23:32 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
if( !item.item_tab.empty() )
|
|
|
|
MakeText( *item.item_tab[0] ); // should be only one item - item_container
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextDefine(Item & item)
|
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
if( !can_use_vars )
|
2010-11-25 23:39:58 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
CreateMsg(L"[def] statement not available");
|
2010-11-25 23:39:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( item.functions.size() == 1 &&
|
|
|
|
item.functions[0].parameters.size() == 1 )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
if( item.functions[0].parameters[0]->name.empty() )
|
|
|
|
{
|
|
|
|
// it is [def name "string"]
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Insert(item.functions[0].name, Vars::variable_string, item.functions[0].parameters[0]->par);
|
2014-10-17 23:36:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// it is [def name [function]]
|
|
|
|
if( Call(*item.functions[0].parameters[0], stream_temp_define, true, empty_stream) )
|
|
|
|
{
|
2014-10-18 00:14:28 +02:00
|
|
|
#ifdef EZC_HAS_SPECIAL_STREAM
|
|
|
|
const std::wstring & str = stream_temp_define.Str();
|
|
|
|
#else
|
|
|
|
const std::wstring & str = stream_temp_define.str();
|
|
|
|
#endif
|
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Insert(item.functions[0].name, Vars::variable_string, str);
|
2014-10-17 23:36:55 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
else
|
2014-10-17 23:36:55 +02:00
|
|
|
if( item.functions.size() == 2 &&
|
|
|
|
item.functions[0].parameters.empty() &&
|
|
|
|
item.functions[1].parameters.empty() )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
typename Functions<StreamType>::UserFunction * function;
|
|
|
|
Item * item_block;
|
|
|
|
std::wstring * variable;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( Find(item.functions[1], &function, &item_block, &variable) )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
if( variable )
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
Vars::Iterator i = vars.Find(*variable);
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2014-10-19 23:09:34 +02:00
|
|
|
if( i != vars.End() )
|
2014-10-17 23:36:55 +02:00
|
|
|
{
|
|
|
|
Vars::Variable & var = i->second;
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Insert(*variable, var.type, var.val);
|
2014-10-17 23:36:55 +02:00
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-19 23:09:34 +02:00
|
|
|
vars.Insert(item.functions[0].name, Vars::variable_alias, item.functions[1].name);
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-17 23:36:55 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CreateMsg(L"[def] unknown function/block/variable", item.functions[1].name.c_str());
|
|
|
|
}
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextFilter(Item & item)
|
|
|
|
{
|
|
|
|
if( item.functions.size() != 1 )
|
|
|
|
return;
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
typename Functions<StreamType>::UserFunction * function;
|
|
|
|
Item * item_block;
|
|
|
|
std::wstring * variable;
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
if( !Find(item.functions[0], &function, &item_block, &variable) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// IMPROVE ME
|
|
|
|
// what about blocks and variables?
|
|
|
|
// may when there is a block we can get the whole content from filter
|
|
|
|
// and put it as the first argument to the block?
|
|
|
|
if( !function )
|
2011-01-26 13:42:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( filter_index >= filter_tab.size() )
|
|
|
|
{
|
|
|
|
CreateMsg(L"Generator exceeded allowed number of filters");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
|
2011-01-26 13:42:49 +01:00
|
|
|
StreamType * old_stream = output_stream;
|
|
|
|
output_stream = filter_tab[filter_index];
|
|
|
|
ClearStream(*output_stream);
|
|
|
|
|
|
|
|
filter_index += 1;
|
|
|
|
|
|
|
|
if( !item.item_tab.empty() )
|
|
|
|
MakeText( *item.item_tab[0] ); // should be only one item - item_container
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_filter = true;
|
2011-01-26 13:42:49 +01:00
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
if( old_stream )
|
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
//Call(function, item.functions[0].params, *old_stream, false, *output_stream);
|
|
|
|
Call(item.functions[0], *old_stream, false, *output_stream);
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
//Call(function, item.functions[0].params, stream_temp1, true, *output_stream);
|
|
|
|
Call(item.functions[0], stream_temp1, true, *output_stream);
|
2013-11-07 11:16:10 +01:00
|
|
|
ClearStream(stream_temp1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearStream(*output_stream);
|
2011-01-26 13:42:49 +01:00
|
|
|
output_stream = old_stream;
|
|
|
|
filter_index -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeEzcStream(Item::Function & fun)
|
|
|
|
{
|
|
|
|
if( output_stream_index.size() > 1 )
|
|
|
|
WriteTmpStreamToStreams();
|
|
|
|
|
|
|
|
output_stream_index.clear();
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
for(size_t i=0 ; i<fun.parameters.size() ; ++i)
|
2013-11-07 11:16:10 +01:00
|
|
|
{
|
2014-10-17 23:36:55 +02:00
|
|
|
size_t index = StrToSize(fun.parameters[i]->par.c_str()); // !! CHECK ME or ->name ?
|
2013-11-07 11:16:10 +01:00
|
|
|
|
|
|
|
if( index < output_stream_tab.size() )
|
|
|
|
{
|
|
|
|
if( !has_skip_stream_index || skip_stream_index != index )
|
|
|
|
output_stream_index.push_back(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we do not print any information if the index is out of range
|
|
|
|
// because we do not know to which stream is should be printed
|
|
|
|
}
|
|
|
|
|
|
|
|
if( output_stream_index.size() > 1 )
|
|
|
|
{
|
2013-11-17 22:53:53 +01:00
|
|
|
ClearStream(output_tmp_stream);
|
|
|
|
output_stream = &output_tmp_stream;
|
2013-11-07 11:16:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_stream_index.size() == 1 )
|
|
|
|
{
|
|
|
|
size_t index = output_stream_index[0];
|
|
|
|
output_stream = output_stream_tab[index];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output_stream = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeTextEzc(Item & item)
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; i<item.functions.size() ; ++i)
|
|
|
|
{
|
|
|
|
if( item.functions[i].name == L"stream" )
|
|
|
|
MakeEzcStream(item.functions[i]);
|
|
|
|
else
|
|
|
|
CreateMsg(L"incorrect argument to [ezc] statement");
|
|
|
|
|
|
|
|
// in the future we can use more builtin functions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-17 23:36:55 +02:00
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
template<class StreamType>
|
2012-02-27 18:39:46 +01:00
|
|
|
bool Generator<StreamType>::LimitAchieved()
|
2010-11-22 02:23:32 +01:00
|
|
|
{
|
|
|
|
if( break_generating )
|
2012-02-27 18:39:46 +01:00
|
|
|
return true;
|
2010-11-22 02:23:32 +01:00
|
|
|
|
|
|
|
if( ++current_item > max_items )
|
|
|
|
{
|
|
|
|
break_generating = true;
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(L"Generator exceeded allowed number of elements");
|
2012-02-27 18:39:46 +01:00
|
|
|
return true;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
if( ++stack_index > stack_tab.size() )
|
|
|
|
{
|
|
|
|
break_generating = true;
|
|
|
|
CreateMsg(L"Generator exceeded the stack size");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StreamType>
|
|
|
|
void Generator<StreamType>::MakeText(Item & item)
|
|
|
|
{
|
|
|
|
if( LimitAchieved() )
|
|
|
|
return;
|
|
|
|
|
2012-02-24 13:04:36 +01:00
|
|
|
is_generating_for = false;
|
|
|
|
is_generating_if = false;
|
|
|
|
is_generating_is = false;
|
|
|
|
is_generating_normal = false;
|
|
|
|
is_generating_filter = false;
|
2012-01-17 23:57:06 +01:00
|
|
|
|
2012-02-27 18:39:46 +01:00
|
|
|
stack_tab[stack_index-1].Clear();
|
|
|
|
|
2010-11-22 02:23:32 +01:00
|
|
|
if ( item.type == Item::item_text ) MakeItemText(item);
|
|
|
|
else if( item.type == Item::item_container )MakeTextContainer(item);
|
|
|
|
else if( item.type == Item::item_normal ) MakeTextNormal(item);
|
|
|
|
else if( item.type == Item::item_if ) MakeTextIf(item);
|
|
|
|
else if( item.type == Item::item_ifno ) MakeTextIfno(item);
|
|
|
|
else if( item.type == Item::item_ifany ) MakeTextIfany(item);
|
|
|
|
else if( item.type == Item::item_ifone ) MakeTextIfone(item);
|
|
|
|
else if( item.type == Item::item_ifanyno ) MakeTextIfanyno(item);
|
|
|
|
else if( item.type == Item::item_ifoneno ) MakeTextIfoneno(item);
|
|
|
|
else if( item.type == Item::item_ifindex ) MakeTextIfindex(item);
|
|
|
|
else if( item.type == Item::item_is ) MakeTextIs(item);
|
|
|
|
else if( item.type == Item::item_isno ) MakeTextIsno(item);
|
|
|
|
else if( item.type == Item::item_def ) MakeTextDefine(item);
|
2012-02-27 18:39:46 +01:00
|
|
|
else if( item.type == Item::item_for ) MakeTextFor(item);
|
2011-01-26 13:42:49 +01:00
|
|
|
else if( item.type == Item::item_filter ) MakeTextFilter(item);
|
2013-11-07 11:16:10 +01:00
|
|
|
else if( item.type == Item::item_ezc ) MakeTextEzc(item);
|
2010-11-22 02:23:32 +01:00
|
|
|
else if( item.type == Item::item_err )
|
2010-11-25 23:39:58 +01:00
|
|
|
CreateMsg(L"a wrong directive");
|
2012-02-27 18:39:46 +01:00
|
|
|
|
|
|
|
RemoveStackFunData(stack_tab[stack_index-1]);
|
|
|
|
stack_index -=1;
|
2010-11-22 02:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Ezc
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|