2014-10-05 21:52:34 +00:00
|
|
|
#include "RezLexer.h"
|
|
|
|
|
|
|
|
#include <boost/wave.hpp>
|
|
|
|
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
|
|
|
|
#include <boost/wave/token_ids.hpp>
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
|
|
#include "RezLexerWaveToken.h"
|
2015-07-18 22:59:46 +00:00
|
|
|
#include "RezWorld.h"
|
|
|
|
#include "Diagnostic.h"
|
2014-10-05 21:52:34 +00:00
|
|
|
|
|
|
|
namespace wave = boost::wave;
|
|
|
|
|
|
|
|
using namespace boost::wave;
|
|
|
|
|
2014-10-16 00:29:12 +00:00
|
|
|
static std::string readContents(std::istream&& instream)
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
instream.unsetf(std::ios::skipws);
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
return std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
|
|
|
|
std::istreambuf_iterator<char>());
|
2014-10-16 00:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string preFilter(std::string str)
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
boost::regex endif("#endif[^\r\n]*");
|
|
|
|
str = boost::regex_replace(str, endif, "#endif");
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
boost::regex dollar_escape("\\\\\\$([a-zA-Z0-9][a-zA-Z0-9])");
|
|
|
|
str = boost::regex_replace(str, dollar_escape, "\\\\0x$1");
|
2014-10-16 00:29:12 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
if(str.size() == 0 || str[str.size()-1] != '\n')
|
|
|
|
str += "\n";
|
|
|
|
return str;
|
2014-10-16 00:29:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:52:34 +00:00
|
|
|
struct load_file_to_string_filtered
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
template <typename IterContextT>
|
|
|
|
class inner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename PositionT>
|
|
|
|
static void init_iterators(IterContextT &iter_ctx,
|
|
|
|
PositionT const &act_pos, language_support language)
|
|
|
|
{
|
|
|
|
typedef typename IterContextT::iterator_type iterator_type;
|
|
|
|
|
|
|
|
// read in the file
|
|
|
|
std::ifstream instream(iter_ctx.filename.c_str());
|
|
|
|
if (!instream.is_open()) {
|
|
|
|
BOOST_WAVE_THROW_CTX(iter_ctx.ctx, preprocess_exception,
|
|
|
|
bad_include_file, iter_ctx.filename.c_str(), act_pos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter_ctx.instring = preFilter(readContents(std::move(instream)));
|
|
|
|
|
|
|
|
iter_ctx.first = iterator_type(
|
|
|
|
iter_ctx.instring.begin(), iter_ctx.instring.end(),
|
|
|
|
PositionT(iter_ctx.filename), language);
|
|
|
|
iter_ctx.last = iterator_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string instring;
|
|
|
|
};
|
2014-10-05 21:52:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef wave::cpplexer::lex_iterator<
|
2019-08-18 11:21:00 +00:00
|
|
|
wave::cpplexer::lex_token<> >
|
|
|
|
lex_iterator_type;
|
2014-10-05 21:52:34 +00:00
|
|
|
typedef wave::context<
|
2019-08-18 11:21:00 +00:00
|
|
|
std::string::iterator, lex_iterator_type,
|
|
|
|
load_file_to_string_filtered>
|
|
|
|
context_type;
|
2014-10-05 21:52:34 +00:00
|
|
|
typedef context_type::iterator_type pp_iterator_type;
|
|
|
|
|
|
|
|
struct RezLexer::Priv
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
std::string input;
|
|
|
|
context_type ctx;
|
|
|
|
pp_iterator_type iter;
|
|
|
|
|
|
|
|
Priv(std::string data, std::string name)
|
|
|
|
: input(data), ctx(input.begin(), input.end(), name.c_str())
|
|
|
|
{
|
|
|
|
}
|
2014-10-05 21:52:34 +00:00
|
|
|
};
|
|
|
|
|
2015-08-27 18:49:55 +00:00
|
|
|
static std::string readInitial(RezWorld& world, std::string filename)
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
std::ifstream in(filename);
|
|
|
|
if(!in.is_open())
|
|
|
|
{
|
|
|
|
world.problem(Diagnostic(Diagnostic::error,
|
|
|
|
"could not open " + filename, yy::location()));
|
|
|
|
}
|
|
|
|
return readContents(std::move(in));
|
2015-08-27 18:49:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 22:59:46 +00:00
|
|
|
RezLexer::RezLexer(RezWorld& world, std::string filename)
|
2019-08-18 11:21:00 +00:00
|
|
|
: RezLexer(world, filename, readInitial(world,filename))
|
2014-10-05 21:52:34 +00:00
|
|
|
{
|
2014-10-16 00:29:12 +00:00
|
|
|
}
|
2014-10-05 21:52:34 +00:00
|
|
|
|
2015-08-27 18:49:55 +00:00
|
|
|
|
|
|
|
|
2015-07-18 22:59:46 +00:00
|
|
|
RezLexer::RezLexer(RezWorld& world, std::string filename, const std::string &data)
|
2019-08-18 11:21:00 +00:00
|
|
|
: world(world), curFile(filename), lastLocation(&curFile)
|
2014-10-16 00:29:12 +00:00
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
pImpl.reset(new Priv(preFilter(data), filename));
|
2014-10-05 21:52:34 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
pImpl->ctx.add_macro_definition("DeRez=0");
|
|
|
|
pImpl->ctx.add_macro_definition("Rez=1");
|
|
|
|
pImpl->ctx.add_macro_definition("true=1");
|
|
|
|
pImpl->ctx.add_macro_definition("false=0");
|
|
|
|
pImpl->ctx.add_macro_definition("TRUE=1");
|
|
|
|
pImpl->ctx.add_macro_definition("FALSE=0");
|
2014-10-05 21:52:34 +00:00
|
|
|
|
2019-08-18 11:21:00 +00:00
|
|
|
pImpl->iter = pImpl->ctx.begin();
|
2014-10-05 21:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RezLexer::~RezLexer()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-16 00:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
void RezLexer::addDefine(std::string str)
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
pImpl->ctx.add_macro_definition(str);
|
2014-10-16 00:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RezLexer::addIncludePath(std::string path)
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
std::size_t pos = path.find(':');
|
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
pImpl->ctx.add_include_path(path.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addIncludePath(path.substr(0,pos));
|
|
|
|
addIncludePath(path.substr(pos + 1));
|
|
|
|
}
|
2014-10-16 00:26:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 21:52:34 +00:00
|
|
|
bool RezLexer::atEnd()
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
return pImpl->iter == pImpl->ctx.end();
|
2014-10-05 21:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RezLexer::WaveToken RezLexer::nextWave()
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if(pImpl->iter == pImpl->ctx.end())
|
|
|
|
return WaveToken();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WaveToken tok = *pImpl->iter++;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const preprocess_exception& e)
|
|
|
|
{
|
|
|
|
curFile = e.file_name();
|
|
|
|
auto yypos = yy::position(&curFile, e.line_no(), e.column_no());
|
|
|
|
yy::location loc(yypos);
|
|
|
|
lastLocation = loc;
|
|
|
|
|
|
|
|
world.problem(Diagnostic(
|
|
|
|
e.severity_level(e.get_errorcode()) >= util::severity_error
|
|
|
|
? Diagnostic::error
|
|
|
|
: Diagnostic::warning,
|
|
|
|
preprocess_exception::error_text(e.get_errorcode()), loc));
|
|
|
|
if(e.is_recoverable())
|
|
|
|
return nextWave();
|
|
|
|
else
|
|
|
|
return WaveToken();
|
|
|
|
}
|
2014-10-05 21:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RezLexer::WaveToken RezLexer::peekWave()
|
|
|
|
{
|
2019-08-18 11:21:00 +00:00
|
|
|
return pImpl->iter == pImpl->ctx.end() ? WaveToken() : *pImpl->iter;
|
2014-10-05 21:52:34 +00:00
|
|
|
}
|
|
|
|
|