home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Token.h < prev   
C/C++ Source or Header  |  2005-11-14  |  7KB  |  240 lines

  1.  
  2. #ifndef __TOKEN_H__
  3. #define __TOKEN_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     idToken is a token read from a file or memory with idLexer or idParser
  9.  
  10. ===============================================================================
  11. */
  12. // RAVEN BEGIN
  13. // jsinger: defines used by the binary lexer and WriteBinaryToken method of idLexer
  14.  
  15. // binary token types
  16. // low order 3 bits form the type
  17. #define BTT_STRING                    0
  18. #define BTT_LITERAL                    1
  19. #define BTT_NUMBER                    2
  20. #define BTT_NAME                    3
  21. #define BTT_PUNCTUATION                4
  22. #define BTT_PUNCTUATION2            5        // when punctuation, least significant bit is used as part of the punctuation type
  23. // 3 bits
  24. #define BTT_SUBTYPE_INT                0
  25. #define BTT_SUBTYPE_UNSIGNEDINT        1
  26. #define BTT_SUBTYPE_LONG            2
  27. #define BTT_SUBTYPE_UNSIGNEDLONG    3
  28. #define BTT_SUBTYPE_FLOAT            4
  29. #define BTT_SUBTYPE_DOUBLE            5
  30. #define BTT_SUBTYPE_IPADDRESS        6
  31. #define BTT_SUBTYPE_IPPORT            7
  32.  
  33. // 2 bits
  34. #define BTT_STORED_1BYTE            0
  35. #define BTT_STORED_2BYTE            1
  36. #define BTT_STORED_4BYTE            2
  37. #define BTT_STORED_8BYTE            3
  38.  
  39. // retrieval macros
  40. #define BTT_GET_TYPE(x)                ((x) & 0x07)
  41. #define BTT_GET_SUBTYPE(x)            (((x) & 0x38)>>3)
  42. #define BTT_GET_STORED_SIZE(x)        (((x) & 0xC0)>>6)
  43. #define BTT_GET_STRING_LENGTH(x)    (((x) & 0xFC)>>3)
  44. #define BTT_GET_PUNCTUATION(x)        (((x)&1)|(((x)>>2)&~1))
  45.  
  46. // punctuation types
  47. #define BTT_PUNC_ASNULLTERMINATED    0
  48. #define BTT_PUNC_RIGHTPAREN            1
  49. #define BTT_PUNC_LEFTBRACE            2
  50. #define BTT_PUNC_RIGHTBRACE            3
  51. #define BTT_PUNC_MINUS                4
  52. #define BTT_PUNC_PLUS                5
  53. #define BTT_PUNC_COMMA                6
  54. #define BTT_PUNC_PLUSPLUS            7
  55. #define BTT_PUNC_LEFTBRACKET        8
  56. #define BTT_PUNC_RIGHTBRACKET        9
  57. #define BTT_PUNC_EQUAL                10
  58. #define BTT_PUNC_EQUALEQUAL            11
  59. #define BTT_PUNC_NOTEQUAL            12
  60. #define BTT_PUNC_PERCENT            13
  61. #define BTT_PUNC_LESSTHAN            14
  62. #define BTT_PUNC_GREATERTHAN        15
  63. #define BTT_PUNC_LOGICALAND            16
  64. #define BTT_PUNC_AMPERSAND            17
  65. #define BTT_PUNC_MINUSMINUS            18
  66. #define BTT_PUNC_HASH                19
  67. #define BTT_PUNC_LESSOREQUAL        20
  68. #define BTT_PUNC_GREATEROREQUAL        21
  69. #define BTT_PUNC_FORWARDSLASH        22
  70. #define BTT_PUNC_SHIFTLEFT            23
  71. #define BTT_PUNC_SHIFTRIGHT            24
  72. #define BTT_PUNC_LEFTPAREN            25
  73. #define BTT_PUNC_SEMICOLON            26
  74. #define BTT_PUNC_ASTERISK            27
  75. #define BTT_PUNC_PERIOD                28
  76. #define BTT_PUNC_DOLLARSIGN            29
  77. #define BTT_PUNC_PLUSEQUAL            30
  78. #define BTT_PUNC_MINUSEQUAL            31
  79. #define BTT_PUNC_TILDE                32
  80. #define BTT_PUNC_EXCLAMATION        33
  81. #define BTT_PUNC_PIPE                34
  82. #define BTT_PUNC_BACKSLASH            35
  83. #define BTT_PUNC_DOUBLEHASH            36
  84. #define BTT_PUNC_DOUBLECOLON        37
  85. #define BTT_PUNC_TIMESEQUAL            38
  86. #define BTT_PUNC_DOUBLEPIPE            39
  87. #define BTT_PUNC_INVERTEDPLING        40
  88. #define BTT_PUNC_INVERTEDQUERY        41
  89.  
  90. // set macro
  91. #define BTT_MAKENUMBER_PREFIX(type, subtype, storedsize) ((unsigned char)((type)|(subtype<<3)|(storedsize<<6)))
  92. #define BTT_MAKESTRING_PREFIX(type, length) ((unsigned char)((type)|((((length)<32)?(length):0)<<3)))
  93. #define BTT_MAKEPUNCTUATION_PREFIX(punc) (((punc<<2)&~7)|(punc&1))|BTT_PUNCTUATION
  94. // RAVEN END
  95.  
  96. // token types
  97. #define TT_STRING                    1        // string
  98. #define TT_LITERAL                    2        // literal
  99. #define TT_NUMBER                    3        // number
  100. #define TT_NAME                        4        // name
  101. #define TT_PUNCTUATION                5        // punctuation
  102.  
  103. // number sub types
  104. #define TT_INTEGER                    0x00001        // integer
  105. #define TT_DECIMAL                    0x00002        // decimal number
  106. #define TT_HEX                        0x00004        // hexadecimal number
  107. #define TT_OCTAL                    0x00008        // octal number
  108. #define TT_BINARY                    0x00010        // binary number
  109. #define TT_LONG                        0x00020        // long int
  110. #define TT_UNSIGNED                    0x00040        // unsigned int
  111. #define TT_FLOAT                    0x00080        // floating point number
  112. #define TT_SINGLE_PRECISION            0x00100        // float
  113. #define TT_DOUBLE_PRECISION            0x00200        // double
  114. #define TT_EXTENDED_PRECISION        0x00400        // long double
  115. #define TT_INFINITE                    0x00800        // infinite 1.#INF
  116. #define TT_INDEFINITE                0x01000        // indefinite 1.#IND
  117. #define TT_NAN                        0x02000        // NaN
  118. #define TT_IPADDRESS                0x04000        // ip address
  119. #define TT_IPPORT                    0x08000        // ip port
  120. #define TT_VALUESVALID                0x10000        // set if intvalue and floatvalue are valid
  121.  
  122. // string sub type is the length of the string
  123. // literal sub type is the ASCII code
  124. // punctuation sub type is the punctuation id
  125. // name sub type is the length of the name
  126.  
  127. class idToken : public idStr {
  128.  
  129.     friend class idParser;
  130.     friend class idLexer;
  131. // RAVEN BEGIN
  132. // jsinger: added to allow Lexer direct access to token internals as well
  133.     friend class Lexer;
  134. // RAVEN END
  135.  
  136. public:
  137.     int                type;                                // token type
  138.     int                subtype;                            // token sub type
  139.     int                line;                                // line in script the token was on
  140.     int                linesCrossed;                        // number of lines crossed in white space before token
  141.     int                flags;                                // token flags, used for recursive defines
  142.  
  143. public:
  144.                     idToken( void );
  145.                     idToken( const idToken *token );
  146.                     ~idToken( void );
  147.  
  148.     void            operator=( const idStr& text );
  149.     void            operator=( const char *text );
  150.  
  151.     double            GetDoubleValue( void );                // double value of TT_NUMBER
  152.     float            GetFloatValue( void );                // float value of TT_NUMBER
  153.     unsigned long    GetUnsignedLongValue( void );        // unsigned long value of TT_NUMBER
  154.     int                GetIntValue( void );                // int value of TT_NUMBER
  155.     int                WhiteSpaceBeforeToken( void ) const;// returns length of whitespace before token
  156.     void            ClearTokenWhiteSpace( void );        // forget whitespace before token
  157.  
  158.     void            NumberValue( void );                // calculate values for a TT_NUMBER
  159.  
  160. private:
  161.     unsigned long    intvalue;                            // integer value
  162.     double            floatvalue;                            // floating point value
  163.     const char *    whiteSpaceStart_p;                    // start of white space before token, only used by idLexer
  164.     const char *    whiteSpaceEnd_p;                    // end of white space before token, only used by idLexer
  165.     idToken *        next;                                // next token in chain, only used by idParser
  166.  
  167.     void            AppendDirty( const char a );        // append character without adding trailing zero
  168. };
  169.  
  170. // RAVEN BEGIN
  171. // rjohnson: initialized floatvalue to prevent fpu exceptin
  172.  
  173. ID_INLINE idToken::idToken( void ) :
  174.     floatvalue(0.0)
  175. {
  176. }
  177.  
  178. // RAVEN END
  179.  
  180. ID_INLINE idToken::idToken( const idToken *token ) {
  181.     *this = *token;
  182. }
  183.  
  184. ID_INLINE idToken::~idToken( void ) {
  185. }
  186.  
  187. ID_INLINE void idToken::operator=( const char *text) {
  188.     *static_cast<idStr *>(this) = text;
  189. }
  190.  
  191. ID_INLINE void idToken::operator=( const idStr& text ) {
  192.     *static_cast<idStr *>(this) = text;
  193. }
  194.  
  195. ID_INLINE double idToken::GetDoubleValue( void ) {
  196.     if ( type != TT_NUMBER ) {
  197.         return 0.0;
  198.     }
  199.     if ( !(subtype & TT_VALUESVALID) ) {
  200.         NumberValue();
  201.     }
  202.     return floatvalue;
  203. }
  204.  
  205. ID_INLINE float idToken::GetFloatValue( void ) {
  206.     return (float) GetDoubleValue();
  207. }
  208.  
  209. ID_INLINE unsigned long    idToken::GetUnsignedLongValue( void ) {
  210.     if ( type != TT_NUMBER ) {
  211.         return 0;
  212.     }
  213.     if ( !(subtype & TT_VALUESVALID) ) {
  214.         NumberValue();
  215.     }
  216.     return intvalue;
  217. }
  218.  
  219. ID_INLINE int idToken::GetIntValue( void ) {
  220.     return (int) GetUnsignedLongValue();
  221. }
  222.  
  223. ID_INLINE int idToken::WhiteSpaceBeforeToken( void ) const {
  224.     return ( whiteSpaceEnd_p > whiteSpaceStart_p );
  225. }
  226.  
  227. ID_INLINE void idToken::AppendDirty( const char a ) {
  228.     EnsureAlloced( len + 2, true );
  229. // RAVEN BEGIN
  230. // jscott: I hate slashes nearly as much as KRABS
  231.     if( a == '\\' ) {
  232.         data[len++] = '/';
  233.     } else {
  234.         data[len++] = a;
  235.     }
  236. // RAVEN END
  237. }
  238.  
  239. #endif /* !__TOKEN_H__ */
  240.