Parser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __PARSER_H__
  21. #define __PARSER_H__
  22. /*
  23. ===============================================================================
  24. C/C++ compatible pre-compiler
  25. ===============================================================================
  26. */
  27. #define DEFINE_FIXED 0x0001
  28. #define BUILTIN_LINE 1
  29. #define BUILTIN_FILE 2
  30. #define BUILTIN_DATE 3
  31. #define BUILTIN_TIME 4
  32. #define BUILTIN_STDC 5
  33. #define INDENT_IF 0x0001
  34. #define INDENT_ELSE 0x0002
  35. #define INDENT_ELIF 0x0004
  36. #define INDENT_IFDEF 0x0008
  37. #define INDENT_IFNDEF 0x0010
  38. // macro definitions
  39. typedef struct define_s {
  40. char * name; // define name
  41. int flags; // define flags
  42. int builtin; // > 0 if builtin define
  43. int numparms; // number of define parameters
  44. idToken * parms; // define parameters
  45. idToken * tokens; // macro tokens (possibly containing parm tokens)
  46. struct define_s *next; // next defined macro in a list
  47. struct define_s *hashnext; // next define in the hash chain
  48. } define_t;
  49. // indents used for conditional compilation directives:
  50. // #if, #else, #elif, #ifdef, #ifndef
  51. typedef struct indent_s {
  52. int type; // indent type
  53. int skip; // true if skipping current indent
  54. idLexer * script; // script the indent was in
  55. struct indent_s *next; // next indent on the indent stack
  56. } indent_t;
  57. class idParser {
  58. public:
  59. // constructor
  60. idParser();
  61. idParser( int flags );
  62. idParser( const char *filename, int flags = 0, bool OSPath = false );
  63. idParser( const char *ptr, int length, const char *name, int flags = 0 );
  64. // destructor
  65. ~idParser();
  66. // load a source file
  67. int LoadFile( const char *filename, bool OSPath = false );
  68. // load a source from the given memory with the given length
  69. // NOTE: the ptr is expected to point at a valid C string: ptr[length] == '\0'
  70. int LoadMemory( const char *ptr, int length, const char *name );
  71. // free the current source
  72. void FreeSource( bool keepDefines = false );
  73. // returns true if a source is loaded
  74. int IsLoaded( void ) const { return idParser::loaded; }
  75. // read a token from the source
  76. int ReadToken( idToken *token );
  77. // expect a certain token, reads the token when available
  78. int ExpectTokenString( const char *string );
  79. // expect a certain token type
  80. int ExpectTokenType( int type, int subtype, idToken *token );
  81. // expect a token
  82. int ExpectAnyToken( idToken *token );
  83. // returns true if the next token equals the given string and removes the token from the source
  84. int CheckTokenString( const char *string );
  85. // returns true if the next token equals the given type and removes the token from the source
  86. int CheckTokenType( int type, int subtype, idToken *token );
  87. // returns true if the next token equals the given string but does not remove the token from the source
  88. int PeekTokenString( const char *string );
  89. // returns true if the next token equals the given type but does not remove the token from the source
  90. int PeekTokenType( int type, int subtype, idToken *token );
  91. // skip tokens until the given token string is read
  92. int SkipUntilString( const char *string );
  93. // skip the rest of the current line
  94. int SkipRestOfLine( void );
  95. // skip the braced section
  96. int SkipBracedSection( bool parseFirstBrace = true );
  97. // parse a braced section into a string
  98. const char * ParseBracedSection( idStr &out, int tabs = -1 );
  99. // parse a braced section into a string, maintaining indents and newlines
  100. const char * ParseBracedSectionExact( idStr &out, int tabs = -1 );
  101. // parse the rest of the line
  102. const char * ParseRestOfLine( idStr &out );
  103. // unread the given token
  104. void UnreadToken( idToken *token );
  105. // read a token only if on the current line
  106. int ReadTokenOnLine( idToken *token );
  107. // read a signed integer
  108. int ParseInt( void );
  109. // read a boolean
  110. bool ParseBool( void );
  111. // read a floating point number
  112. float ParseFloat( void );
  113. // parse matrices with floats
  114. int Parse1DMatrix( int x, float *m );
  115. int Parse2DMatrix( int y, int x, float *m );
  116. int Parse3DMatrix( int z, int y, int x, float *m );
  117. // get the white space before the last read token
  118. int GetLastWhiteSpace( idStr &whiteSpace ) const;
  119. // Set a marker in the source file (there is only one marker)
  120. void SetMarker( void );
  121. // Get the string from the marker to the current position
  122. void GetStringFromMarker( idStr& out, bool clean = false );
  123. // add a define to the source
  124. int AddDefine( const char *string );
  125. // add builtin defines
  126. void AddBuiltinDefines( void );
  127. // set the source include path
  128. void SetIncludePath( const char *path );
  129. // set the punctuation set
  130. void SetPunctuations( const punctuation_t *p );
  131. // returns a pointer to the punctuation with the given id
  132. const char * GetPunctuationFromId( int id );
  133. // get the id for the given punctuation
  134. int GetPunctuationId( const char *p );
  135. // set lexer flags
  136. void SetFlags( int flags );
  137. // get lexer flags
  138. int GetFlags( void ) const;
  139. // returns the current filename
  140. const char * GetFileName( void ) const;
  141. // get current offset in current script
  142. const int GetFileOffset( void ) const;
  143. // get file time for current script
  144. const ID_TIME_T GetFileTime( void ) const;
  145. // returns the current line number
  146. const int GetLineNum( void ) const;
  147. // print an error message
  148. void Error( const char *str, ... ) const id_attribute((format(printf,2,3)));
  149. // print a warning message
  150. void Warning( const char *str, ... ) const id_attribute((format(printf,2,3)));
  151. // add a global define that will be added to all opened sources
  152. static int AddGlobalDefine( const char *string );
  153. // remove the given global define
  154. static int RemoveGlobalDefine( const char *name );
  155. // remove all global defines
  156. static void RemoveAllGlobalDefines( void );
  157. // set the base folder to load files from
  158. static void SetBaseFolder( const char *path );
  159. private:
  160. int loaded; // set when a source file is loaded from file or memory
  161. idStr filename; // file name of the script
  162. idStr includepath; // path to include files
  163. bool OSPath; // true if the file was loaded from an OS path
  164. const punctuation_t *punctuations; // punctuations to use
  165. int flags; // flags used for script parsing
  166. idLexer * scriptstack; // stack with scripts of the source
  167. idToken * tokens; // tokens to read first
  168. define_t * defines; // list with macro definitions
  169. define_t ** definehash; // hash chain with defines
  170. indent_t * indentstack; // stack with indents
  171. int skip; // > 0 if skipping conditional code
  172. const char* marker_p;
  173. static define_t *globaldefines; // list with global defines added to every source loaded
  174. private:
  175. void PushIndent( int type, int skip );
  176. void PopIndent( int *type, int *skip );
  177. void PushScript( idLexer *script );
  178. int ReadSourceToken( idToken *token );
  179. int ReadLine( idToken *token );
  180. int UnreadSourceToken( idToken *token );
  181. int ReadDefineParms( define_t *define, idToken **parms, int maxparms );
  182. int StringizeTokens( idToken *tokens, idToken *token );
  183. int MergeTokens( idToken *t1, idToken *t2 );
  184. int ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
  185. int ExpandDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
  186. int ExpandDefineIntoSource( idToken *deftoken, define_t *define );
  187. void AddGlobalDefinesToSource( void );
  188. define_t * CopyDefine( define_t *define );
  189. define_t * FindHashedDefine(define_t **definehash, const char *name);
  190. int FindDefineParm( define_t *define, const char *name );
  191. void AddDefineToHash(define_t *define, define_t **definehash);
  192. static void PrintDefine( define_t *define );
  193. static void FreeDefine( define_t *define );
  194. static define_t *FindDefine( define_t *defines, const char *name );
  195. static define_t *DefineFromString( const char *string);
  196. define_t * CopyFirstDefine( void );
  197. int Directive_include( void );
  198. int Directive_undef( void );
  199. int Directive_if_def( int type );
  200. int Directive_ifdef( void );
  201. int Directive_ifndef( void );
  202. int Directive_else( void );
  203. int Directive_endif( void );
  204. int EvaluateTokens( idToken *tokens, signed long int *intvalue, double *floatvalue, int integer );
  205. int Evaluate( signed long int *intvalue, double *floatvalue, int integer );
  206. int DollarEvaluate( signed long int *intvalue, double *floatvalue, int integer);
  207. int Directive_define( void );
  208. int Directive_elif( void );
  209. int Directive_if( void );
  210. int Directive_line( void );
  211. int Directive_error( void );
  212. int Directive_warning( void );
  213. int Directive_pragma( void );
  214. void UnreadSignToken( void );
  215. int Directive_eval( void );
  216. int Directive_evalfloat( void );
  217. int ReadDirective( void );
  218. int DollarDirective_evalint( void );
  219. int DollarDirective_evalfloat( void );
  220. int ReadDollarDirective( void );
  221. };
  222. ID_INLINE const char *idParser::GetFileName( void ) const {
  223. if ( idParser::scriptstack ) {
  224. return idParser::scriptstack->GetFileName();
  225. }
  226. else {
  227. return "";
  228. }
  229. }
  230. ID_INLINE const int idParser::GetFileOffset( void ) const {
  231. if ( idParser::scriptstack ) {
  232. return idParser::scriptstack->GetFileOffset();
  233. }
  234. else {
  235. return 0;
  236. }
  237. }
  238. ID_INLINE const ID_TIME_T idParser::GetFileTime( void ) const {
  239. if ( idParser::scriptstack ) {
  240. return idParser::scriptstack->GetFileTime();
  241. }
  242. else {
  243. return 0;
  244. }
  245. }
  246. ID_INLINE const int idParser::GetLineNum( void ) const {
  247. if ( idParser::scriptstack ) {
  248. return idParser::scriptstack->GetLineNum();
  249. }
  250. else {
  251. return 0;
  252. }
  253. }
  254. #endif /* !__PARSER_H__ */