changed: the interface in this version is not the same as previous
added: class Functions, you can define your own set of functions and then
put them into the Generator
added: variables
parser is able to parse the "def" directive:
[def variable "value"]
[def variable another_variable]
[def variable another_function]
variables are put into a Functions object
added: class Info has a reference to std::ostringstream (out)
added: limit for a "for" directive
limit for the whole tree (when genereting)
this is to protect for a case when someone makes an infinite loop
changed: many small changes
git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@88 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
1 parent
bf585d3716
commit
54be9c0489
2 files changed
+438
-163
No files matched your search
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007, Tomasz Sowa
|
||||
* Copyright (c) 2007-2008, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -56,10 +56,14 @@
|
||||
namespace Ezc
|
||||
{
|
||||
|
||||
void CreateMsg(std::ostringstream & o, const char * type, const char * arg = 0);
|
||||
std::string CreateMsg(const char * type, const char * arg = 0);
|
||||
void SplitUnixDirectory(const char * name, std::string & dir, std::string & file);
|
||||
void SplitUnixDirectory(const std::string & name, std::string & dir, std::string & file);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Pattern
|
||||
{
|
||||
@@ -71,13 +75,17 @@ public:
|
||||
|
||||
struct Item
|
||||
{
|
||||
// change the name to 'Type'
|
||||
enum ItemType
|
||||
{
|
||||
item_none, item_container, item_text, item_ifany, item_for,
|
||||
item_else, item_end, item_err, item_normal, item_ifindex,
|
||||
item_include, item_is, item_ifone, item_comment
|
||||
item_include, item_is, item_ifone, item_comment, item_def
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
ItemType type;
|
||||
std::string text;
|
||||
std::vector<Item*> item_table;
|
||||
@@ -111,7 +119,7 @@ private:
|
||||
void SkipWhiteCharacters();
|
||||
|
||||
std::string ReadDirective();
|
||||
std::string ReadString();
|
||||
std::string ReadString(bool skip_first_quote = false);
|
||||
std::string ReadDirectiveOrString();
|
||||
|
||||
void CreateTreeReadItemDirectiveCheckEnding(Item & item);
|
||||
@@ -123,6 +131,7 @@ private:
|
||||
void ReadDirectiveFor(Item & item);
|
||||
void ReadDirectiveComment(Item & item);
|
||||
void ReadDirectiveInclude(Item & item);
|
||||
void ReadDirectiveDef(Item & item);
|
||||
|
||||
void CreateTreeReadItemDirective(Item & item);
|
||||
|
||||
@@ -138,45 +147,101 @@ private:
|
||||
}; // Pattern
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct Info
|
||||
{
|
||||
std::string text;
|
||||
// this variables you can set in your function
|
||||
std::ostringstream & out;
|
||||
std::string out_string;
|
||||
bool result;
|
||||
|
||||
// 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;
|
||||
|
||||
Info();
|
||||
|
||||
Info(std::ostringstream & o);
|
||||
void Clear();
|
||||
|
||||
bool IsTrue() const;
|
||||
bool IsFalse() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Functions
|
||||
{
|
||||
public:
|
||||
|
||||
typedef void (*UserFunction)(Info &);
|
||||
enum Type { function, variable };
|
||||
|
||||
struct Function
|
||||
{
|
||||
Type type;
|
||||
|
||||
UserFunction user_function;
|
||||
std::string variable;
|
||||
|
||||
int iter;
|
||||
bool is_for; // true if is used by a [for] statement
|
||||
bool is_running; // true if this function (if is) is currently running
|
||||
|
||||
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
|
||||
{
|
||||
private:
|
||||
typedef void (*UserFunction)(Info &);
|
||||
|
||||
struct UserInfo
|
||||
{
|
||||
UserFunction user_function;
|
||||
int iter;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, UserInfo> UserInfoTable;
|
||||
|
||||
public:
|
||||
|
||||
void Insert(const std::string & key, UserFunction ufunction);
|
||||
std::string & Generate(Pattern & data);
|
||||
std::string & String();
|
||||
void Clear();
|
||||
Generator(std::ostringstream &, Pattern & data, Functions & functions);
|
||||
void Generate();
|
||||
|
||||
private:
|
||||
|
||||
UserInfoTable user_info_table;
|
||||
std::string otext;
|
||||
|
||||
bool Find(const std::string & key, UserInfo ** user_info);
|
||||
std::ostringstream & output_stream;
|
||||
Pattern & pattern;
|
||||
Functions & functions;
|
||||
|
||||
Info info1, info2;
|
||||
int loop;
|
||||
|
||||
bool Find(const std::string & key, Functions::Function ** function);
|
||||
|
||||
|
||||
bool Call(const std::string & name, Info & info, Functions::Function ** pfun = 0);
|
||||
void Call(Functions::Function * function, Info & info);
|
||||
|
||||
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, UserInfo * user_info, bool & result);
|
||||
bool MakeTextIfindexnumber(Pattern::Item & item, Functions::Function * function, bool & result);
|
||||
void MakeTextIfany(Pattern::Item & item);
|
||||
void MakeTextIfone(Pattern::Item & item);
|
||||
void MakeTextIfindex(Pattern::Item & item);
|
||||
@@ -184,6 +249,7 @@ private:
|
||||
void MakeTextContainer(Pattern::Item & item);
|
||||
void MakeTextNormal(Pattern::Item & item);
|
||||
void MakeTextIs(Pattern::Item & item);
|
||||
void MakeTextDefine(Pattern::Item & item);
|
||||
void MakeText(Pattern::Item & item);
|
||||
|
||||
}; // Generator
|
||||
|
||||
Reference in new issue
Block a user