-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
#ifndef MSP_DATAFILE_PARSER_H_
#define MSP_DATAFILE_PARSER_H_
-#include <istream>
#include <string>
+#include <msp/core/noncopyable.h>
#include "input.h"
namespace Msp {
namespace DataFile {
-class Statement;
-struct Token;
+class Loader;
+class LoaderAction;
+class ParserMode;
+struct Statement;
+struct StatementKey;
-class Parser
+/**
+Frontend for loading datafiles. Handles switching between text and binary
+formats. A Parser evaluates into a boolean value indicating whether more
+statements may be read.
+*/
+class Parser: private NonCopyable
{
-public:
- Parser(std::istream &, const std::string &);
- Statement parse();
- operator bool() const { return in; }
private:
- Input in;
+ Input in;
+ std::string main_src;
std::string src;
- bool good;
-
- Statement parse_(const Token *);
- Token parse_token();
- bool is_delimiter(int);
- bool isodigit(int);
- std::string unescape_string(const std::string &);
- std::string get_location();
- void parse_error(int, int);
+ bool good;
+ ParserMode *mode;
+
+public:
+ Parser(IO::Base &i, const std::string &s);
+ ~Parser();
+
+ /** Reads a statement from the input. If the end of input was reached, an
+ empty invalid statement will be returned. If an error occurs, the parser
+ will be marked as bad and no more statements may be read, even if the
+ exception was caught. */
+ Statement parse(bool raw = false);
+
+private:
+ void process_control_statement(const Statement &);
+
+public:
+ /** Returns a key for the next statement, consisting of its keyword and
+ signature. Not supported in all modes. */
+ const StatementKey *peek(unsigned);
+
+ /** Parses a statement and feeds its arguments to an action. The action
+ must be appropriate for the statement. Use peek() to determine the
+ statement's signature. */
+ bool parse_and_load(unsigned, Loader &, const LoaderAction &);
+
+ operator bool() const { return good && in; }
};
} // namespace DataFile