ezc/src/ezc.h

338 lines
9.5 KiB
C++

/*
* This file is a part of EZC -- Easy templating in C++
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2007-2010, Tomasz Sowa
* 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 headerfileezc
#define headerfileezc
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstring>
namespace Ezc
{
class Pattern
{
public:
Pattern();
void ParseFile(const std::string & file_name);
void ParseFile(const char * file_name);
void ParseString(const std::string & str);
void ParseString(const char * str);
// 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);
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);
void Clear();
void CreateMsg(std::ostringstream & o, const char * type, const char * arg = 0);
std::string CreateMsg(const char * type, const char * arg = 0);
struct Item
{
enum Type
{
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
};
struct Function
{
std::string name; // function name
std::vector<std::string> params; // function parameters
};
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;
Item();
Item(const Item & i);
Item & operator=(const Item & i);
void CopyItemTable(const Item & i);
~Item();
Item * AddItem(const Item * porg = 0);
Item * AddItem(const Item & porg);
void ClearItems();
Type LastItemType();
void DeleteLastItem();
void Clear();
};
Item item_root;
private:
const char * itext;
// allowing include tag
// default: true
bool allow_include;
// 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
bool delete_white_text_items;
// 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;
int include_level, include_level_max;
std::string commentary_start, commentary_stop;
std::string ReadFile(const std::string & name);
std::string ReadFile(const char * name);
bool CheckFileName(const char * name);
bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result);
int ReadCharInText();
bool IsWhite(int c);
void SkipWhite();
void CheckWhiteAndDelete(std::string & s);
bool IsNameChar(int c);
bool IsDigit(int c);
bool IsPositiveNumber(const std::string & str);
bool ReadName(std::string & name);
bool ReadString(std::string & str);
bool ReadFunction(Item::Function & function);
bool ReadParams(Item::Function & function);
bool ReadFunctions(Item & item);
void ReadDirectiveIf(Item & item);
void ReadDirectiveIfno(Item & item);
void ReadDirectiveIfany(Item & item);
void ReadDirectiveIfone(Item & item);
void ReadDirectiveIfanyno(Item & item);
void ReadDirectiveIfoneno(Item & item);
void ReadDirectiveIs(Item & item);
void ReadDirectiveIsno(Item & item);
void ReadDirectiveIfindex(Item & item);
void ReadDirectiveFor(Item & item);
void ReadDirectiveComment(Item & item);
void ReadDirectiveInclude(Item & item);
void ReadDirectiveDef(Item & item);
void ReadDirectiveNormal(const std::string & name, Item & item);
void CreateTreeReadItemDirectiveCheckEnding(Item & item);
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);
void CreateTreeReadIncludeSkipAllowFlag(Item & item);
}; // class Pattern
struct Info
{
// output stream
std::ostringstream & out;
// 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
const std::string & par;
// this is set by Generator
// normally is 0
// in a [for] statement it indicates the number of the current iteration (the first is 0)
int iter;
// 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;
// arguments: output_stream, table_of_parameters, the_first_parameter
Info(std::ostringstream & o, std::vector<std::string> & pars, const std::string & p);
void Clear();
};
// functions or variables
class Functions
{
public:
typedef void (*UserFunction)(Info &);
enum Type { function, variable };
struct Function
{
Type type;
UserFunction user_function; // used when type is 'function'
std::string variable; // used when type is 'variable'
int iter;
bool is_for; // true if is used by a [for] statement
bool is_running; // true if this function is currently executed
Function();
};
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
{
public:
Generator();
Generator(std::ostringstream & o, Pattern & p, Functions & f);
void Set(std::ostringstream & o, Pattern & p, Functions & f);
void Set(std::ostringstream & o);
void Set(Pattern & p);
void Set(Functions & f);
void SetMax(int max_items_, int max_for_items_);
void Generate();
private:
std::ostringstream * output_stream;
Pattern * pattern;
Functions * functions;
bool break_generating;
int current_item;
int max_items;
int max_for_items;
// an empty string for info objects
const std::string empty;
bool Find(const std::string & key, Functions::Function ** function);
void Call(Functions::Function * function, Info & info);
bool Call(const std::string & name, Info & info, Functions::Function ** pfun = 0);
bool Call(Pattern::Item::Function & function, bool * info_res = 0, Functions::Function ** pfun = 0);
void CallUserFunction(Functions::Function * function, Info & info);
void CallVariable(Functions::Function * function, Info & info);
void MakeTextIf_go(Pattern::Item & item, bool result);
bool MakeTextIfindexnumber(Pattern::Item & item, Functions::Function * function, bool & result);
void MakeTextIf(Pattern::Item & item);
void MakeTextIfno(Pattern::Item & item);
void MakeTextIfany(Pattern::Item & item);
void MakeTextIfone(Pattern::Item & item);
void MakeTextIfanyno(Pattern::Item & item);
void MakeTextIfoneno(Pattern::Item & item);
void MakeTextIfindex(Pattern::Item & item);
void MakeTextForLoop(Pattern::Item & item, Functions::Function * function);
void MakeTextFor(Pattern::Item & item);
void MakeTextContainer(Pattern::Item & item);
void MakeTextNormal(Pattern::Item & item);
void MakeTextIs(Pattern::Item & item);
void MakeTextIsno(Pattern::Item & item);
void MakeTextDefine(Pattern::Item & item);
void MakeText(Pattern::Item & item);
}; // class Generator
} // namespace Ezc
#endif