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