diff options
author | deva <deva> | 2010-08-12 10:57:04 +0000 |
---|---|---|
committer | deva <deva> | 2010-08-12 10:57:04 +0000 |
commit | d9338083192084613e5530b02710b796252d342b (patch) | |
tree | e0ec2b36e0de62328e5fd5d3b597f6ee71d1b18f /server/src | |
parent | dbab8458dcce186e7eb7a114a83f759d7db5445a (diff) |
New scripting system part2.
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/luaresume.cc | 8 | ||||
-rw-r--r-- | server/src/luaresume.h | 2 | ||||
-rw-r--r-- | server/src/macroparser.cc | 65 | ||||
-rw-r--r-- | server/src/macroparser.h | 3 | ||||
-rw-r--r-- | server/src/resumeparser.cc | 122 | ||||
-rw-r--r-- | server/src/template.h | 2 |
6 files changed, 82 insertions, 120 deletions
diff --git a/server/src/luaresume.cc b/server/src/luaresume.cc index eb6d90b..a74f1f5 100644 --- a/server/src/luaresume.cc +++ b/server/src/luaresume.cc @@ -34,7 +34,7 @@ #define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" -static int _getValue(lua_State *L) +static int _value(lua_State *L) { Pracro::checkParameters(L, Pracro::T_STRING, @@ -51,7 +51,7 @@ static int _getValue(lua_State *L) return 1; } - std::string value = lua->getValue(name); + std::string value = lua->value(name); lua_pushstring(L, value.c_str()); return 1; @@ -71,7 +71,7 @@ LUAResume::LUAResume(Commit &c) lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - lua_register(L, "getValue", _getValue); + lua_register(L, "value", _value); } LUAResume::~LUAResume() @@ -79,7 +79,7 @@ LUAResume::~LUAResume() lua_close(L); } -std::string LUAResume::getValue(std::string name) +std::string LUAResume::value(std::string name) { if(commit.fields.find(name) == commit.fields.end()) { ERR(luaresume, "LUAResume: No such field '%s'\n", name.c_str()); diff --git a/server/src/luaresume.h b/server/src/luaresume.h index 5a1896e..1132f26 100644 --- a/server/src/luaresume.h +++ b/server/src/luaresume.h @@ -40,7 +40,7 @@ public: std::string run(std::string program); - std::string getValue(std::string name); + std::string value(std::string name); void error(std::string message); diff --git a/server/src/macroparser.cc b/server/src/macroparser.cc index fe7f6e3..2bd482e 100644 --- a/server/src/macroparser.cc +++ b/server/src/macroparser.cc @@ -84,6 +84,7 @@ MacroParser::MacroParser(std::string macrofile) m = NULL; current_map = NULL; current_script = NULL; + current_resume_script = NULL; file = macrofile; @@ -101,9 +102,9 @@ MacroParser::~MacroParser() void MacroParser::characterData(std::string &data) { - if(state == RESUME) { - assert(m); // No macro present! - m->resume.attributes["format"].append(data); + if(state == RESUME_SCRIPT) { + assert(current_resume_script); // No macro present! + current_resume_script->code.append(data); } if(state == MAP) { @@ -117,7 +118,8 @@ void MacroParser::characterData(std::string &data) } } -void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes) +void MacroParser::startTag(std::string name, + std::map< std::string, std::string> attributes) { // Create macro and enable parsing of queries, maps and widgets if(name == "macro") { @@ -203,18 +205,36 @@ void MacroParser::startTag(std::string name, std::map< std::string, std::string> return; } - // Create Query + // Create script if(name == "script") { - if(state != SCRIPTS) error("script found outside scripts."); - state = SCRIPT; - - assert(m); // No macro is currently available, cannot create map! - - Script s; - s.attributes = attributes; - m->scripts.push_back(s); - current_script = &(m->scripts.back()); + assert(m); // No macro is currently available, cannot create script! + + switch(state) { + case SCRIPTS: + { + state = SCRIPT; + + Script s; + s.attributes = attributes; + m->scripts.push_back(s); + current_script = &(m->scripts.back()); + } + break; + case RESUME: + { + state = RESUME_SCRIPT; + + Script s; + s.attributes = attributes; + m->resume_scripts.push_back(s); + current_resume_script = &(m->resume_scripts.back()); + } + break; + default: + error("<script> tag found outside <scripts> or <resume> tags."); + break; + } return; } @@ -278,8 +298,21 @@ void MacroParser::endTag(std::string name) } if(name == "scripts") state = MACRO; if(name == "script") { - current_script = NULL; - state = SCRIPTS; + switch(state) { + case SCRIPT: + current_script = NULL; + state = SCRIPTS; + break; + + case RESUME_SCRIPT: + current_resume_script = NULL; + state = RESUME; + break; + + default: + // tag mismatch? + break; + } } if(name == "widgets") state = MACRO; diff --git a/server/src/macroparser.h b/server/src/macroparser.h index 86a1487..eaa5638 100644 --- a/server/src/macroparser.h +++ b/server/src/macroparser.h @@ -35,13 +35,13 @@ class MacroParser : public SAXParser { UNDEFINED, MACRO, RESUME, + RESUME_SCRIPT, QUERIES, QUERY, MAPS, MAP, WIDGETS, SCRIPTS, - SCRIPT_INCLUDE, SCRIPT } ParserState; @@ -75,6 +75,7 @@ private: Macro *m; Map *current_map; Script *current_script; + Script *current_resume_script; std::vector< Widget* > widgetstack; // Error callback function. diff --git a/server/src/resumeparser.cc b/server/src/resumeparser.cc index 37b36f4..c80c2f0 100644 --- a/server/src/resumeparser.cc +++ b/server/src/resumeparser.cc @@ -31,110 +31,36 @@ #include "luaresume.h" #include "configuration.h" -static std::string resume_parser_format(Resume &r, Commit &commit) +std::string resume_parser(Macro ¯o, Commit &commit) { - const char* format = r.attributes["format"].c_str(); - - std::string resume; - std::string var; + LUAResume luaresume(commit); - const char *p = format; - const char *theend = p + strlen(format); - while(p < theend) { - switch(*p) { - case '$': - p++; - switch(*p) { - case '$': - resume.append(1, *p); - break; + std::string code; - case '{': - p++; - var = ""; - // Parser - while(p < theend && *p != '}') { - var.append(1, *p); - p++; + std::vector< Script >::iterator spi = macro.resume_scripts.begin(); + while(spi != macro.resume_scripts.end()) { + if(spi->attributes.find("src") != spi->attributes.end()) { + std::string src = spi->attributes["src"]; + std::string file = + Conf::xml_basedir + "/include/" + src; + FILE *fp = fopen(file.c_str(), "r"); + if(fp) { + char buf[64]; + size_t sz; + std::string inc; + while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) { + inc.append(buf, sz); } - // p++; - //printf("[%s]\n", var.c_str()); - // resume += "var(" + var + ")"; - resume += commit.fields[var]; - break; - - default: - resume.append(1, *p); - // printf("Illigal $ command\n"); - break; - } - p++; - break; - - case '\\': - p++; - switch(*p) { - case 't': - resume.append(1, '\t'); - break; - case 'n': - resume.append(1, '\n'); - break; - case 'r': - resume.append(1, '\r'); - break; - case '\\': - default: - resume.append(1, *p); - break; + fclose(fp); + code += "\n-- BEGIN INCLUDE: '" + src + "'\n"; + code += inc; + code += "\n-- END INCLUDE: '" + src + "'\n"; } - p++; - break; - - default: - resume.append(1, *p); - p++; - break; + } else { + code += spi->code; } + spi++; } - return resume; -} - -static std::string resume_parser_lua(Macro ¯o, Commit &commit) -{ - LUAResume luaresume(commit); - std::string lua; - /* - std::vector< ScriptInclude >::iterator spii = - macro.script_includes.begin(); - while(spii != macro.script_includes.end()) { - std::string file = - Conf::xml_basedir + "/include/" + spii->attributes["file"]; - FILE *fp = fopen(file.c_str(), "r"); - if(fp) { - char buf[64]; - size_t sz; - std::string inc; - while((sz = fread(buf, 1, sizeof(buf), fp)) != 0) { - lua.append(buf, sz); - } - fclose(fp); - } - spii++; - } - */ - lua += macro.resume.attributes["format"]; - - return luaresume.run(lua); -} - -std::string resume_parser(Macro ¯o, Commit &commit) -{ - if(macro.resume.attributes["language"] == "lua") { - return resume_parser_lua(macro, commit); - } - - // Default to pracro format language. - return resume_parser_format(macro.resume, commit); + return luaresume.run(code); } diff --git a/server/src/template.h b/server/src/template.h index 1ca69ff..479e6ee 100644 --- a/server/src/template.h +++ b/server/src/template.h @@ -42,6 +42,7 @@ public: class Script { public: attr_t attributes; + std::string code; }; class Map { @@ -65,6 +66,7 @@ public: std::vector< Query > queries; maps_t maps; std::vector< Script > scripts; + std::vector< Script > resume_scripts; Widget widgets; attr_t attributes; Resume resume; |