Fixed for tag.

This commit is contained in:
brad Arant 2024-07-08 18:32:56 -07:00
parent d10ed783fa
commit dd5768ea66
20 changed files with 199 additions and 78 deletions

View File

@ -3,20 +3,22 @@
namespace jet { namespace jet {
Expression::Expression(coreutils::ZString &expression) : MString("zzz") { Expression::Expression(coreutils::ZString &expression) : MString() {
std::cout << "Expression construction:" << expression << std::endl; std::cout << "Expression construction:" << expression << std::endl;
// if(expression.equals("true"))
// MString("true"); if(isNumber(0, expression)) {
// else if(expression.equals("false"))
// MString("false"); } else if(expression.equals("true"))
// else if(expression.equals("")) write(expression);
// MString("false"); else if(expression.equals("false"))
// else write(expression);
// MString("true");
} }
Expression::~Expression() { Expression::~Expression() {}
std::cout << "Expression destruction:" << getLength() << std::endl;
bool Expression::isNumber(int reg, coreutils::ZString &expression) {
return false;
} }
} }

View File

@ -10,6 +10,9 @@ namespace jet {
public: public:
Expression(coreutils::ZString &expression); Expression(coreutils::ZString &expression);
virtual ~Expression(); virtual ~Expression();
private:
bool isNumber(int reg, coreutils::ZString &expression);
}; };
} }

View File

@ -3,12 +3,19 @@
namespace jet { namespace jet {
KeywordValue::KeywordValue(coreutils::ZString &data) { KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() {
while(!data.eod()) {
if(data.ifNext("$[")) {
coreutils::ZString varName(data.getTokenExclude("]"));
data.ifNext("]");
write(global.variables[varName]);
} else {
write(data.charAt(0));
data.nextChar();
}
}
} }
KeywordValue::~KeywordValue() { KeywordValue::~KeywordValue() {}
std::cout << "KeywordValue destruction: " << getLength() << std::endl;
}
} }

View File

@ -2,17 +2,18 @@
#define __KeywordValue_h__ #define __KeywordValue_h__
#include "MString.h" #include "MString.h"
#include "Global.h"
namespace jet { namespace jet {
/// ///
/// KeywordValue will read the data ZString until a double quote (") is reached. /// KeywordValue will read the data ZString and convert any variable references.
/// ///
class KeywordValue : public coreutils::MString { class KeywordValue : public coreutils::MString {
public: public:
KeywordValue(coreutils::ZString &data); KeywordValue(coreutils::ZString data, Global &global);
virtual ~KeywordValue(); virtual ~KeywordValue();
}; };

72
Tag.cpp
View File

@ -1,5 +1,6 @@
#include "Tag.h" #include "Tag.h"
#include "Exception.h" #include "Exception.h"
#include "KeywordValue.h"
#include "Log.h" #include "Log.h"
#include "__mysql.h" #include "__mysql.h"
#include "__for.h" #include "__for.h"
@ -12,8 +13,9 @@
namespace jet { namespace jet {
Tag::Tag(coreutils::ZString &in, coreutils::MString &parent, Global &global) Tag::Tag(coreutils::ZString &in, coreutils::MString &parent, Global &global, coreutils::ZString splitTagName)
: ZString(in), parent(parent), global(global) { : ZString(in), parent(parent), global(global) {
this->splitTagName = splitTagName;
if(in.ifNext("<")) { if(in.ifNext("<")) {
name = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!"); name = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!");
if(in.startsWith(" ") || in.startsWith("/") || in.startsWith(">")) { if(in.startsWith(" ") || in.startsWith("/") || in.startsWith(">")) {
@ -23,38 +25,54 @@ namespace jet {
in.skipWhitespace(); in.skipWhitespace();
if(in.ifNext(">")) { if(in.ifNext(">")) {
hasContainer = true; hasContainer = true;
hasContainer2 = false;
finished = true; finished = true;
break; break;
} else if(in.ifNext("/>")) { } else if(in.ifNext("/>")) {
hasContainer = false; hasContainer = false;
hasContainer2 = false;
finished = true; finished = true;
break; break;
} }
if(!finished) { if(!finished) {
coreutils::ZString keywordName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); coreutils::ZString keywordName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
if(in.ifNext("=\"")) { if(in.ifNext("=\"")) {
variables[keywordName] = in.getTokenExclude("\""); variables[keywordName] = KeywordValue(in.getTokenExclude("\""), global);
} }
if(!in.ifNext("\"")) {} if(!in.ifNext("\"")) {}
} }
} }
if(hasContainer) { if(hasContainer) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "has Container: " << hasContainer; coreutils::Log(coreutils::LOG_DEBUG_2) << "has Container: " << hasContainer;
bool hasSplitTag = splitTagName == "" ? false: true;
char *start = in.getCursor(); char *start = in.getCursor();
while(!in.eod()) { while(!in.eod()) {
char *end = in.getCursor(); char *end = in.getCursor();
if(ifEndTagName(in)) { if(ifEndTagName(in)) {
container = coreutils::ZString(start, end - start); if(hasContainer2)
coreutils::Log(coreutils::LOG_DEBUG_2) << "Container: [" << container << "]"; container2 = coreutils::ZString(start, end - start);
else
container = coreutils::ZString(start, end - start);
break; break;
} else if(ifNested(in)) {} } else if(hasSplitTag && ifSplitTagName(in)) {
else hasContainer2 = true;
container = coreutils::ZString(start, end - start);
in.ifNext("<else>");
start = in.getCursor();
} else if(ifNested(in)) {
} else
in.nextChar(); in.nextChar();
} }
} }
setZString(in.parsed()); setZString(in.parsed());
if(hasContainer && evaluate) if(variableDefined("eval")) {
parseContainer(container); if(variables["eval"] == "yes") {
evaluate = true;
} else if(variables["eval"] == "no") {
evaluate = false;
} else
throw coreutils::Exception("Keyword 'eval' must be 'yes' or 'no'.");
}
} }
} else } else
throw coreutils::Exception("First character of (in) must be a <."); throw coreutils::Exception("First character of (in) must be a <.");
@ -67,7 +85,11 @@ namespace jet {
else else
if(output) if(output)
copyContainer(container, parent); copyContainer(container, parent);
std::cout << "Destructing " << name << std::endl; }
void Tag::processContainer(coreutils::ZString &container) {
if(hasContainer && evaluate)
parseContainer(container);
} }
void Tag::parseContainer(coreutils::ZString &in) { void Tag::parseContainer(coreutils::ZString &in) {
@ -95,10 +117,26 @@ namespace jet {
} else if(ifTagName(in, "while")) { } else if(ifTagName(in, "while")) {
__while _while(in, out, global); __while _while(in, out, global);
continue; continue;
} else {
out.write(in.charAt(0));
in.nextChar();
} }
} else if(in.ifNext("#[")) {
coreutils::MString varName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
if(in.ifNext("]"))
out.write(variables[varName]);
else
throw coreutils::Exception("Local variable name has a syntax error.");
} else if(in.ifNext("$[")) {
coreutils::MString varName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
if(in.ifNext("]"))
out.write(global.variables[varName]);
else
throw coreutils::Exception("Global variable name has a syntax error.");
} else {
out.write(in.charAt(0));
in.nextChar();
} }
out.write(in.charAt(0));
in.nextChar();
} }
} }
@ -162,6 +200,18 @@ namespace jet {
return false; return false;
} }
bool Tag::ifSplitTagName(coreutils::ZString &in) {
in.push();
if(in.ifNext("<"))
if(in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").equals(splitTagName)) {
if(in.ifNext(">"))
in.pop();
return true;
}
in.pop();
return false;
}
int Tag::skipBlankLine(coreutils::ZString in) { int Tag::skipBlankLine(coreutils::ZString in) {
ZString temp = in.getTokenInclude(" \t"); ZString temp = in.getTokenInclude(" \t");
if(ifNext("\n")) if(ifNext("\n"))

10
Tag.h
View File

@ -11,18 +11,23 @@ namespace jet {
class Tag : public coreutils::ZString { class Tag : public coreutils::ZString {
public: public:
Tag(coreutils::ZString &in, coreutils::MString &parent, Global &global); Tag(coreutils::ZString &in, coreutils::MString &parent, Global &global, coreutils::ZString splitTagName = "");
virtual ~Tag(); virtual ~Tag();
coreutils::ZString name; coreutils::ZString name;
coreutils::ZString container; coreutils::ZString container;
coreutils::ZString container2;
protected: protected:
bool hasContainer; bool hasContainer;
bool hasContainer2;
std::map<coreutils::ZString, coreutils::MString> variables; std::map<coreutils::ZString, coreutils::MString> variables;
bool variableDefined(coreutils::ZString variable); bool variableDefined(coreutils::ZString variable);
void parseContainer(coreutils::ZString &in); void parseContainer(coreutils::ZString &in);
void processContainer(coreutils::ZString &container);
void copyContainer(coreutils::ZString &in, coreutils::MString &out); void copyContainer(coreutils::ZString &in, coreutils::MString &out);
coreutils::MString out;
bool output = true; bool output = true;
bool evaluate = true; bool evaluate = true;
bool filterBlankLines = false; bool filterBlankLines = false;
@ -32,7 +37,7 @@ namespace jet {
private: private:
Global &global; Global &global;
coreutils::MString &parent; coreutils::MString &parent;
coreutils::MString out; coreutils::ZString splitTagName;
int skipBlankLine(coreutils::ZString in); int skipBlankLine(coreutils::ZString in);
@ -41,6 +46,7 @@ namespace jet {
bool ifTagName(coreutils::ZString &in, const char *tag); bool ifTagName(coreutils::ZString &in, const char *tag);
bool ifTagName(coreutils::ZString &in); bool ifTagName(coreutils::ZString &in);
bool ifEndTagName(coreutils::ZString &in); bool ifEndTagName(coreutils::ZString &in);
bool ifSplitTagName(coreutils::ZString &in);
}; };

View File

@ -5,14 +5,28 @@
namespace jet { namespace jet {
__for::__for(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __for::__for(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) {
if(!variableDefined(coreutils::ZString("begin"))) double counter = 0.0f;
throw coreutils::Exception("begin keyword must be specified."); double end = 0.0f;
if(!variableDefined(coreutils::ZString("end"))) double step = 0.0f;
throw coreutils::Exception("end keyword must be specified."); bool nameDefined = variableDefined("name");
if(variableDefined(coreutils::ZString("start"))) {
for(;;) { counter = variables["start"].asDouble();
parseContainer(container);
} }
if(variableDefined(coreutils::ZString("end"))) {
end = variables["end"].asDouble();
}
if(variableDefined(coreutils::ZString("step"))) {
step = variables["step"].asDouble();
}
for(double ix = counter; ix <= end; ix += step) {
if(nameDefined)
variables[variables["name"]] = ix;
processContainer(container);
container.reset();
}
// for (auto i = variables.begin(); i != variables.end(); i++)
// std::cout << i->first << "=[" << i->second << "]" << std::endl;}
} }

View File

@ -9,7 +9,7 @@ namespace jet {
class __for : public Tag { class __for : public Tag {
public: public:
__for(coreutils::ZString &in, coreutils::MString &out, Global &global); __for(coreutils::ZString &in, coreutils::MString &parent, Global &global);
}; };

View File

@ -1,3 +1,4 @@
#include "__if.h" #include "__if.h"
#include "Exception.h" #include "Exception.h"
#include <iostream> #include <iostream>
@ -5,7 +6,7 @@
namespace jet { namespace jet {
__if::__if(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __if::__if(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global, "else") {
coreutils::MString result; coreutils::MString result;
@ -14,7 +15,7 @@ namespace jet {
throw coreutils::Exception("Either value1 or expr can be specified but not both."); throw coreutils::Exception("Either value1 or expr can be specified but not both.");
if(variableDefined("value2")) { if(variableDefined("value2")) {
if(variableDefined("type")) { if(variableDefined("type")) {
result = "true";
} else } else
throw coreutils::Exception("type expected if value1 and value2 specified."); throw coreutils::Exception("type expected if value1 and value2 specified.");
} else } else
@ -25,12 +26,12 @@ namespace jet {
if(variableDefined("type")) if(variableDefined("type"))
throw coreutils::Exception("type should not be specified with expr."); throw coreutils::Exception("type should not be specified with expr.");
result = Expression(variables["expr"]); result = Expression(variables["expr"]);
std::cout << "result1: " << result << std::endl;
}
std::cout << "result2: " << result << std::endl;
if(result.boolValue()) {
parseContainer(container);
} }
if(result.boolValue())
processContainer(container);
else
if(hasContainer2)
processContainer(container2);
} }

2
__if.h
View File

@ -11,7 +11,7 @@ namespace jet {
class __if : public Tag { class __if : public Tag {
public: public:
__if(coreutils::ZString &in, coreutils::MString &out, Global &global); __if(coreutils::ZString &in, coreutils::MString &parent, Global &global);
}; };

View File

@ -3,8 +3,8 @@
namespace jet { namespace jet {
__jet::__jet(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __jet::__jet(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
parseContainer(container); processContainer(container);
} }
} }

View File

@ -10,7 +10,7 @@ namespace jet {
class __jet : public Tag { class __jet : public Tag {
public: public:
__jet(coreutils::ZString &in, coreutils::MString &out, Global &global); __jet(coreutils::ZString &in, coreutils::MString &parent, Global &global);
}; };

View File

@ -3,8 +3,8 @@
namespace jet { namespace jet {
__mysql::__mysql(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __mysql::__mysql(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
parseContainer(container); processContainer(container);
} }
} }

View File

@ -11,7 +11,7 @@ namespace jet {
class __mysql : public Tag { class __mysql : public Tag {
public: public:
__mysql(coreutils::ZString &in, coreutils::MString &out, Global &global); __mysql(coreutils::ZString &in, coreutils::MString &parent, Global &global);
}; };

View File

@ -3,12 +3,12 @@
namespace jet { namespace jet {
__read::__read(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __read::__read(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
if(!variableDefined(coreutils::ZString("file"))) if(!variableDefined(coreutils::ZString("file")))
throw coreutils::Exception("file keyword must be specified."); throw coreutils::Exception("file keyword must be specified.");
if(hasContainer) if(hasContainer)
throw coreutils::Exception("missing / at end of tag definition."); throw coreutils::Exception("Missing / at end of tag definition. Not a container tag.");
// process file request here. // process file request here.

View File

@ -8,7 +8,7 @@ namespace jet {
class __read : public Tag { class __read : public Tag {
public: public:
__read(coreutils::ZString &in, coreutils::MString &out, Global &global); __read(coreutils::ZString &in, coreutils::MString &parent, Global &global);
}; };

View File

@ -1,20 +1,34 @@
#include "__set.h" #include "__set.h"
#include "Exception.h" #include "Exception.h"
#include "Expression.h"
#include <iostream> #include <iostream>
namespace jet { namespace jet {
__set::__set(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { __set::__set(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
output = false; output = false;
processContainer(container);
if(!variableDefined("name")) if(!variableDefined("name"))
throw coreutils::Exception("set tag must have name defined."); throw coreutils::Exception("set tag must have name defined.");
if(variableDefined("value") && hasContainer) if(!variableDefined("expr") && variableDefined("value") && hasContainer)
throw coreutils::Exception("set tag cannot have both value and a container."); throw coreutils::Exception("set tag cannot have both value and a container.");
if(!variableDefined("value") && !hasContainer) if(variableDefined("expr") && !variableDefined("value") && hasContainer)
throw coreutils::Exception("set tag must have a value or a container."); throw coreutils::Exception("set tag cannot have both expr and a container.");
if(variableDefined("expr") && variableDefined("value") && !hasContainer)
throw coreutils::Exception("set tag cannot have both expr and value.");
if(!variableDefined("expr") && !variableDefined("value") && !hasContainer)
throw coreutils::Exception("set tag must have a value, expr or a container.");
if(!variableDefined("scope") || (variables["scope"] == "global")) { if(!variableDefined("scope") || (variables["scope"] == "global")) {
if(hasContainer) { if(variableDefined("expr")) {
global.variables[variables["name"]] = container.parsed(); if(variableDefined("eval"))
throw coreutils::Exception("Cannot use eval with expr.");
global.variables[variables["name"]] = Expression(variables["expr"]);
} else if(hasContainer) {
if(evaluate) {
global.variables[variables["name"]] = out;
} else {
global.variables[variables["name"]] = container;
}
} else } else
global.variables[variables["name"]] = variables["value"]; global.variables[variables["name"]] = variables["value"];
} else { } else {

View File

@ -11,7 +11,9 @@ namespace jet {
class __set : public Tag { class __set : public Tag {
public: public:
__set(coreutils::ZString &in, coreutils::MString &out, Global &global); __set(coreutils::ZString &in, coreutils::MString &parent, Global &global);
protected:
}; };

BIN
testjet

Binary file not shown.

View File

@ -5,20 +5,41 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
coreutils::ZString data("<jet>\n" coreutils::ZString data("<jet name=\"localname\">\n"
" <set name=\"varname\" value=\"vardata\" scope=\"global\" />\n" " <html>\n"
" <set name=\"thename\">this is the value</set>\n" " <set name=\"ix\" value=\"1\" />\n"
" <set name=\"theexpr\" expr=\"true\" />\n"
" <set name=\"varname$[ix]\" value=\"vardata\" scope=\"global\" />\n"
" <set name=\"noeval\" eval=\"no\">this is the value store in #[name].</set>\n"
" <set name=\"thename\" eval=\"yes\">this is the value store in #[name].</set>\n"
" <set name=\"newname\" scope=\"global\">another container value</set>\n" " <set name=\"newname\" scope=\"global\">another container value</set>\n"
" >>>$[noeval]<<<\n"
" >>>$[thename]<<<\n"
" local: >>>#[name]<<<\n"
" <mysql key=\"uu\">\n" " <mysql key=\"uu\">\n"
" <if value1=\"X\" value2=\"Y\" type=\"eq\">\n" " <if value1=\"X\" value2=\"Y\" type=\"eq\">\n"
" 789\n" " 789\n"
" <if expr=\"true\">\n" " <if expr=\"false\">\n"
" 123\n" " 123\n"
" <else>\n"
" 456\n"
" </if>\n" " </if>\n"
" </if>\n" " </if>\n"
" </mysql>\n" " </mysql>\n"
" <for name=\"ix\" start=\"1\" end=\"5\" step=\".1\">\n"
" -->#[ix]<--\n"
" </for>\n"
" </html>\n"
"</jet>\n"); "</jet>\n");
coreutils::ZString data2("<jet>\n"
" <if expr=\"false\">\n"
" XXX\n"
" <else>\n"
" YYY\n"
" </if>\n"
"</jet>\n");
// coreutils::ZString data("<jet>ABC<jet>HIJ</jet>XYZ</jet>\n"); // coreutils::ZString data("<jet>ABC<jet>HIJ</jet>XYZ</jet>\n");
std::cout << "---------\n" << data << "----------\n" << std::endl; std::cout << "---------\n" << data << "----------\n" << std::endl;