dict[-3] = StatementKey("__flt", "i");
}
-Statement BinaryParser::parse(bool raw)
-{
- while(1)
- {
- Statement st = parse_statement(raw);
- if(st.keyword=="__kwd")
- {
- int id = st.args[0].get<int>();
- if(id<=0)
- throw bad_definition("__kwd");
-
- const string &kw = st.args[1].get<const string &>();
- const string &args = st.args[2].get<const string &>();
- for(string::const_iterator i=args.begin(); i!=args.end(); ++i)
- for(unsigned j=0; valid_signatures[j]!=*i; ++j)
- if(!valid_signatures[j])
- throw bad_definition("__kwd");
-
- dict[id] = StatementKey(kw, args);
- }
- else if(st.keyword=="__str")
- {
- int id = st.args[0].get<int>();
- if(id<=0)
- throw bad_definition("__str");
-
- strings[id] = st.args[1].get<const string &>();
- }
- else if(st.keyword=="__flt")
- float_precision = st.args[0].get<unsigned>();
- else
- return st;
-
- if(raw)
- return st;
- }
-}
-
-Statement BinaryParser::parse_statement(bool raw)
+Statement BinaryParser::parse()
{
while(first && in.peek()=='\n')
in.get();
unsigned nsub = parse_int();
for(unsigned j = 0; j<nsub; ++j)
- result.sub.push_back(parse(raw));
+ result.sub.push_back(parse());
result.valid = true;
return result;
}
+void BinaryParser::process_control_statement(const Statement &st)
+{
+ if(st.keyword=="__kwd")
+ {
+ int id = st.args[0].get<int>();
+ if(id<=0)
+ throw bad_definition("__kwd");
+
+ const string &kw = st.args[1].get<const string &>();
+ const string &args = st.args[2].get<const string &>();
+ for(string::const_iterator i=args.begin(); i!=args.end(); ++i)
+ for(unsigned j=0; valid_signatures[j]!=*i; ++j)
+ if(!valid_signatures[j])
+ throw bad_definition("__kwd");
+
+ dict[id] = StatementKey(kw, args);
+ }
+ else if(st.keyword=="__str")
+ {
+ int id = st.args[0].get<int>();
+ if(id<=0)
+ throw bad_definition("__str");
+
+ strings[id] = st.args[1].get<const string &>();
+ }
+ else if(st.keyword=="__flt")
+ float_precision = st.args[0].get<unsigned>();
+}
+
IntType::Store BinaryParser::parse_int()
{
IntType::Store result = 0;
public:
BinaryParser(Input &i, const std::string &s);
- virtual Statement parse(bool);
+ virtual Statement parse();
+ virtual void process_control_statement(const Statement &);
private:
- Statement parse_statement(bool);
IntType::Store parse_int();
FloatType::Store parse_float();
StringType::Store parse_string();
{
while(1)
{
- Statement st = mode->parse(raw);
- if(st.keyword=="__bin")
+ Statement st = mode->parse();
+ if(!st.keyword.compare(0, 2, "__"))
{
- delete mode;
- mode = new BinaryParser(in, src);
+ st.control = true;
+ process_control_statement(st);
}
- else if(st.keyword=="__text")
- {
- delete mode;
- mode = new TextParser(in, src);
- }
- else if(st.keyword=="__z")
- in.set_decompress();
- else if(st.keyword=="__src")
- {
- string s = st.args[0].get<string>();
- if(s.empty())
- src = main_src;
- else
- src = format("%s[%s]", main_src, s);
- }
- else if(st.keyword=="__end")
- {
- good = false;
- return raw ? st : Statement();
- }
- else
- return st;
- if(raw)
+ if(raw || !st.control)
return st;
+ else if(!good) // This will occur with an __end statement
+ return Statement();
}
}
catch(const exception &e)
}
}
+void Parser::process_control_statement(const Statement &st)
+{
+ if(st.keyword=="__bin")
+ {
+ delete mode;
+ mode = new BinaryParser(in, src);
+ }
+ else if(st.keyword=="__text")
+ {
+ delete mode;
+ mode = new TextParser(in, src);
+ }
+ else if(st.keyword=="__z")
+ in.set_decompress();
+ else if(st.keyword=="__src")
+ {
+ string s = st.args[0].get<string>();
+ if(s.empty())
+ src = main_src;
+ else
+ src = format("%s[%s]", main_src, s);
+ }
+ else if(st.keyword=="__end")
+ good = false;
+ else
+ mode->process_control_statement(st);
+}
+
} // namespace DataFile
} // namespace Msp
*/
Statement parse(bool raw = false);
+private:
+ void process_control_statement(const Statement &);
+
+public:
operator bool() const { return good && in; }
};
public:
virtual ~ParserMode() { }
- virtual Statement parse(bool) = 0;
+ virtual Statement parse() = 0;
+ virtual void process_control_statement(const Statement &) { }
};
} // namespace DataFile
std::string keyword;
Arguments args;
bool valid;
+ bool control;
std::string source;
unsigned line;
std::list<Statement> sub;
ParserMode(i, s)
{ }
-Statement TextParser::parse(bool)
+Statement TextParser::parse()
{
return parse_statement(0);
}
public:
TextParser(Input &, const std::string &);
- virtual Statement parse(bool);
+ virtual Statement parse();
protected:
Statement parse_statement(const Token *);
Token parse_token();
DataFile::Statement st = parser.parse(true);
if(st.valid)
{
- bool sys = !st.keyword.compare(0, 2, "__");
- if(collection && !sys)
+ if(collection && !st.control)
{
if(st.get_signature()=="s")
{
else
collection = false;
}
- if(!sys || st.keyword=="__src")
+ if(!st.control || st.keyword=="__src")
writer->write(st);
}
}
while(parser)
{
DataFile::Statement st = parser.parse(true);
- if(st.valid && (st.keyword.compare(0, 2, "__") || st.keyword=="__src" || debug))
+ if(st.valid && (!st.control || st.keyword=="__src" || debug))
writer->write(st);
}