2009-06-20 16:10:55 +04:00
|
|
|
/*
|
2011-01-21 04:21:09 +03:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2009-06-20 16:10:55 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
2009-06-20 16:10:55 +04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
|
|
|
#include "openscad.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2009-06-20 16:10:55 +04:00
|
|
|
#include "parser_yacc.h"
|
2010-12-03 20:43:00 +03:00
|
|
|
#include <QStack>
|
2010-02-01 06:11:29 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2011-01-25 01:58:19 +03:00
|
|
|
|
|
|
|
//isatty for visual c++
|
|
|
|
#ifdef _MSC_VER
|
2011-07-21 06:06:18 +04:00
|
|
|
extern "C" int __cdecl _isatty(int _FileHandle);
|
2011-01-25 01:58:19 +03:00
|
|
|
#define isatty _isatty
|
|
|
|
#endif
|
|
|
|
|
2010-11-30 23:44:05 +03:00
|
|
|
QString* stringcontents;
|
2009-06-21 23:04:41 +04:00
|
|
|
int lexerget_lineno(void);
|
2009-07-26 20:36:52 +04:00
|
|
|
#ifdef __GNUC__
|
2009-07-06 11:58:12 +04:00
|
|
|
static void yyunput(int, char*) __attribute__((unused));
|
2009-07-26 20:36:52 +04:00
|
|
|
#endif
|
2009-06-23 23:56:46 +04:00
|
|
|
extern const char *parser_input_buffer;
|
2010-02-01 06:11:29 +03:00
|
|
|
extern const char *parser_source_path;
|
2009-06-23 23:56:46 +04:00
|
|
|
|
2009-07-25 15:33:45 +04:00
|
|
|
#define YY_INPUT(buf,result,max_size) { \
|
|
|
|
if (yyin && yyin != stdin) { \
|
|
|
|
int c = fgetc(yyin); \
|
|
|
|
if (c >= 0) { \
|
|
|
|
result = 1; \
|
|
|
|
buf[0] = c; \
|
|
|
|
} else { \
|
|
|
|
result = YY_NULL; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
if (*parser_input_buffer) { \
|
|
|
|
result = 1; \
|
|
|
|
buf[0] = *(parser_input_buffer++); \
|
2009-10-19 22:27:47 +04:00
|
|
|
parser_error_pos++; \
|
2009-07-25 15:33:45 +04:00
|
|
|
} else { \
|
|
|
|
result = YY_NULL; \
|
|
|
|
} \
|
|
|
|
} \
|
2009-06-23 23:56:46 +04:00
|
|
|
}
|
2009-06-21 23:04:41 +04:00
|
|
|
|
2010-12-03 20:43:00 +03:00
|
|
|
void includefile();
|
|
|
|
QDir sourcepath();
|
|
|
|
QStack<QDir> path_stack;
|
|
|
|
|
|
|
|
QString filename;
|
|
|
|
QString filepath;
|
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%}
|
|
|
|
|
|
|
|
%option yylineno
|
|
|
|
%option noyywrap
|
|
|
|
|
2010-11-30 23:44:05 +03:00
|
|
|
%x comment string
|
2010-12-09 15:18:24 +03:00
|
|
|
%x include
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2010-11-02 17:17:03 +03:00
|
|
|
DIGIT [0-9]
|
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%%
|
|
|
|
|
2010-12-03 20:43:00 +03:00
|
|
|
include[ \t\r\n>]*"<" { BEGIN(include); }
|
|
|
|
<include>{
|
2011-04-30 19:12:06 +04:00
|
|
|
[^\t\r\n>]*"/" { filepath = yytext; }
|
2010-12-09 15:18:24 +03:00
|
|
|
[^\t\r\n>/]+ { filename = yytext; }
|
|
|
|
">" { BEGIN(INITIAL); includefile(); }
|
2010-02-28 01:30:36 +03:00
|
|
|
}
|
|
|
|
|
2010-12-03 20:43:00 +03:00
|
|
|
|
2010-02-28 01:30:36 +03:00
|
|
|
use[ \t\r\n>]*"<"[^ \t\r\n>]+">" {
|
|
|
|
QString filename(yytext);
|
|
|
|
filename.remove(QRegExp("^use[ \t\r\n>]*<"));
|
|
|
|
filename.remove(QRegExp(">$"));
|
2010-02-28 16:48:04 +03:00
|
|
|
QFileInfo finfo(QDir(parser_source_path), filename);
|
|
|
|
if (!finfo.exists()) {
|
|
|
|
finfo = QFileInfo(QDir(librarydir), filename);
|
|
|
|
}
|
|
|
|
handle_dep(finfo.absoluteFilePath());
|
|
|
|
parserlval.text = strdup(finfo.absoluteFilePath().toLocal8Bit());
|
2010-02-28 01:30:36 +03:00
|
|
|
return TOK_USE;
|
|
|
|
}
|
|
|
|
|
|
|
|
"<"[^ \t\r\n>]+">" {
|
2009-07-25 15:33:45 +04:00
|
|
|
char *filename = strdup(yytext+1);
|
|
|
|
filename[strlen(filename)-1] = 0;
|
2010-02-03 07:15:34 +03:00
|
|
|
QFileInfo finfo(QDir(parser_source_path), filename);
|
|
|
|
if (!finfo.exists()) {
|
|
|
|
finfo = QFileInfo(QDir(librarydir), filename);
|
|
|
|
}
|
|
|
|
|
2010-11-02 14:13:44 +03:00
|
|
|
PRINTF("DEPRECATED: Support for implicit include will be removed in future releases. Use `include <filename>' instead.");
|
2010-02-01 06:11:29 +03:00
|
|
|
handle_dep(finfo.absoluteFilePath());
|
|
|
|
yyin = fopen(finfo.absoluteFilePath().toLocal8Bit(), "r");
|
2009-07-25 15:33:45 +04:00
|
|
|
if (!yyin) {
|
|
|
|
PRINTF("WARNING: Can't open input file `%s'.", filename);
|
|
|
|
} else {
|
|
|
|
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
}
|
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
<<EOF>> {
|
2010-12-03 20:43:00 +03:00
|
|
|
if(!path_stack.isEmpty())
|
|
|
|
path_stack.pop();
|
2009-12-04 10:33:02 +03:00
|
|
|
if (yyin && yyin != stdin)
|
2009-07-25 15:33:45 +04:00
|
|
|
fclose(yyin);
|
|
|
|
yypop_buffer_state();
|
|
|
|
if (!YY_CURRENT_BUFFER)
|
|
|
|
yyterminate();
|
|
|
|
}
|
|
|
|
|
2009-06-23 14:31:25 +04:00
|
|
|
"module" return TOK_MODULE;
|
|
|
|
"function" return TOK_FUNCTION;
|
2010-02-12 16:20:15 +03:00
|
|
|
"if" return TOK_IF;
|
|
|
|
"else" return TOK_ELSE;
|
2009-06-23 14:31:25 +04:00
|
|
|
|
|
|
|
"true" return TOK_TRUE;
|
|
|
|
"false" return TOK_FALSE;
|
|
|
|
"undef" return TOK_UNDEF;
|
|
|
|
|
2010-11-02 17:17:03 +03:00
|
|
|
{DIGIT}+|{DIGIT}*\.{DIGIT}+|{DIGIT}+\.{DIGIT}* { parserlval.number = QString(yytext).toDouble(); return TOK_NUMBER; }
|
2009-06-26 08:32:52 +04:00
|
|
|
"$"?[a-zA-Z0-9_]+ { parserlval.text = strdup(yytext); return TOK_ID; }
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2010-11-30 23:44:05 +03:00
|
|
|
\" { BEGIN(string); stringcontents = new QString(); }
|
|
|
|
<string>{
|
|
|
|
\\n { stringcontents->append('\n'); }
|
|
|
|
\\t { stringcontents->append('\t'); }
|
|
|
|
\\r { stringcontents->append('\r'); }
|
|
|
|
\\\\ { stringcontents->append('\\'); }
|
|
|
|
\\\" { stringcontents->append('"'); }
|
|
|
|
[^\\\n\"]+ { stringcontents->append(lexertext); }
|
|
|
|
\" { BEGIN(INITIAL);
|
|
|
|
parserlval.text = strdup(stringcontents->toLocal8Bit());
|
|
|
|
delete stringcontents;
|
|
|
|
return TOK_STRING; }
|
|
|
|
}
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2009-06-23 14:31:25 +04:00
|
|
|
[\n\r\t ]
|
2010-01-13 10:47:53 +03:00
|
|
|
\/\/[^\n]*\n?
|
2009-06-20 16:10:55 +04:00
|
|
|
"/*" BEGIN(comment);
|
|
|
|
<comment>"*/" BEGIN(INITIAL);
|
|
|
|
<comment>.|\n
|
|
|
|
|
2009-07-01 12:06:06 +04:00
|
|
|
"<=" return LE;
|
|
|
|
">=" return GE;
|
|
|
|
"==" return EQ;
|
|
|
|
"!=" return NE;
|
|
|
|
"&&" return AND;
|
|
|
|
"||" return OR;
|
|
|
|
|
|
|
|
. { return yytext[0]; }
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2010-12-03 20:43:00 +03:00
|
|
|
%%
|
|
|
|
|
|
|
|
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);
|
2010-12-11 15:41:23 +03:00
|
|
|
path_stack.pop();
|
2010-12-03 20:43:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
filename.clear();
|
|
|
|
|
|
|
|
yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
|
|
|
|
}
|
|
|
|
|