#include "dispatch.hpp" #include #include #include #include namespace edf { namespace { enum class Operation { Define, Update, Erase, Post, Cancel, Retime, Advance }; struct Instruction { Operation operation; std::array arguments{}; }; class Parser { public: explicit Parser(std::string_view text) : text_(text) {} bool instruction(Instruction& output) { std::size_t begin = cursor_; while (cursor_ < text_.size() && text_[cursor_] >= 'a' && text_[cursor_] <= 'z') { ++cursor_; } std::string_view name = text_.substr(begin, cursor_ - begin); std::size_t count = 0; if (name == "define") { output.operation = Operation::Define; count = 3; } else if (name == "update") { output.operation = Operation::Update; count = 3; } else if (name == "erase") { output.operation = Operation::Erase; count = 1; } else if (name == "post") { output.operation = Operation::Post; count = 3; } else if (name == "cancel") { output.operation = Operation::Cancel; count = 1; } else if (name == "retime") { output.operation = Operation::Retime; count = 2; } else if (name == "advance") { output.operation = Operation::Advance; count = 1; } else { return false; } if (!take('(')) { return false; } for (std::size_t index = 0; index < count; ++index) { if (index != 0 && !take(',')) { return false; } if (!number(output.arguments[index])) { return false; } } return take(')'); } bool done() const { return cursor_ == text_.size(); } bool separator() { return take(';'); } private: bool take(char token) { if (cursor_ == text_.size() || text_[cursor_] != token) { return false; } ++cursor_; return true; } bool number(std::int64_t& output) { if (cursor_ == text_.size() || text_[cursor_] < '0' || text_[cursor_] > '9') { return false; } std::int64_t result = 0; while (cursor_ < text_.size() && text_[cursor_] >= '0' && text_[cursor_] <= '9') { int digit = text_[cursor_] - '0'; if (result > (std::numeric_limits::max() - digit) / 10) { return false; } result = result * 10 + digit; ++cursor_; } output = result; return true; } std::string_view text_; std::size_t cursor_ = 0; }; int execute(Dispatcher& dispatcher, const Instruction& instruction) { const auto& a = instruction.arguments; switch (instruction.operation) { case Operation::Define: return dispatcher.define(a[0], a[1], a[2]); case Operation::Update: return dispatcher.update(a[0], a[1], a[2]); case Operation::Erase: return dispatcher.erase(a[0]); case Operation::Post: return dispatcher.post(a[0], a[1], a[2]); case Operation::Cancel: return dispatcher.cancel(a[0]); case Operation::Retime: return dispatcher.retime(a[0], a[1]); case Operation::Advance: return dispatcher.advance(a[0]); } return Invalid; } } PlanResult apply_plan(Dispatcher& dispatcher, std::string_view text) { if (text.size() > 4096) { return {Limit, 0}; } if (text.empty()) { return {Ok, 0}; } Parser parser(text); std::vector instructions; while (true) { if (instructions.size() == 32) { return {Limit, 32}; } Instruction instruction{}; if (!parser.instruction(instruction)) { return {Syntax, instructions.size()}; } instructions.push_back(instruction); if (parser.done()) { break; } if (!parser.separator()) { return {Syntax, instructions.size() - 1}; } } Dispatcher trial(dispatcher); for (std::size_t index = 0; index < instructions.size(); ++index) { int code = execute(trial, instructions[index]); if (code != Ok) { return {code, index}; } } dispatcher = trial; return {Ok, instructions.size()}; } }