]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/loader.cpp
Cosmetic changes
[libs/datafile.git] / source / loader.cpp
index 655514e5057c57a3c062317241abeeff5f9a08bb..7e910b8a73c5d79adc63b161a2e01248edf50211 100644 (file)
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/core/raii.h>
+#include <msp/strings/format.h>
+#include "except.h"
 #include "loader.h"
+#include "type.h"
 
 using namespace std;
 
-namespace Msp {
-namespace DataFile {
+namespace {
 
-Loader::~Loader()
+int signature_match(char s, char a)
 {
-       for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
-               delete i->second;
+       if(s==a)
+               return 3;
+       if(s==Msp::DataFile::IntType::signature && a==Msp::DataFile::FloatType::signature)
+               return 1;
+       return 0;
+}
+
+int signature_match(const string &st_sig, const string &act_sig)
+{
+       if(act_sig=="*")
+               return 1;
+       else if(act_sig.size()==2 && act_sig[1]=='*')
+       {
+               int match = 3;
+               for(auto i=st_sig.begin(); (i!=st_sig.end() && match); ++i)
+                       match = min(match, signature_match(*i, act_sig[0]));
+
+               return match;
+       }
+       else if(st_sig.size()==act_sig.size())
+       {
+               int match = 3;
+               for(unsigned i=0; (i<st_sig.size() && match); ++i)
+                       match = min(match, signature_match(st_sig[i], act_sig[i]));
+
+               return match ? match+1 : 0;
+       }
+       else
+               return 0;
+}
+
 }
 
+
+namespace Msp {
+namespace DataFile {
+
 void Loader::load(Parser &p)
 {
+       if(!actions)
+               throw logic_error("no actions");
+
        while(p)
        {
-               Statement st = p.parse();
-               if(st.valid)
-                       load_statement(st);
+               if(p.peek(0))
+                       load_direct(p, 0);
+               else if(p)  // Peek may have processed an __end, so recheck goodness
+               {
+                       // Parse in raw mode so we can peek immediately after a mode change
+                       Statement st = p.parse(true);
+                       if(st.valid && !st.control)
+                               load_statement(st);
+               }
        }
        finish();
+       for(Loader *l: aux_loaders)
+               l->finish();
 }
 
 void Loader::load(const Statement &st)
 {
-       for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
-               load_statement(*i);
+       if(!actions)
+               throw logic_error("no actions");
+
+       for(const Statement &s: st.sub)
+               load_statement(s);
        finish();
+       for(Loader *l: aux_loaders)
+               l->finish();
+}
+
+void Loader::load_direct(Parser &p, unsigned l)
+{
+       SetForScope<Parser *> set_parser(cur_parser, &p);
+       SetForScope<unsigned> set_level(cur_level, l);
+
+       while(p)
+       {
+               const StatementKey *key = p.peek(l);
+               if(!key)
+                       break;
+
+               LoaderAction *act = find_action(*key);
+               if(act)
+               {
+                       SetFlag set_direct(direct);
+                       if(!p.parse_and_load(l, *this, *act))
+                               throw logic_error("direct load failed");
+               }
+               else
+                       load_statement(p.parse());
+       }
 }
 
 void Loader::load_statement(const Statement &st)
 {
-       cur_st = &st;
-       ActionMap::iterator j = actions.find(st.keyword);
-       if(j==actions.end())
-               throw_at(KeyError("Unknown keyword", st.keyword), st.get_location());
-       if(j->second)
+       SetForScope<const Statement *> set_cst(cur_st, &st);
+
+       try
        {
-               try
+               StatementKey key(st.keyword, st.get_signature());
+
+               if(!aux_loaders.empty() && !has_action(key))
                {
-                       j->second->execute(*this, st);
+                       for(Loader *l: aux_loaders)
+                               if(l->has_action(key))
+                                       return l->load_statement(st);
                }
-               catch(Exception &e)
+
+               LoaderAction *act = find_action(key);
+               if(act)
                {
-                       if(!e.where()[0])
-                               e.at(st.get_location());
-                       throw;
+                       sub_loaded = false;
+                       act->execute(*this, st);
+                       if(check_sub_loads && !st.sub.empty() && !sub_loaded)
+                               throw logic_error("substatements ignored");
                }
        }
-       cur_st = 0;
+       catch(const data_error &e)
+       {
+               if(e.get_source()!=st.source)
+                       throw data_error(st.source, st.line, e);
+               else
+                       throw;
+       }
+       catch(const exception &e)
+       {
+               throw data_error(st.source, st.line, e);
+       }
 }
 
 void Loader::load_sub_with(Loader &ldr)
 {
-       if(!cur_st)
-               throw InvalidState("load_sub called without current statement");
+       if(direct)
+       {
+               ldr.load_direct(*cur_parser, cur_level+1);
+               ldr.finish();
+               for(Loader *l: ldr.aux_loaders)
+                       l->finish();
+       }
+       else if(cur_st)
+       {
+               ldr.load(*cur_st);
+               sub_loaded = true;
+       }
+       else
+               throw logic_error("no current statement");
+}
 
-       ldr.load(*cur_st);
+void Loader::set_actions(ActionMap &a)
+{
+       if(actions)
+               throw logic_error("actions already set");
+
+       actions = &a;
+       if(a.empty())
+               init_actions();
 }
 
-void Loader::add(const string &k, LoaderAction *a)
+void Loader::add(const string &kwd, LoaderAction *act)
 {
-       ActionMap::iterator i = actions.find(k);
-       if(i!=actions.end())
+       if(!actions)
+               actions = &local_actions;
+
+       StatementKey key(kwd, (act ? act->get_signature() : "*"));
+       ActionMap::iterator i = actions->find(key);
+       if(i!=actions->end())
        {
                delete i->second;
-               i->second = a;
+               i->second = act;
        }
        else
-               actions[k] = a;
+               (*actions)[key] = act;
+}
+
+void Loader::add_auxiliary_loader(Loader &ldr)
+{
+       aux_loaders.push_back(&ldr);
+}
+
+bool Loader::has_action(const StatementKey &key) const
+{
+       if(!actions)
+               return false;
+
+       auto i = actions->lower_bound(StatementKey(key.keyword, string()));
+       for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i)
+               if(signature_match(key.signature, i->first.signature))
+                       return true;
+       return false;
+}
+
+LoaderAction *Loader::find_action(const StatementKey &key) const
+{
+       if(!actions)
+               return nullptr;
+
+       auto begin = actions->lower_bound(StatementKey(key.keyword, string()));
+       auto end = actions->upper_bound(StatementKey(key.keyword, "~"));
+
+       if(begin==end)
+               throw unknown_keyword(key.keyword);
+
+       LoaderAction *act = nullptr;
+       int match = 0;
+       for(auto i=begin; i!=end; ++i)
+       {
+               int m = signature_match(key.signature, i->first.signature);
+               if(m>match)
+               {
+                       act = i->second;
+                       match = m;
+               }
+       }
+
+       if(!match)
+               throw invalid_signature(key.keyword, key.signature);
+
+       return act;
+}
+
+const string &Loader::get_source() const
+{
+       if(!cur_st)
+               throw logic_error("no current statement");
+       return cur_st->source;
+}
+
+const string &Loader::get_keyword() const
+{
+       if(!cur_st)
+               throw logic_error("no current statement");
+       return cur_st->keyword;
+}
+
+
+Loader::ActionMap::~ActionMap()
+{
+       for(const auto &kvp: *this)
+               delete kvp.second;
 }
 
 } // namespace DataFile