bootstrap

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-11-25 02:11:19 +03:00
commit ca04975d54
62 files changed
+3381

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
#include <vls/Server.hpp>
#include <vls/Json.hpp>
#include <exception>
#include <ostream>
namespace vls {
namespace {
const Json *member(const Json::Object &object, std::string_view key) {
const auto found = object.find(key);
return found == object.end() ? nullptr : &found->second;
}
const std::string *string(const Json *value) {
return value ? std::get_if<std::string>(&value->value) : nullptr;
}
DispatchResult error(std::string_view id, int code, std::string_view message) {
return {.response = "{\"jsonrpc\":\"2.0\",\"id\":" + std::string{id} + ",\"error\":{\"code\":" +
std::to_string(code) + ",\"message\":" + quote_json(message) + "}}",
.exit_code = std::nullopt};
}
DispatchResult result(std::string_view id, std::string_view value) {
return {.response = "{\"jsonrpc\":\"2.0\",\"id\":" + std::string{id} +
",\"result\":" + std::string{value} + "}",
.exit_code = std::nullopt};
}
} // namespace
DispatchResult Server::handle(std::string_view body) {
auto parsed = parse_json(body);
if (!parsed)
return error("null", -32700, "Parse error");
const auto *object = std::get_if<Json::Object>(&parsed->value);
if (!object)
return error("null", -32600, "Invalid Request");
const auto *id_value = member(*object, "id");
const auto *method_value = member(*object, "method");
const auto *method = string(method_value);
const auto *version = string(member(*object, "jsonrpc"));
const auto *params = member(*object, "params");
const auto *result_value = member(*object, "result");
const auto *error_value = member(*object, "error");
// This shell issues no requests. Valid peer responses are unmatched and
// must be ignored, including errors whose correlation ID is null.
if (version && *version == "2.0" && !method_value && id_value &&
((result_value != nullptr) != (error_value != nullptr))) {
const bool valid_id = std::holds_alternative<std::nullptr_t>(id_value->value) ||
request_id(*id_value).has_value();
bool valid_payload = result_value != nullptr;
if (error_value) {
if (const auto *details = std::get_if<Json::Object>(&error_value->value)) {
if (const auto *code = member(*details, "code");
code && string(member(*details, "message"))) {
const auto checked = request_id(*code);
valid_payload = checked && std::holds_alternative<std::int32_t>(*checked);
}
}
}
if (valid_id && valid_payload)
return {};
}
const bool notification = !id_value;
std::string id{"null"};
if (id_value) {
auto checked = request_id(*id_value);
if (!checked)
return error("null", -32600, "Invalid Request");
id = encode_id(*checked);
}
if (!version || *version != "2.0" || !method || result_value || error_value) {
// Method-bearing objects without IDs are notifications, even when
// malformed. They must never elicit a response.
if (notification && method_value)
return {};
return error(id, -32600, "Invalid Request");
}
if (notification) {
if (*method == "exit")
return {.response = std::nullopt, .exit_code = state_ == State::shutdown ? 0 : 1};
return {};
}
if (state_ == State::shutdown)
return error(id, -32600, "Invalid Request");
if (state_ == State::awaiting_initialize && *method != "initialize")
return error(id, -32002, "Server not initialized");
if (*method == "initialize") {
if (state_ != State::awaiting_initialize)
return error(id, -32600, "Invalid Request");
if (!params || !std::holds_alternative<Json::Object>(params->value))
return error(id, -32602, "initialize params must be an object");
state_ = State::running;
return result(id, "{\"capabilities\":{},\"serverInfo\":{\"name\":\"VoidLanguageServer\","
"\"version\":\"0.1.0\"}}");
}
if (params && !std::holds_alternative<Json::Object>(params->value) &&
!std::holds_alternative<Json::Array>(params->value) &&
!std::holds_alternative<std::nullptr_t>(params->value))
return error(id, -32600, "Invalid Request");
if (*method == "shutdown") {
if (params && !std::holds_alternative<std::nullptr_t>(params->value))
return error(id, -32602, "shutdown takes no params");
state_ = State::shutdown;
return result(id, "null");
}
return error(id, -32601, "Method not found");
}
int run_stdio(std::istream &input, std::ostream &output, std::ostream &diagnostics,
FrameLimits limits) {
try {
Server server;
for (;;) {
auto frame = read_frame(input, limits);
if (!frame) {
diagnostics << "vls: " << frame.error() << '\n';
return 1;
}
if (!*frame) {
diagnostics << "vls: unexpected EOF before exit notification\n";
return 1;
}
auto dispatched = server.handle(**frame);
if (dispatched.response) {
const auto written = write_frame(output, *dispatched.response);
if (!written) {
diagnostics << "vls: " << written.error() << '\n';
return 1;
}
}
if (dispatched.exit_code)
return *dispatched.exit_code;
}
} catch (const std::exception &) {
diagnostics << "vls: failed to process protocol stream\n";
return 1;
}
}
} // namespace vls