Add action file loops and display the GPLv3 notice at console startup
This commit is contained in:
1 parent
12c7332337
commit
b8799a62e7
9 files changed
+305
-47
No files matched your search
+107
-32
@@ -284,6 +284,41 @@ struct Loader
|
||||
std::size_t loads = 0;
|
||||
std::size_t total_bytes = 0;
|
||||
|
||||
void Append(ActionCommand command)
|
||||
{
|
||||
if (program.commands.size() >= kMaxCommands)
|
||||
Fail(command.source, command.line, "expanded command limit exceeded (100000)");
|
||||
program.commands.push_back(std::move(command));
|
||||
}
|
||||
|
||||
void LoadInstruction(std::string_view verb, std::string_view line, const std::filesystem::path &source,
|
||||
std::size_t line_number)
|
||||
{
|
||||
if (verb == "import")
|
||||
{
|
||||
const auto name = TakeToken(line);
|
||||
if (!IsSimpleName(name) || !Trim(line).empty())
|
||||
Fail(source, line_number, "import requires exactly one simple action name");
|
||||
const auto imported = source.parent_path() / (std::string(name) + ".act");
|
||||
std::error_code ec;
|
||||
const auto resolved = std::filesystem::canonical(imported, ec);
|
||||
if (ec)
|
||||
Fail(source, line_number, "cannot import '" + std::string(name) + "': " + ec.message());
|
||||
if (resolved.parent_path() != source.parent_path())
|
||||
Fail(source, line_number, "import escapes its action directory");
|
||||
try
|
||||
{
|
||||
Load(resolved, name);
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
Fail(source, line_number, "import '" + std::string(name) + "': " + error.what());
|
||||
}
|
||||
return;
|
||||
}
|
||||
Append(ParseCommand(verb, line, source, line_number));
|
||||
}
|
||||
|
||||
void Load(const std::filesystem::path &path, std::string_view expected_name = {})
|
||||
{
|
||||
std::error_code ec;
|
||||
@@ -329,6 +364,23 @@ struct Loader
|
||||
remaining.remove_prefix(3);
|
||||
bool header_seen = false;
|
||||
std::size_t line_number = 0;
|
||||
struct OpenLoop
|
||||
{
|
||||
std::size_t start;
|
||||
bool block;
|
||||
};
|
||||
// Each file must balance its own blocks, including when imported as a single statement.
|
||||
std::vector<OpenLoop> loops;
|
||||
const auto closeLoop = [&] {
|
||||
auto &start = program.commands[loops.back().start];
|
||||
start.loopEnd = program.commands.size();
|
||||
ActionCommand end{};
|
||||
end.kind = ActionKind::LoopEnd;
|
||||
end.source = source;
|
||||
end.line = line_number;
|
||||
Append(std::move(end));
|
||||
loops.pop_back();
|
||||
};
|
||||
while (!remaining.empty())
|
||||
{
|
||||
++line_number;
|
||||
@@ -365,47 +417,70 @@ struct Loader
|
||||
continue;
|
||||
if (!header_seen)
|
||||
Fail(source, line_number, "missing action header before first instruction");
|
||||
// A single-line body may itself begin with another do. Parse it iteratively.
|
||||
std::string expanded;
|
||||
try
|
||||
for (;;)
|
||||
{
|
||||
expanded = bindings.Expand(line, kMaxFileBytes);
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
Fail(source, line_number, error.what());
|
||||
}
|
||||
if (expanded.empty()) continue;
|
||||
expandedBytes += expanded.size();
|
||||
if (expandedBytes > kMaxTotalBytes)
|
||||
Fail(source, line_number, "total expanded action text exceeds 16 MiB");
|
||||
line = expanded;
|
||||
const auto verb = TakeToken(line);
|
||||
if (verb == "import")
|
||||
{
|
||||
const auto name = TakeToken(line);
|
||||
if (!IsSimpleName(name) || !Trim(line).empty())
|
||||
Fail(source, line_number, "import requires exactly one simple action name");
|
||||
const auto imported = source.parent_path() / (std::string(name) + ".act");
|
||||
const auto resolved = std::filesystem::canonical(imported, ec);
|
||||
if (ec)
|
||||
Fail(source, line_number, "cannot import '" + std::string(name) + "': " + ec.message());
|
||||
if (resolved.parent_path() != source.parent_path())
|
||||
Fail(source, line_number, "import escapes its action directory");
|
||||
try
|
||||
{
|
||||
Load(resolved, name);
|
||||
expanded = bindings.Expand(line, kMaxFileBytes);
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
Fail(source, line_number, "import '" + std::string(name) + "': " + error.what());
|
||||
Fail(source, line_number, error.what());
|
||||
}
|
||||
continue;
|
||||
expandedBytes += expanded.size();
|
||||
if (expandedBytes > kMaxTotalBytes)
|
||||
Fail(source, line_number, "total expanded action text exceeds 16 MiB");
|
||||
line = expanded;
|
||||
const auto verb = TakeToken(line);
|
||||
line = Trim(line);
|
||||
if (verb == "do")
|
||||
{
|
||||
ActionCommand start{};
|
||||
start.kind = ActionKind::LoopBegin;
|
||||
start.source = source;
|
||||
start.line = line_number;
|
||||
const auto count = TakeToken(line);
|
||||
if (count != "always")
|
||||
{
|
||||
std::uint64_t repetitions = 0;
|
||||
const auto parsed = std::from_chars(count.data(), count.data() + count.size(), repetitions);
|
||||
if (count.empty() || parsed.ec != std::errc{} || parsed.ptr != count.data() + count.size())
|
||||
Fail(source, line_number, "do requires a nonnegative integer or always");
|
||||
start.repetitions = repetitions;
|
||||
}
|
||||
loops.push_back({program.commands.size(), false});
|
||||
Append(std::move(start));
|
||||
line = Trim(line);
|
||||
if (line.empty()) break;
|
||||
continue;
|
||||
}
|
||||
if (verb == "begin")
|
||||
{
|
||||
if (loops.empty() || loops.back().block || !line.empty())
|
||||
Fail(source, line_number, "expected begin only after do <count>");
|
||||
loops.back().block = true;
|
||||
break;
|
||||
}
|
||||
if (verb == "end")
|
||||
{
|
||||
if (loops.empty() || !loops.back().block || !line.empty())
|
||||
Fail(source, line_number, "end requires a matching do block with a complete body");
|
||||
closeLoop();
|
||||
}
|
||||
else if (!verb.empty()) // alias and define were processed while expanding.
|
||||
LoadInstruction(verb, line, source, line_number);
|
||||
|
||||
// A complete statement also completes enclosing unbracketed loops.
|
||||
while (!loops.empty() && !loops.back().block)
|
||||
closeLoop();
|
||||
break;
|
||||
}
|
||||
auto command = ParseCommand(verb, line, source, line_number);
|
||||
if (program.commands.size() >= kMaxCommands)
|
||||
Fail(source, line_number, "expanded command limit exceeded (100000)");
|
||||
program.commands.push_back(std::move(command));
|
||||
}
|
||||
if (!loops.empty())
|
||||
Fail(source, program.commands[loops.back().start].line,
|
||||
loops.back().block ? "missing end for do block" : "missing statement after do");
|
||||
if (!header_seen)
|
||||
Fail(source, line_number == 0 ? 1 : line_number, "missing action header");
|
||||
}
|
||||
@@ -434,7 +509,7 @@ std::optional<ActionCommand> ParseInteractiveCommand(std::string_view text)
|
||||
if (text.empty())
|
||||
return std::nullopt;
|
||||
const auto verb = TakeToken(text);
|
||||
if (verb == "wait" || verb == "import")
|
||||
if (verb == "wait" || verb == "import" || verb == "do" || verb == "begin" || verb == "end")
|
||||
Fail(source, 1, std::string(verb) + " is only available in action files; use run file.act");
|
||||
return ParseCommand(verb, text, source, 1);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user