1 #include <msp/strings/format.h>
2 #include "binaryparser.h"
4 #include "jsonparser.h"
7 #include "textparser.h"
14 Parser::Parser(IO::Base &i, const string &s):
22 mode = new JsonParser(in, src);
24 mode = new TextParser(in, src);
32 Statement Parser::parse(bool raw)
35 throw logic_error("Parser::parse() !good");
41 Statement st = mode->parse();
42 if(!st.keyword.compare(0, 2, "__"))
45 process_control_statement(st);
48 if(raw || !st.control)
50 else if(!good) // This will occur with an __end statement
54 catch(const exception &e)
57 if(dynamic_cast<const data_error *>(&e))
60 throw data_error(src, in.get_line_number(), e);
64 void Parser::process_control_statement(const Statement &st)
66 if(st.keyword=="__bin")
69 mode = new BinaryParser(in, src);
71 while(in.peek()=='\n')
74 else if(st.keyword=="__text")
77 mode = new TextParser(in, src);
79 else if(st.keyword=="__z")
81 else if(st.keyword=="__src")
83 string s = st.args[0].get<string>();
87 src = format("%s[%s]", main_src, s);
89 else if(st.keyword=="__end")
92 mode->process_control_statement(st);
95 const StatementKey *Parser::peek(unsigned level)
99 const StatementKey *key = mode->peek(level);
100 if(key && !key->keyword.compare(0, 2, "__"))
101 process_control_statement(mode->parse());
109 bool Parser::parse_and_load(unsigned level, Loader &ldr, const LoaderAction &act)
111 // Peek first to get any control statements processed
113 return mode->parse_and_load(level, ldr, act);
116 } // namespace DataFile