3 This file is part of libmspdatafile
4 Copyright © 2006-2008, 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/formatter.h>
16 bool signature_match(char s, char a)
20 if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
25 bool signature_match(const string &st_sig, const string &act_sig)
29 else if(act_sig.size()==2 && act_sig[1]=='*')
31 for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
37 else if(st_sig.size()==act_sig.size())
39 for(unsigned i=0; i<st_sig.size(); ++i)
40 if(!signature_match(st_sig[i], act_sig[i]))
56 for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
60 void Loader::load(Parser &p)
64 Statement st = p.parse();
71 void Loader::load(const Statement &st)
73 for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
78 void Loader::load_statement(const Statement &st)
84 LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature()));
86 act->execute(*this, st);
92 e.at(st.get_location());
100 void Loader::load_sub_with(Loader &ldr)
103 throw InvalidState("load_sub called without current statement");
108 void Loader::add(const string &kwd, LoaderAction *act)
110 ActionKey key(kwd, act->get_signature());
111 ActionMap::iterator i = actions.find(key);
121 LoaderAction *Loader::find_action(const ActionKey &key) const
123 ActionMap::const_iterator begin = actions.lower_bound(ActionKey(key.keyword, string()));
124 ActionMap::const_iterator end = actions.upper_bound(ActionKey(key.keyword, "~"));
127 throw KeyError("Unknown keyword", key.keyword);
129 for(ActionMap::const_iterator i=begin; i!=end; ++i)
130 if(signature_match(key.signature, i->first.signature))
133 throw KeyError(format("Keyword '%s' does not accept signature '%s'", key.keyword, key.signature));
137 Loader::ActionKey::ActionKey(const string &k, const string &s):
142 bool Loader::ActionKey::operator<(const ActionKey &other) const
144 if(keyword!=other.keyword)
145 return keyword<other.keyword;
146 return signature<other.signature;
149 } // namespace DataFile