]> git.tdb.fi Git - libs/gl.git/blob - source/programparser.h
Rename ProgramSyntax::Context to Stage
[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::const_iterator iter;
41         std::deque<std::string> next_tokens;
42         ProgramSyntax::Module main_module;
43         ProgramSyntax::Module *cur_module;
44         ProgramSyntax::Stage *cur_stage;
45         std::set<std::string> declared_types;
46
47         static Operator operators[];
48
49 public:
50         ProgramSyntax::Module &parse(const std::string &);
51         ProgramSyntax::Module &parse(IO::Base &);
52
53 private:
54         void parse_source(ProgramSyntax::Module &);
55
56         const std::string &peek_token(unsigned = 0);
57         std::string parse_token();
58         std::string parse_token_();
59         std::string parse_identifier();
60         std::string parse_number();
61         std::string parse_other();
62         bool skip_comment_and_whitespace();
63         void expect(const std::string &);
64         std::string expect_type();
65         std::string expect_identifier();
66         bool check(const std::string &);
67
68         static bool is_interface_qualifier(const std::string &);
69         static bool is_sampling_qualifier(const std::string &);
70         static bool is_qualifier(const std::string &);
71         static bool is_builtin_type(const std::string &);
72         bool is_type(const std::string &);
73         bool is_identifier(const std::string &);
74
75         ProgramSyntax::Node *parse_global_declaration();
76         ProgramSyntax::Node *parse_statement();
77         ProgramSyntax::Layout *parse_layout();
78         void parse_block(ProgramSyntax::Block &, bool);
79         ProgramSyntax::Expression *parse_expression(unsigned = 0);
80         ProgramSyntax::BinaryExpression *parse_binary(ProgramSyntax::Expression *, const Operator *);
81         ProgramSyntax::FunctionCall *parse_function_call(ProgramSyntax::VariableReference *);
82         ProgramSyntax::StructDeclaration *parse_struct_declaration();
83         ProgramSyntax::VariableDeclaration *parse_variable_declaration();
84         ProgramSyntax::FunctionDeclaration *parse_function_declaration();
85         void parse_function_parameter_list(ProgramSyntax::FunctionDeclaration &);
86         ProgramSyntax::InterfaceBlock *parse_interface_block();
87         ProgramSyntax::Conditional *parse_conditional();
88         ProgramSyntax::Iteration *parse_iteration();
89         ProgramSyntax::Passthrough *parse_passthrough();
90         ProgramSyntax::Return *parse_return();
91 };
92
93 } // namespace GL
94 } // namespace Msp
95
96 #endif