Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

l_script.h (8224B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      7 Quake III Arena source code is free software; you can redistribute it
      8 and/or modify it under the terms of the GNU General Public License as
      9 published by the Free Software Foundation; either version 2 of the License,
     10 or (at your option) any later version.
     11 
     12 Quake III Arena source code is distributed in the hope that it will be
     13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 GNU General Public License for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 
     23 /*****************************************************************************
     24  * name:		l_script.h
     25  *
     26  * desc:		lexicographical parser
     27  *
     28  * $Archive: /source/code/botlib/l_script.h $
     29  *
     30  *****************************************************************************/
     31 
     32 //undef if binary numbers of the form 0b... or 0B... are not allowed
     33 #define BINARYNUMBERS
     34 //undef if not using the token.intvalue and token.floatvalue
     35 #define NUMBERVALUE
     36 //use dollar sign also as punctuation
     37 #define DOLLAR
     38 
     39 //maximum token length
     40 #define MAX_TOKEN					1024
     41 
     42 #if defined(BSPC) && !defined(QDECL)
     43 #define QDECL
     44 #endif
     45 
     46 
     47 //script flags
     48 #define SCFL_NOERRORS				0x0001
     49 #define SCFL_NOWARNINGS				0x0002
     50 #define SCFL_NOSTRINGWHITESPACES	0x0004
     51 #define SCFL_NOSTRINGESCAPECHARS	0x0008
     52 #define SCFL_PRIMITIVE				0x0010
     53 #define SCFL_NOBINARYNUMBERS		0x0020
     54 #define SCFL_NONUMBERVALUES		0x0040
     55 
     56 //token types
     57 #define TT_STRING						1			// string
     58 #define TT_LITERAL					2			// literal
     59 #define TT_NUMBER						3			// number
     60 #define TT_NAME						4			// name
     61 #define TT_PUNCTUATION				5			// punctuation
     62 
     63 //string sub type
     64 //---------------
     65 //		the length of the string
     66 //literal sub type
     67 //----------------
     68 //		the ASCII code of the literal
     69 //number sub type
     70 //---------------
     71 #define TT_DECIMAL					0x0008	// decimal number
     72 #define TT_HEX							0x0100	// hexadecimal number
     73 #define TT_OCTAL						0x0200	// octal number
     74 #ifdef BINARYNUMBERS
     75 #define TT_BINARY						0x0400	// binary number
     76 #endif //BINARYNUMBERS
     77 #define TT_FLOAT						0x0800	// floating point number
     78 #define TT_INTEGER					0x1000	// integer number
     79 #define TT_LONG						0x2000	// long number
     80 #define TT_UNSIGNED					0x4000	// unsigned number
     81 //punctuation sub type
     82 //--------------------
     83 #define P_RSHIFT_ASSIGN				1
     84 #define P_LSHIFT_ASSIGN				2
     85 #define P_PARMS						3
     86 #define P_PRECOMPMERGE				4
     87 
     88 #define P_LOGIC_AND					5
     89 #define P_LOGIC_OR					6
     90 #define P_LOGIC_GEQ					7
     91 #define P_LOGIC_LEQ					8
     92 #define P_LOGIC_EQ					9
     93 #define P_LOGIC_UNEQ					10
     94 
     95 #define P_MUL_ASSIGN					11
     96 #define P_DIV_ASSIGN					12
     97 #define P_MOD_ASSIGN					13
     98 #define P_ADD_ASSIGN					14
     99 #define P_SUB_ASSIGN					15
    100 #define P_INC							16
    101 #define P_DEC							17
    102 
    103 #define P_BIN_AND_ASSIGN			18
    104 #define P_BIN_OR_ASSIGN				19
    105 #define P_BIN_XOR_ASSIGN			20
    106 #define P_RSHIFT						21
    107 #define P_LSHIFT						22
    108 
    109 #define P_POINTERREF					23
    110 #define P_CPP1							24
    111 #define P_CPP2							25
    112 #define P_MUL							26
    113 #define P_DIV							27
    114 #define P_MOD							28
    115 #define P_ADD							29
    116 #define P_SUB							30
    117 #define P_ASSIGN						31
    118 
    119 #define P_BIN_AND						32
    120 #define P_BIN_OR						33
    121 #define P_BIN_XOR						34
    122 #define P_BIN_NOT						35
    123 
    124 #define P_LOGIC_NOT					36
    125 #define P_LOGIC_GREATER				37
    126 #define P_LOGIC_LESS					38
    127 
    128 #define P_REF							39
    129 #define P_COMMA						40
    130 #define P_SEMICOLON					41
    131 #define P_COLON						42
    132 #define P_QUESTIONMARK				43
    133 
    134 #define P_PARENTHESESOPEN			44
    135 #define P_PARENTHESESCLOSE			45
    136 #define P_BRACEOPEN					46
    137 #define P_BRACECLOSE					47
    138 #define P_SQBRACKETOPEN				48
    139 #define P_SQBRACKETCLOSE			49
    140 #define P_BACKSLASH					50
    141 
    142 #define P_PRECOMP						51
    143 #define P_DOLLAR						52
    144 //name sub type
    145 //-------------
    146 //		the length of the name
    147 
    148 //punctuation
    149 typedef struct punctuation_s
    150 {
    151 	char *p;						//punctuation character(s)
    152 	int n;							//punctuation indication
    153 	struct punctuation_s *next;		//next punctuation
    154 } punctuation_t;
    155 
    156 //token
    157 typedef struct token_s
    158 {
    159 	char string[MAX_TOKEN];			//available token
    160 	int type;						//last read token type
    161 	int subtype;					//last read token sub type
    162 #ifdef NUMBERVALUE
    163 	unsigned long int intvalue;	//integer value
    164 	long double floatvalue;			//floating point value
    165 #endif //NUMBERVALUE
    166 	char *whitespace_p;				//start of white space before token
    167 	char *endwhitespace_p;			//start of white space before token
    168 	int line;						//line the token was on
    169 	int linescrossed;				//lines crossed in white space
    170 	struct token_s *next;			//next token in chain
    171 } token_t;
    172 
    173 //script file
    174 typedef struct script_s
    175 {
    176 	char filename[1024];			//file name of the script
    177 	char *buffer;					//buffer containing the script
    178 	char *script_p;					//current pointer in the script
    179 	char *end_p;					//pointer to the end of the script
    180 	char *lastscript_p;				//script pointer before reading token
    181 	char *whitespace_p;				//begin of the white space
    182 	char *endwhitespace_p;			//end of the white space
    183 	int length;						//length of the script in bytes
    184 	int line;						//current line in script
    185 	int lastline;					//line before reading token
    186 	int tokenavailable;				//set by UnreadLastToken
    187 	int flags;						//several script flags
    188 	punctuation_t *punctuations;	//the punctuations used in the script
    189 	punctuation_t **punctuationtable;
    190 	token_t token;					//available token
    191 	struct script_s *next;			//next script in a chain
    192 } script_t;
    193 
    194 //read a token from the script
    195 int PS_ReadToken(script_t *script, token_t *token);
    196 //expect a certain token
    197 int PS_ExpectTokenString(script_t *script, char *string);
    198 //expect a certain token type
    199 int PS_ExpectTokenType(script_t *script, int type, int subtype, token_t *token);
    200 //expect a token
    201 int PS_ExpectAnyToken(script_t *script, token_t *token);
    202 //returns true when the token is available
    203 int PS_CheckTokenString(script_t *script, char *string);
    204 //returns true an reads the token when a token with the given type is available
    205 int PS_CheckTokenType(script_t *script, int type, int subtype, token_t *token);
    206 //skip tokens until the given token string is read
    207 int PS_SkipUntilString(script_t *script, char *string);
    208 //unread the last token read from the script
    209 void PS_UnreadLastToken(script_t *script);
    210 //unread the given token
    211 void PS_UnreadToken(script_t *script, token_t *token);
    212 //returns the next character of the read white space, returns NULL if none
    213 char PS_NextWhiteSpaceChar(script_t *script);
    214 //remove any leading and trailing double quotes from the token
    215 void StripDoubleQuotes(char *string);
    216 //remove any leading and trailing single quotes from the token
    217 void StripSingleQuotes(char *string);
    218 //read a possible signed integer
    219 signed long int ReadSignedInt(script_t *script);
    220 //read a possible signed floating point number
    221 long double ReadSignedFloat(script_t *script);
    222 //set an array with punctuations, NULL restores default C/C++ set
    223 void SetScriptPunctuations(script_t *script, punctuation_t *p);
    224 //set script flags
    225 void SetScriptFlags(script_t *script, int flags);
    226 //get script flags
    227 int GetScriptFlags(script_t *script);
    228 //reset a script
    229 void ResetScript(script_t *script);
    230 //returns true if at the end of the script
    231 int EndOfScript(script_t *script);
    232 //returns a pointer to the punctuation with the given number
    233 char *PunctuationFromNum(script_t *script, int num);
    234 //load a script from the given file at the given offset with the given length
    235 script_t *LoadScriptFile(const char *filename);
    236 //load a script from the given memory with the given length
    237 script_t *LoadScriptMemory(char *ptr, int length, char *name);
    238 //free a script
    239 void FreeScript(script_t *script);
    240 //set the base folder to load files from
    241 void PS_SetBaseFolder(char *path);
    242 //print a script error with filename and line number
    243 void QDECL ScriptError(script_t *script, char *str, ...);
    244 //print a script warning with filename and line number
    245 void QDECL ScriptWarning(script_t *script, char *str, ...);
    246 
    247