forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessor.hpp
109 lines (95 loc) · 3.06 KB
/
preprocessor.hpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#pragma once
#include <stack>
#include <vector>
#include <unordered_map>
#include <memory>
#include "lexer.hpp"
#include "filesystem.hpp"
namespace reshadefx
{
class preprocessor
{
public:
struct macro
{
std::string replacement_list;
bool is_function_like = false, is_variadic = false;
std::vector<std::string> parameters;
};
void add_include_path(const reshade::filesystem::path &path);
bool add_macro_definition(const std::string &name, const macro ¯o);
bool add_macro_definition(const std::string &name, const std::string &value = "1");
const std::string ¤t_output() const { return _output; }
const std::string ¤t_errors() const { return _errors; }
const std::vector<std::string> ¤t_pragmas() const { return _pragmas; }
bool run(const reshade::filesystem::path &file_path);
bool run(const reshade::filesystem::path &file_path, std::vector<reshade::filesystem::path> &included_files);
private:
struct if_level
{
lexer::token token;
bool value, skipping;
if_level *parent;
};
struct input_level
{
input_level(const std::string &name, const std::string &text, input_level *parent) :
_name(name),
_lexer(new lexer(text, false, false, true, false)),
_parent(parent)
{
_next_token.id = lexer::tokenid::unknown;
_next_token.offset = _next_token.length = 0;
}
std::string _name;
std::unique_ptr<lexer> _lexer;
lexer::token _next_token;
size_t _offset;
std::stack<if_level> _if_stack;
input_level *_parent;
};
void error(const location &location, const std::string &message);
void warning(const location &location, const std::string &message);
lexer ¤t_lexer();
inline lexer::token current_token() const { return _token; }
std::stack<if_level> ¤t_if_stack();
if_level ¤t_if_level();
void push(const std::string &input, const std::string &name = std::string());
bool peek(lexer::tokenid token) const;
void consume();
void consume_until(lexer::tokenid token);
bool accept(lexer::tokenid token);
bool expect(lexer::tokenid token);
void parse();
void parse_def();
void parse_undef();
void parse_if();
void parse_ifdef();
void parse_ifndef();
void parse_elif();
void parse_else();
void parse_endif();
void parse_error();
void parse_warning();
void parse_pragma();
void parse_include();
bool evaluate_expression();
bool evaluate_identifier_as_macro();
void expand_macro(const macro ¯o, const std::vector<std::string> &arguments, std::string &out);
void create_macro_replacement_list(macro ¯o);
bool _success = true;
lexer::token _token;
std::stack<input_level> _input_stack;
location _output_location;
std::string _output, _errors, _current_token_raw_data;
int _recursion_count = 0;
std::unordered_map<std::string, macro> _macros;
std::vector<std::string> _pragmas;
std::vector<reshade::filesystem::path> _include_paths;
std::unordered_map<std::string, std::string> _filecache;
};
}