DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Token.h (5700B)


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