]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.cpp
8d4f2bea35b07f0cf485cbc50a8d22fe146b5cd1
[libs/datafile.git] / source / loader.cpp
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "loader.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace DataFile {
14
15 void Loader::load(const Statement &st)
16 {
17         for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
18                 load_statement(*i);
19         finish();
20 }
21
22 void Loader::load(Parser &p)
23 {
24         while(p)
25         {
26                 Statement st=p.parse();
27                 if(st.valid)
28                         load_statement(st);
29         }
30         finish();
31 }
32
33 Loader::~Loader()
34 {
35         for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
36                 delete i->second;
37 }
38
39 void Loader::load_sub_with(Loader &ldr)
40 {
41         if(!cur_st)
42                 throw InvalidState("load_sub called without current statement");
43
44         ldr.load(*cur_st);
45 }
46
47 void Loader::add(const string &k, LoaderAction *a)
48 {
49         ActionMap::iterator i=actions.find(k);
50         if(i!=actions.end())
51         {
52                 delete i->second;
53                 i->second=a;
54         }
55         else
56                 actions[k]=a;
57 }
58
59 void Loader::load_statement(const Statement &st)
60 {
61         cur_st=&st;
62         ActionMap::iterator j=actions.find(st.keyword);
63         if(j==actions.end())
64                 throw_at(KeyError("Unknown keyword", st.keyword), st.get_location());
65         if(j->second)
66         {
67                 try
68                 {
69                         j->second->execute(*this, st);
70                 }
71                 catch(Exception &e)
72                 {
73                         if(!e.where()[0])
74                                 e.at(st.get_location());
75                         throw;
76                 }
77         }
78         cur_st=0;
79 }
80
81 } // namespace DataFile
82 } // namespace Msp