]> git.tdb.fi Git - libs/gl.git/blob - source/programparser.h
Appease the compiler with some extra copy-ctor's and operator='s
[libs/gl.git] / source / programparser.h
1 #ifndef MSP_GL_PROGRAMPARSER_H_
2 #define MSP_GL_PROGRAMPARSER_H_
3
4 #include <deque>
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <msp/io/base.h>
9 #include "programsyntax.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class ProgramParser
15 {
16 private:
17         enum OperatorType
18         {
19                 NO_OPERATOR,
20                 BINARY,
21                 PREFIX,
22                 POSTFIX
23         };
24
25         enum Associativity
26         {
27                 LEFT_TO_RIGHT,
28                 RIGHT_TO_LEFT
29         };
30
31         struct Operator
32         {
33                 const char token[4];
34                 unsigned precedence;
35                 OperatorType type;
36                 Associativity assoc;
37         };
38
39         std::string source;
40         std::string source_name;
41         unsigned source_index;
42         unsigned current_line;
43         std::string::const_iterator iter;
44         std::string::const_iterator source_end;
45         bool allow_preprocess;
46         bool allow_stage_change;
47         std::string last_token;
48         std::deque<std::string> next_tokens;
49         ProgramSyntax::Module *module;
50         ProgramSyntax::Stage *cur_stage;
51         std::set<std::string> declared_types;
52
53         static Operator operators[];
54
55 public:
56         ProgramParser();
57         ~ProgramParser();
58
59         ProgramSyntax::Module &parse(const std::string &, const std::string &, unsigned = 0);
60         ProgramSyntax::Module &parse(IO::Base &, const std::string &, unsigned = 0);
61
62 private:
63         void parse_source();
64
65         std::string format_error(const std::string &);
66         std::string format_syntax_error(const std::string &);
67
68         const std::string &peek_token(unsigned = 0);
69         const std::string &parse_token();
70         std::string parse_token_();
71         std::string parse_identifier();
72         std::string parse_number();
73         std::string parse_other();
74         void skip_comment_and_whitespace();
75         void expect(const std::string &);
76         std::string expect_type();
77         std::string expect_identifier();
78         bool check(const std::string &);
79
80         static bool is_interface_qualifier(const std::string &);
81         static bool is_sampling_qualifier(const std::string &);
82         static bool is_interpolation_qualifier(const std::string &);
83         static bool is_precision_qualifier(const std::string &);
84         static bool is_qualifier(const std::string &);
85         static bool is_builtin_type(const std::string &);
86         bool is_type(const std::string &);
87         bool is_identifier(const std::string &);
88
89         void preprocess();
90         void preprocess_version();
91         void preprocess_pragma();
92         void preprocess_pragma_msp();
93         void preprocess_stage();
94
95         RefPtr<ProgramSyntax::Statement> parse_global_declaration();
96         RefPtr<ProgramSyntax::Statement> parse_statement();
97         RefPtr<ProgramSyntax::Import> parse_import();
98         RefPtr<ProgramSyntax::Precision> parse_precision();
99         RefPtr<ProgramSyntax::Layout> parse_layout();
100         void parse_block(ProgramSyntax::Block &, bool);
101         RefPtr<ProgramSyntax::Expression> parse_expression(unsigned = 0);
102         RefPtr<ProgramSyntax::BinaryExpression> parse_binary(const RefPtr<ProgramSyntax::Expression> &, const Operator *);
103         RefPtr<ProgramSyntax::FunctionCall> parse_function_call(const ProgramSyntax::VariableReference &);
104         RefPtr<ProgramSyntax::StructDeclaration> parse_struct_declaration();
105         RefPtr<ProgramSyntax::VariableDeclaration> parse_variable_declaration();
106         RefPtr<ProgramSyntax::FunctionDeclaration> parse_function_declaration();
107         RefPtr<ProgramSyntax::InterfaceBlock> parse_interface_block();
108         RefPtr<ProgramSyntax::Conditional> parse_conditional();
109         RefPtr<ProgramSyntax::Iteration> parse_for();
110         RefPtr<ProgramSyntax::Iteration> parse_while();
111         RefPtr<ProgramSyntax::Passthrough> parse_passthrough();
112         RefPtr<ProgramSyntax::Return> parse_return();
113 };
114
115 } // namespace GL
116 } // namespace Msp
117
118 #endif