cpp: fix trailing tokens with new tokeniser

This commit is contained in:
Danny Robson 2021-12-17 12:07:08 +10:00
parent 458f109c6f
commit 642d9b95c6

14
cpp.cpp
View File

@ -39,12 +39,16 @@ processor::add (std::string token, std::unique_ptr<directive> handler)
void
processor::process (std::ostream &os, const std::filesystem::path &src) const
{
const auto data = cruft::slurp<char> (src);
auto data = cruft::slurp<char> (src);
context ctx;
ctx.source.push (src);
// Workaround to prevent an empty trailing line after line splitting
if (!data.empty () and data.back () == '\n')
data.pop_back ();
cruft::tokeniser<const char*> tok (data, '\n');
process (os, ctx, cruft::view (tok.begin (), tok.end ()));
process (os, ctx, cruft::view (tok));
}
@ -68,7 +72,6 @@ processor::process (
if (equal (*head, "#endif"))
return l;
if (equal (*head, "#define")) {
auto key = head;
++key;
@ -150,7 +153,10 @@ include::process (std::ostream &os,
std::filesystem::path fragment { name.begin (), name.end () };
const auto target = ctx.source.top ().parent_path () / fragment;
const auto data = cruft::slurp<char> (target);
auto data = cruft::slurp<char> (target);
if (!data.empty () and data.back () == '\n')
data.pop_back ();
ctx.source.push (target);
cruft::tokeniser<const char*> tok (data, '\n');