mirror of https://github.com/vitalif/openscad
Ported lexer code from QFile to boost filesystem
parent
51fada9a58
commit
bafbc89aa0
114
src/lexer.l
114
src/lexer.l
|
@ -30,10 +30,11 @@
|
||||||
#include "printutils.h"
|
#include "printutils.h"
|
||||||
#include "parsersettings.h"
|
#include "parsersettings.h"
|
||||||
#include "parser_yacc.h"
|
#include "parser_yacc.h"
|
||||||
#include <QStack>
|
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QDir>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
using namespace boost::filesystem;
|
||||||
|
|
||||||
//isatty for visual c++ and mingw-cross-env
|
//isatty for visual c++ and mingw-cross-env
|
||||||
#if defined __WIN32__ && ! defined _MSC_VER
|
#if defined __WIN32__ && ! defined _MSC_VER
|
||||||
|
@ -44,7 +45,7 @@ extern "C" int __cdecl _isatty(int _FileHandle);
|
||||||
#define isatty _isatty
|
#define isatty _isatty
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QString* stringcontents;
|
std::string stringcontents;
|
||||||
int lexerget_lineno(void);
|
int lexerget_lineno(void);
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
static void yyunput(int, char*) __attribute__((unused));
|
static void yyunput(int, char*) __attribute__((unused));
|
||||||
|
@ -73,12 +74,12 @@ extern const char *parser_source_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void includefile();
|
void includefile();
|
||||||
QDir sourcepath();
|
path sourcepath();
|
||||||
QStack<QDir> path_stack;
|
std::vector<path> path_stack;
|
||||||
QStack<FILE*> openfiles;
|
std::vector<FILE*> openfiles;
|
||||||
|
|
||||||
QString filename;
|
std::string filename;
|
||||||
QString filepath;
|
std::string filepath;
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
@ -87,6 +88,7 @@ QString filepath;
|
||||||
|
|
||||||
%x comment string
|
%x comment string
|
||||||
%x include
|
%x include
|
||||||
|
%x use
|
||||||
|
|
||||||
D [0-9]
|
D [0-9]
|
||||||
E [Ee][+-]?{D}+
|
E [Ee][+-]?{D}+
|
||||||
|
@ -101,34 +103,36 @@ include[ \t\r\n>]*"<" { BEGIN(include); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
use[ \t\r\n>]*"<"[^\t\r\n>]+">" {
|
use[ \t\r\n>]*"<" { BEGIN(use); }
|
||||||
QString filename(yytext);
|
<use>{
|
||||||
filename.remove(QRegExp("^use[ \t\r\n>]*<"));
|
[^\t\r\n>]+ { filename = yytext; }
|
||||||
filename.remove(QRegExp(">$"));
|
">" {
|
||||||
QFileInfo finfo(QDir(parser_source_path), filename);
|
BEGIN(INITIAL);
|
||||||
if (!finfo.exists()) {
|
path usepath = path(parser_source_path) / filename;
|
||||||
finfo = QFileInfo(QDir(librarydir), filename);
|
if (!exists(usepath)) {
|
||||||
|
usepath = librarydir / filename;
|
||||||
}
|
}
|
||||||
handle_dep(finfo.absoluteFilePath().toStdString());
|
handle_dep(absolute(usepath).generic_string());
|
||||||
parserlval.text = strdup(finfo.absoluteFilePath().toLocal8Bit());
|
parserlval.text = strdup(absolute(usepath).c_str());
|
||||||
return TOK_USE;
|
return TOK_USE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
"<"[^ \t\r\n>]+">" {
|
"<"[^ \t\r\n>]+">" {
|
||||||
char *filename = strdup(yytext+1);
|
char *filename = strdup(yytext+1);
|
||||||
filename[strlen(filename)-1] = 0;
|
filename[strlen(filename)-1] = 0;
|
||||||
QFileInfo finfo(QDir(parser_source_path), filename);
|
path incpath = path(parser_source_path) / filename;
|
||||||
if (!finfo.exists()) {
|
if (!exists(incpath)) {
|
||||||
finfo = QFileInfo(QDir(librarydir), filename);
|
incpath = librarydir / filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
PRINTF("DEPRECATED: Support for implicit include will be removed in future releases. Use `include <filename>' instead.");
|
PRINTF("DEPRECATED: Support for implicit include will be removed in future releases. Use `include <filename>' instead.");
|
||||||
handle_dep(finfo.absoluteFilePath().toStdString());
|
handle_dep(absolute(incpath).generic_string());
|
||||||
yyin = fopen(finfo.absoluteFilePath().toLocal8Bit(), "r");
|
yyin = fopen(absolute(incpath).c_str(), "r");
|
||||||
if (!yyin) {
|
if (!yyin) {
|
||||||
PRINTF("WARNING: Can't open input file `%s'.", filename);
|
PRINTF("WARNING: Can't open input file `%s'.", filename);
|
||||||
} else {
|
} else {
|
||||||
openfiles.append(yyin);
|
openfiles.push_back(yyin);
|
||||||
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
|
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
|
||||||
BEGIN(INITIAL);
|
BEGIN(INITIAL);
|
||||||
}
|
}
|
||||||
|
@ -136,11 +140,11 @@ use[ \t\r\n>]*"<"[^\t\r\n>]+">" {
|
||||||
}
|
}
|
||||||
|
|
||||||
<<EOF>> {
|
<<EOF>> {
|
||||||
if(!path_stack.empty())
|
if(!path_stack.empty()) path_stack.pop_back();
|
||||||
path_stack.pop();
|
|
||||||
if (yyin && yyin != stdin) {
|
if (yyin && yyin != stdin) {
|
||||||
assert(!openfiles.empty());
|
assert(!openfiles.empty());
|
||||||
fclose(openfiles.pop());
|
fclose(openfiles.back());
|
||||||
|
openfiles.pop_back();
|
||||||
}
|
}
|
||||||
yypop_buffer_state();
|
yypop_buffer_state();
|
||||||
if (!YY_CURRENT_BUFFER)
|
if (!YY_CURRENT_BUFFER)
|
||||||
|
@ -158,20 +162,19 @@ use[ \t\r\n>]*"<"[^\t\r\n>]+">" {
|
||||||
|
|
||||||
{D}+{E}? |
|
{D}+{E}? |
|
||||||
{D}*\.{D}+{E}? |
|
{D}*\.{D}+{E}? |
|
||||||
{D}+\.{D}*{E}? { parserlval.number = QString(yytext).toDouble(); return TOK_NUMBER; }
|
{D}+\.{D}*{E}? { parserlval.number = boost::lexical_cast<double>(yytext); return TOK_NUMBER; }
|
||||||
"$"?[a-zA-Z0-9_]+ { parserlval.text = strdup(yytext); return TOK_ID; }
|
"$"?[a-zA-Z0-9_]+ { parserlval.text = strdup(yytext); return TOK_ID; }
|
||||||
|
|
||||||
\" { BEGIN(string); stringcontents = new QString(); }
|
\" { BEGIN(string); stringcontents.clear(); }
|
||||||
<string>{
|
<string>{
|
||||||
\\n { stringcontents->append('\n'); }
|
\\n { stringcontents += '\n'; }
|
||||||
\\t { stringcontents->append('\t'); }
|
\\t { stringcontents += '\t'; }
|
||||||
\\r { stringcontents->append('\r'); }
|
\\r { stringcontents += '\r'; }
|
||||||
\\\\ { stringcontents->append('\\'); }
|
\\\\ { stringcontents += '\\'; }
|
||||||
\\\" { stringcontents->append('"'); }
|
\\\" { stringcontents += '"'; }
|
||||||
[^\\\n\"]+ { stringcontents->append(lexertext); }
|
[^\\\n\"]+ { stringcontents += lexertext; }
|
||||||
\" { BEGIN(INITIAL);
|
\" { BEGIN(INITIAL);
|
||||||
parserlval.text = strdup(stringcontents->toLocal8Bit());
|
parserlval.text = strdup(stringcontents.c_str());
|
||||||
delete stringcontents;
|
|
||||||
return TOK_STRING; }
|
return TOK_STRING; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,12 +195,11 @@ use[ \t\r\n>]*"<"[^\t\r\n>]+">" {
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
QDir sourcepath()
|
path sourcepath()
|
||||||
{
|
{
|
||||||
if(!path_stack.empty())
|
if (!path_stack.empty()) return path_stack.back();
|
||||||
return path_stack.top();
|
|
||||||
|
return path(parser_source_path);
|
||||||
return QDir(parser_source_path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -207,29 +209,29 @@ QDir sourcepath()
|
||||||
*/
|
*/
|
||||||
void includefile()
|
void includefile()
|
||||||
{
|
{
|
||||||
if (filename.isEmpty()) return;
|
if (filename.empty()) return;
|
||||||
|
|
||||||
QDir dirinfo(sourcepath());
|
path dirinfo = sourcepath();
|
||||||
if (!filepath.isEmpty()) {
|
if (!filepath.empty()) {
|
||||||
dirinfo.cd(filepath);
|
dirinfo /= filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
QFileInfo finfo(dirinfo, filename);
|
path finfo = dirinfo / filename;
|
||||||
if (!finfo.exists()) {
|
if (!exists(finfo)) {
|
||||||
finfo = QFileInfo(QFileInfo(QDir(librarydir), filepath).dir(), filename);
|
finfo = librarydir / filepath / filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath.clear();
|
filepath.clear();
|
||||||
path_stack.push(dirinfo);
|
path_stack.push_back(dirinfo);
|
||||||
|
|
||||||
handle_dep(finfo.absoluteFilePath().toStdString());
|
handle_dep(absolute(finfo).generic_string());
|
||||||
yyin = fopen(finfo.absoluteFilePath().toLocal8Bit(), "r");
|
yyin = fopen(absolute(finfo).c_str(), "r");
|
||||||
if (!yyin) {
|
if (!yyin) {
|
||||||
PRINTA("WARNING: Can't open input file `%1'.", filename);
|
PRINTF("WARNING: Can't open input file `%s'.", filename.c_str());
|
||||||
path_stack.pop();
|
path_stack.pop_back();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
openfiles.append(yyin);
|
openfiles.push_back(yyin);
|
||||||
filename.clear();
|
filename.clear();
|
||||||
|
|
||||||
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
|
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
|
||||||
|
@ -241,7 +243,7 @@ void includefile()
|
||||||
*/
|
*/
|
||||||
void lexerdestroy()
|
void lexerdestroy()
|
||||||
{
|
{
|
||||||
foreach (FILE *f, openfiles) fclose(f);
|
BOOST_FOREACH (FILE *f, openfiles) fclose(f);
|
||||||
openfiles.clear();
|
openfiles.clear();
|
||||||
path_stack.clear();
|
path_stack.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,29 @@
|
||||||
#include "parsersettings.h"
|
#include "parsersettings.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
QString librarydir;
|
using namespace boost::filesystem;
|
||||||
|
|
||||||
|
std::string librarydir;
|
||||||
|
|
||||||
void parser_init()
|
void parser_init()
|
||||||
{
|
{
|
||||||
QDir libdir(QApplication::instance()->applicationDirPath());
|
path libdir(QApplication::instance()->applicationDirPath().toStdString());
|
||||||
|
path tmpdir;
|
||||||
#ifdef Q_WS_MAC
|
#ifdef Q_WS_MAC
|
||||||
libdir.cd("../Resources"); // Libraries can be bundled
|
libdir /= "../Resources"; // Libraries can be bundled
|
||||||
if (!libdir.exists("libraries")) libdir.cd("../../..");
|
if (!is_directory(libdir / "libraries")) libdir /= "../../..";
|
||||||
#elif defined(Q_OS_UNIX)
|
#elif defined(Q_OS_UNIX)
|
||||||
if (libdir.cd("../share/openscad/libraries")) {
|
if (is_directory(tmpdir = libdir / "../share/openscad/libraries")) {
|
||||||
librarydir = libdir.path();
|
librarydir = tmpdir.generic_string();
|
||||||
} else if (libdir.cd("../../share/openscad/libraries")) {
|
} else if (is_directory(tmpdir = libdir / "../../share/openscad/libraries")) {
|
||||||
librarydir = libdir.path();
|
librarydir = tmpdir.generic_string();
|
||||||
} else if (libdir.cd("../../libraries")) {
|
} else if (is_directory(tmpdir = libdir / "../../libraries")) {
|
||||||
librarydir = libdir.path();
|
librarydir = tmpdir.generic_string();
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
if (libdir.cd("libraries")) {
|
if (is_directory(tmpdir = libdir / "libraries")) {
|
||||||
librarydir = libdir.path();
|
librarydir = tmpdir.generic_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#ifndef PARSERSETTINGS_H_
|
#ifndef PARSERSETTINGS_H_
|
||||||
#define PARSERSETTINGS_H_
|
#define PARSERSETTINGS_H_
|
||||||
|
|
||||||
#include <QString>
|
#include <string>
|
||||||
|
|
||||||
extern QString librarydir;
|
extern std::string librarydir;
|
||||||
extern int parser_error_pos;
|
extern int parser_error_pos;
|
||||||
|
|
||||||
void parser_init();
|
void parser_init();
|
||||||
|
|
Loading…
Reference in New Issue