Broken everything trying to get variable stuff situated.

This commit is contained in:
Brad Arant 2024-10-17 14:02:02 -07:00
parent 4972799e7a
commit 062a023683
8 changed files with 51 additions and 38 deletions

View File

@ -37,24 +37,22 @@ namespace jet {
sessions.erase(sessionId);
}
coreutils::ZString Global::getVariable(coreutils::ZString &variable) {
coreutils::ZString Global::getVariable(coreutils::ZString &variable, std::map<coreutils::ZString, coreutils::MString> &variables) {
if(variable.ifNext("$[")) {
coreutils::MString name;
coreutils::MString modifier;
if(variable.ifNext("!")) {
return variables[renderVariableName(variable, name, modifier)];
renderVariableName(variable, name, modifier, variables);
return variables[name];
} if(variable.ifNext(":")) {
// TODO: should only allow CGI variable name. Allow variable substitution.
} if(variable.ifNext("@")) {
// TODO: should only allow environment variables. Allow substitution.
} if(variable.ifNext("$")) {
return getenv(renderVariableName(variable, name, modifier).c_str());
// TODO: should only allow session variables. Allow substitution.
} if(variable.ifNext("%")) {
renderVariableName(variable, name, modifier, variables);
return getenv(name.c_str());
} else {
renderVariableName(variable, name, modifier);
if(!variable.ifNext("]")) {
std::cout << "unparsed: " << variable.unparsed() << std::endl;
throw coreutils::Exception("expecting ] to close variable name.");
}
renderVariableName(variable, name, modifier, variables);
name.split(".");
if(name.getList().size() == 1)
return variables[name[0]];
@ -69,18 +67,23 @@ namespace jet {
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
}
coreutils::MString Global::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier) {
name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext("]"));
else if(variable.ifNext(";")) {
modifier = renderVariableName(variable, modifier, modifier);
}
else if(variable.startsWith("$[")) {
name << getVariable(variable);
}
return name;
void Global::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map<coreutils::ZString, coreutils::MString> &variables) {
while(!variable.ifNext("]")) {
name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext(";")) {
renderVariableName(variable, modifier, modifier, variables);
return;
}
else if(variable.startsWith("$[")) {
name << getVariable(variable, variables);
}
else if(variable.startsWith("#[")) {
name << getVariable(variable, variables);
}
}
return;
}
__mysql * Global::getSession(coreutils::MString sessionId) {
if(sessions.find(sessionId) == sessions.end())
throw coreutils::Exception("requested session is not available.");

View File

@ -18,8 +18,8 @@ namespace jet {
bool sessionExists(coreutils::MString sessionId);
void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId);
coreutils::ZString getVariable(coreutils::ZString &variable);
coreutils::MString renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier);
coreutils::ZString getVariable(coreutils::ZString &variable, std::map<coreutils::ZString, coreutils::MString> &variables);
void renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map<coreutils::ZString, coreutils::MString> &variables);
__mysql * getSession(coreutils::MString sessionId);
coreutils::ZString getSessionVariable(coreutils::MString &splitName);
void outputHeaders();

View File

@ -3,10 +3,10 @@
namespace jet {
KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() {
KeywordValue::KeywordValue(coreutils::ZString data, Global &global, std::map<coreutils::ZString, coreutils::MString> &variables) : MString() {
while(!data.eod()) {
if(data.startsWith("$[")) {
write(global.getVariable(data));
if(data.startsWith("$[") || data.startsWith("#[")) {
write(global.getVariable(data, variables));
} else {
write(data.charAt(0));
data.nextChar();

View File

@ -13,7 +13,7 @@ namespace jet {
class KeywordValue : public coreutils::MString {
public:
KeywordValue(coreutils::ZString data, Global &global);
KeywordValue(coreutils::ZString data, Global &global, std::map<coreutils::ZString, coreutils::MString> &variables);
virtual ~KeywordValue();
};

12
Tag.cpp
View File

@ -46,7 +46,7 @@ namespace jet {
if(!finished) {
coreutils::ZString keywordName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
if(in.ifNext("=\"")) {
variables[keywordName] = KeywordValue(in.getTokenExclude("\""), global);
variables[keywordName] = KeywordValue(in.getTokenExclude("\""), global, variables);
}
if(!in.ifNext("\"")) {}
}
@ -163,14 +163,8 @@ namespace jet {
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.startsWith("$[")) {
out.write(global.getVariable(in));
} else if(in.startsWith("$[") || in.startsWith("#[")) {
out.write(global.getVariable(in, variables));
} else {
out.write(in.charAt(0));
in.nextChar();

BIN
jet-2.0

Binary file not shown.

View File

@ -5,9 +5,9 @@
<html>
---
<set name="modified1" value="ABCD" />
$[modified1;TOHEX]
$[modified1]
<set name="tohex" value="TOHEX" />
$[modified1;$[tohex]]
$[modified1]
---
$[nonexistant]
$[$HOME]

16
testvar.jet Executable file
View File

@ -0,0 +1,16 @@
#!./jet-2.0
<jet filterblanklines="true" trimlines="true">
<set name="ix" value="1" />
<set name="letterx" value="x" />
<set name="var1" value="this is a test" />
<set name="1var1" value="This is another test" />
<set name="var1var" value="Yet another test" />
<set name="var11" value="it seems to work." />
$[$[ix]var$[ix]]
$[var$[ix]]
$[var$[ix]var]
$[var$[i$[letterx]]var]
$[letterx;TOHEX]
$[var$[i$[letterx]]$[ix]]
$[var$[i$[letterx]]$[i$[letterx]]]
</jet>