1 #include <msp/strings/format.h>
16 Set(T &r, const T &v): ref(r), orig(r) { r = v; }
17 ~Set() { ref = orig; }
20 bool signature_match(char s, char a)
24 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
29 bool signature_match(const string &st_sig, const string &act_sig)
33 else if(act_sig.size()==2 && act_sig[1]=='*')
35 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
41 else if(st_sig.size()==act_sig.size())
43 for(unsigned i=0; i<st_sig.size(); ++i)
44 if(!signature_match(st_sig[i], act_sig[i]))
59 class unknown_keyword: public runtime_error
62 unknown_keyword(const std::string &k):
66 virtual ~unknown_keyword() throw() { }
70 class invalid_signature: public runtime_error
73 invalid_signature(const std::string &k, const std::string & s):
74 runtime_error(format("%s %s", k, s))
77 virtual ~invalid_signature() throw() { }
83 check_sub_loads(false)
88 for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
92 void Loader::load(Parser &p)
96 Statement st = p.parse();
103 void Loader::load(const Statement &st)
105 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
110 void Loader::load_statement(const Statement &st)
112 Set<const Statement *> set_cst(cur_st, &st);
116 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
120 act->execute(*this, st);
121 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
122 throw logic_error("substatements ignored");
125 catch(const data_error &)
129 catch(const exception &e)
131 throw data_error(st.source, st.line, e);
135 void Loader::load_sub_with(Loader &ldr)
138 throw logic_error("no current statement");
144 void Loader::add(const string &kwd, LoaderAction *act)
146 ActionKey key(kwd, (act ? act->get_signature() : "*"));
147 ActionMap::iterator i = actions.find(key);
157 LoaderAction *Loader::find_action(const ActionKey &key) const
159 ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
160 ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
163 throw unknown_keyword(key.keyword);
165 for(ActionMap::const_iterator i=begin; i!=end; ++i)
166 if(signature_match(key.signature, i->first.signature))
169 throw invalid_signature(key.keyword, key.signature);
172 const string &Loader::get_source() const
175 throw logic_error("no current statement");
176 return cur_st->source;
180 Loader::ActionKey::ActionKey(const string &k, const string &s):
185 bool Loader::ActionKey::operator<(const ActionKey &other) const
187 if(keyword!=other.keyword)
188 return keyword<other.keyword;
189 return signature<other.signature;
192 } // namespace DataFile