]> git.tdb.fi Git - libs/datafile.git/blob - source/parser.h
Implement proper copy semantics
[libs/datafile.git] / source / parser.h
1 #ifndef MSP_DATAFILE_PARSER_H_
2 #define MSP_DATAFILE_PARSER_H_
3
4 #include <string>
5 #include <msp/core/noncopyable.h>
6 #include "input.h"
7
8 namespace Msp {
9 namespace DataFile {
10
11 class Loader;
12 class LoaderAction;
13 class ParserMode;
14 class Statement;
15 struct StatementKey;
16
17 /**
18 Frontend for loading datafiles.  Handles switching between text and binary
19 formats.  A Parser evaluates into a boolean value indicating whether more
20 statements may be read.
21 */
22 class Parser: private NonCopyable
23 {
24 private:
25         Input in;
26         std::string main_src;
27         std::string src;
28         bool good;
29         ParserMode *mode;
30
31 public:
32         Parser(IO::Base &i, const std::string &s);
33         ~Parser();
34
35         /** Reads a statement from the input.  If the end of input was reached, an
36         empty invalid statement will be returned.  If an error occurs, the parser
37         will be marked as bad and no more statements may be read, even if the
38         exception was caught. */
39         Statement parse(bool raw = false);
40
41 private:
42         void process_control_statement(const Statement &);
43
44 public:
45         /** Returns a key for the next statement, consisting of its keyword and
46         signature.  Not supported in all modes. */
47         const StatementKey *peek(unsigned);
48
49         /** Parses a statement and feeds its arguments to an action.  The action
50         must be appropriate for the statement.  Use peek() to determine the
51         statement's signature. */
52         bool parse_and_load(unsigned, Loader &, const LoaderAction &);
53
54         operator bool() const { return good && in; }
55 };
56
57 } // namespace DataFile
58 } // namespace Msp
59
60 #endif