Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 1 | //===--- JSONRPCDispatcher.h - Main JSON parser entry point -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H |
| 12 | |
| 13 | #include "clang/Basic/LLVM.h" |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/Support/YAMLParser.h" |
Ilya Biryukov | 687b92a | 2017-05-16 15:23:55 +0000 | [diff] [blame] | 16 | #include <iosfwd> |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 17 | #include <mutex> |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 18 | |
| 19 | namespace clang { |
| 20 | namespace clangd { |
| 21 | |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 22 | /// Encapsulates output and logs streams and provides thread-safe access to |
| 23 | /// them. |
| 24 | class JSONOutput { |
| 25 | public: |
| 26 | JSONOutput(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs) |
| 27 | : Outs(Outs), Logs(Logs) {} |
| 28 | |
| 29 | /// Emit a JSONRPC message. |
| 30 | void writeMessage(const Twine &Message); |
| 31 | |
Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 32 | /// Write to the logging stream. |
| 33 | void log(const Twine &Message); |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 34 | |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 35 | private: |
| 36 | llvm::raw_ostream &Outs; |
| 37 | llvm::raw_ostream &Logs; |
| 38 | |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 39 | std::mutex StreamMutex; |
| 40 | }; |
| 41 | |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 42 | /// Callback for messages sent to the server, called by the JSONRPCDispatcher. |
| 43 | class Handler { |
| 44 | public: |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 45 | Handler(JSONOutput &Output) : Output(Output) {} |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 46 | virtual ~Handler() = default; |
| 47 | |
| 48 | /// Called when the server receives a method call. This is supposed to return |
| 49 | /// a result on Outs. The default implementation returns an "unknown method" |
| 50 | /// error to the client and logs a warning. |
| 51 | virtual void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID); |
| 52 | /// Called when the server receives a notification. No result should be |
| 53 | /// written to Outs. The default implemetation logs a warning. |
| 54 | virtual void handleNotification(llvm::yaml::MappingNode *Params); |
| 55 | |
| 56 | protected: |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 57 | JSONOutput &Output; |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 58 | |
Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 59 | /// Helper to write a JSONRPC result to Output. |
| 60 | void writeMessage(const Twine &Message) { Output.writeMessage(Message); } |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /// Main JSONRPC entry point. This parses the JSONRPC "header" and calls the |
| 64 | /// registered Handler for the method received. |
| 65 | class JSONRPCDispatcher { |
| 66 | public: |
| 67 | /// Create a new JSONRPCDispatcher. UnknownHandler is called when an unknown |
| 68 | /// method is received. |
| 69 | JSONRPCDispatcher(std::unique_ptr<Handler> UnknownHandler) |
| 70 | : UnknownHandler(std::move(UnknownHandler)) {} |
| 71 | |
| 72 | /// Registers a Handler for the specified Method. |
| 73 | void registerHandler(StringRef Method, std::unique_ptr<Handler> H); |
| 74 | |
| 75 | /// Parses a JSONRPC message and calls the Handler for it. |
| 76 | bool call(StringRef Content) const; |
| 77 | |
| 78 | private: |
| 79 | llvm::StringMap<std::unique_ptr<Handler>> Handlers; |
| 80 | std::unique_ptr<Handler> UnknownHandler; |
| 81 | }; |
| 82 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 83 | /// Parses input queries from LSP client (coming from \p In) and runs call |
| 84 | /// method of \p Dispatcher for each query. |
| 85 | /// After handling each query checks if \p IsDone is set true and exits the loop |
| 86 | /// if it is. |
| 87 | /// Input stream(\p In) must be opened in binary mode to avoid preliminary |
| 88 | /// replacements of \r\n with \n. |
| 89 | void runLanguageServerLoop(std::istream &In, JSONOutput &Out, |
| 90 | JSONRPCDispatcher &Dispatcher, bool &IsDone); |
| 91 | |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 92 | } // namespace clangd |
| 93 | } // namespace clang |
| 94 | |
| 95 | #endif |