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