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(auto 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)
57 void Loader::load(Parser &p)
60 throw logic_error("no actions");
66 else if(p) // Peek may have processed an __end, so recheck goodness
68 // Parse in raw mode so we can peek immediately after a mode change
69 Statement st = p.parse(true);
70 if(st.valid && !st.control)
77 void Loader::load(const Statement &st)
80 throw logic_error("no actions");
82 for(const Statement &s: st.sub)
87 void Loader::load_direct(Parser &p, unsigned l)
89 SetForScope<Parser *> set_parser(cur_parser, &p);
90 SetForScope<unsigned> set_level(cur_level, l);
94 const StatementKey *key = p.peek(l);
98 LoaderAction *act = find_action(*key);
101 SetFlag set_direct(direct);
102 if(!p.parse_and_load(l, *this, *act))
103 throw logic_error("direct load failed");
106 load_statement(p.parse());
110 void Loader::load_statement(const Statement &st)
112 SetForScope<const Statement *> set_cst(cur_st, &st);
116 StatementKey key(st.keyword, st.get_signature());
118 if(!aux_loaders.empty() && !has_action(key))
120 for(Loader *l: aux_loaders)
121 if(l->has_action(key))
122 return l->load_statement(st);
125 LoaderAction *act = find_action(key);
129 act->execute(*this, st);
130 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
131 throw logic_error("substatements ignored");
134 catch(const data_error &e)
136 if(e.get_source()!=st.source)
137 throw data_error(st.source, st.line, e);
141 catch(const exception &e)
143 throw data_error(st.source, st.line, e);
147 void Loader::load_sub_with(Loader &ldr)
151 ldr.load_direct(*cur_parser, cur_level+1);
160 throw logic_error("no current statement");
163 void Loader::set_actions(ActionMap &a)
166 throw logic_error("actions already set");
173 void Loader::add(const string &kwd, LoaderAction *act)
176 actions = &local_actions;
178 StatementKey key(kwd, (act ? act->get_signature() : "*"));
179 ActionMap::iterator i = actions->find(key);
180 if(i!=actions->end())
186 (*actions)[key] = act;
189 void Loader::add_auxiliary_loader(Loader &ldr)
191 aux_loaders.push_back(&ldr);
194 bool Loader::has_action(const StatementKey &key) const
199 auto i = actions->lower_bound(StatementKey(key.keyword, string()));
200 for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i)
201 if(signature_match(key.signature, i->first.signature))
206 LoaderAction *Loader::find_action(const StatementKey &key) const
211 auto begin = actions->lower_bound(StatementKey(key.keyword, string()));
212 auto end = actions->upper_bound(StatementKey(key.keyword, "~"));
215 throw unknown_keyword(key.keyword);
217 LoaderAction *act = 0;
219 for(auto i=begin; i!=end; ++i)
221 int m = signature_match(key.signature, i->first.signature);
230 throw invalid_signature(key.keyword, key.signature);
235 const string &Loader::get_source() const
238 throw logic_error("no current statement");
239 return cur_st->source;
242 const string &Loader::get_keyword() const
245 throw logic_error("no current statement");
246 return cur_st->keyword;
250 Loader::ActionMap::~ActionMap()
252 for(const auto &kvp: *this)
256 } // namespace DataFile