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)
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]))
50 class unknown_keyword: public runtime_error
53 unknown_keyword(const std::string &k):
57 virtual ~unknown_keyword() throw() { }
61 class invalid_signature: public runtime_error
64 invalid_signature(const std::string &k, const std::string &s):
65 runtime_error(format("%s %s", k, s))
68 virtual ~invalid_signature() throw() { }
75 check_sub_loads(false)
80 for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
84 void Loader::load(Parser &p)
92 // Parse in raw mode so we can peek immediately after a mode change
93 Statement st = p.parse(true);
94 if(st.valid && !st.control)
101 void Loader::load(const Statement &st)
103 for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
108 void Loader::load_direct(Parser &p, unsigned l)
110 SetForScope<Parser *> set_parser(cur_parser, &p);
111 SetForScope<unsigned> set_level(cur_level, l);
115 const StatementKey *key = p.peek(l);
119 LoaderAction *act = find_action(*key);
122 SetFlag set_direct(direct);
123 if(!p.parse_and_load(l, *this, *act))
124 throw logic_error("direct load failed");
127 load_statement(p.parse());
131 void Loader::load_statement(const Statement &st)
133 SetForScope<const Statement *> set_cst(cur_st, &st);
137 StatementKey key(st.keyword, st.get_signature());
139 if(!aux_loaders.empty() && !has_action(key))
141 for(list<Loader *>::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i)
142 if((*i)->has_action(key))
143 return (*i)->load_statement(st);
146 LoaderAction *act = find_action(key);
150 act->execute(*this, st);
151 if(check_sub_loads && !st.sub.empty() && !sub_loaded)
152 throw logic_error("substatements ignored");
155 catch(const data_error &)
159 catch(const exception &e)
161 throw data_error(st.source, st.line, e);
165 void Loader::load_sub_with(Loader &ldr)
169 ldr.load_direct(*cur_parser, cur_level+1);
178 throw logic_error("no current statement");
181 void Loader::add(const string &kwd, LoaderAction *act)
183 StatementKey key(kwd, (act ? act->get_signature() : "*"));
184 ActionMap::iterator i = actions.find(key);
194 void Loader::add_auxiliary_loader(Loader &ldr)
196 aux_loaders.push_back(&ldr);
199 bool Loader::has_action(const StatementKey &key) const
201 ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string()));
202 for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i)
203 if(signature_match(key.signature, i->first.signature))
208 LoaderAction *Loader::find_action(const StatementKey &key) const
210 ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string()));
211 ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~"));
214 throw unknown_keyword(key.keyword);
216 for(ActionMap::const_iterator i=begin; i!=end; ++i)
217 if(signature_match(key.signature, i->first.signature))
220 throw invalid_signature(key.keyword, key.signature);
223 const string &Loader::get_source() const
226 throw logic_error("no current statement");
227 return cur_st->source;
230 } // namespace DataFile