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):
21 mode = new JsonParser(in, src);
23 mode = new TextParser(in, src);
31 Statement Parser::parse(bool raw)
34 throw logic_error("Parser::parse() !good");
40 Statement st = mode->parse();
41 if(!st.keyword.compare(0, 2, "__"))
44 process_control_statement(st);
47 if(raw || !st.control)
49 else if(!good) // This will occur with an __end statement
53 catch(const exception &e)
56 if(dynamic_cast<const data_error *>(&e))
59 throw data_error(src, in.get_line_number(), e);
63 void Parser::process_control_statement(const Statement &st)
65 if(st.keyword=="__bin")
68 mode = new BinaryParser(in, src);
70 while(in.peek()=='\n')
73 else if(st.keyword=="__text")
76 mode = new TextParser(in, src);
78 else if(st.keyword=="__z")
80 else if(st.keyword=="__src")
82 string s = st.args[0].get<string>();
86 src = format("%s[%s]", main_src, s);
88 else if(st.keyword=="__end")
91 mode->process_control_statement(st);
94 const StatementKey *Parser::peek(unsigned level)
98 const StatementKey *key = mode->peek(level);
99 if(key && !key->keyword.compare(0, 2, "__"))
100 process_control_statement(mode->parse());
108 bool Parser::parse_and_load(unsigned level, Loader &ldr, const LoaderAction &act)
110 // Peek first to get any control statements processed
112 return mode->parse_and_load(level, ldr, act);
115 } // namespace DataFile