-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessor.h
More file actions
320 lines (262 loc) · 12.2 KB
/
preprocessor.h
File metadata and controls
320 lines (262 loc) · 12.2 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Preprocessor 0.7
Copyright (c) 2005 Anthony Casteel
Copyright (c) 2015 Anton "Cvet" Tsvetinsky, Grzegorz "Atom" Jagiella
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
under addons & utilities or at
http://www.omnisu.com
Anthony Casteel
jm@omnisu.com
*/
/*
* This version has been modified and improved by Anton "Cvet" Tsvetinsky and Rotators team.
* http://github.com/rotators/angelscript-preprocessor/
*/
#ifndef PREPROCESSOR_H
#define PREPROCESSOR_H
#include <stdio.h>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <vector>
#define PREPROCESSOR_VERSION_STRING "0.7"
#ifndef UNUSED_VAR
#define UNUSED_VAR(x) (void)(x)
#endif
struct Preprocessor
{
/************************************************************************/
/* Streams */
/************************************************************************/
struct OutStream
{
virtual ~OutStream() {}
virtual void Write( const char* str, size_t len ) { UNUSED_VAR(str); UNUSED_VAR(len); }
OutStream& operator<<( const std::string& in )
{
Write( in.c_str(), in.length() );
return *this;
}
OutStream& operator<<( const char* in )
{
return operator<<( std::string( in ) );
}
template<typename T>
OutStream& operator<<( const T& in )
{
std::stringstream strstr;
strstr << in;
std::string str;
strstr >> str;
Write( str.c_str(), str.length() );
return *this;
}
};
struct StringOutStream: public OutStream
{
std::string String;
virtual ~StringOutStream() {}
virtual void Write( const char* str, size_t len )
{
String.append( str, len );
}
};
/************************************************************************/
/* Include file translator */
/************************************************************************/
struct IncludeFileTranslator
{
virtual ~IncludeFileTranslator() {};
virtual void Call( std::string& file ) = 0;
};
IncludeFileTranslator* IncludeTranslator;
/************************************************************************/
/* Line number translator */
/************************************************************************/
struct LineNumberTranslator
{
struct Entry
{
std::string File;
unsigned int StartLine;
unsigned int Offset;
};
std::vector<Entry> lines;
Entry& Search( unsigned int linenumber );
void AddLineRange( const std::string& file, unsigned int start_line, unsigned int offset );
};
/************************************************************************/
/* Lexems */
/************************************************************************/
struct Lexem
{
enum LexemType
{
IDENTIFIER, // Names which can be expanded.
COMMA, // ,
SEMICOLON,
OPEN, // {[(
CLOSE, // }])
PREPROCESSOR, // Begins with #
NEWLINE,
WHITESPACE,
IGNORED,
COMMENT,
STRING,
NUMBER,
BACKSLASH,
};
std::string Value;
LexemType Type;
};
typedef std::list<Lexem> LexemList;
typedef LexemList::iterator LLITR;
static const std::string Numbers;
static const std::string IdentifierStart;
static const std::string IdentifierBody;
static const std::string HexNumbers;
static const std::string Trivials;
static const Lexem::LexemType TrivialTypes[12];
/************************************************************************/
/* Loader */
/************************************************************************/
struct FileLoader
{
virtual ~FileLoader() {}
virtual bool LoadFile( const std::string& dir, const std::string& file_name, std::vector<char>& data );
};
/************************************************************************/
/* Define table */
/************************************************************************/
typedef std::map<std::string,int> ArgSet;
struct DefineEntry
{
LexemList Lexems;
ArgSet Arguments;
};
typedef std::map<std::string,DefineEntry> DefineTable;
DefineTable CustomDefines;
/************************************************************************/
/* Pragmas */
/************************************************************************/
struct Pragma
{
struct Instance
{
std::string Text;
std::string CurrentFile;
unsigned int CurrentFileLine;
std::string RootFile;
unsigned int GlobalLine;
};
struct Callback
{
virtual ~Callback() {}
virtual void CallPragma( const std::string& name, const Pragma::Instance& pi ) = 0;
};
};
Preprocessor();
/************************************************************************/
/* Pre preprocess settings */
/************************************************************************/
bool IsDefined( const std::string& str );
void Define( const std::string& str );
void Define( const std::string& str, const std::string& val );
void Undef( const std::string& str );
void UndefAll();
/************************************************************************/
/* Preprocess */
/************************************************************************/
int Preprocess( std::string file_path, OutStream& result, OutStream* errors = NULL, FileLoader* loader = NULL, bool skip_pragmas = false );
void PrintMessage( const std::string& msg );
void PrintWarningMessage( const std::string& warnmsg );
void PrintErrorMessage( const std::string& errmsg );
std::string PrependRootPath( const std::string& filename );
static std::string RemoveQuotes( const std::string& in );
static std::string IntToString( int i );
static bool SearchString( std::string str, char in );
static bool IsHex( char in );
static bool IsIdentifierStart( char in );
static bool IsIdentifierBody( char in );
static bool IsNumber( char in );
static bool IsTrivial( char in );
static char* ParseBlockComment( char* start, char* end, Lexem& out );
static char* ParseCharacterLiteral( char* start, char* end, Lexem& out );
void ParseDefine( DefineTable& define_table, LexemList& def_lexems );
LLITR ParseDefineArguments( LLITR itr, LLITR end, LexemList& lexems, std::vector<LexemList>& args );
static char* ParseFloatingPoint( char* start, char* end, Lexem& out );
void ParseIf( LexemList& directive, std::string& name_out );
LLITR ParseIfDef( LLITR itr, LLITR end );
void ParseUndef( LexemList& directive, DefineTable& define_table );
static char* ParseHexConstant( char* start, char* end, Lexem& out );
static char* ParseIdentifier( char* start, char* end, Lexem& out );
static char* ParseLexem( char* start, char* end, Lexem& out );
static char* ParseLineComment( char* start, char* end, Lexem& out );
static char* ParseNumber( char* start, char* end, Lexem& out );
static LLITR ParsePreprocessor( LexemList& lexems, LLITR itr, LLITR end );
static char* ParseStringLiteral( char* start, char* end, char quote, Lexem& out );
LLITR ParseStatement( LLITR itr, LLITR end, LexemList& dest );
static int Lex( char* begin, char* end, std::list<Lexem>& results );
LLITR ExpandDefine( LLITR itr, LLITR ent, LexemList& lexems, DefineTable& define_table );
bool ConvertExpression( LexemList& expression, LexemList& output );
int EvaluateConvertedExpression( DefineTable& define_table, LexemList& expr );
bool EvaluateExpression( DefineTable& define_table, LexemList& directive );
static std::string AddPaths( const std::string& first, const std::string& second );
void ParsePragma( LexemList& args );
static void ParseTextLine( LexemList& directive, std::string& message );
static void SetLineMacro( DefineTable& define_table, unsigned int line );
static void SetFileMacro( DefineTable& define_table, const std::string& file );
void RecursivePreprocess( std::string filename, FileLoader& file_source, LexemList& lexems, DefineTable& define_table );
static void PrintLexemList( LexemList& out, OutStream& destination );
/************************************************************************/
/* Expressions */
/************************************************************************/
static void PreprocessLexem( LLITR it, LexemList& lexems );
static bool IsOperator( const Lexem& lexem );
static bool IsIdentifier( const Lexem& lexem );
static bool IsLeft( const Lexem& lexem );
static bool IsRight( const Lexem& lexem );
static int OperPrecedence( const Lexem& lexem );
static bool OperLeftAssoc( const Lexem& lexem );
LineNumberTranslator* GetLineNumberTranslator();
std::string ResolveOriginalFile( unsigned int line_number, LineNumberTranslator* lnt = NULL );
unsigned int ResolveOriginalLine( unsigned int line_number, LineNumberTranslator* lnt = NULL );
std::vector<std::string>& GetFileDependencies();
std::vector<std::string>& GetFilesPreprocessed();
std::vector<std::string>& GetParsedPragmas();
Pragma::Callback* CurPragmaCallback;
void SetPragmaCallback( Pragma::Callback* callback );
void CallPragma( const std::string& name, std::string pragma );
/************************************************************************/
/* */
/************************************************************************/
OutStream* Errors;
unsigned int ErrorsCount;
LineNumberTranslator* LNT;
std::string RootFile;
std::string RootPath;
std::string CurrentFile;
unsigned int CurrentLine;
unsigned int LinesThisFile;
bool SkipPragmas;
std::vector<std::string> FileDependencies;
std::vector<std::string> FilesPreprocessed;
std::vector<std::string> Pragmas;
};
#endif // PREPROCESSOR_H