doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __PARSER_H__
30 #define __PARSER_H__
31 
32 /*
33 ===============================================================================
34 
35  C/C++ compatible pre-compiler
36 
37 ===============================================================================
38 */
39 
40 #define DEFINE_FIXED 0x0001
41 
42 #define BUILTIN_LINE 1
43 #define BUILTIN_FILE 2
44 #define BUILTIN_DATE 3
45 #define BUILTIN_TIME 4
46 #define BUILTIN_STDC 5
47 
48 #define INDENT_IF 0x0001
49 #define INDENT_ELSE 0x0002
50 #define INDENT_ELIF 0x0004
51 #define INDENT_IFDEF 0x0008
52 #define INDENT_IFNDEF 0x0010
53 
54 // macro definitions
55 typedef struct define_s {
56  char * name; // define name
57  int flags; // define flags
58  int builtin; // > 0 if builtin define
59  int numparms; // number of define parameters
60  idToken * parms; // define parameters
61  idToken * tokens; // macro tokens (possibly containing parm tokens)
62  struct define_s *next; // next defined macro in a list
63  struct define_s *hashnext; // next define in the hash chain
64 } define_t;
65 
66 // indents used for conditional compilation directives:
67 // #if, #else, #elif, #ifdef, #ifndef
68 typedef struct indent_s {
69  int type; // indent type
70  int skip; // true if skipping current indent
71  idLexer * script; // script the indent was in
72  struct indent_s *next; // next indent on the indent stack
73 } indent_t;
74 
75 
76 class idParser {
77 
78 public:
79  // constructor
80  idParser();
81  idParser( int flags );
82  idParser( const char *filename, int flags = 0, bool OSPath = false );
83  idParser( const char *ptr, int length, const char *name, int flags = 0 );
84  // destructor
85  ~idParser();
86  // load a source file
87  int LoadFile( const char *filename, bool OSPath = false );
88  // load a source from the given memory with the given length
89  // NOTE: the ptr is expected to point at a valid C string: ptr[length] == '\0'
90  int LoadMemory( const char *ptr, int length, const char *name );
91  // free the current source
92  void FreeSource( bool keepDefines = false );
93  // returns true if a source is loaded
94  int IsLoaded( void ) const { return idParser::loaded; }
95  // read a token from the source
96  int ReadToken( idToken *token );
97  // expect a certain token, reads the token when available
98  int ExpectTokenString( const char *string );
99  // expect a certain token type
100  int ExpectTokenType( int type, int subtype, idToken *token );
101  // expect a token
102  int ExpectAnyToken( idToken *token );
103  // returns true if the next token equals the given string and removes the token from the source
104  int CheckTokenString( const char *string );
105  // returns true if the next token equals the given type and removes the token from the source
106  int CheckTokenType( int type, int subtype, idToken *token );
107  // returns true if the next token equals the given string but does not remove the token from the source
108  int PeekTokenString( const char *string );
109  // returns true if the next token equals the given type but does not remove the token from the source
110  int PeekTokenType( int type, int subtype, idToken *token );
111  // skip tokens until the given token string is read
112  int SkipUntilString( const char *string );
113  // skip the rest of the current line
114  int SkipRestOfLine( void );
115  // skip the braced section
116  int SkipBracedSection( bool parseFirstBrace = true );
117  // parse a braced section into a string
118  const char * ParseBracedSection( idStr &out, int tabs = -1 );
119  // parse a braced section into a string, maintaining indents and newlines
120  const char * ParseBracedSectionExact( idStr &out, int tabs = -1 );
121  // parse the rest of the line
122  const char * ParseRestOfLine( idStr &out );
123  // unread the given token
124  void UnreadToken( idToken *token );
125  // read a token only if on the current line
126  int ReadTokenOnLine( idToken *token );
127  // read a signed integer
128  int ParseInt( void );
129  // read a boolean
130  bool ParseBool( void );
131  // read a floating point number
132  float ParseFloat( void );
133  // parse matrices with floats
134  int Parse1DMatrix( int x, float *m );
135  int Parse2DMatrix( int y, int x, float *m );
136  int Parse3DMatrix( int z, int y, int x, float *m );
137  // get the white space before the last read token
138  int GetLastWhiteSpace( idStr &whiteSpace ) const;
139  // Set a marker in the source file (there is only one marker)
140  void SetMarker( void );
141  // Get the string from the marker to the current position
142  void GetStringFromMarker( idStr& out, bool clean = false );
143  // add a define to the source
144  int AddDefine( const char *string );
145  // add builtin defines
146  void AddBuiltinDefines( void );
147  // set the source include path
148  void SetIncludePath( const char *path );
149  // set the punctuation set
150  void SetPunctuations( const punctuation_t *p );
151  // returns a pointer to the punctuation with the given id
152  const char * GetPunctuationFromId( int id );
153  // get the id for the given punctuation
154  int GetPunctuationId( const char *p );
155  // set lexer flags
156  void SetFlags( int flags );
157  // get lexer flags
158  int GetFlags( void ) const;
159  // returns the current filename
160  const char * GetFileName( void ) const;
161  // get current offset in current script
162  const int GetFileOffset( void ) const;
163  // get file time for current script
164  const ID_TIME_T GetFileTime( void ) const;
165  // returns the current line number
166  const int GetLineNum( void ) const;
167  // print an error message
168  void Error( const char *str, ... ) const id_attribute((format(printf,2,3)));
169  // print a warning message
170  void Warning( const char *str, ... ) const id_attribute((format(printf,2,3)));
171 
172  // add a global define that will be added to all opened sources
173  static int AddGlobalDefine( const char *string );
174  // remove the given global define
175  static int RemoveGlobalDefine( const char *name );
176  // remove all global defines
177  static void RemoveAllGlobalDefines( void );
178  // set the base folder to load files from
179  static void SetBaseFolder( const char *path );
180 
181 private:
182  int loaded; // set when a source file is loaded from file or memory
183  idStr filename; // file name of the script
184  idStr includepath; // path to include files
185  bool OSPath; // true if the file was loaded from an OS path
186  const punctuation_t *punctuations; // punctuations to use
187  int flags; // flags used for script parsing
188  idLexer * scriptstack; // stack with scripts of the source
189  idToken * tokens; // tokens to read first
190  define_t * defines; // list with macro definitions
191  define_t ** definehash; // hash chain with defines
192  indent_t * indentstack; // stack with indents
193  int skip; // > 0 if skipping conditional code
195 
196  static define_t *globaldefines; // list with global defines added to every source loaded
197 
198 private:
199  void PushIndent( int type, int skip );
200  void PopIndent( int *type, int *skip );
201  void PushScript( idLexer *script );
202  int ReadSourceToken( idToken *token );
203  int ReadLine( idToken *token );
204  int UnreadSourceToken( idToken *token );
205  int ReadDefineParms( define_t *define, idToken **parms, int maxparms );
206  int StringizeTokens( idToken *tokens, idToken *token );
207  int MergeTokens( idToken *t1, idToken *t2 );
208  int ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
209  int ExpandDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
210  int ExpandDefineIntoSource( idToken *deftoken, define_t *define );
211  void AddGlobalDefinesToSource( void );
212  define_t * CopyDefine( define_t *define );
213  define_t * FindHashedDefine(define_t **definehash, const char *name);
214  int FindDefineParm( define_t *define, const char *name );
215  void AddDefineToHash(define_t *define, define_t **definehash);
216  static void PrintDefine( define_t *define );
217  static void FreeDefine( define_t *define );
218  static define_t *FindDefine( define_t *defines, const char *name );
219  static define_t *DefineFromString( const char *string);
220  define_t * CopyFirstDefine( void );
221  int Directive_include( void );
222  int Directive_undef( void );
223  int Directive_if_def( int type );
224  int Directive_ifdef( void );
225  int Directive_ifndef( void );
226  int Directive_else( void );
227  int Directive_endif( void );
228  int EvaluateTokens( idToken *tokens, signed long int *intvalue, double *floatvalue, int integer );
229  int Evaluate( signed long int *intvalue, double *floatvalue, int integer );
230  int DollarEvaluate( signed long int *intvalue, double *floatvalue, int integer);
231  int Directive_define( void );
232  int Directive_elif( void );
233  int Directive_if( void );
234  int Directive_line( void );
235  int Directive_error( void );
236  int Directive_warning( void );
237  int Directive_pragma( void );
238  void UnreadSignToken( void );
239  int Directive_eval( void );
240  int Directive_evalfloat( void );
241  int ReadDirective( void );
242  int DollarDirective_evalint( void );
243  int DollarDirective_evalfloat( void );
244  int ReadDollarDirective( void );
245 };
246 
247 ID_INLINE const char *idParser::GetFileName( void ) const {
248  if ( idParser::scriptstack ) {
250  }
251  else {
252  return "";
253  }
254 }
255 
256 ID_INLINE const int idParser::GetFileOffset( void ) const {
257  if ( idParser::scriptstack ) {
259  }
260  else {
261  return 0;
262  }
263 }
264 
265 ID_INLINE const ID_TIME_T idParser::GetFileTime( void ) const {
266  if ( idParser::scriptstack ) {
268  }
269  else {
270  return 0;
271  }
272 }
273 
274 ID_INLINE const int idParser::GetLineNum( void ) const {
275  if ( idParser::scriptstack ) {
277  }
278  else {
279  return 0;
280  }
281 }
282 
283 #endif /* !__PARSER_H__ */
int type
Definition: Parser.h:69
void Error(const char *str,...) const id_attribute((format(printf
Definition: Parser.cpp:318
idToken * tokens
Definition: Parser.h:61
static define_t * DefineFromString(const char *string)
Definition: Parser.cpp:295
int loaded
Definition: Parser.h:182
int builtin
Definition: Parser.h:58
int AddDefine(const char *string)
Definition: Parser.cpp:1177
void void static int AddGlobalDefine(const char *string)
Definition: Parser.cpp:54
void UnreadToken(idToken *token)
Definition: Parser.cpp:2745
int ExpandDefineIntoSource(idToken *deftoken, define_t *define)
Definition: Parser.cpp:890
const char * GetFileName(void)
Definition: Lexer.h:282
const int GetFileOffset(void) const
Definition: Parser.h:256
int MergeTokens(idToken *t1, idToken *t2)
Definition: Parser.cpp:596
int Parse3DMatrix(int z, int y, int x, float *m)
Definition: Parser.cpp:2880
int DollarDirective_evalfloat(void)
Definition: Parser.cpp:2274
define_t * defines
Definition: Parser.h:190
char * name
Definition: Parser.h:56
GLenum GLsizei GLenum format
Definition: glext.h:2846
int SkipUntilString(const char *string)
Definition: Parser.cpp:2575
#define private
Definition: TypeInfo.cpp:30
#define const
Definition: getdate.c:251
int EvaluateTokens(idToken *tokens, signed long int *intvalue, double *floatvalue, int integer)
Definition: Parser.cpp:1368
int UnreadSourceToken(idToken *token)
Definition: Parser.cpp:468
~idParser()
Definition: Parser.cpp:3249
const ID_TIME_T GetFileTime(void)
Definition: Lexer.h:290
GLenum GLint GLint y
Definition: glext.h:2849
int Directive_eval(void)
Definition: Parser.cpp:2103
int ReadDefineParms(define_t *define, idToken **parms, int maxparms)
Definition: Parser.cpp:482
int ReadDirective(void)
Definition: Parser.cpp:2163
define_t * CopyFirstDefine(void)
Definition: Parser.cpp:658
int Directive_if(void)
Definition: Parser.cpp:2007
int ExpandDefine(idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken)
Definition: Parser.cpp:763
struct indent_s indent_t
int FindDefineParm(define_t *define, const char *name)
Definition: Parser.cpp:214
int skip
Definition: Parser.h:70
int LoadMemory(const char *ptr, int length, const char *name)
Definition: Parser.cpp:3049
int DollarEvaluate(signed long int *intvalue, double *floatvalue, int integer)
Definition: Parser.cpp:1884
void FreeSource(bool keepDefines=false)
Definition: Parser.cpp:3084
idToken * tokens
Definition: Parser.h:189
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
int ExpectTokenString(const char *string)
Definition: Parser.cpp:2402
void SetIncludePath(const char *path)
Definition: Parser.cpp:2967
Definition: Token.h:71
int flags
Definition: Parser.h:187
const char * ParseBracedSection(idStr &out, int tabs=-1)
Definition: Parser.cpp:2655
idToken * parms
Definition: Parser.h:60
GLenum GLint x
Definition: glext.h:2849
int IsLoaded(void) const
Definition: Parser.h:94
define_t * FindHashedDefine(define_t **definehash, const char *name)
Definition: Parser.cpp:180
int Directive_include(void)
Definition: Parser.cpp:936
const ID_TIME_T GetFileTime(void) const
Definition: Parser.h:265
idLexer * scriptstack
Definition: Parser.h:188
const int GetFileOffset(void)
Definition: Lexer.h:286
int ReadToken(idToken *token)
Definition: Parser.cpp:2338
const int GetLineNum(void)
Definition: Lexer.h:294
int Directive_elif(void)
Definition: Parser.cpp:1985
Definition: Lexer.h:137
bool ParseBool(void)
Definition: Parser.cpp:2797
int GetFlags(void) const
Definition: Parser.cpp:3004
void GetStringFromMarker(idStr &out, bool clean=false)
Definition: Parser.cpp:2929
int Directive_warning(void)
Definition: Parser.cpp:2054
int Directive_pragma(void)
Definition: Parser.cpp:2070
const int GetLineNum(void) const
Definition: Parser.h:274
int ParseInt(void)
Definition: Parser.cpp:2775
int Directive_line(void)
Definition: Parser.cpp:2024
int PeekTokenString(const char *string)
Definition: Parser.cpp:2532
int Directive_error(void)
Definition: Parser.cpp:2038
int Parse1DMatrix(int x, float *m)
Definition: Parser.cpp:2834
idParser()
Definition: Parser.cpp:3175
int Directive_ifndef(void)
Definition: Parser.cpp:1241
define_t * CopyDefine(define_t *define)
Definition: Parser.cpp:233
static int RemoveGlobalDefine(const char *name)
Definition: Parser.cpp:71
int Directive_else(void)
Definition: Parser.cpp:1250
void AddGlobalDefinesToSource(void)
Definition: Parser.cpp:1193
indent_t * indentstack
Definition: Parser.h:192
static void PrintDefine(define_t *define)
Definition: Parser.cpp:120
void PopIndent(int *type, int *skip)
Definition: Parser.cpp:369
int GetPunctuationId(const char *p)
Definition: Parser.cpp:3154
void void Warning(const char *str,...) const id_attribute((format(printf
Definition: Parser.cpp:335
const char * path
Definition: sws.c:117
int SkipRestOfLine(void)
Definition: Parser.cpp:2591
float ParseFloat(void)
Definition: Parser.cpp:2812
void SetFlags(int flags)
Definition: Parser.cpp:2990
const char * GetPunctuationFromId(int id)
Definition: Parser.cpp:3133
static void FreeDefine(define_t *define)
Definition: Parser.cpp:273
const punctuation_t * punctuations
Definition: Parser.h:186
int Evaluate(signed long int *intvalue, double *floatvalue, int integer)
Definition: Parser.cpp:1793
int Directive_undef(void)
Definition: Parser.cpp:1016
define_t ** definehash
Definition: Parser.h:191
int ExpectAnyToken(idToken *token)
Definition: Parser.cpp:2476
idLexer * script
Definition: Parser.h:71
const char * ParseBracedSectionExact(idStr &out, int tabs=-1)
Definition: Parser.cpp:2642
struct indent_s * next
Definition: Parser.h:72
idStr filename
Definition: Parser.h:183
void AddDefineToHash(define_t *define, define_t **definehash)
Definition: Parser.cpp:167
void SetPunctuations(const punctuation_t *p)
Definition: Parser.cpp:2981
int ReadDollarDirective(void)
Definition: Parser.cpp:2306
int Directive_if_def(int type)
Definition: Parser.cpp:1207
int ReadSourceToken(idToken *token)
Definition: Parser.cpp:414
bool OSPath
Definition: Parser.h:185
int ExpandBuiltinDefine(idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken)
Definition: Parser.cpp:674
int DollarDirective_evalint(void)
Definition: Parser.cpp:2242
static define_t * FindDefine(define_t *defines, const char *name)
Definition: Parser.cpp:198
struct define_s * hashnext
Definition: Parser.h:63
struct define_s define_t
int Directive_define(void)
Definition: Parser.cpp:1060
#define id_attribute(x)
Definition: sys_public.h:139
void UnreadSignToken(void)
Definition: Parser.cpp:2084
int numparms
Definition: Parser.h:59
idStr includepath
Definition: Parser.h:184
void PushIndent(int type, int skip)
Definition: Parser.cpp:352
static void SetBaseFolder(const char *path)
Definition: Parser.cpp:45
int LoadFile(const char *filename, bool OSPath=false)
Definition: Parser.cpp:3013
const char * ParseRestOfLine(idStr &out)
Definition: Parser.cpp:2723
const GLcharARB * name
Definition: glext.h:3629
void PushScript(idLexer *script)
Definition: Parser.cpp:395
int PeekTokenType(int type, int subtype, idToken *token)
Definition: Parser.cpp:2553
int CheckTokenString(const char *string)
Definition: Parser.cpp:2491
Definition: Str.h:116
GLsizei const GLcharARB const GLint * length
Definition: glext.h:3599
void SetMarker(void)
Definition: Parser.cpp:2918
int SkipBracedSection(bool parseFirstBrace=true)
Definition: Parser.cpp:2611
static define_t * globaldefines
Definition: Parser.h:196
int Directive_ifdef(void)
Definition: Parser.cpp:1232
int ReadTokenOnLine(idToken *token)
Definition: Parser.cpp:2754
static void RemoveAllGlobalDefines(void)
Definition: Parser.cpp:97
const char * marker_p
Definition: Parser.h:194
int skip
Definition: Parser.h:193
struct define_s * next
Definition: Parser.h:62
void AddBuiltinDefines(void)
Definition: Parser.cpp:623
GLfloat GLfloat p
Definition: glext.h:4674
GLdouble GLdouble z
Definition: glext.h:3067
int flags
Definition: Parser.h:57
int StringizeTokens(idToken *tokens, idToken *token)
Definition: Parser.cpp:578
int Parse2DMatrix(int y, int x, float *m)
Definition: Parser.cpp:2856
int ReadLine(idToken *token)
Definition: Parser.cpp:913
const char * GetFileName(void) const
Definition: Parser.h:247
int CheckTokenType(int type, int subtype, idToken *token)
Definition: Parser.cpp:2511
int ExpectTokenType(int type, int subtype, idToken *token)
Definition: Parser.cpp:2422
int Directive_endif(void)
Definition: Parser.cpp:1271
int Directive_evalfloat(void)
Definition: Parser.cpp:2133
int GetLastWhiteSpace(idStr &whiteSpace) const
Definition: Parser.cpp:2904