1 #include <msp/strings/format.h>
9 bool signature_match(char s, char a)
13 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
18 bool signature_match(const string &st_sig, const string &act_sig)
22 else if(act_sig.size()==2 && act_sig[1]=='*')
24 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
30 else if(st_sig.size()==act_sig.size())
32 for(unsigned i=0; i<st_sig.size(); ++i)
33 if(!signature_match(st_sig[i], act_sig[i]))
49 allow_pointer_reload(true),
50 check_sub_loads(false)
55 for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
59 void Loader::load(Parser &p)
63 Statement st = p.parse();
70 void Loader::load(const Statement &st)
72 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
77 void Loader::load_statement(const Statement &st)
83 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
87 act->execute(*this, st);
88 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
89 throw Exception("Substatements were not loaded");
96 e.at(st.get_location());
104 void Loader::load_sub_with(Loader &ldr)
107 throw InvalidState("load_sub called without current statement");
113 void Loader::add(const string &kwd, LoaderAction *act)
115 ActionKey key(kwd, (act ? act->get_signature() : "*"));
116 ActionMap::iterator i = actions.find(key);
126 LoaderAction *Loader::find_action(const ActionKey &key) const
128 ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
129 ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
132 throw KeyError("Unknown keyword", key.keyword);
134 for(ActionMap::const_iterator i=begin; i!=end; ++i)
135 if(signature_match(key.signature, i->first.signature))
138 throw KeyError(format("Keyword '%s' does not accept signature '%s'", key.keyword, key.signature));
142 Loader::ActionKey::ActionKey(const string &k, const string &s):
147 bool Loader::ActionKey::operator<(const ActionKey &other) const
149 if(keyword!=other.keyword)
150 return keyword<other.keyword;
151 return signature<other.signature;
154 } // namespace DataFile