diff options
author | Giles Bathgate <gilesbathgate@gmail.com> | 2010-12-03 17:43:00 (GMT) |
---|---|---|
committer | Giles Bathgate <gilesbathgate@gmail.com> | 2010-12-03 17:43:00 (GMT) |
commit | 5ef31011f39106318981d758967b64b5ee2fb275 (patch) | |
tree | fe1eae66424ea9a7a2d030dbeb312076cf95b6ea | |
parent | ff249dfefb59a7d999210be269d1169c3db5646d (diff) |
Added support for nested includes.
-rw-r--r-- | src/lexer.l | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/src/lexer.l b/src/lexer.l index 86006d0..91606c6 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -28,6 +28,7 @@ #include "openscad.h" #include "printutils.h" #include "parser_yacc.h" +#include <QStack> #include <QFileInfo> #include <QDir> QString* stringcontents; @@ -58,35 +59,33 @@ extern const char *parser_source_path; } \ } +void includefile(); +QDir sourcepath(); +QStack<QDir> path_stack; + +QString filename; +QString filepath; + %} %option yylineno %option noyywrap %x comment string +%x include path DIGIT [0-9] %% -include[ \t\r\n>]*"<"[^ \t\r\n>]+">" { - QString filename(yytext); - filename.remove(QRegExp("^include[ \t\r\n>]*<")); - filename.remove(QRegExp(">$")); - QFileInfo finfo(QDir(parser_source_path), filename); - if (!finfo.exists()) { - finfo = QFileInfo(QDir(librarydir), filename); - } - handle_dep(finfo.absoluteFilePath()); - yyin = fopen(finfo.absoluteFilePath().toLocal8Bit(), "r"); - if (!yyin) { - PRINTA("WARNING: Can't open input file `%1'.", filename); - } else { - yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); - BEGIN(INITIAL); - } +include[ \t\r\n>]*"<" { BEGIN(include); } +<include>{ +<path>[^\t\r\n>]+"/" { filepath = yytext; } +<path>[^\t\r\n>/]+ { filename = yytext; } +<path>">" { BEGIN(INITIAL); includefile(); } } + use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { QString filename(yytext); filename.remove(QRegExp("^use[ \t\r\n>]*<")); @@ -121,6 +120,8 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { } <<EOF>> { + if(!path_stack.isEmpty()) + path_stack.pop(); if (yyin && yyin != stdin) fclose(yyin); yypop_buffer_state(); @@ -169,3 +170,42 @@ use[ \t\r\n>]*"<"[^ \t\r\n>]+">" { . { return yytext[0]; } +%% + +QDir sourcepath() +{ + if(!path_stack.isEmpty()) + return path_stack.top(); + + return QDir(parser_source_path); +} + +void includefile() +{ + if(filename.isEmpty()) + return; + + if(filepath.isEmpty()) { + path_stack.push(sourcepath()); + } else { + QFileInfo dirinfo(sourcepath(),filepath); + path_stack.push(dirinfo.dir()); + filepath.clear(); + } + + QFileInfo finfo(sourcepath(), filename); + if (!finfo.exists()) { + finfo = QFileInfo(QDir(librarydir), filename); + } + + handle_dep(finfo.absoluteFilePath()); + yyin = fopen(finfo.absoluteFilePath().toLocal8Bit(), "r"); + if (!yyin) { + PRINTA("WARNING: Can't open input file `%1'.", filename); + return; + } + filename.clear(); + + yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); +} + |