diff options
-rw-r--r-- | src/lexer.l | 24 | ||||
-rw-r--r-- | src/parser.y | 3 |
2 files changed, 22 insertions, 5 deletions
diff --git a/src/lexer.l b/src/lexer.l index 4c0ddea..3cd4a19 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -33,6 +33,7 @@ #include <QStack> #include <QFileInfo> #include <QDir> +#include <assert.h> //isatty for visual c++ #ifdef _MSC_VER @@ -71,6 +72,7 @@ extern const char *parser_source_path; void includefile(); QDir sourcepath(); QStack<QDir> path_stack; +QStack<FILE*> openfiles; QString filename; QString filepath; @@ -122,6 +124,7 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { if (!yyin) { PRINTF("WARNING: Can't open input file `%s'.", filename); } else { + openfiles.append(yyin); yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); BEGIN(INITIAL); } @@ -129,10 +132,12 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { } <<EOF>> { - if(!path_stack.isEmpty()) + if(!path_stack.empty()) path_stack.pop(); - if (yyin && yyin != stdin) - fclose(yyin); + if (yyin && yyin != stdin) { + assert(!openfiles.empty()); + fclose(openfiles.pop()); + } yypop_buffer_state(); if (!YY_CURRENT_BUFFER) yyterminate(); @@ -183,7 +188,7 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { QDir sourcepath() { - if(!path_stack.isEmpty()) + if(!path_stack.empty()) return path_stack.top(); return QDir(parser_source_path); @@ -214,8 +219,19 @@ void includefile() path_stack.pop(); return; } + openfiles.append(yyin); filename.clear(); yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); } +/*! + In case of an error, this will make sure we clean up our custom data structures + and close all files. +*/ +void lexerdestroy() +{ + foreach (FILE *f, openfiles) fclose(f); + openfiles.clear(); + path_stack.clear(); +} diff --git a/src/parser.y b/src/parser.y index 2d78d1f..33fbc3f 100644 --- a/src/parser.y +++ b/src/parser.y @@ -575,6 +575,7 @@ void yyerror (char const *s) module = NULL; } +extern void lexerdestroy(); extern FILE *lexerin; extern const char *parser_input_buffer; const char *parser_input_buffer; @@ -592,7 +593,7 @@ AbstractModule *parse(const char *text, const char *path, int debug) parserdebug = debug; parserparse(); - + lexerdestroy(); lexerlex_destroy(); if (!module) |