History

parser yacc shave stage two -- refactor

Ge Wang committed 3 years ago

Commit 6736cd51 parent 682f0d615 files · +399 −257Browse files

Showing the changes to src/core/chuck_parse.cpp (1 of 15 files). Show all

Unified · Split

src/core/chuck_parse.cpp+120−181
@@ -32,11 +32,11 @@ · 31 unmodified lines
3232 //-----------------------------------------------------------------------------
3333 #include "chuck_parse.h"
3434 #include "chuck_errmsg.h"
35−#include <string.h>
36−
37−#ifdef __ANDROID__
35+#include "util_string.h"
3836 #include "util_platforms.h"
39−#endif
37+
38+#include <string.h>
39+#include <string>
4040
4141 #ifdef __PLATFORM_WIN32__
4242 #ifndef __CHUNREAL_ENGINE__
@@ -48,226 +48,165 @@ · 5 unmodified lines
4848 #endif // #ifndef __CHUNREAL_ENGINE__
4949 #endif // #ifdef __PLATFORM_WIN32__
5050
51+#include <sys/stat.h>
52+#if defined(__PLATFORM_WIN32__)
53+ #include "dirent_win32.h"
54+#endif
55+
5156 using namespace std;
5257
53−// max path len
54−static const t_CKUINT MAX_FILENAME_LEN = 2048;
5558 // global
56−static char g_filename[MAX_FILENAME_LEN+1] = "";
57−
58−// external
59+static std::string g_filename;
60+// file to parse
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
5968 extern "C" {
60− extern FILE *yyin;
69+ extern FILE * yyin;
6170 }
6271
6372
6473
6574
6675 //-----------------------------------------------------------------------------
67−// name: open_cat_ck()
68−// desc: ...
76+// name: chuck_parse()
77+// desc: INPUT: chuck code (either from file or actual code) to be parsed
78+// OUTPUT: abstract syntax tree representation of the code
6979 //-----------------------------------------------------------------------------
70−FILE * open_cat_ck( c_str fname )
80+t_CKBOOL chuck_parse( const std::string & fname, const std::string & codeLiteral )
7181 {
72−#ifdef __ANDROID__
73− if( strncmp(fname, "jar:", strlen("jar:")) == 0 )
82+ // return value
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 != "" )
7491 {
75− int fd = 0;
76− if( !ChuckAndroid::copyJARURLFileToTemporary(fname, &fd) )
77− {
78− EM_error2( 0, "unable to download from JAR URL: %s", fname );
79− return NULL;
80− }
81− return fdopen(fd, "rb");
92+ CK_FPRINTF_STDERR( "[chuck](parser): (internal) code and FILE descriptor both present!\n" );
93+ CK_FPRINTF_STDERR( "[chuck](parser): |- ignoring FILE descriptor...\n" );
8294 }
83−#endif // __ANDROID__
8495
85− FILE * fd = fopen(fname, "rb");
86− if( !fd )
96+ // copy it
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
87110 {
88− if( !strstr( fname, ".ck" ) && !strstr( fname, ".CK" ) )
111+ // test it
112+ if( !fd )
89113 {
90− strcat( fname, ".ck" );
91− fd = fopen( fname, "rb" );
114+ // open, could potentially change g_filename
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; }
92121 }
93− }
94− return fd;
95−}
96−
97122
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 );
98127
128+ // reset yyin to fd
129+ yyrestart( fd );
130+ // set to initial condition | 1.5.0.5 (ge) added
131+ yyinitial();
99132
100−//-----------------------------------------------------------------------------
101−// name: win32_tmpfile()
102−// desc: replacement for broken tmpfile() on Windows Vista + 7
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;
133+ // check
134+ if( yyin == NULL ) goto cleanup;
135+ }
110136
111−#ifndef __CHUNREAL_ENGINE__
112− if( GetTempPath(256, tmp_path) == 0 )
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;
137+ // reset
138+ if( EM_reset( g_filename.c_str() ) == FALSE ) goto cleanup;
118139
119−#ifndef __CHUNREAL_ENGINE__
120− if( GetTempFileName(tmp_path, "mA", 0, file_path) == 0 )
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;
140+ // TODO: clean g_program
141+ g_program = NULL;
126142
127− file = fopen( file_path, "wb+D" );
143+ // parse
144+ if( !(yyparse() == 0) ) goto cleanup;
128145
129− return file;
130−}
131−#endif // #ifdef __PLATFORM_WIN32__
146+ // flag success
147+ ret = TRUE;
132148
149+cleanup:
133150
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+ }
134157
158+ // set to NULL | 1.5.0.5 (ge) added to reset yyin
159+ yyin = NULL;
135160
136−//-----------------------------------------------------------------------------
137−// name: android_tmpfile()
138−// desc: replacement for broken tmpfile() on Android
139−//-----------------------------------------------------------------------------
140−#ifdef __ANDROID__
161+ // clean up lexer buffer, if we used one
162+ if( yyCodeBuffer )
163+ { yy_delete_buffer( yyCodeBuffer ); yyCodeBuffer = NULL; }
141164
142−FILE * android_tmpfile()
143−{
144− return ChuckAndroid::getTemporaryFile();
165+ return ret;
145166 }
146167
147−#endif
148−
149168
150169
151170
152−//-----------------------------------------------------------------------------
153−// name: chuck_parse()
154−// desc: ...
155−//-----------------------------------------------------------------------------
156−t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code )
171+//------------------------------------------------------------------------------
172+// name: fd2parse_cleanup()
173+// desc: clean up fd2parse
174+//------------------------------------------------------------------------------
175+void fd2parse_cleanup()
157176 {
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−
253177 // check
254− if( yyin == NULL ) goto cleanup;
178+ if( g_fd2parse && g_fd2autoClose )
179+ { fclose( g_fd2parse ); }
255180
256− // TODO: clean g_program
257− g_program = NULL;
181+ // reset
182+ g_fd2parse = NULL;
183+ g_fd2autoClose = FALSE;
184+}
258185
259− // parse
260− if( !(yyparse( ) == 0) ) goto cleanup;
261186
262− // flag success
263− ret = TRUE;
264187
265−cleanup:
266188
267− // done
268− if( clo ) fclose( fd );
189+//------------------------------------------------------------------------------
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();
269206
270− return ret;
207+ // copy
208+ g_fd2parse = fd;
209+ g_fd2autoClose = autoClose;
271210 }
272211
273212

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t