2007-01-26 20:29:36 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of EZC -- Easy templating in C++
|
|
|
|
* and is distributed under the (new) BSD licence.
|
2010-07-20 01:07:00 +02:00
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
2007-01-26 20:29:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2010-01-28 17:14:47 +01:00
|
|
|
* Copyright (c) 2007-2010, Tomasz Sowa
|
2007-01-26 20:29:36 +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.
|
|
|
|
*/
|
|
|
|
|
2007-01-27 14:52:18 +01:00
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
#ifndef headerfileezc
|
|
|
|
#define headerfileezc
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2007-01-25 22:42:54 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2007-01-24 21:01:55 +01:00
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
namespace Ezc
|
2007-01-24 21:01:55 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
class Pattern
|
|
|
|
{
|
|
|
|
public:
|
2010-07-20 01:07:00 +02:00
|
|
|
|
2009-01-24 00:18:54 +01:00
|
|
|
Pattern();
|
|
|
|
|
2007-01-27 14:52:18 +01:00
|
|
|
void ParseFile(const std::string & file_name);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ParseFile(const char * file_name);
|
2009-01-24 00:18:54 +01:00
|
|
|
void ParseString(const std::string & str);
|
|
|
|
void ParseString(const char * str);
|
2010-02-15 01:19:31 +01:00
|
|
|
|
|
|
|
// first we're trying to read a file from directory dir
|
|
|
|
// if there is no such a file there then we try read from dir2
|
|
|
|
// (the second dir2 can be empty - it will not be used)
|
|
|
|
void Directory(const char * dir, const char * dir2 = 0);
|
|
|
|
void Directory(const std::string & dir);
|
|
|
|
void Directory(const std::string & dir, const std::string & dir2);
|
2010-07-20 01:07:00 +02:00
|
|
|
|
|
|
|
void AllowInclude(bool allow);
|
|
|
|
void DeleteWhiteTextItems(bool del);
|
|
|
|
|
|
|
|
void SetCommentary(const char * com_start, const char * com_stop);
|
|
|
|
void SetCommentary(const std::string & com_start, const std::string & com_stop);
|
|
|
|
|
|
|
|
void SetIncludeMax(int include_max);
|
2010-02-15 01:19:31 +01:00
|
|
|
|
2010-01-28 17:14:47 +01:00
|
|
|
void Clear();
|
2007-01-24 21:01:55 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
|
|
|
|
void CreateMsg(std::ostringstream & o, const char * type, const char * arg = 0);
|
|
|
|
std::string CreateMsg(const char * type, const char * arg = 0);
|
|
|
|
|
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
struct Item
|
|
|
|
{
|
2010-07-20 01:07:00 +02:00
|
|
|
enum Type
|
2007-01-24 21:01:55 +01:00
|
|
|
{
|
2010-07-20 01:07:00 +02:00
|
|
|
item_none, item_container, item_text, item_normal, item_is, item_isno,
|
|
|
|
item_if, item_ifno, item_ifany, item_ifone, item_ifanyno, item_ifoneno, item_ifindex,
|
|
|
|
item_for, item_else, item_end, item_err, item_include, item_comment, item_def
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
struct Function
|
2010-01-25 06:19:54 +01:00
|
|
|
{
|
2010-07-20 01:07:00 +02:00
|
|
|
std::string name; // function name
|
|
|
|
std::vector<std::string> params; // function parameters
|
2010-01-25 06:19:54 +01:00
|
|
|
};
|
2008-12-10 02:46:10 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
Type type;
|
|
|
|
std::string text; // used in: item_text, item_include (as a file name)
|
|
|
|
std::vector<Item*> item_table; // !! zamienic na 'items'?
|
|
|
|
std::vector<Function> functions;
|
2007-01-24 21:01:55 +01:00
|
|
|
|
|
|
|
Item();
|
2007-01-26 20:29:36 +01:00
|
|
|
Item(const Item & i);
|
|
|
|
Item & operator=(const Item & i);
|
|
|
|
void CopyItemTable(const Item & i);
|
2007-01-24 21:01:55 +01:00
|
|
|
~Item();
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
Item * AddItem(const Item * porg = 0);
|
|
|
|
Item * AddItem(const Item & porg);
|
|
|
|
void ClearItems();
|
|
|
|
Type LastItemType();
|
|
|
|
void DeleteLastItem();
|
|
|
|
void Clear();
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Item item_root;
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
const char * itext;
|
|
|
|
|
2009-12-09 01:34:17 +01:00
|
|
|
// allowing include tag
|
|
|
|
// default: true
|
2009-01-24 00:18:54 +01:00
|
|
|
bool allow_include;
|
|
|
|
|
2009-12-09 01:34:17 +01:00
|
|
|
// if true all text-items which have only white characters (with new lines as well)
|
|
|
|
// will be deleted - useful in *.txt templates
|
|
|
|
// this not actually delete the whole item but only the string
|
|
|
|
// the item will be present with an empty string
|
|
|
|
// default: false
|
2010-07-20 01:07:00 +02:00
|
|
|
bool delete_white_text_items;
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-02-15 01:19:31 +01:00
|
|
|
// first we're trying to read a file from 'directory'
|
|
|
|
// if there is no such a file there then we try read from 'directory2'
|
|
|
|
// we read from these directories only if they are not empty
|
|
|
|
std::string directory, directory2;
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
int include_level, include_level_max;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string commentary_start, commentary_stop;
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-01-25 06:19:54 +01:00
|
|
|
std::string ReadFile(const std::string & name);
|
2007-01-26 20:29:36 +01:00
|
|
|
std::string ReadFile(const char * name);
|
2010-07-20 01:07:00 +02:00
|
|
|
bool CheckFileName(const char * name);
|
2010-02-15 01:19:31 +01:00
|
|
|
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
int ReadCharInText();
|
2009-12-09 01:34:17 +01:00
|
|
|
bool IsWhite(int c);
|
2010-07-20 01:07:00 +02:00
|
|
|
void SkipWhite();
|
2009-12-09 01:34:17 +01:00
|
|
|
void CheckWhiteAndDelete(std::string & s);
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
bool IsNameChar(int c);
|
|
|
|
bool IsDigit(int c);
|
|
|
|
bool IsPositiveNumber(const std::string & str);
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
bool ReadName(std::string & name);
|
|
|
|
bool ReadString(std::string & str);
|
|
|
|
bool ReadFunction(Item::Function & function);
|
|
|
|
bool ReadParams(Item::Function & function);
|
|
|
|
bool ReadFunctions(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
void ReadDirectiveIf(Item & item);
|
2008-12-23 21:08:11 +01:00
|
|
|
void ReadDirectiveIfno(Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void ReadDirectiveIfany(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveIfone(Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void ReadDirectiveIfanyno(Item & item);
|
|
|
|
void ReadDirectiveIfoneno(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveIs(Item & item);
|
2010-02-11 21:03:51 +01:00
|
|
|
void ReadDirectiveIsno(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveIfindex(Item & item);
|
|
|
|
void ReadDirectiveFor(Item & item);
|
2007-01-27 14:52:18 +01:00
|
|
|
void ReadDirectiveComment(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void ReadDirectiveInclude(Item & item);
|
2008-12-10 02:46:10 +01:00
|
|
|
void ReadDirectiveDef(Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void ReadDirectiveNormal(const std::string & name, Item & item);
|
2007-01-27 14:52:18 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
void CreateTreeReadItemDirectiveCheckEnding(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void CreateTreeReadItemDirective(Item & item);
|
|
|
|
void CreateTreeReadItemText(Item & item);
|
|
|
|
bool CreateTreeReadItem(Item & item);
|
|
|
|
void CreateTreeReadIf(Item & item);
|
|
|
|
void CreateTreeReadFor(Item & item);
|
|
|
|
void CreateTree(Item & item);
|
|
|
|
void CreateTreeReadInclude(Item & item);
|
2010-02-15 01:19:31 +01:00
|
|
|
void CreateTreeReadIncludeSkipAllowFlag(Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
}; // class Pattern
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
struct Info
|
|
|
|
{
|
2010-05-30 23:07:15 +02:00
|
|
|
// output stream
|
2008-12-10 02:46:10 +01:00
|
|
|
std::ostringstream & out;
|
2010-01-25 06:19:54 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
// table of parameters
|
|
|
|
std::vector<std::string> & params;
|
|
|
|
|
|
|
|
// the first parameter
|
|
|
|
// you can always use it even if there is not any parameters
|
|
|
|
// in such a way the reference points to an empty valid string
|
2010-05-30 23:07:15 +02:00
|
|
|
const std::string & par;
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
// this is set by Generator
|
|
|
|
// normally is 0
|
|
|
|
// in a [for] statement it indicates the number of the current iteration (the first is 0)
|
2007-01-26 20:29:36 +01:00
|
|
|
int iter;
|
|
|
|
|
2010-05-30 23:07:15 +02:00
|
|
|
// return value from a user function (default false if not set directly by the user function)
|
|
|
|
// for a variable it is set to true if the variable is not empty
|
|
|
|
bool res;
|
2008-12-10 02:46:10 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
// arguments: output_stream, table_of_parameters, the_first_parameter
|
|
|
|
Info(std::ostringstream & o, std::vector<std::string> & pars, const std::string & p);
|
2008-12-10 02:46:10 +01:00
|
|
|
void Clear();
|
2007-01-24 21:01:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
|
2010-01-25 06:19:54 +01:00
|
|
|
// functions or variables
|
2008-12-10 02:46:10 +01:00
|
|
|
class Functions
|
2007-01-26 20:29:36 +01:00
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
public:
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
typedef void (*UserFunction)(Info &);
|
2008-12-10 02:46:10 +01:00
|
|
|
enum Type { function, variable };
|
2007-01-26 20:29:36 +01:00
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
struct Function
|
2007-01-26 20:29:36 +01:00
|
|
|
{
|
2008-12-10 02:46:10 +01:00
|
|
|
Type type;
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
UserFunction user_function; // used when type is 'function'
|
|
|
|
std::string variable; // used when type is 'variable'
|
2008-12-10 02:46:10 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
int iter;
|
|
|
|
bool is_for; // true if is used by a [for] statement
|
|
|
|
bool is_running; // true if this function is currently executed
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
Function();
|
2007-01-26 20:29:36 +01:00
|
|
|
};
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
void Insert(const std::string & key, UserFunction ufunction); // inserting a function
|
|
|
|
void Insert(const std::string & key, const char * var); // inserting a variable
|
|
|
|
void Insert(const std::string & key, const std::string & var); // inserting a variable
|
|
|
|
|
|
|
|
bool Find(const std::string & key, Function ** fun);
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::map<std::string, Function> FunctionsTable;
|
|
|
|
FunctionsTable functions_table;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Generator
|
|
|
|
{
|
2007-01-26 20:29:36 +01:00
|
|
|
public:
|
|
|
|
|
2010-05-30 23:07:15 +02:00
|
|
|
Generator();
|
|
|
|
Generator(std::ostringstream & o, Pattern & p, Functions & f);
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
void Set(std::ostringstream & o, Pattern & p, Functions & f);
|
|
|
|
void Set(std::ostringstream & o);
|
|
|
|
void Set(Pattern & p);
|
|
|
|
void Set(Functions & f);
|
2010-05-30 23:07:15 +02:00
|
|
|
void SetMax(int max_items_, int max_for_items_);
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
void Generate();
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2010-05-30 23:07:15 +02:00
|
|
|
std::ostringstream * output_stream;
|
2010-07-20 01:07:00 +02:00
|
|
|
Pattern * pattern;
|
2010-05-30 23:07:15 +02:00
|
|
|
Functions * functions;
|
2008-12-10 02:46:10 +01:00
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
bool break_generating;
|
2010-05-30 23:07:15 +02:00
|
|
|
int current_item;
|
|
|
|
int max_items;
|
|
|
|
int max_for_items;
|
|
|
|
|
|
|
|
// an empty string for info objects
|
|
|
|
const std::string empty;
|
|
|
|
|
2008-12-10 02:46:10 +01:00
|
|
|
bool Find(const std::string & key, Functions::Function ** function);
|
|
|
|
|
2010-05-30 23:07:15 +02:00
|
|
|
void Call(Functions::Function * function, Info & info);
|
|
|
|
bool Call(const std::string & name, Info & info, Functions::Function ** pfun = 0);
|
2010-07-20 01:07:00 +02:00
|
|
|
bool Call(Pattern::Item::Function & function, bool * info_res = 0, Functions::Function ** pfun = 0);
|
2008-12-10 02:46:10 +01:00
|
|
|
|
|
|
|
void CallUserFunction(Functions::Function * function, Info & info);
|
|
|
|
void CallVariable(Functions::Function * function, Info & info);
|
|
|
|
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIf_go(Pattern::Item & item, bool result);
|
2008-12-10 02:46:10 +01:00
|
|
|
bool MakeTextIfindexnumber(Pattern::Item & item, Functions::Function * function, bool & result);
|
2010-07-20 01:07:00 +02:00
|
|
|
void MakeTextIf(Pattern::Item & item);
|
2008-12-23 21:08:11 +01:00
|
|
|
void MakeTextIfno(Pattern::Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void MakeTextIfany(Pattern::Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIfone(Pattern::Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void MakeTextIfanyno(Pattern::Item & item);
|
|
|
|
void MakeTextIfoneno(Pattern::Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextIfindex(Pattern::Item & item);
|
2010-05-30 23:07:15 +02:00
|
|
|
void MakeTextForLoop(Pattern::Item & item, Functions::Function * function);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeTextFor(Pattern::Item & item);
|
|
|
|
void MakeTextContainer(Pattern::Item & item);
|
|
|
|
void MakeTextNormal(Pattern::Item & item);
|
2010-07-20 01:07:00 +02:00
|
|
|
void MakeTextIs(Pattern::Item & item);
|
|
|
|
void MakeTextIsno(Pattern::Item & item);
|
2008-12-10 02:46:10 +01:00
|
|
|
void MakeTextDefine(Pattern::Item & item);
|
2007-01-26 20:29:36 +01:00
|
|
|
void MakeText(Pattern::Item & item);
|
|
|
|
|
2010-07-20 01:07:00 +02:00
|
|
|
}; // class Generator
|
2007-01-26 20:29:36 +01:00
|
|
|
|
|
|
|
} // namespace Ezc
|
|
|
|
|
|
|
|
|
2007-01-24 21:01:55 +01:00
|
|
|
#endif
|