Showing the changes to src/core/chuck_parse.cpp (1 of 15 files). Show all
Unified · Split
| @@ -32,11 +32,11 @@ · 31 unmodified lines | |||
| 32 | //----------------------------------------------------------------------------- | 32 | //----------------------------------------------------------------------------- |
| 33 | #include "chuck_parse.h" | 33 | #include "chuck_parse.h" |
| 34 | #include "chuck_errmsg.h" | 34 | #include "chuck_errmsg.h" |
| 35 | #include <string.h> | 35 | #include "util_string.h" |
| 36 | |||
| 37 | #ifdef __ANDROID__ | ||
| 38 | #include "util_platforms.h" | 36 | #include "util_platforms.h" |
| 39 | #endif | 37 | |
| 38 | #include <string.h> | ||
| 39 | #include <string> | ||
| 40 | 40 | ||
| 41 | #ifdef __PLATFORM_WIN32__ | 41 | #ifdef __PLATFORM_WIN32__ |
| 42 | #ifndef __CHUNREAL_ENGINE__ | 42 | #ifndef __CHUNREAL_ENGINE__ |
| @@ -48,226 +48,165 @@ · 5 unmodified lines | |||
| 48 | #endif // #ifndef __CHUNREAL_ENGINE__ | 48 | #endif // #ifndef __CHUNREAL_ENGINE__ |
| 49 | #endif // #ifdef __PLATFORM_WIN32__ | 49 | #endif // #ifdef __PLATFORM_WIN32__ |
| 50 | 50 | ||
| 51 | #include <sys/stat.h> | ||
| 52 | #if defined(__PLATFORM_WIN32__) | ||
| 53 | #include "dirent_win32.h" | ||
| 54 | #endif | ||
| 55 | |||
| 51 | using namespace std; | 56 | using namespace std; |
| 52 | 57 | ||
| 53 | // max path len | ||
| 54 | static const t_CKUINT MAX_FILENAME_LEN = 2048; | ||
| 55 | // global | 58 | // global |
| 56 | static char g_filename[MAX_FILENAME_LEN+1] = ""; | 59 | static std::string g_filename; |
| 57 | 60 | // file to parse | |
| 58 | // external | 61 | static FILE * g_fd2parse = NULL; |
| 62 | // whether to auto close | ||
| 63 | static t_CKBOOL g_fd2autoClose = FALSE; | ||
| 64 | // clean up function | ||
| 65 | static void fd2parse_cleanup(); | ||
| 66 | |||
| 67 | // external from lexer | ||
| 59 | extern "C" { | 68 | extern "C" { |
| 60 | extern FILE *yyin; | 69 | extern FILE * yyin; |
| 61 | } | 70 | } |
| 62 | 71 | ||
| 63 | 72 | ||
| 64 | 73 | ||
| 65 | 74 | ||
| 66 | //----------------------------------------------------------------------------- | 75 | //----------------------------------------------------------------------------- |
| 67 | // name: open_cat_ck() | 76 | // name: chuck_parse() |
| 68 | // desc: ... | 77 | // desc: INPUT: chuck code (either from file or actual code) to be parsed |
| 78 | // OUTPUT: abstract syntax tree representation of the code | ||
| 69 | //----------------------------------------------------------------------------- | 79 | //----------------------------------------------------------------------------- |
| 70 | FILE * open_cat_ck( c_str fname ) | 80 | t_CKBOOL chuck_parse( const std::string & fname, const std::string & codeLiteral ) |
| 71 | { | 81 | { |
| 72 | #ifdef __ANDROID__ | 82 | // return value |
| 73 | if( strncmp(fname, "jar:", strlen("jar:")) == 0 ) | 83 | t_CKBOOL ret = FALSE; |
| 84 | // file descriptor | normally NULL unless g_fd2parse is set | ||
| 85 | FILE * fd = g_fd2parse; | ||
| 86 | // our own lexer/parser buffer | ||
| 87 | YY_BUFFER_STATE yyCodeBuffer = NULL; | ||
| 88 | |||
| 89 | // check for conflict | ||
| 90 | if( fd && codeLiteral != "" ) | ||
| 74 | { | 91 | { |
| 75 | int fd = 0; | 92 | CK_FPRINTF_STDERR( "[chuck](parser): (internal) code and FILE descriptor both present!\n" ); |
| 76 | if( !ChuckAndroid::copyJARURLFileToTemporary(fname, &fd) ) | 93 | CK_FPRINTF_STDERR( "[chuck](parser): |- ignoring FILE descriptor...\n" ); |
| 77 | { | ||
| 78 | EM_error2( 0, "unable to download from JAR URL: %s", fname ); | ||
| 79 | return NULL; | ||
| 80 | } | ||
| 81 | return fdopen(fd, "rb"); | ||
| 82 | } | 94 | } |
| 83 | #endif // __ANDROID__ | ||
| 84 | 95 | ||
| 85 | FILE * fd = fopen(fname, "rb"); | 96 | // copy it |
| 86 | if( !fd ) | 97 | g_filename = fname; |
| 98 | |||
| 99 | // if actual code was passed in | ||
| 100 | if( codeLiteral != "" ) | ||
| 101 | { | ||
| 102 | // clean lexer | ||
| 103 | yyrestart( NULL ); | ||
| 104 | // load string (yy_scan_string will copy the C string) | ||
| 105 | yyCodeBuffer = yy_scan_string( codeLiteral.c_str() ); | ||
| 106 | // if could load | ||
| 107 | if( !yyCodeBuffer ) goto cleanup; | ||
| 108 | } | ||
| 109 | else | ||
| 87 | { | 110 | { |
| 88 | if( !strstr( fname, ".ck" ) && !strstr( fname, ".CK" ) ) | 111 | // test it |
| 112 | if( !fd ) | ||
| 89 | { | 113 | { |
| 90 | strcat( fname, ".ck" ); | 114 | // open, could potentially change g_filename |
| 91 | fd = fopen( fname, "rb" ); | 115 | fd = ck_openFileAutoExt( g_filename, ".ck" ); |
| 116 | // check file open result | ||
| 117 | if( !fd ) // if couldn't open | ||
| 118 | { g_filename = fname; } // revert filename | ||
| 119 | else if( ck_isdir(g_filename) ) // check for directory; if so, clean up | ||
| 120 | { EM_error2( 0, "cannot parse file: '%s' is a directory", mini(fname.c_str()) ); goto cleanup; } | ||
| 92 | } | 121 | } |
| 93 | } | ||
| 94 | return fd; | ||
| 95 | } | ||
| 96 | |||
| 97 | 122 | ||
| 123 | // if still none | ||
| 124 | if( !fd ) { EM_error2( 0, "no such file: '%s'", mini(fname.c_str()) ); goto cleanup; } | ||
| 125 | // set to beginning0 | ||
| 126 | else fseek( fd, 0, SEEK_SET ); | ||
| 98 | 127 | ||
| 128 | // reset yyin to fd | ||
| 129 | yyrestart( fd ); | ||
| 130 | // set to initial condition | 1.5.0.5 (ge) added | ||
| 131 | yyinitial(); | ||
| 99 | 132 | ||
| 100 | //----------------------------------------------------------------------------- | 133 | // check |
| 101 | // name: win32_tmpfile() | 134 | if( yyin == NULL ) goto cleanup; |
| 102 | // desc: replacement for broken tmpfile() on Windows Vista + 7 | 135 | } |
| 103 | //----------------------------------------------------------------------------- | ||
| 104 | #ifdef __PLATFORM_WIN32__ | ||
| 105 | FILE *win32_tmpfile() | ||
| 106 | { | ||
| 107 | char tmp_path[MAX_PATH]; | ||
| 108 | char file_path[MAX_PATH]; | ||
| 109 | FILE * file = NULL; | ||
| 110 | 136 | ||
| 111 | #ifndef __CHUNREAL_ENGINE__ | 137 | // reset |
| 112 | if( GetTempPath(256, tmp_path) == 0 ) | 138 | if( EM_reset( g_filename.c_str() ) == FALSE ) goto cleanup; |
| 113 | #else | ||
| 114 | // 1.5.0.0 (ge) | #chunreal explicit call ASCII version | ||
| 115 | if( GetTempPathA(256, tmp_path) == 0 ) | ||
| 116 | #endif | ||
| 117 | return NULL; | ||
| 118 | 139 | ||
| 119 | #ifndef __CHUNREAL_ENGINE__ | 140 | // TODO: clean g_program |
| 120 | if( GetTempFileName(tmp_path, "mA", 0, file_path) == 0 ) | 141 | g_program = NULL; |
| 121 | #else | ||
| 122 | // 1.5.0.0 (ge) | #chunreal explicit call ASCII version | ||
| 123 | if( GetTempFileNameA(tmp_path, "mA", 0, file_path) == 0 ) | ||
| 124 | #endif | ||
| 125 | return NULL; | ||
| 126 | 142 | ||
| 127 | file = fopen( file_path, "wb+D" ); | 143 | // parse |
| 144 | if( !(yyparse() == 0) ) goto cleanup; | ||
| 128 | 145 | ||
| 129 | return file; | 146 | // flag success |
| 130 | } | 147 | ret = TRUE; |
| 131 | #endif // #ifdef __PLATFORM_WIN32__ | ||
| 132 | 148 | ||
| 149 | cleanup: | ||
| 133 | 150 | ||
| 151 | // clean up the file descriptor | ||
| 152 | if( fd ) { | ||
| 153 | // check fd2parse | ||
| 154 | if( g_fd2parse ) { fd2parse_cleanup(); fd = NULL; } | ||
| 155 | else { fclose( fd ); fd = NULL; } | ||
| 156 | } | ||
| 134 | 157 | ||
| 158 | // set to NULL | 1.5.0.5 (ge) added to reset yyin | ||
| 159 | yyin = NULL; | ||
| 135 | 160 | ||
| 136 | //----------------------------------------------------------------------------- | 161 | // clean up lexer buffer, if we used one |
| 137 | // name: android_tmpfile() | 162 | if( yyCodeBuffer ) |
| 138 | // desc: replacement for broken tmpfile() on Android | 163 | { yy_delete_buffer( yyCodeBuffer ); yyCodeBuffer = NULL; } |
| 139 | //----------------------------------------------------------------------------- | ||
| 140 | #ifdef __ANDROID__ | ||
| 141 | 164 | ||
| 142 | FILE * android_tmpfile() | 165 | return ret; |
| 143 | { | ||
| 144 | return ChuckAndroid::getTemporaryFile(); | ||
| 145 | } | 166 | } |
| 146 | 167 | ||
| 147 | #endif | ||
| 148 | |||
| 149 | 168 | ||
| 150 | 169 | ||
| 151 | 170 | ||
| 152 | //----------------------------------------------------------------------------- | 171 | //------------------------------------------------------------------------------ |
| 153 | // name: chuck_parse() | 172 | // name: fd2parse_cleanup() |
| 154 | // desc: ... | 173 | // desc: clean up fd2parse |
| 155 | //----------------------------------------------------------------------------- | 174 | //------------------------------------------------------------------------------ |
| 156 | t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) | 175 | void fd2parse_cleanup() |
| 157 | { | 176 | { |
| 158 | t_CKBOOL clo = FALSE; | ||
| 159 | t_CKBOOL ret = FALSE; | ||
| 160 | |||
| 161 | // sanity check | ||
| 162 | if( fd && code ) | ||
| 163 | { | ||
| 164 | CK_FPRINTF_STDERR( "[chuck](via parser): (internal) both fd and code specified!\n" ); | ||
| 165 | return FALSE; | ||
| 166 | } | ||
| 167 | |||
| 168 | // prepare code | ||
| 169 | if( code ) | ||
| 170 | { | ||
| 171 | // ! | ||
| 172 | assert( fd == NULL ); | ||
| 173 | // generate temp file | ||
| 174 | #ifdef __PLATFORM_WIN32__ | ||
| 175 | fd = win32_tmpfile(); | ||
| 176 | #elif defined (__ANDROID__) | ||
| 177 | fd = android_tmpfile(); | ||
| 178 | #else | ||
| 179 | fd = tmpfile(); | ||
| 180 | #endif | ||
| 181 | // on some systems, tmpfile() can return NULL | ||
| 182 | if( !fd ) { EM_error2( 0, "unable to create temp file" ); return FALSE; } | ||
| 183 | // flag it to close | ||
| 184 | clo = TRUE; | ||
| 185 | // write | ||
| 186 | fwrite( code, sizeof(char), strlen(code), fd ); | ||
| 187 | } | ||
| 188 | |||
| 189 | /* | ||
| 190 | // use code from memory buffer if its available | ||
| 191 | if( code ) | ||
| 192 | { | ||
| 193 | // copy name | ||
| 194 | strcpy( g_filename, fname ); | ||
| 195 | // reset | ||
| 196 | if( EM_reset( g_filename, NULL ) == FALSE ) goto cleanup; | ||
| 197 | |||
| 198 | // TODO: clean g_program | ||
| 199 | g_program = NULL; | ||
| 200 | |||
| 201 | // clean | ||
| 202 | yyrestart( NULL ); | ||
| 203 | |||
| 204 | // load string (yy_scan_string will copy the C string) | ||
| 205 | YY_BUFFER_STATE ybs = yy_scan_string( code ); | ||
| 206 | if( !ybs ) goto cleanup; | ||
| 207 | |||
| 208 | // parse | ||
| 209 | if( !( yyparse() == 0 ) ) goto cleanup; | ||
| 210 | |||
| 211 | // delete the lexer buffer | ||
| 212 | yy_delete_buffer( ybs ); | ||
| 213 | |||
| 214 | } | ||
| 215 | */ | ||
| 216 | |||
| 217 | // check length | 1.4.1.0 (ge) added | ||
| 218 | t_CKUINT len = strlen( fname ); | ||
| 219 | if( len > MAX_FILENAME_LEN ) | ||
| 220 | { | ||
| 221 | EM_error2( 0, "filename length (%d) exceeds max (%d) set by compiler...", | ||
| 222 | len, MAX_FILENAME_LEN ); | ||
| 223 | EM_error2( 0, "filename in question: '%s'", fname ); | ||
| 224 | EM_error2( 0, "compiler bailing out..." ); | ||
| 225 | return FALSE; | ||
| 226 | } | ||
| 227 | // remember filename | ||
| 228 | strcpy( g_filename, fname ); | ||
| 229 | |||
| 230 | // test it | ||
| 231 | if( !fd ) { | ||
| 232 | fd = open_cat_ck( g_filename ); | ||
| 233 | if( !fd ) strcpy( g_filename, fname ); | ||
| 234 | else clo = TRUE; | ||
| 235 | } | ||
| 236 | |||
| 237 | // reset | ||
| 238 | if( EM_reset( g_filename, fd ) == FALSE ) goto cleanup; | ||
| 239 | |||
| 240 | // lexer/parser | ||
| 241 | // TODO: if( yyin ) { fclose( yyin ); yyin = NULL; } | ||
| 242 | |||
| 243 | // if no fd, open | ||
| 244 | if( !fd ) { fd = fopen( g_filename, "r" ); if( fd ) clo = TRUE; } | ||
| 245 | // if still none | ||
| 246 | if( !fd ) { EM_error2( 0, "no such file or directory" ); goto cleanup; } | ||
| 247 | // set to beginning | ||
| 248 | else fseek( fd, 0, SEEK_SET ); | ||
| 249 | |||
| 250 | // reset yyin to fd | ||
| 251 | yyrestart( fd ); | ||
| 252 | |||
| 253 | // check | 177 | // check |
| 254 | if( yyin == NULL ) goto cleanup; | 178 | if( g_fd2parse && g_fd2autoClose ) |
| 179 | { fclose( g_fd2parse ); } | ||
| 255 | 180 | ||
| 256 | // TODO: clean g_program | 181 | // reset |
| 257 | g_program = NULL; | 182 | g_fd2parse = NULL; |
| 183 | g_fd2autoClose = FALSE; | ||
| 184 | } | ||
| 258 | 185 | ||
| 259 | // parse | ||
| 260 | if( !(yyparse( ) == 0) ) goto cleanup; | ||
| 261 | 186 | ||
| 262 | // flag success | ||
| 263 | ret = TRUE; | ||
| 264 | 187 | ||
| 265 | cleanup: | ||
| 266 | 188 | ||
| 267 | // done | 189 | //------------------------------------------------------------------------------ |
| 268 | if( clo ) fclose( fd ); | 190 | // name: fd2parse_set() |
| 191 | // desc: special FILE input mode | added 1.5.0.5 (ge) | ||
| 192 | // set an already open FILE descriptor `fd` for one time use | ||
| 193 | // by the next call to go(), which will use `fd` as the input | ||
| 194 | // to the parser (NOTE in any invocation of go(), `codeLiteral` | ||
| 195 | // and `fd` should not both be non-empty, otherwise a warning | ||
| 196 | // will be output and the `codeLiteral` will take precedence | ||
| 197 | // and the `fd` will be cleaned up and skipped | ||
| 198 | // MEMORY: if `autoClose` is true, the compiler will automatically | ||
| 199 | // call fclose() on `fd` on the next call to go(), regardless of | ||
| 200 | // the aforementioned conflict with `codeLiteral` | ||
| 201 | //------------------------------------------------------------------------------ | ||
| 202 | void fd2parse_set( FILE * fd, t_CKBOOL autoClose ) | ||
| 203 | { | ||
| 204 | // clean up | ||
| 205 | fd2parse_cleanup(); | ||
| 269 | 206 | ||
| 270 | return ret; | 207 | // copy |
| 208 | g_fd2parse = fd; | ||
| 209 | g_fd2autoClose = autoClose; | ||
| 271 | } | 210 | } |
| 272 | 211 | ||
| 273 | 212 | ||