]> git.tdb.fi Git - libs/datafile.git/blob - source/parser.cpp
Update formatter.h -> format.h
[libs/datafile.git] / source / parser.cpp
1 #include <msp/strings/format.h>
2 #include "binaryparser.h"
3 #include "parser.h"
4 #include "statement.h"
5 #include "textparser.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace DataFile {
11
12 Parser::Parser(IO::Base &i, const string &s):
13         in(i),
14         main_src(s),
15         src(s),
16         good(true),
17         mode(new TextParser(in, src))
18 { }
19
20 Parser::~Parser()
21 {
22         delete mode;
23 }
24
25 Statement Parser::parse()
26 {
27         if(!good)
28                 throw Exception("Parser is not good");
29
30         try
31         {
32                 while(1)
33                 {
34                         Statement st = mode->parse();
35                         if(st.keyword=="__bin")
36                         {
37                                 delete mode;
38                                 mode = new BinaryParser(in, src);
39                         }
40                         else if(st.keyword=="__text")
41                         {
42                                 delete mode;
43                                 mode = new TextParser(in, src);
44                         }
45                         else if(st.keyword=="__src")
46                         {
47                                 string s = st.args[0].get<string>();
48                                 if(s.empty())
49                                         src = main_src;
50                                 else
51                                         src = format("%s[%s]", main_src, s);
52                         }
53                         else
54                                 return st;
55                 }
56         }
57         catch(const Exception &e)
58         {
59                 good = false;
60                 throw;
61         }
62 }
63
64 } // namespace DataFile
65 } // namespace Msp