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