]> git.tdb.fi Git - libs/datafile.git/blob - source/parser.cpp
Use the Sfinae helper struct from mspcore
[libs/datafile.git] / source / parser.cpp
1 #include <msp/strings/format.h>
2 #include "binaryparser.h"
3 #include "dataerror.h"
4 #include "parser.h"
5 #include "statement.h"
6 #include "textparser.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace DataFile {
12
13 Parser::Parser(IO::Base &i, const string &s):
14         in(i),
15         main_src(s),
16         src(s),
17         good(true),
18         mode(new TextParser(in, src))
19 { }
20
21 Parser::~Parser()
22 {
23         delete mode;
24 }
25
26 Statement Parser::parse(bool raw)
27 {
28         if(!good)
29                 throw logic_error("Parser::parse() !good");
30
31         try
32         {
33                 while(1)
34                 {
35                         Statement st = mode->parse(raw);
36                         if(st.keyword=="__bin")
37                         {
38                                 delete mode;
39                                 mode = new BinaryParser(in, src);
40                         }
41                         else if(st.keyword=="__text")
42                         {
43                                 delete mode;
44                                 mode = new TextParser(in, src);
45                         }
46                         else if(st.keyword=="__z")
47                                 in.set_decompress();
48                         else if(st.keyword=="__src")
49                         {
50                                 string s = st.args[0].get<string>();
51                                 if(s.empty())
52                                         src = main_src;
53                                 else
54                                         src = format("%s[%s]", main_src, s);
55                         }
56                         else if(st.keyword=="__end")
57                         {
58                                 good = false;
59                                 return raw ? st : Statement();
60                         }
61                         else
62                                 return st;
63
64                         if(raw)
65                                 return st;
66                 }
67         }
68         catch(const exception &e)
69         {
70                 good = false;
71                 if(dynamic_cast<const data_error *>(&e))
72                         throw;
73                 else
74                         throw data_error(src, in.get_line_number(), e);
75         }
76 }
77
78 } // namespace DataFile
79 } // namespace Msp