]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/parser.cpp
Add a special keyword to terminate parsing
[libs/datafile.git] / source / parser.cpp
index 06e5a8c33745e78e3855a32f4148e939e93e390f..fce12e245b3f6ab011e4ca45851b0086847a4e5f 100644 (file)
@@ -1,12 +1,6 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#include <cctype>
-#include <sstream>
+#include <msp/strings/format.h>
 #include "binaryparser.h"
+#include "dataerror.h"
 #include "parser.h"
 #include "statement.h"
 #include "textparser.h"
@@ -18,6 +12,7 @@ namespace DataFile {
 
 Parser::Parser(IO::Base &i, const string &s):
        in(i),
+       main_src(s),
        src(s),
        good(true),
        mode(new TextParser(in, src))
@@ -31,31 +26,49 @@ Parser::~Parser()
 Statement Parser::parse()
 {
        if(!good)
-               throw Exception("Parser is not good");
+               throw logic_error("Parser::parse() !good");
 
        try
        {
                while(1)
                {
-                       Statement st=mode->parse();
+                       Statement st = mode->parse();
                        if(st.keyword=="__bin")
                        {
                                delete mode;
-                               mode=new BinaryParser(in, src);
+                               mode = new BinaryParser(in, src);
                        }
                        else if(st.keyword=="__text")
                        {
                                delete mode;
-                               mode=new TextParser(in, src);
+                               mode = new TextParser(in, src);
+                       }
+                       else if(st.keyword=="__z")
+                               in.set_decompress();
+                       else if(st.keyword=="__src")
+                       {
+                               string s = st.args[0].get<string>();
+                               if(s.empty())
+                                       src = main_src;
+                               else
+                                       src = format("%s[%s]", main_src, s);
+                       }
+                       else if(st.keyword=="__end")
+                       {
+                               good = false;
+                               return Statement();
                        }
                        else
                                return st;
                }
        }
-       catch(const Exception &e)
+       catch(const exception &e)
        {
-               good=false;
-               throw;
+               good = false;
+               if(dynamic_cast<const data_error *>(&e))
+                       throw;
+               else
+                       throw data_error(src, in.get_line_number(), e);
        }
 }