001package org.clafer.lsp; 002 003import java.util.concurrent.ExecutionException; 004import org.eclipse.lsp4j.jsonrpc.Launcher; 005import org.eclipse.lsp4j.launch.LSPLauncher; 006import org.eclipse.lsp4j.services.LanguageClient; 007 008/** 009 * Runs the Clafer language server on stdin and stdout: {@code clafer-ls}, 010 * or {@code java -jar clafer-ls.jar}. Nothing else may write to stdout, so 011 * messages go to stderr. 012 */ 013public final class Main { 014 015 private Main() { 016 } 017 018 /** 019 * @return the name, the version and the build date, as the other Clafer 020 * tools print them: {@code clafer-ls v0.6.0.2026-11-26} 021 */ 022 static String version() { 023 return "clafer-ls v" + number(); 024 } 025 026 /** 027 * @return the version and the build date, {@code 0.6.0.2026-11-26}, from 028 * the version.properties that Maven fills in 029 */ 030 static String number() { 031 java.util.Properties p = new java.util.Properties(); 032 try (java.io.InputStream in = Main.class.getResourceAsStream("version.properties")) { 033 p.load(in); 034 } catch (java.io.IOException | NullPointerException e) { 035 return "unknown"; 036 } 037 return p.getProperty("version") + "." + p.getProperty("releasedate"); 038 } 039 040 public static void main(String[] args) throws InterruptedException { 041 for (String a : args) { 042 if (a.equals("--version")) { 043 System.out.println(version()); 044 return; 045 } 046 if (a.equals("--help") || a.equals("-h")) { 047 System.out.println("clafer-ls: the Clafer language server, over stdin and stdout (LSP)."); 048 System.out.println("Start it from an editor's LSP client; it takes no options but --version."); 049 return; 050 } 051 // --stdio, which some clients pass, is what the server does anyway 052 } 053 // the JSON-RPC messages are the only output on stdout 054 java.io.PrintStream out = System.out; 055 System.setOut(System.err); 056 ClaferLanguageServer server = new ClaferLanguageServer(System::exit); 057 Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(server, System.in, out); 058 server.connect(launcher.getRemoteProxy()); 059 try { 060 launcher.startListening().get(); 061 } catch (ExecutionException e) { 062 System.err.println("clafer-ls: " + e.getCause()); 063 System.exit(1); 064 } 065 } 066}