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