]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programparser.h
Require import module names to be identifiers
[libs/gl.git] / source / programparser.h
index 0a91c500b1a24884401614d7188953a77ae809a8..72817e7ede06f8fe9607a2c17d8d99a503ea6c8e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <deque>
 #include <map>
+#include <set>
 #include <string>
 #include <msp/io/base.h>
 #include "programsyntax.h"
@@ -13,26 +14,63 @@ namespace GL {
 class ProgramParser
 {
 private:
+       enum OperatorType
+       {
+               NO_OPERATOR,
+               BINARY,
+               PREFIX,
+               POSTFIX
+       };
+
+       enum Associativity
+       {
+               LEFT_TO_RIGHT,
+               RIGHT_TO_LEFT
+       };
+
+       struct Operator
+       {
+               const char token[4];
+               unsigned precedence;
+               OperatorType type;
+               Associativity assoc;
+       };
+
        std::string source;
+       std::string source_name;
+       unsigned current_line;
        std::string::const_iterator iter;
+       std::string::const_iterator source_end;
+       bool allow_preprocess;
+       bool allow_stage_change;
+       std::string last_token;
        std::deque<std::string> next_tokens;
-       ProgramSyntax::Module main_module;
-       ProgramSyntax::Module *cur_module;
+       ProgramSyntax::Module *module;
+       ProgramSyntax::Stage *cur_stage;
+       std::set<std::string> declared_types;
+
+       static Operator operators[];
 
 public:
-       ProgramSyntax::Module &parse(const std::string &);
-       ProgramSyntax::Module &parse(IO::Base &);
+       ProgramParser();
+       ~ProgramParser();
+
+       ProgramSyntax::Module &parse(const std::string &, const std::string &);
+       ProgramSyntax::Module &parse(IO::Base &, const std::string &);
 
 private:
-       void parse_source(ProgramSyntax::Module &);
+       void parse_source();
+
+       std::string format_error(const std::string &);
+       std::string format_syntax_error(const std::string &);
 
        const std::string &peek_token(unsigned = 0);
-       std::string parse_token();
+       const std::string &parse_token();
        std::string parse_token_();
        std::string parse_identifier();
        std::string parse_number();
        std::string parse_other();
-       bool skip_comment_and_whitespace();
+       void skip_comment_and_whitespace();
        void expect(const std::string &);
        std::string expect_type();
        std::string expect_identifier();
@@ -40,23 +78,36 @@ private:
 
        static bool is_interface_qualifier(const std::string &);
        static bool is_sampling_qualifier(const std::string &);
+       static bool is_interpolation_qualifier(const std::string &);
+       static bool is_precision_qualifier(const std::string &);
        static bool is_qualifier(const std::string &);
        static bool is_builtin_type(const std::string &);
        bool is_type(const std::string &);
+       bool is_identifier(const std::string &);
+
+       void preprocess();
+       void preprocess_pragma();
+       void preprocess_pragma_msp();
+       void preprocess_stage();
 
-       ProgramSyntax::Node *parse_global_declaration();
-       ProgramSyntax::Node *parse_statement();
-       ProgramSyntax::Layout *parse_layout();
+       RefPtr<ProgramSyntax::Statement> parse_global_declaration();
+       RefPtr<ProgramSyntax::Statement> parse_statement();
+       RefPtr<ProgramSyntax::Import> parse_import();
+       RefPtr<ProgramSyntax::Precision> parse_precision();
+       RefPtr<ProgramSyntax::Layout> parse_layout();
        void parse_block(ProgramSyntax::Block &, bool);
-       void parse_expression(ProgramSyntax::Expression &);
-       ProgramSyntax::StructDeclaration *parse_struct_declaration();
-       ProgramSyntax::VariableDeclaration *parse_variable_declaration();
-       ProgramSyntax::FunctionDeclaration *parse_function_declaration();
-       void parse_function_parameter_list(ProgramSyntax::FunctionDeclaration &);
-       ProgramSyntax::InterfaceBlock *parse_interface_block();
-       ProgramSyntax::Conditional *parse_conditional();
-       ProgramSyntax::Iteration *parse_iteration();
-       ProgramSyntax::Return *parse_return();
+       RefPtr<ProgramSyntax::Expression> parse_expression(unsigned = 0);
+       RefPtr<ProgramSyntax::BinaryExpression> parse_binary(const RefPtr<ProgramSyntax::Expression> &, const Operator *);
+       RefPtr<ProgramSyntax::FunctionCall> parse_function_call(const ProgramSyntax::VariableReference &);
+       RefPtr<ProgramSyntax::StructDeclaration> parse_struct_declaration();
+       RefPtr<ProgramSyntax::VariableDeclaration> parse_variable_declaration();
+       RefPtr<ProgramSyntax::FunctionDeclaration> parse_function_declaration();
+       RefPtr<ProgramSyntax::InterfaceBlock> parse_interface_block();
+       RefPtr<ProgramSyntax::Conditional> parse_conditional();
+       RefPtr<ProgramSyntax::Iteration> parse_for();
+       RefPtr<ProgramSyntax::Iteration> parse_while();
+       RefPtr<ProgramSyntax::Passthrough> parse_passthrough();
+       RefPtr<ProgramSyntax::Return> parse_return();
 };
 
 } // namespace GL