1 #include <msp/strings/format.h>
2 #include "binaryparser.h"
6 #include "textparser.h"
13 Parser::Parser(IO::Base &i, const string &s):
18 mode(new TextParser(in, src))
26 Statement Parser::parse(bool raw)
29 throw logic_error("Parser::parse() !good");
35 Statement st = mode->parse();
36 if(!st.keyword.compare(0, 2, "__"))
39 process_control_statement(st);
42 if(raw || !st.control)
44 else if(!good) // This will occur with an __end statement
48 catch(const exception &e)
51 if(dynamic_cast<const data_error *>(&e))
54 throw data_error(src, in.get_line_number(), e);
58 void Parser::process_control_statement(const Statement &st)
60 if(st.keyword=="__bin")
63 mode = new BinaryParser(in, src);
65 while(in.peek()=='\n')
68 else if(st.keyword=="__text")
71 mode = new TextParser(in, src);
73 else if(st.keyword=="__z")
75 else if(st.keyword=="__src")
77 string s = st.args[0].get<string>();
81 src = format("%s[%s]", main_src, s);
83 else if(st.keyword=="__end")
86 mode->process_control_statement(st);
89 const StatementKey *Parser::peek(unsigned level)
93 const StatementKey *key = mode->peek(level);
94 if(key && !key->keyword.compare(0, 2, "__"))
95 process_control_statement(mode->parse());
101 bool Parser::parse_and_load(unsigned level, Loader &ldr, const LoaderAction &act)
103 // Peek first to get any control statements processed
105 return mode->parse_and_load(level, ldr, act);
108 } // namespace DataFile