1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 bool signature_match(char s, char a)
15 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
20 bool signature_match(const string &st_sig, const string &act_sig)
24 else if(act_sig.size()==2 && act_sig[1]=='*')
26 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
27 if(!signature_match(*i, act_sig[0]))
32 else if(st_sig.size()==act_sig.size())
34 for(unsigned i=0; i<st_sig.size(); ++i)
35 if(!signature_match(st_sig[i], act_sig[i]))
53 check_sub_loads(false)
58 for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
62 void Loader::load(Parser &p)
68 else if(p) // Peek may have processed an __end, so recheck goodness
70 // Parse in raw mode so we can peek immediately after a mode change
71 Statement st = p.parse(true);
72 if(st.valid && !st.control)
79 void Loader::load(const Statement &st)
81 for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
86 void Loader::load_direct(Parser &p, unsigned l)
88 SetForScope<Parser *> set_parser(cur_parser, &p);
89 SetForScope<unsigned> set_level(cur_level, l);
93 const StatementKey *key = p.peek(l);
97 LoaderAction *act = find_action(*key);
100 SetFlag set_direct(direct);
101 if(!p.parse_and_load(l, *this, *act))
102 throw logic_error("direct load failed");
105 load_statement(p.parse());
109 void Loader::load_statement(const Statement &st)
111 SetForScope<const Statement *> set_cst(cur_st, &st);
115 StatementKey key(st.keyword, st.get_signature());
117 if(!aux_loaders.empty() && !has_action(key))
119 for(list<Loader *>::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i)
120 if((*i)->has_action(key))
121 return (*i)->load_statement(st);
124 LoaderAction *act = find_action(key);
128 act->execute(*this, st);
129 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
130 throw logic_error("substatements ignored");
133 catch(const data_error &)
137 catch(const exception &e)
139 throw data_error(st.source, st.line, e);
143 void Loader::load_sub_with(Loader &ldr)
147 ldr.load_direct(*cur_parser, cur_level+1);
156 throw logic_error("no current statement");
159 void Loader::add(const string &kwd, LoaderAction *act)
161 StatementKey key(kwd, (act ? act->get_signature() : "*"));
162 ActionMap::iterator i = actions.find(key);
172 void Loader::add_auxiliary_loader(Loader &ldr)
174 aux_loaders.push_back(&ldr);
177 bool Loader::has_action(const StatementKey &key) const
179 ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string()));
180 for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i)
181 if(signature_match(key.signature, i->first.signature))
186 LoaderAction *Loader::find_action(const StatementKey &key) const
188 ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string()));
189 ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~"));
192 throw unknown_keyword(key.keyword);
194 for(ActionMap::const_iterator i=begin; i!=end; ++i)
195 if(signature_match(key.signature, i->first.signature))
198 throw invalid_signature(key.keyword, key.signature);
201 const string &Loader::get_source() const
204 throw logic_error("no current statement");
205 return cur_st->source;
208 const string &Loader::get_keyword() const
211 throw logic_error("no current statement");
212 return cur_st->keyword;
215 } // namespace DataFile