From 0379974d5eed79e0544b068d379ae1ef212450e8 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Thu, 19 Sep 2024 14:40:07 -0700 Subject: [PATCH] more work on variables. --- Global.cpp | 26 ++++++++++++++++++++++++++ Global.h | 5 ++++- Tag.cpp | 8 ++------ testjet.cpp | 13 +++++-------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Global.cpp b/Global.cpp index a1c4112..d8c8160 100644 --- a/Global.cpp +++ b/Global.cpp @@ -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; + } } diff --git a/Global.h b/Global.h index fb444f3..d89ec15 100644 --- a/Global.h +++ b/Global.h @@ -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 variables; std::map sessions; + + private: + coreutils::MString renderVariableName(coreutils::ZString &variable); }; diff --git a/Tag.cpp b/Tag.cpp index 633589f..6dd662d 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -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(); diff --git a/testjet.cpp b/testjet.cpp index 368c36e..2d258b9 100644 --- a/testjet.cpp +++ b/testjet.cpp @@ -6,7 +6,7 @@ int main(int argc, char **argv) { - coreutils::ZString data("\n" + coreutils::ZString data2("\n" " This is a comment and should not show up in the output.\n" " \n" " \n" @@ -20,7 +20,7 @@ int main(int argc, char **argv) { " another container value\n" " >>>$[noeval]<<<\n" " >>>$[thename]<<<\n" - " local: >>>#[name]<<<\n" + " local: >>>#[name$[ix]]<<<\n" " \n" " \n" " 789\n" @@ -41,12 +41,9 @@ int main(int argc, char **argv) { " \n" "\n"); - coreutils::ZString data2("\n" - " \n" - " XXX\n" - " \n" - " YYY\n" - " \n" + coreutils::ZString data("\n" + " \n" + " $[test]\n" "\n"); // coreutils::ZString data("ABCHIJXYZ\n");