summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-json-rpc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/compiler-core/slang-json-rpc.cpp')
-rw-r--r--source/compiler-core/slang-json-rpc.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/source/compiler-core/slang-json-rpc.cpp b/source/compiler-core/slang-json-rpc.cpp
new file mode 100644
index 000000000..15da92ff0
--- /dev/null
+++ b/source/compiler-core/slang-json-rpc.cpp
@@ -0,0 +1,48 @@
+#include "slang-json-rpc.h"
+
+namespace Slang {
+
+// https://www.jsonrpc.org/specification
+
+// m_sourceManager.initialize(nullptr, nullptr);
+// m_diagnosticSink.init(&m_sourceManager, &JSONLexer::calcLexemeLocation);
+
+/* static */SlangResult JSONRPCUtil::parseJSON(const UnownedStringSlice& slice, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
+{
+ SourceManager* sourceManager = sink->getSourceManager();
+
+ // Now need to parse as JSON
+ String contents(slice);
+ SourceFile* sourceFile = sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents);
+ SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());
+
+ JSONLexer lexer;
+ lexer.init(sourceView, sink);
+
+ JSONBuilder builder(container);
+
+ JSONParser parser;
+ SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, sink));
+
+ outValue = builder.getRootValue();
+ return SLANG_OK;
+}
+
+SlangResult JSONRPCUtil::parseJSONAndConsume(HTTPPacketConnection* connection, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
+{
+ if (!connection->hasContent())
+ {
+ return SLANG_FAIL;
+ }
+
+ auto content = connection->getContent();
+
+ UnownedStringSlice text((const char*)content.begin(), content.getCount());
+ SlangResult res = parseJSON(text, container, sink, outValue);
+
+ // Consume the content
+ connection->consumeContent();
+ return res;
+}
+
+} // namespace Slang