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

@ -31,4 +31,30 @@ namespace jet {
sessions.erase(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,11 +18,14 @@ namespace jet {
bool sessionExists(coreutils::MString sessionId); bool sessionExists(coreutils::MString sessionId);
void addSession(coreutils::MString sessionId, __mysql *mysql); void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId); 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, coreutils::MString> variables;
std::map<coreutils::MString, __mysql *> sessions; 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]); out.write(variables[varName]);
else else
throw coreutils::Exception("Local variable name has a syntax error."); throw coreutils::Exception("Local variable name has a syntax error.");
} else if(in.ifNext("$[")) { } else if(in.startsWith("$[")) {
coreutils::MString varName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); out.write(global.getVariable(in));
if(in.ifNext("]"))
out.write(global.variables[varName]);
else
throw coreutils::Exception("Global variable name has a syntax error.");
} else { } else {
out.write(in.charAt(0)); out.write(in.charAt(0));
in.nextChar(); in.nextChar();

View File

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