]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/loader.cpp
Provide access to the keyword of the current statement
[libs/datafile.git] / source / loader.cpp
index c2de4011232c93da747bfdead7df18af72c6087d..997e7c9eec27f7f480c711291f6a47f997c7039e 100644 (file)
@@ -1,6 +1,6 @@
 #include <msp/core/raii.h>
 #include <msp/strings/format.h>
-#include "dataerror.h"
+#include "except.h"
 #include "loader.h"
 #include "type.h"
 
@@ -24,7 +24,7 @@ bool signature_match(const string &st_sig, const string &act_sig)
        else if(act_sig.size()==2 && act_sig[1]=='*')
        {
                for(string::const_iterator i=st_sig.begin(); i!=st_sig.end(); ++i)
-                       if(*i!=act_sig[0])
+                       if(!signature_match(*i, act_sig[0]))
                                return false;
 
                return true;
@@ -47,30 +47,9 @@ bool signature_match(const string &st_sig, const string &act_sig)
 namespace Msp {
 namespace DataFile {
 
-class unknown_keyword: public runtime_error
-{
-public:
-       unknown_keyword(const std::string &k):
-               runtime_error(k)
-       { }
-
-       virtual ~unknown_keyword() throw() { }
-};
-
-
-class invalid_signature: public runtime_error
-{
-public:
-       invalid_signature(const std::string &k, const std::string &s):
-               runtime_error(format("%s %s", k, s))
-       { }
-
-       virtual ~invalid_signature() throw() { }
-};
-
-
 Loader::Loader():
        cur_st(0),
+       direct(false),
        check_sub_loads(false)
 { }
 
@@ -84,9 +63,15 @@ void Loader::load(Parser &p)
 {
        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();
 }
@@ -98,6 +83,29 @@ void Loader::load(const Statement &st)
        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)
 {
        SetForScope<const Statement *> set_cst(cur_st, &st);
@@ -134,11 +142,18 @@ void Loader::load_statement(const Statement &st)
 
 void Loader::load_sub_with(Loader &ldr)
 {
-       if(!cur_st)
+       if(direct)
+       {
+               ldr.load_direct(*cur_parser, cur_level+1);
+               ldr.finish();
+       }
+       else if(cur_st)
+       {
+               ldr.load(*cur_st);
+               sub_loaded = true;
+       }
+       else
                throw logic_error("no current statement");
-
-       ldr.load(*cur_st);
-       sub_loaded = true;
 }
 
 void Loader::add(const string &kwd, LoaderAction *act)
@@ -190,5 +205,12 @@ const string &Loader::get_source() const
        return cur_st->source;
 }
 
+const string &Loader::get_keyword() const
+{
+       if(!cur_st)
+               throw logic_error("no current statement");
+       return cur_st->keyword;
+}
+
 } // namespace DataFile
 } // namespace Msp