diff --git a/Global.cpp b/Global.cpp index f1c3c62..3cc227e 100644 --- a/Global.cpp +++ b/Global.cpp @@ -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 &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 &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."); diff --git a/Global.h b/Global.h index bad9292..4b03cca 100644 --- a/Global.h +++ b/Global.h @@ -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 &variables); + void renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map &variables); __mysql * getSession(coreutils::MString sessionId); coreutils::ZString getSessionVariable(coreutils::MString &splitName); void outputHeaders(); diff --git a/KeywordValue.cpp b/KeywordValue.cpp index c4c21f5..eebf8d7 100644 --- a/KeywordValue.cpp +++ b/KeywordValue.cpp @@ -3,10 +3,10 @@ namespace jet { - KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() { + KeywordValue::KeywordValue(coreutils::ZString data, Global &global, std::map &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(); diff --git a/KeywordValue.h b/KeywordValue.h index 3b98484..0fb0d71 100644 --- a/KeywordValue.h +++ b/KeywordValue.h @@ -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 &variables); virtual ~KeywordValue(); }; diff --git a/Tag.cpp b/Tag.cpp index 923a588..296ed1f 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -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(); diff --git a/jet-2.0 b/jet-2.0 index 6e6b7e6..b1ecff4 100755 Binary files a/jet-2.0 and b/jet-2.0 differ diff --git a/testjet.jet b/testjet.jet index e9b98d0..f1c97cf 100755 --- a/testjet.jet +++ b/testjet.jet @@ -5,9 +5,9 @@ --- - $[modified1;TOHEX] + $[modified1] - $[modified1;$[tohex]] + $[modified1] --- $[nonexistant] $[$HOME] diff --git a/testvar.jet b/testvar.jet new file mode 100755 index 0000000..69d8a7e --- /dev/null +++ b/testvar.jet @@ -0,0 +1,16 @@ +#!./jet-2.0 + + + + + + + +$[$[ix]var$[ix]] +$[var$[ix]] +$[var$[ix]var] +$[var$[i$[letterx]]var] +$[letterx;TOHEX] +$[var$[i$[letterx]]$[ix]] +$[var$[i$[letterx]]$[i$[letterx]]] +