1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 int signature_match(char s, char a)
15 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
20 int signature_match(const string &st_sig, const string &act_sig)
24 else if(act_sig.size()==2 && act_sig[1]=='*')
27 for(string::const_iterator i=st_sig.begin(); (i!=st_sig.end() && match); ++i)
28 match = min(match, signature_match(*i, act_sig[0]));
32 else if(st_sig.size()==act_sig.size())
35 for(unsigned i=0; (i<st_sig.size() && match); ++i)
36 match = min(match, signature_match(st_sig[i], act_sig[i]));
38 return match ? match+1 : 0;
54 check_sub_loads(false)
59 for(ActionMap::iterator i=local_actions.begin(); i!=local_actions.end(); ++i)
63 void Loader::load(Parser &p)
66 throw logic_error("no actions");
72 else if(p) // Peek may have processed an __end, so recheck goodness
74 // Parse in raw mode so we can peek immediately after a mode change
75 Statement st = p.parse(true);
76 if(st.valid && !st.control)
83 void Loader::load(const Statement &st)
86 throw logic_error("no actions");
88 for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
93 void Loader::load_direct(Parser &p, unsigned l)
95 SetForScope<Parser *> set_parser(cur_parser, &p);
96 SetForScope<unsigned> set_level(cur_level, l);
100 const StatementKey *key = p.peek(l);
104 LoaderAction *act = find_action(*key);
107 SetFlag set_direct(direct);
108 if(!p.parse_and_load(l, *this, *act))
109 throw logic_error("direct load failed");
112 load_statement(p.parse());
116 void Loader::load_statement(const Statement &st)
118 SetForScope<const Statement *> set_cst(cur_st, &st);
122 StatementKey key(st.keyword, st.get_signature());
124 if(!aux_loaders.empty() && !has_action(key))
126 for(vector<Loader *>::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i)
127 if((*i)->has_action(key))
128 return (*i)->load_statement(st);
131 LoaderAction *act = find_action(key);
135 act->execute(*this, st);
136 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
137 throw logic_error("substatements ignored");
140 catch(const data_error &)
144 catch(const exception &e)
146 throw data_error(st.source, st.line, e);
150 void Loader::load_sub_with(Loader &ldr)
154 ldr.load_direct(*cur_parser, cur_level+1);
163 throw logic_error("no current statement");
166 void Loader::set_actions(ActionMap &a)
169 throw logic_error("actions already set");
176 void Loader::add(const string &kwd, LoaderAction *act)
179 actions = &local_actions;
181 StatementKey key(kwd, (act ? act->get_signature() : "*"));
182 ActionMap::iterator i = actions->find(key);
183 if(i!=actions->end())
189 (*actions)[key] = act;
192 void Loader::add_auxiliary_loader(Loader &ldr)
194 aux_loaders.push_back(&ldr);
197 bool Loader::has_action(const StatementKey &key) const
202 ActionMap::const_iterator i = actions->lower_bound(StatementKey(key.keyword, string()));
203 for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i)
204 if(signature_match(key.signature, i->first.signature))
209 LoaderAction *Loader::find_action(const StatementKey &key) const
214 ActionMap::const_iterator begin = actions->lower_bound(StatementKey(key.keyword, string()));
215 ActionMap::const_iterator end = actions->upper_bound(StatementKey(key.keyword, "~"));
218 throw unknown_keyword(key.keyword);
220 LoaderAction *act = 0;
222 for(ActionMap::const_iterator i=begin; i!=end; ++i)
224 int m = signature_match(key.signature, i->first.signature);
233 throw invalid_signature(key.keyword, key.signature);
238 const string &Loader::get_source() const
241 throw logic_error("no current statement");
242 return cur_st->source;
245 const string &Loader::get_keyword() const
248 throw logic_error("no current statement");
249 return cur_st->keyword;
252 } // namespace DataFile