Token.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 __TOKEN_H__
  21. #define __TOKEN_H__
  22. /*
  23. ===============================================================================
  24. idToken is a token read from a file or memory with idLexer or idParser
  25. ===============================================================================
  26. */
  27. // token types
  28. #define TT_STRING 1 // string
  29. #define TT_LITERAL 2 // literal
  30. #define TT_NUMBER 3 // number
  31. #define TT_NAME 4 // name
  32. #define TT_PUNCTUATION 5 // punctuation
  33. // number sub types
  34. #define TT_INTEGER 0x00001 // integer
  35. #define TT_DECIMAL 0x00002 // decimal number
  36. #define TT_HEX 0x00004 // hexadecimal number
  37. #define TT_OCTAL 0x00008 // octal number
  38. #define TT_BINARY 0x00010 // binary number
  39. #define TT_LONG 0x00020 // long int
  40. #define TT_UNSIGNED 0x00040 // unsigned int
  41. #define TT_FLOAT 0x00080 // floating point number
  42. #define TT_SINGLE_PRECISION 0x00100 // float
  43. #define TT_DOUBLE_PRECISION 0x00200 // double
  44. #define TT_EXTENDED_PRECISION 0x00400 // long double
  45. #define TT_INFINITE 0x00800 // infinite 1.#INF
  46. #define TT_INDEFINITE 0x01000 // indefinite 1.#IND
  47. #define TT_NAN 0x02000 // NaN
  48. #define TT_IPADDRESS 0x04000 // ip address
  49. #define TT_IPPORT 0x08000 // ip port
  50. #define TT_VALUESVALID 0x10000 // set if intvalue and floatvalue are valid
  51. // string sub type is the length of the string
  52. // literal sub type is the ASCII code
  53. // punctuation sub type is the punctuation id
  54. // name sub type is the length of the name
  55. class idToken : public idStr {
  56. friend class idParser;
  57. friend class idLexer;
  58. public:
  59. int type; // token type
  60. int subtype; // token sub type
  61. int line; // line in script the token was on
  62. int linesCrossed; // number of lines crossed in white space before token
  63. int flags; // token flags, used for recursive defines
  64. public:
  65. idToken( void );
  66. idToken( const idToken *token );
  67. ~idToken( void );
  68. void operator=( const idStr& text );
  69. void operator=( const char *text );
  70. double GetDoubleValue( void ); // double value of TT_NUMBER
  71. float GetFloatValue( void ); // float value of TT_NUMBER
  72. unsigned long GetUnsignedLongValue( void ); // unsigned long value of TT_NUMBER
  73. int GetIntValue( void ); // int value of TT_NUMBER
  74. int WhiteSpaceBeforeToken( void ) const;// returns length of whitespace before token
  75. void ClearTokenWhiteSpace( void ); // forget whitespace before token
  76. void NumberValue( void ); // calculate values for a TT_NUMBER
  77. private:
  78. unsigned long intvalue; // integer value
  79. double floatvalue; // floating point value
  80. const char * whiteSpaceStart_p; // start of white space before token, only used by idLexer
  81. const char * whiteSpaceEnd_p; // end of white space before token, only used by idLexer
  82. idToken * next; // next token in chain, only used by idParser
  83. void AppendDirty( const char a ); // append character without adding trailing zero
  84. };
  85. ID_INLINE idToken::idToken( void ) {
  86. }
  87. ID_INLINE idToken::idToken( const idToken *token ) {
  88. *this = *token;
  89. }
  90. ID_INLINE idToken::~idToken( void ) {
  91. }
  92. ID_INLINE void idToken::operator=( const char *text) {
  93. *static_cast<idStr *>(this) = text;
  94. }
  95. ID_INLINE void idToken::operator=( const idStr& text ) {
  96. *static_cast<idStr *>(this) = text;
  97. }
  98. ID_INLINE double idToken::GetDoubleValue( void ) {
  99. if ( type != TT_NUMBER ) {
  100. return 0.0;
  101. }
  102. if ( !(subtype & TT_VALUESVALID) ) {
  103. NumberValue();
  104. }
  105. return floatvalue;
  106. }
  107. ID_INLINE float idToken::GetFloatValue( void ) {
  108. return (float) GetDoubleValue();
  109. }
  110. ID_INLINE unsigned long idToken::GetUnsignedLongValue( void ) {
  111. if ( type != TT_NUMBER ) {
  112. return 0;
  113. }
  114. if ( !(subtype & TT_VALUESVALID) ) {
  115. NumberValue();
  116. }
  117. return intvalue;
  118. }
  119. ID_INLINE int idToken::GetIntValue( void ) {
  120. return (int) GetUnsignedLongValue();
  121. }
  122. ID_INLINE int idToken::WhiteSpaceBeforeToken( void ) const {
  123. return ( whiteSpaceEnd_p > whiteSpaceStart_p );
  124. }
  125. ID_INLINE void idToken::AppendDirty( const char a ) {
  126. EnsureAlloced( len + 2, true );
  127. data[len++] = a;
  128. }
  129. #endif /* !__TOKEN_H__ */