TokenParser.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. void idTokenParser::LoadFromParser( idParser &parser, const char *guiName ) {
  23. idToken tok;
  24. idTokenIndexes tokIdxs;
  25. tokIdxs.SetName( guiName );
  26. while ( parser.ReadToken( &tok ) ) {
  27. tokIdxs.Append( tokens.AddUnique( idBinaryToken( tok ) ) );
  28. }
  29. guiTokenIndexes.Append( tokIdxs );
  30. currentToken = 0;
  31. }
  32. void idTokenParser::LoadFromFile( const char *filename ) {
  33. Clear();
  34. idFile *inFile = fileSystem->OpenFileReadMemory( filename );
  35. if ( inFile != NULL ) {
  36. int num;
  37. inFile->ReadBig( num );
  38. guiTokenIndexes.SetNum( num );
  39. for ( int i = 0; i < num; i++ ) {
  40. guiTokenIndexes[ i ].Read( inFile );
  41. }
  42. inFile->ReadBig( num );
  43. tokens.SetNum( num );
  44. for ( int i = 0; i < num; i++ ) {
  45. tokens[ i ].Read( inFile );
  46. }
  47. }
  48. delete inFile;
  49. preloaded = ( tokens.Num() > 0 );
  50. }
  51. void idTokenParser::WriteToFile ( const char *filename ) {
  52. if ( preloaded ) {
  53. return;
  54. }
  55. idFile *outFile = fileSystem->OpenFileWrite( filename, "fs_basepath" );
  56. if ( outFile != NULL ) {
  57. outFile->WriteBig( ( int )guiTokenIndexes.Num() );
  58. for ( int i = 0; i < guiTokenIndexes.Num(); i++ ) {
  59. guiTokenIndexes[ i ].Write( outFile );
  60. }
  61. outFile->WriteBig( ( int )tokens.Num() );
  62. for ( int i = 0; i < tokens.Num(); i++ ) {
  63. tokens[ i ].Write( outFile );
  64. }
  65. }
  66. delete outFile;
  67. }
  68. bool idTokenParser::StartParsing( const char * filename ) {
  69. currentTokenList = -1;
  70. for ( int i = 0; i < guiTokenIndexes.Num(); i++ ) {
  71. if ( idStr::Icmp( filename, guiTokenIndexes[ i ].GetName() ) == 0 ) {
  72. currentTokenList = i;
  73. break;
  74. }
  75. }
  76. currentToken = 0;
  77. return ( currentTokenList != -1 );
  78. }
  79. bool idTokenParser::ReadToken( idToken * tok ) {
  80. if ( currentToken >= 0 && currentToken < guiTokenIndexes[ currentTokenList ].Num() ) {
  81. tok->Clear();
  82. idBinaryToken &btok = tokens[ guiTokenIndexes[ currentTokenList ][ currentToken ] ];
  83. *tok = btok.token;
  84. tok->type = btok.tokenType;
  85. tok->subtype = btok.tokenSubType;
  86. currentToken++;
  87. return true;
  88. }
  89. return false;
  90. }
  91. int idTokenParser::ExpectTokenString( const char *string ) {
  92. idToken token;
  93. if ( !ReadToken( &token ) ) {
  94. Error( "couldn't find expected '%s'", string );
  95. return 0;
  96. }
  97. if ( token != string ) {
  98. Error( "expected '%s' but found '%s'", string, token.c_str() );
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. // expect a certain token type
  104. int idTokenParser::ExpectTokenType( int type, int subtype, idToken *token ) {
  105. idStr str;
  106. if ( !ReadToken( token ) ) {
  107. Error( "couldn't read expected token" );
  108. return 0;
  109. }
  110. if ( token->type != type ) {
  111. switch( type ) {
  112. case TT_STRING: str = "string"; break;
  113. case TT_LITERAL: str = "literal"; break;
  114. case TT_NUMBER: str = "number"; break;
  115. case TT_NAME: str = "name"; break;
  116. case TT_PUNCTUATION: str = "punctuation"; break;
  117. default: str = "unknown type"; break;
  118. }
  119. Error( "expected a %s but found '%s'", str.c_str(), token->c_str() );
  120. return 0;
  121. }
  122. if ( token->type == TT_NUMBER ) {
  123. if ( (token->subtype & subtype) != subtype ) {
  124. str.Clear();
  125. if ( subtype & TT_DECIMAL ) str = "decimal ";
  126. if ( subtype & TT_HEX ) str = "hex ";
  127. if ( subtype & TT_OCTAL ) str = "octal ";
  128. if ( subtype & TT_BINARY ) str = "binary ";
  129. if ( subtype & TT_UNSIGNED ) str += "unsigned ";
  130. if ( subtype & TT_LONG ) str += "long ";
  131. if ( subtype & TT_FLOAT ) str += "float ";
  132. if ( subtype & TT_INTEGER ) str += "integer ";
  133. str.StripTrailing( ' ' );
  134. Error( "expected %s but found '%s'", str.c_str(), token->c_str() );
  135. return 0;
  136. }
  137. }
  138. else if ( token->type == TT_PUNCTUATION ) {
  139. if ( subtype < 0 ) {
  140. Error( "BUG: wrong punctuation subtype" );
  141. return 0;
  142. }
  143. if ( token->subtype != subtype ) {
  144. //Error( "expected '%s' but found '%s'", idLexer::GetPunctuationFromId( subtype ), token->c_str() );
  145. return 0;
  146. }
  147. }
  148. return 1;
  149. }
  150. // expect a token
  151. int idTokenParser::ExpectAnyToken( idToken *token ) {
  152. if (!ReadToken( token )) {
  153. Error( "couldn't read expected token" );
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. void idTokenParser::UnreadToken( const idToken *token ) {
  159. if ( currentToken == 0 || currentToken >= guiTokenIndexes[ currentTokenList ].Num() ) {
  160. idLib::common->FatalError( "idTokenParser::unreadToken, unread token twice\n" );
  161. }
  162. currentToken--;
  163. }
  164. void idTokenParser::Error( VERIFY_FORMAT_STRING const char *str, ... ) {
  165. char text[MAX_STRING_CHARS];
  166. va_list ap;
  167. va_start(ap, str);
  168. vsprintf(text, str, ap);
  169. va_end(ap);
  170. idLib::common->Warning( text );
  171. }
  172. void idTokenParser::Warning( VERIFY_FORMAT_STRING const char *str, ... ) {
  173. char text[MAX_STRING_CHARS];
  174. va_list ap;
  175. va_start(ap, str);
  176. vsprintf(text, str, ap);
  177. va_end(ap);
  178. idLib::common->Warning( text );
  179. }
  180. int idTokenParser::ParseInt() {
  181. idToken token;
  182. if ( !ReadToken( &token ) ) {
  183. Error( "couldn't read expected integer" );
  184. return 0;
  185. }
  186. if ( token.type == TT_PUNCTUATION && token == "-" ) {
  187. ExpectTokenType( TT_NUMBER, TT_INTEGER, &token );
  188. return -((signed int) token.GetIntValue());
  189. }
  190. else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  191. Error( "expected integer value, found '%s'", token.c_str() );
  192. }
  193. return token.GetIntValue();
  194. }
  195. // read a boolean
  196. bool idTokenParser::ParseBool() {
  197. idToken token;
  198. if ( !ExpectTokenType( TT_NUMBER, 0, &token ) ) {
  199. Error( "couldn't read expected boolean" );
  200. return false;
  201. }
  202. return ( token.GetIntValue() != 0 );
  203. }
  204. // read a floating point number. If errorFlag is NULL, a non-numeric token will
  205. // issue an Error(). If it isn't NULL, it will issue a Warning() and set *errorFlag = true
  206. float idTokenParser::ParseFloat( bool *errorFlag ) {
  207. idToken token;
  208. if ( errorFlag ) {
  209. *errorFlag = false;
  210. }
  211. if ( !ReadToken( &token ) ) {
  212. if ( errorFlag ) {
  213. Warning( "couldn't read expected floating point number" );
  214. *errorFlag = true;
  215. } else {
  216. Error( "couldn't read expected floating point number" );
  217. }
  218. return 0;
  219. }
  220. if ( token.type == TT_PUNCTUATION && token == "-" ) {
  221. ExpectTokenType( TT_NUMBER, 0, &token );
  222. return -token.GetFloatValue();
  223. }
  224. else if ( token.type != TT_NUMBER ) {
  225. if ( errorFlag ) {
  226. Warning( "expected float value, found '%s'", token.c_str() );
  227. *errorFlag = true;
  228. } else {
  229. Error( "expected float value, found '%s'", token.c_str() );
  230. }
  231. }
  232. return token.GetFloatValue();
  233. }