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;
50 void Loader::load(Parser &p)
53 throw logic_error("no actions");
59 else if(p) // Peek may have processed an __end, so recheck goodness
61 // Parse in raw mode so we can peek immediately after a mode change
62 Statement st = p.parse(true);
63 if(st.valid && !st.control)
68 for(Loader *l: aux_loaders)
72 void Loader::load(const Statement &st)
75 throw logic_error("no actions");
77 for(const Statement &s: st.sub)
80 for(Loader *l: aux_loaders)
84 void Loader::load_direct(Parser &p, unsigned l)
86 SetForScope<Parser *> set_parser(cur_parser, &p);
87 SetForScope<unsigned> set_level(cur_level, l);
91 const StatementKey *key = p.peek(l);
95 LoaderAction *act = find_action(*key);
98 SetFlag set_direct(direct);
99 if(!p.parse_and_load(l, *this, *act))
100 throw logic_error("direct load failed");
103 load_statement(p.parse());
107 void Loader::load_statement(const Statement &st)
109 SetForScope<const Statement *> set_cst(cur_st, &st);
113 StatementKey key(st.keyword, st.get_signature());
115 if(!aux_loaders.empty() && !has_action(key))
117 for(Loader *l: aux_loaders)
118 if(l->has_action(key))
119 return l->load_statement(st);
122 LoaderAction *act = find_action(key);
126 act->execute(*this, st);
127 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
128 throw logic_error("substatements ignored");
131 catch(const data_error &e)
133 if(e.get_source()!=st.source)
134 throw data_error(st.source, st.line, e);
138 catch(const exception &e)
140 throw data_error(st.source, st.line, e);
144 void Loader::load_sub_with(Loader &ldr)
148 ldr.load_direct(*cur_parser, cur_level+1);
150 for(Loader *l: ldr.aux_loaders)
159 throw logic_error("no current statement");
162 void Loader::set_actions(ActionMap &a)
165 throw logic_error("actions already set");
172 void Loader::add(const string &kwd, LoaderAction *act)
175 actions = &local_actions;
177 StatementKey key(kwd, (act ? act->get_signature() : "*"));
178 ActionMap::iterator i = actions->find(key);
179 if(i!=actions->end())
185 (*actions)[key] = act;
188 void Loader::add_auxiliary_loader(Loader &ldr)
190 aux_loaders.push_back(&ldr);
193 bool Loader::has_action(const StatementKey &key) const
198 auto i = actions->lower_bound(StatementKey(key.keyword, string()));
199 for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i)
200 if(signature_match(key.signature, i->first.signature))
205 LoaderAction *Loader::find_action(const StatementKey &key) const
210 auto begin = actions->lower_bound(StatementKey(key.keyword, string()));
211 auto end = actions->upper_bound(StatementKey(key.keyword, "~"));
214 throw unknown_keyword(key.keyword);
216 LoaderAction *act = nullptr;
218 for(auto i=begin; i!=end; ++i)
220 int m = signature_match(key.signature, i->first.signature);
229 throw invalid_signature(key.keyword, key.signature);
234 const string &Loader::get_source() const
237 throw logic_error("no current statement");
238 return cur_st->source;
241 const string &Loader::get_keyword() const
244 throw logic_error("no current statement");
245 return cur_st->keyword;
249 Loader::ActionMap::~ActionMap()
251 for(const auto &kvp: *this)
255 } // namespace DataFile