| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/string/ustring.h" |
| 34 |
#include "core/templates/list.h" |
| 35 |
#include "core/templates/local_vector.h" |
| 36 |
#include "core/templates/rb_map.h" |
| 37 |
#include "core/templates/rb_set.h" |
| 38 |
|
| 39 |
#include "core/object/script_language.h" |
| 40 |
#include "scene/resources/shader_include.h" |
| 41 |
|
| 42 |
class ShaderPreprocessor { |
| 43 |
public: |
| 44 |
enum CompletionType { |
| 45 |
COMPLETION_TYPE_NONE, |
| 46 |
COMPLETION_TYPE_DIRECTIVE, |
| 47 |
COMPLETION_TYPE_PRAGMA_DIRECTIVE, |
| 48 |
COMPLETION_TYPE_PRAGMA, |
| 49 |
COMPLETION_TYPE_CONDITION, |
| 50 |
COMPLETION_TYPE_INCLUDE_PATH, |
| 51 |
}; |
| 52 |
|
| 53 |
struct FilePosition { |
| 54 |
String file; |
| 55 |
int line = 0; |
| 56 |
}; |
| 57 |
|
| 58 |
struct Region { |
| 59 |
String file; |
| 60 |
int from_line = -1; |
| 61 |
int to_line = -1; |
| 62 |
bool enabled = false; |
| 63 |
Region *parent = nullptr; |
| 64 |
}; |
| 65 |
|
| 66 |
private: |
| 67 |
struct Token { |
| 68 |
char32_t text; |
| 69 |
int line; |
| 70 |
|
| 71 |
Token(); |
| 72 |
Token(char32_t p_text, int p_line); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
class Tokenizer { |
| 77 |
public: |
| 78 |
String code; |
| 79 |
int line; |
| 80 |
int index; |
| 81 |
int size; |
| 82 |
LocalVector<Token> generated; |
| 83 |
|
| 84 |
private: |
| 85 |
void add_generated(const Token &p_t); |
| 86 |
char32_t next(); |
| 87 |
|
| 88 |
public: |
| 89 |
int get_line() const; |
| 90 |
int get_index() const; |
| 91 |
char32_t peek(); |
| 92 |
int consume_line_continuations(int p_offset); |
| 93 |
|
| 94 |
void get_and_clear_generated(LocalVector<char32_t> *r_out); |
| 95 |
void backtrack(char32_t p_what); |
| 96 |
LocalVector<Token> advance(char32_t p_what); |
| 97 |
void skip_whitespace(); |
| 98 |
bool consume_empty_line(); |
| 99 |
String get_identifier(bool *r_is_cursor = nullptr, bool p_started = false); |
| 100 |
String peek_identifier(); |
| 101 |
Token get_token(); |
| 102 |
|
| 103 |
Tokenizer(const String &p_code); |
| 104 |
}; |
| 105 |
|
| 106 |
class CommentRemover { |
| 107 |
private: |
| 108 |
LocalVector<char32_t> stripped; |
| 109 |
String code; |
| 110 |
int index; |
| 111 |
int line; |
| 112 |
int comment_line_open; |
| 113 |
int comments_open; |
| 114 |
int strings_open; |
| 115 |
|
| 116 |
public: |
| 117 |
String get_error() const; |
| 118 |
int get_error_line() const; |
| 119 |
char32_t peek() const; |
| 120 |
|
| 121 |
bool advance(char32_t p_what); |
| 122 |
String strip(); |
| 123 |
|
| 124 |
CommentRemover(const String &p_code); |
| 125 |
}; |
| 126 |
|
| 127 |
struct Define { |
| 128 |
Vector<String> arguments; |
| 129 |
String body; |
| 130 |
bool is_builtin = false; |
| 131 |
}; |
| 132 |
|
| 133 |
struct Branch { |
| 134 |
Vector<bool> conditions; |
| 135 |
Branch *parent = nullptr; |
| 136 |
bool else_defined = false; |
| 137 |
|
| 138 |
Branch() {} |
| 139 |
|
| 140 |
Branch(bool p_condition, Branch *p_parent) : |
| 141 |
parent(p_parent) { |
| 142 |
conditions.push_back(p_condition); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
struct State { |
| 147 |
RBMap<String, Define *> defines; |
| 148 |
List<Branch> branches; |
| 149 |
Branch *current_branch = nullptr; |
| 150 |
int condition_depth = 0; |
| 151 |
RBSet<String> includes; |
| 152 |
List<uint64_t> cyclic_include_hashes; |
| 153 |
int include_depth = 0; |
| 154 |
String current_filename; |
| 155 |
String current_shader_type; |
| 156 |
String error; |
| 157 |
List<FilePosition> include_positions; |
| 158 |
bool save_regions = false; |
| 159 |
RBMap<String, List<Region>> regions; |
| 160 |
Region *previous_region = nullptr; |
| 161 |
bool disabled = false; |
| 162 |
CompletionType completion_type = COMPLETION_TYPE_NONE; |
| 163 |
HashSet<Ref<ShaderInclude>> shader_includes; |
| 164 |
bool completion_show_defines = false; |
| 165 |
}; |
| 166 |
|
| 167 |
private: |
| 168 |
LocalVector<char32_t> output; |
| 169 |
State *state = nullptr; |
| 170 |
|
| 171 |
private: |
| 172 |
static bool is_char_word(char32_t p_char); |
| 173 |
static bool is_char_space(char32_t p_char); |
| 174 |
static bool is_char_end(char32_t p_char); |
| 175 |
static String vector_to_string(const LocalVector<char32_t> &p_v, int p_start = 0, int p_end = -1); |
| 176 |
static String tokens_to_string(const LocalVector<Token> &p_tokens); |
| 177 |
|
| 178 |
void _set_expected_error(const String &p_what, int p_line) { |
| 179 |
set_error(vformat(RTR("Expected a '%s'."), p_what), p_line); |
| 180 |
} |
| 181 |
|
| 182 |
void _set_unexpected_token_error(const String &p_what, int p_line) { |
| 183 |
set_error(vformat(RTR("Unexpected token: '%s'."), p_what), p_line); |
| 184 |
} |
| 185 |
|
| 186 |
void process_directive(Tokenizer *p_tokenizer); |
| 187 |
void process_define(Tokenizer *p_tokenizer); |
| 188 |
void process_elif(Tokenizer *p_tokenizer); |
| 189 |
void process_else(Tokenizer *p_tokenizer); |
| 190 |
void process_endif(Tokenizer *p_tokenizer); |
| 191 |
void process_error(Tokenizer *p_tokenizer); |
| 192 |
void process_if(Tokenizer *p_tokenizer); |
| 193 |
void process_ifdef(Tokenizer *p_tokenizer); |
| 194 |
void process_ifndef(Tokenizer *p_tokenizer); |
| 195 |
void process_include(Tokenizer *p_tokenizer); |
| 196 |
void process_pragma(Tokenizer *p_tokenizer); |
| 197 |
void process_undef(Tokenizer *p_tokenizer); |
| 198 |
|
| 199 |
void add_region(int p_line, bool p_enabled, Region *p_parent_region); |
| 200 |
void start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue = false); |
| 201 |
|
| 202 |
Error expand_condition(const String &p_string, int p_line, String &r_result); |
| 203 |
void expand_output_macros(int p_start, int p_line); |
| 204 |
Error expand_macros(const String &p_string, int p_line, String &r_result); |
| 205 |
bool expand_macros_once(const String &p_line, int p_line_number, const RBMap<String, Define *>::Element *p_define_pair, String &r_expanded); |
| 206 |
bool find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start); |
| 207 |
void concatenate_macro_body(String &r_body); |
| 208 |
|
| 209 |
String next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives); |
| 210 |
void add_to_output(const String &p_str); |
| 211 |
void set_error(const String &p_error, int p_line); |
| 212 |
|
| 213 |
static Define *create_define(const String &p_body); |
| 214 |
void insert_builtin_define(String p_name, String p_value, State &p_state); |
| 215 |
|
| 216 |
void clear_state(); |
| 217 |
|
| 218 |
Error preprocess(State *p_state, const String &p_code, String &r_result); |
| 219 |
|
| 220 |
public: |
| 221 |
typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *); |
| 222 |
|
| 223 |
Error preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text = nullptr, List<FilePosition> *r_error_position = nullptr, List<Region> *r_regions = nullptr, HashSet<Ref<ShaderInclude>> *r_includes = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_options = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines = nullptr, IncludeCompletionFunction p_include_completion_func = nullptr); |
| 224 |
|
| 225 |
static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords = false); |
| 226 |
static void get_pragma_list(List<String> *r_pragmas); |
| 227 |
|
| 228 |
ShaderPreprocessor(); |
| 229 |
~ShaderPreprocessor(); |
| 230 |
}; |