diff --git a/doomsday/libdeng2/include/de/ScriptedInfo b/doomsday/libdeng2/include/de/ScriptedInfo new file mode 100644 index 0000000000..2dacab2715 --- /dev/null +++ b/doomsday/libdeng2/include/de/ScriptedInfo @@ -0,0 +1 @@ +#include "scriptsys/scriptedinfo.h" diff --git a/doomsday/libdeng2/include/de/scriptsys/scriptedinfo.h b/doomsday/libdeng2/include/de/scriptsys/scriptedinfo.h new file mode 100644 index 0000000000..06e44daa90 --- /dev/null +++ b/doomsday/libdeng2/include/de/scriptsys/scriptedinfo.h @@ -0,0 +1,89 @@ +/** @file scriptedinfo.h Info document tree with script context. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#ifndef LIBDENG2_SCRIPTEDINFO_H +#define LIBDENG2_SCRIPTEDINFO_H + +#include "../Info" +#include "../File" + +namespace de { + +/** + * Info document tree with a script context and built-in support for handling + * expressions and embedded scripts. + * + * Analogous to an HTML document with embedded JavaScript: Info acts as the + * declarative, structured document and Doomsday Script is the procedural + * programming language. + * + * An instance of ScriptedInfo represents an Info document. It has its own + * private script execution context, in which expressions can be evaluated and + * scripts run. After a ScriptedInfo has been parsed, all the embedded scripts + * are run and the Info elements become variables and values in the local + * namespace. + * + * Each block of a ScriptedInfo document has a couple of special elements + * that alter how the block is processed: + * + * - The "condition" element that may be present in any block determines + * whether the block is processed or skipped. The value of the "condition" + * element is evaluated as a script, and if it evaluates to False, the + * entire block is ignored. + * + * - The contents of any previously processed block (or any record available in + * the namespace) can be copied with the special inheritance element + * (named ":"): + *
type1 first-block { key = value }
+ * type2 example-block : first-block {}
+ * + * Here @c first-block would be treated as a variable name in the document's + * local namespace, referring to the block above, which has already been + * added to the local namespace (elements are processed sequentially from + * beginning to end). + */ +class DENG2_PUBLIC ScriptedInfo +{ +public: + ScriptedInfo(); + + void clear(); + + void parse(String const &source); + + void parse(File const &file); + + /** + * Evaluates one or more statements and returns the result. All processing + * is done in the document's local script context. Changes to the context's + * global namespace are allowed and will remain in effect after the + * evaluation finishes. + * + * @param source Statements or expression to evaluate. + * + * @return Result value. Caller gets ownership. + */ + Value *evaluate(String const &source); + +private: + DENG2_PRIVATE(d) +}; + +} // namespace de + +#endif // LIBDENG2_SCRIPTEDINFO_H diff --git a/doomsday/libdeng2/scriptsys.pri b/doomsday/libdeng2/scriptsys.pri index c3c5beab2d..2a617c5bb9 100644 --- a/doomsday/libdeng2/scriptsys.pri +++ b/doomsday/libdeng2/scriptsys.pri @@ -27,6 +27,7 @@ HEADERS += \ include/de/PrintStatement \ include/de/Process \ include/de/Script \ + include/de/ScriptedInfo \ include/de/ScriptLex \ include/de/ScriptSystem \ include/de/Statement \ @@ -64,6 +65,7 @@ HEADERS += \ include/de/scriptsys/printstatement.h \ include/de/scriptsys/process.h \ include/de/scriptsys/script.h \ + include/de/scriptsys/scriptedinfo.h \ include/de/scriptsys/scriptlex.h \ include/de/scriptsys/scriptsystem.h \ include/de/scriptsys/statement.h \ @@ -100,12 +102,11 @@ SOURCES += \ src/scriptsys/printstatement.cpp \ src/scriptsys/process.cpp \ src/scriptsys/script.cpp \ + src/scriptsys/scriptedinfo.cpp \ src/scriptsys/scriptlex.cpp \ + src/scriptsys/scriptsystem.cpp \ src/scriptsys/statement.cpp \ src/scriptsys/tokenbuffer.cpp \ src/scriptsys/tokenrange.cpp \ src/scriptsys/trystatement.cpp \ src/scriptsys/whilestatement.cpp - -SOURCES += \ - src/scriptsys/scriptsystem.cpp diff --git a/doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp b/doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp new file mode 100644 index 0000000000..eed7583b38 --- /dev/null +++ b/doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp @@ -0,0 +1,206 @@ +/** @file scriptedinfo.cpp Info document tree with script context. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "de/ScriptedInfo" +#include "de/Script" +#include "de/Process" +#include "de/ArrayValue" +#include "de/RecordValue" + +namespace de { + +DENG2_PIMPL(ScriptedInfo) +{ + Info info; ///< Original full parsed contents. + QScopedPointer