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>
|
|
|
|
using namespace boost::filesystem;
|
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;
|
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();
|
2011-12-25 02:08:38 +04:00
|
|
|
path sourcepath();
|
|
|
|
std::vector<path> path_stack;
|
|
|
|
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
|
|
|
|
|
2010-11-30 23:44:05 +03:00
|
|
|
%x comment string
|
2010-12-09 15:18:24 +03:00
|
|
|
%x include
|
2011-12-25 02:08:38 +04:00
|
|
|
%x 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
|
|
|
%%
|
|
|
|
|
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
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
use[ \t\r\n>]*"<" { BEGIN(use); }
|
|
|
|
<use>{
|
|
|
|
[^\t\r\n>]+ { filename = yytext; }
|
|
|
|
">" {
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
path usepath = path(parser_source_path) / filename;
|
|
|
|
if (!exists(usepath)) {
|
|
|
|
usepath = librarydir / filename;
|
2010-02-28 16:48:04 +03:00
|
|
|
}
|
2011-12-25 02:08:38 +04:00
|
|
|
handle_dep(absolute(usepath).generic_string());
|
|
|
|
parserlval.text = strdup(absolute(usepath).c_str());
|
2010-02-28 01:30:36 +03:00
|
|
|
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
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
\" { BEGIN(string); stringcontents.clear(); }
|
2010-11-30 23:44:05 +03:00
|
|
|
<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?
|
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
|
|
|
%%
|
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
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();
|
|
|
|
|
|
|
|
return 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
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
path dirinfo = sourcepath();
|
|
|
|
if (!filepath.empty()) {
|
|
|
|
dirinfo /= filepath;
|
2011-12-13 00:53:21 +04:00
|
|
|
}
|
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
path finfo = dirinfo / filename;
|
|
|
|
if (!exists(finfo)) {
|
|
|
|
finfo = librarydir / filepath / filename;
|
2011-12-13 00:53:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
filepath.clear();
|
2011-12-25 02:08:38 +04:00
|
|
|
path_stack.push_back(dirinfo);
|
2011-12-13 00:53:21 +04:00
|
|
|
|
2011-12-25 02:08:38 +04:00
|
|
|
handle_dep(absolute(finfo).generic_string());
|
|
|
|
yyin = fopen(absolute(finfo).c_str(), "r");
|
2011-12-13 00:53:21 +04:00
|
|
|
if (!yyin) {
|
2011-12-25 02:08:38 +04:00
|
|
|
PRINTF("WARNING: Can't open input file `%s'.", filename.c_str());
|
|
|
|
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();
|
|
|
|
}
|