2018-04-01 14:49:10 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-04-01 14:49:10 +10:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "cpp.hpp"
|
|
|
|
|
|
|
|
#include "io.hpp"
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::cpp::include;
|
|
|
|
using cruft::cpp::passthrough;
|
|
|
|
using cruft::cpp::processor;
|
2018-04-01 14:49:10 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
processor::processor ()
|
|
|
|
{
|
|
|
|
m_directives.insert ({
|
|
|
|
"include",
|
|
|
|
std::make_unique<include> (*this)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
processor::add (std::string token, std::unique_ptr<directive> handler)
|
|
|
|
{
|
|
|
|
m_directives.emplace (std::pair {std::move (token), std::move (handler)});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-12-05 19:12:03 +11:00
|
|
|
processor::process (std::ostream &os, const std::filesystem::path &src) const
|
2018-04-01 14:49:10 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto data = cruft::slurp<char> (src);
|
2018-04-01 14:49:10 +10:00
|
|
|
context ctx;
|
|
|
|
ctx.source.push (src);
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::tokeniser<const char*> tok (data, '\n');
|
|
|
|
process (os, ctx, cruft::view (tok.begin (), tok.end ()));
|
2018-04-01 14:49:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::tokeniser<const char*>::iterator
|
2018-04-01 14:49:10 +10:00
|
|
|
processor::process (
|
|
|
|
std::ostream &os,
|
|
|
|
context &ctx,
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<cruft::tokeniser<const char*>::iterator> lines) const
|
2018-04-01 14:49:10 +10:00
|
|
|
{
|
|
|
|
for (auto l = lines.begin (), last = lines.end (); l != last; ++l) {
|
|
|
|
if (l->empty () || (*l)[0] != '#') {
|
|
|
|
os << *l << '\n';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto tokens = cruft::tokeniser (*l, ' ');
|
2018-04-01 14:49:10 +10:00
|
|
|
auto head = tokens.begin ();
|
|
|
|
auto head_string = std::string { head->begin () + 1, head->size () - 1 };
|
|
|
|
|
2020-02-17 14:29:09 +11:00
|
|
|
if (equal (*head, "#endif"))
|
2018-04-01 14:49:10 +10:00
|
|
|
return l;
|
|
|
|
|
|
|
|
|
2020-02-17 14:29:09 +11:00
|
|
|
if (equal (*head, "#define")) {
|
2018-04-01 14:49:10 +10:00
|
|
|
auto key = head;
|
|
|
|
++key;
|
|
|
|
|
|
|
|
if (key == tokens.end ())
|
|
|
|
throw std::runtime_error ("expected token for define");
|
|
|
|
|
|
|
|
std::string key_string { key->begin (), key->size () };
|
|
|
|
|
|
|
|
auto val = key;
|
|
|
|
++val;
|
|
|
|
ctx.defines[key_string] = val == tokens.end () ? "" : std::string (val->begin (), val->size ());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:29:09 +11:00
|
|
|
if (equal (*head, "#ifndef")) {
|
2018-04-01 14:49:10 +10:00
|
|
|
auto tail = head + 1;
|
|
|
|
if (tail == tokens.end ())
|
|
|
|
throw std::runtime_error ("expected token for define");
|
|
|
|
|
|
|
|
// recurse and parse the block...
|
|
|
|
std::string name { tail->begin (), tail->end () };
|
|
|
|
if (ctx.defines.find (name) == ctx.defines.cend ()) {
|
|
|
|
l = process (os, ctx, {++l, lines.end ()});
|
|
|
|
// ... or skip until and endif
|
|
|
|
} else {
|
2020-02-17 14:29:09 +11:00
|
|
|
for (++l; l != lines.end () && !equal (*l, "#endif"); ++l)
|
2018-04-01 14:49:10 +10:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we've got the expected endif
|
2020-02-17 14:29:09 +11:00
|
|
|
if (l == lines.cend () || !equal (*l, "#endif"))
|
2018-04-01 14:49:10 +10:00
|
|
|
throw std::runtime_error ("expected #endif");
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto handler = m_directives.find (head_string);
|
|
|
|
if (handler == m_directives.end ())
|
|
|
|
throw unknown_directive (head_string);
|
|
|
|
|
2018-07-13 14:05:54 +10:00
|
|
|
lines = lines.consume (handler->second->process (os, ctx, {l, lines.end()}));
|
2018-04-01 14:49:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return lines.end ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
passthrough::passthrough (const std::string &name):
|
|
|
|
m_name (name)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::tokeniser<const char*>::iterator
|
2018-04-01 14:49:10 +10:00
|
|
|
passthrough::process (std::ostream &os,
|
|
|
|
context&,
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<cruft::tokeniser<const char*>::iterator> lines) const
|
2018-04-01 14:49:10 +10:00
|
|
|
{
|
|
|
|
os << *lines.begin () << '\n';
|
|
|
|
return lines.begin ()++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
include::include (processor &_parent):
|
|
|
|
m_parent (_parent)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::tokeniser<const char*>::iterator
|
2018-04-01 14:49:10 +10:00
|
|
|
include::process (std::ostream &os,
|
|
|
|
context &ctx,
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<cruft::tokeniser<const char*>::iterator> lines) const
|
2018-04-01 14:49:10 +10:00
|
|
|
{
|
|
|
|
const auto name = lines.begin ()->slice (strlen("#include '"), -2);
|
2018-12-05 19:12:03 +11:00
|
|
|
std::filesystem::path fragment { name.begin (), name.end () };
|
2018-04-01 14:49:10 +10:00
|
|
|
|
|
|
|
const auto target = ctx.source.top ().parent_path () / fragment;
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto data = cruft::slurp<char> (target);
|
2018-04-01 14:49:10 +10:00
|
|
|
|
|
|
|
ctx.source.push (target);
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::tokeniser<const char*> tok (data, '\n');
|
|
|
|
m_parent.process (os, ctx, cruft::view (tok.begin (), tok.end ()));
|
2018-04-01 14:49:10 +10:00
|
|
|
|
|
|
|
ctx.source.pop ();
|
|
|
|
|
|
|
|
return lines.begin ()++;
|
|
|
|
}
|