]> git.tdb.fi Git - libs/gl.git/blob - source/programparser.h
Implement an actual expression parser
[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 <string>
7 #include <msp/io/base.h>
8 #include "programsyntax.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class ProgramParser
14 {
15 private:
16         enum OperatorType
17         {
18                 NO_OPERATOR,
19                 BINARY,
20                 PREFIX,
21                 POSTFIX
22         };
23
24         enum Associativity
25         {
26                 LEFT_TO_RIGHT,
27                 RIGHT_TO_LEFT
28         };
29
30         struct Operator
31         {
32                 const char token[4];
33                 unsigned precedence;
34                 OperatorType type;
35                 Associativity assoc;
36         };
37
38         std::string source;
39         std::string::const_iterator iter;
40         std::deque<std::string> next_tokens;
41         ProgramSyntax::Module main_module;
42         ProgramSyntax::Module *cur_module;
43
44         static Operator operators[];
45
46 public:
47         ProgramSyntax::Module &parse(const std::string &);
48         ProgramSyntax::Module &parse(IO::Base &);
49
50 private:
51         void parse_source(ProgramSyntax::Module &);
52
53         const std::string &peek_token(unsigned = 0);
54         std::string parse_token();
55         std::string parse_token_();
56         std::string parse_identifier();
57         std::string parse_number();
58         std::string parse_other();
59         bool skip_comment_and_whitespace();
60         void expect(const std::string &);
61         std::string expect_type();
62         std::string expect_identifier();
63         bool check(const std::string &);
64
65         static bool is_interface_qualifier(const std::string &);
66         static bool is_sampling_qualifier(const std::string &);
67         static bool is_qualifier(const std::string &);
68         static bool is_builtin_type(const std::string &);
69         bool is_type(const std::string &);
70         bool is_identifier(const std::string &);
71
72         ProgramSyntax::Node *parse_global_declaration();
73         ProgramSyntax::Node *parse_statement();
74         ProgramSyntax::Layout *parse_layout();
75         void parse_block(ProgramSyntax::Block &, bool);
76         ProgramSyntax::Expression *parse_expression(unsigned = 0);
77         ProgramSyntax::BinaryExpression *parse_binary(ProgramSyntax::Expression *, const Operator *);
78         ProgramSyntax::FunctionCall *parse_function_call(ProgramSyntax::VariableReference *);
79         ProgramSyntax::StructDeclaration *parse_struct_declaration();
80         ProgramSyntax::VariableDeclaration *parse_variable_declaration();
81         ProgramSyntax::FunctionDeclaration *parse_function_declaration();
82         void parse_function_parameter_list(ProgramSyntax::FunctionDeclaration &);
83         ProgramSyntax::InterfaceBlock *parse_interface_block();
84         ProgramSyntax::Conditional *parse_conditional();
85         ProgramSyntax::Iteration *parse_iteration();
86         ProgramSyntax::Return *parse_return();
87 };
88
89 } // namespace GL
90 } // namespace Msp
91
92 #endif