more work on variables.

This commit is contained in:
Brad Arant 2024-09-19 14:40:07 -07:00
parent 7453db9bd9
commit 0379974d5e
4 changed files with 37 additions and 15 deletions

View File

@ -30,5 +30,31 @@ namespace jet {
void Global::removeSession(coreutils::MString sessionId) {
sessions.erase(sessionId);
}
coreutils::ZString Global::getVariable(coreutils::ZString &variable) {
if(variable.ifNext("$[")) {
if(variable.ifNext("!")) {
return variables[renderVariableName(variable)];
} if(variable.ifNext(":")) {
// TODO: should only allow CGI variable name. Allow variable substitution.
} if(variable.ifNext("@")) {
// TODO: should only allow environment variables. Allow substitution.
} else {
return variables[renderVariableName(variable)];
}
throw coreutils::Exception("expected variable name or type designator.");
} if(variable.ifNext("#[")) {
// TODO: this local variable type has no special naming conventions. We
// just need to do nested variable substitution for the entry.
}
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
}
coreutils::MString Global::renderVariableName(coreutils::ZString &variable) {
coreutils::MString name = variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext("]"))
return name;
}
}

View File

@ -18,10 +18,13 @@ namespace jet {
bool sessionExists(coreutils::MString sessionId);
void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId);
void getVariable(ZString variable);
coreutils::ZString getVariable(coreutils::ZString &variable);
std::map<coreutils::MString, coreutils::MString> variables;
std::map<coreutils::MString, __mysql *> sessions;
private:
coreutils::MString renderVariableName(coreutils::ZString &variable);
};

View File

@ -147,12 +147,8 @@ namespace jet {
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 if(in.startsWith("$[")) {
out.write(global.getVariable(in));
} else {
out.write(in.charAt(0));
in.nextChar();

View File

@ -6,7 +6,7 @@
int main(int argc, char **argv) {
coreutils::ZString data("<jet name=\"localname\" filterblanklines=\"true\">\n"
coreutils::ZString data2("<jet name1=\"localname\" filterblanklines=\"true\">\n"
" <comment>This is a comment and should not show up in the output.</comment>\n"
" <html>\n"
" <set name=\"ix\" value=\"1\" />\n"
@ -20,7 +20,7 @@ int main(int argc, char **argv) {
" <set name=\"newname\" scope=\"global\">another container value</set>\n"
" >>>$[noeval]<<<\n"
" >>>$[thename]<<<\n"
" local: >>>#[name]<<<\n"
" local: >>>#[name$[ix]]<<<\n"
" <mysql key=\"uu\">\n"
" <if value1=\"X\" value2=\"Y\" type=\"ne\">\n"
" 789\n"
@ -41,12 +41,9 @@ int main(int argc, char **argv) {
" </html>\n"
"</jet>\n");
coreutils::ZString data2("<jet>\n"
" <if expr=\"false\">\n"
" XXX\n"
" <else>\n"
" YYY\n"
" </if>\n"
coreutils::ZString data("<jet>\n"
" <set name=\"test\" value=\"0123456789\" />\n"
" $[test]\n"
"</jet>\n");
// coreutils::ZString data("<jet>ABC<jet>HIJ</jet>XYZ</jet>\n");