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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
2011-09-04 02:03:16 +04:00
|
|
|
#include "handle_dep.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2011-12-25 01:02:37 +04:00
|
|
|
#include "parsersettings.h"
|
2009-06-20 16:10:55 +04:00
|
|
|
#include "parser_yacc.h"
|
2011-09-11 03:53:25 +04:00
|
|
|
#include <assert.h>
|
2011-12-25 02:08:38 +04:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
2012-01-09 19:51:38 +04:00
|
|
|
namespace fs = boost::filesystem;
|
2012-01-14 08:02:15 +04:00
|
|
|
#include "boosty.h"
|
2011-01-25 01:58:19 +03:00
|
|
|
|
2011-10-26 04:10:28 +04:00
|
|
|
//isatty for visual c++ and mingw-cross-env
|
|
|
|
#if defined __WIN32__ && ! defined _MSC_VER
|
|
|
|
#include "unistd.h"
|
|
|
|
#endif
|
|
|
|
#if defined __WIN32__ || defined _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
|
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
std::string 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;
|
2012-02-04 04:43:53 +04:00
|
|
|
extern std::string 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();
|
2012-01-09 19:51:38 +04:00
|
|
|
fs::path sourcepath();
|
|
|
|
std::vector<fs::path> path_stack;
|
2011-12-25 02:08:38 +04:00
|
|
|
std::vector<FILE*> openfiles;
|
2010-12-03 20:43:00 +03:00
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
std::string filename;
|
|
|
|
std::string filepath;
|
2010-12-03 20:43:00 +03:00
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%}
|
|
|
|
|
|
|
|
%option yylineno
|
|
|
|
%option noyywrap
|
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
%x cond_comment cond_string
|
|
|
|
%x cond_include
|
|
|
|
%x cond_use
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2011-11-05 19:22:48 +04:00
|
|
|
D [0-9]
|
|
|
|
E [Ee][+-]?{D}+
|
2010-11-02 17:17:03 +03:00
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%%
|
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
include[ \t\r\n>]*"<" { BEGIN(cond_include); }
|
|
|
|
<cond_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
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
use[ \t\r\n>]*"<" { BEGIN(cond_use); }
|
|
|
|
<cond_use>{
|
2011-12-25 02:08:38 +04:00
|
|
|
[^\t\r\n>]+ { filename = yytext; }
|
|
|
|
">" {
|
2012-02-04 04:43:53 +04:00
|
|
|
PRINTB("USE: %s", filename);
|
2011-12-25 02:08:38 +04:00
|
|
|
BEGIN(INITIAL);
|
2012-01-14 06:25:09 +04:00
|
|
|
fs::path usepath;
|
2012-02-03 01:50:51 +04:00
|
|
|
if (boosty::is_absolute(fs::path(filename))) {
|
2012-01-14 06:25:09 +04:00
|
|
|
usepath = filename;
|
|
|
|
}
|
|
|
|
else {
|
2012-02-03 02:11:54 +04:00
|
|
|
usepath = sourcepath() / filename;
|
2012-01-14 06:25:09 +04:00
|
|
|
if (!fs::exists(usepath)) {
|
2012-02-03 01:50:51 +04:00
|
|
|
usepath = boosty::absolute(fs::path(get_librarydir()) / filename);
|
2012-01-14 06:25:09 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-04 05:40:46 +04:00
|
|
|
/* Only accept regular files which exists */
|
|
|
|
if (usepath.has_parent_path() && fs::exists(usepath)) {
|
|
|
|
handle_dep(usepath.string());
|
|
|
|
parserlval.text = strdup(usepath.string().c_str());
|
|
|
|
return TOK_USE;
|
|
|
|
}
|
2011-12-25 02:08:38 +04:00
|
|
|
}
|
2010-02-28 01:30:36 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 15:33:45 +04:00
|
|
|
<<EOF>> {
|
2011-12-25 02:08:38 +04:00
|
|
|
if(!path_stack.empty()) path_stack.pop_back();
|
2011-09-11 03:53:25 +04:00
|
|
|
if (yyin && yyin != stdin) {
|
|
|
|
assert(!openfiles.empty());
|
2011-12-25 02:08:38 +04:00
|
|
|
fclose(openfiles.back());
|
|
|
|
openfiles.pop_back();
|
2011-09-11 03:53:25 +04:00
|
|
|
}
|
2009-07-25 15:33:45 +04:00
|
|
|
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;
|
|
|
|
|
2011-11-05 19:22:48 +04:00
|
|
|
{D}+{E}? |
|
|
|
|
{D}*\.{D}+{E}? |
|
2011-12-25 02:08:38 +04:00
|
|
|
{D}+\.{D}*{E}? { parserlval.number = boost::lexical_cast<double>(yytext); return TOK_NUMBER; }
|
2011-11-05 19:22:48 +04:00
|
|
|
"$"?[a-zA-Z0-9_]+ { parserlval.text = strdup(yytext); return TOK_ID; }
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
\" { BEGIN(cond_string); stringcontents.clear(); }
|
|
|
|
<cond_string>{
|
2011-12-25 02:08:38 +04:00
|
|
|
\\n { stringcontents += '\n'; }
|
|
|
|
\\t { stringcontents += '\t'; }
|
|
|
|
\\r { stringcontents += '\r'; }
|
|
|
|
\\\\ { stringcontents += '\\'; }
|
|
|
|
\\\" { stringcontents += '"'; }
|
|
|
|
[^\\\n\"]+ { stringcontents += lexertext; }
|
2010-11-30 23:44:05 +03:00
|
|
|
\" { BEGIN(INITIAL);
|
2011-12-25 02:08:38 +04:00
|
|
|
parserlval.text = strdup(stringcontents.c_str());
|
2010-11-30 23:44:05 +03:00
|
|
|
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?
|
2012-01-09 19:51:38 +04:00
|
|
|
"/*" BEGIN(cond_comment);
|
|
|
|
<cond_comment>"*/" BEGIN(INITIAL);
|
|
|
|
<cond_comment>.|\n
|
2009-06-20 16:10:55 +04:00
|
|
|
|
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
|
|
|
%%
|
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
fs::path sourcepath()
|
2010-12-03 20:43:00 +03:00
|
|
|
{
|
2011-12-25 02:08:38 +04:00
|
|
|
if (!path_stack.empty()) return path_stack.back();
|
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
return fs::path(parser_source_path);
|
2010-12-03 20:43:00 +03:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:53:21 +04:00
|
|
|
/*
|
|
|
|
Rules for include <path/file>
|
|
|
|
1) include <sourcepath/path/file>
|
|
|
|
2) include <librarydir/path/file>
|
|
|
|
*/
|
2010-12-03 20:43:00 +03:00
|
|
|
void includefile()
|
|
|
|
{
|
2011-12-25 02:08:38 +04:00
|
|
|
if (filename.empty()) return;
|
2011-12-13 00:53:21 +04:00
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
fs::path dirinfo = sourcepath();
|
2011-12-25 02:08:38 +04:00
|
|
|
if (!filepath.empty()) {
|
2012-02-03 01:50:51 +04:00
|
|
|
if (boosty::is_absolute(fs::path(filepath))) {
|
2012-01-14 06:25:09 +04:00
|
|
|
dirinfo = filepath;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dirinfo /= filepath;
|
|
|
|
}
|
2011-12-13 00:53:21 +04:00
|
|
|
}
|
|
|
|
|
2012-01-09 19:51:38 +04:00
|
|
|
fs::path finfo = dirinfo / filename;
|
2011-12-25 02:08:38 +04:00
|
|
|
if (!exists(finfo)) {
|
2012-02-03 01:50:51 +04:00
|
|
|
finfo = fs::path(get_librarydir()) / filepath / filename;
|
2011-12-13 00:53:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
filepath.clear();
|
2012-02-03 02:11:54 +04:00
|
|
|
path_stack.push_back(finfo.parent_path());
|
2011-12-13 00:53:21 +04:00
|
|
|
|
2012-01-14 08:02:15 +04:00
|
|
|
handle_dep(boosty::absolute(finfo).string());
|
|
|
|
yyin = fopen(boosty::absolute(finfo).string().c_str(), "r");
|
2011-12-13 00:53:21 +04:00
|
|
|
if (!yyin) {
|
2012-01-25 06:11:12 +04:00
|
|
|
PRINTB("WARNING: Can't open input file '%s'.", filename);
|
2011-12-25 02:08:38 +04:00
|
|
|
path_stack.pop_back();
|
2011-12-13 00:53:21 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-12-25 02:08:38 +04:00
|
|
|
openfiles.push_back(yyin);
|
2011-12-13 00:53:21 +04:00
|
|
|
filename.clear();
|
|
|
|
|
|
|
|
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
|
2010-12-03 20:43:00 +03:00
|
|
|
}
|
|
|
|
|
2011-09-11 03:53:25 +04:00
|
|
|
/*!
|
|
|
|
In case of an error, this will make sure we clean up our custom data structures
|
|
|
|
and close all files.
|
|
|
|
*/
|
|
|
|
void lexerdestroy()
|
|
|
|
{
|
2011-12-25 02:08:38 +04:00
|
|
|
BOOST_FOREACH (FILE *f, openfiles) fclose(f);
|
2011-09-11 03:53:25 +04:00
|
|
|
openfiles.clear();
|
|
|
|
path_stack.clear();
|
|
|
|
}
|