From 10f52c80acfc00e854f71274de9b715d785d32cf Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 26 Oct 2006 23:33:33 +0000 Subject: [PATCH] improved handling of aliasses and warnings git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@51 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/stylesheet.cpp | 1 + src/script/scriptable.cpp | 12 ++++++++++-- src/script/scriptable.hpp | 7 +++++-- src/util/io/reader.cpp | 25 +++++++++++++++++++------ src/util/io/reader.hpp | 8 +++++++- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/data/stylesheet.cpp b/src/data/stylesheet.cpp index a516a828..d5c27039 100644 --- a/src/data/stylesheet.cpp +++ b/src/data/stylesheet.cpp @@ -40,6 +40,7 @@ IMPLEMENT_REFLECTION(StyleSheet) { tag.addAlias(300, _("dpi"), _("card_dpi")); tag.addAlias(300, _("background"), _("card_background")); tag.addAlias(300, _("info_style"), _("set_info_style")); + tag.addAlias(300, _("align"), _("alignment")); REFLECT(game); REFLECT(full_name); diff --git a/src/script/scriptable.cpp b/src/script/scriptable.cpp index afe4a6e5..74b413dc 100644 --- a/src/script/scriptable.cpp +++ b/src/script/scriptable.cpp @@ -32,12 +32,20 @@ ScriptValueP OptionalScript::invoke(Context& ctx) const { } } +void OptionalScript::parse(Reader& reader) { + try { + script = ::parse(unparsed); + } catch (const ParseError& e) { + reader.warning(e.what()); + } +} + + // custom reflection, different for each type template <> void Reader::handle(OptionalScript& os) { handle(os.unparsed); - // read the script - os.script = parse(os.unparsed); + os.parse(*this); } template <> void Writer::handle(const OptionalScript& os) { diff --git a/src/script/scriptable.hpp b/src/script/scriptable.hpp index 39230d3d..fdbdfb1a 100644 --- a/src/script/scriptable.hpp +++ b/src/script/scriptable.hpp @@ -55,10 +55,12 @@ class OptionalScript { } return false; } - + private: ScriptP script; ///< The script, may be null if there is no script String unparsed; ///< Unparsed script, for writing back to a file + // parse the unparsed string, while reading + void parse(Reader&); DECLARE_REFLECTION(); template friend class Scriptable; }; @@ -95,7 +97,8 @@ template void Reader::handle(Scriptable& s) { handle(s.script.unparsed); if (starts_with(s.script.unparsed, _("script:"))) { - s.script.script = parse(s.script.unparsed); + s.script.unparsed = s.script.unparsed.substr(7); + s.script.parse(*this); } else { handle(value); } diff --git a/src/util/io/reader.cpp b/src/util/io/reader.cpp index 40507d37..667b2d1f 100644 --- a/src/util/io/reader.cpp +++ b/src/util/io/reader.cpp @@ -52,7 +52,14 @@ void Reader::handleAppVersion() { } void Reader::warning(const String& msg) { - wxMessageBox((msg + _("\nOn line: ")) << line_number << _("\nIn file: ") << filename, _("Warning"), wxOK | wxICON_EXCLAMATION); + warnings += String(_("\nOn line ")) << line_number << _(": \t") << msg; +} + +void Reader::showWarnings() { + if (!warnings.empty()) { + wxMessageBox(_("Warnings while reading file:\n") + filename + _("\n") + warnings, _("Warning"), wxOK | wxICON_EXCLAMATION); + warnings.clear(); + } } bool Reader::enterBlock(const Char* name) { @@ -118,14 +125,20 @@ void Reader::readLine() { } key = cannocial_name_form(trim(line.substr(indent, pos - indent))); value = pos == String::npos ? _("") : trim_left(line.substr(pos+1)); - // aliasses? - map::const_iterator it = aliasses.find(key); - if (it != aliasses.end()) { - key = it->second; - } } void Reader::unknownKey() { + // aliasses? + map::const_iterator it = aliasses.find(key); + if (it != aliasses.end()) { + if (aliasses.find(it->second) != aliasses.end()) { + // alias points to another alias, don't follow it, there is the risk of infinite loops + } else { + // try this key instead + key = it->second; + return; + } + } if (indent == expected_indent) { warning(_("Unexpected key: '") + key + _("'")); do { diff --git a/src/util/io/reader.hpp b/src/util/io/reader.hpp index 97e0f2e5..94c0d5d7 100644 --- a/src/util/io/reader.hpp +++ b/src/util/io/reader.hpp @@ -42,6 +42,8 @@ class Reader { /** Used for "include file" keys. */ Reader(const String& filename); + ~Reader() { showWarnings(); } + /// Tell the reflection code we are reading inline bool reading() const { return true; } /// Is the thing currently being read 'complex', i.e. does it have children @@ -52,8 +54,10 @@ class Reader { /// Read and check the application version void handleAppVersion(); - /// Show a warning message, but continue reading + /// Add a warning message, but continue reading void warning(const String& msg); + /// Show all warning messages, but continue reading + void showWarnings(); // --------------------------------------------------- : Handling objects /// Handle an object: read it if it's name matches @@ -111,6 +115,8 @@ class Reader { InputStreamP input; /// Text stream wrapping the input stream wxTextInputStream stream; + /// Accumulated warning messages + String warnings; // --------------------------------------------------- : Reading the stream