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