You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
10 KiB

7 months ago
  1. <!doctype html>
  2. <title>CodeMirror: C-like mode</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../../doc/docs.css">
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="../../addon/edit/matchbrackets.js"></script>
  8. <link rel="stylesheet" href="../../addon/hint/show-hint.css">
  9. <script src="../../addon/hint/show-hint.js"></script>
  10. <script src="clike.js"></script>
  11. <style>.CodeMirror {border: 2px inset #dee;}</style>
  12. <div id=nav>
  13. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
  14. <ul>
  15. <li><a href="../../index.html">Home</a>
  16. <li><a href="../../doc/manual.html">Manual</a>
  17. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  18. </ul>
  19. <ul>
  20. <li><a href="../index.html">Language modes</a>
  21. <li><a class=active href="#">C-like</a>
  22. </ul>
  23. </div>
  24. <article>
  25. <h2>C-like mode</h2>
  26. <div><textarea id="c-code">
  27. /* C demo code */
  28. #include <zmq.h>
  29. #include <pthread.h>
  30. #include <semaphore.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <malloc.h>
  35. typedef struct {
  36. void* arg_socket;
  37. zmq_msg_t* arg_msg;
  38. char* arg_string;
  39. unsigned long arg_len;
  40. int arg_int, arg_command;
  41. int signal_fd;
  42. int pad;
  43. void* context;
  44. sem_t sem;
  45. } acl_zmq_context;
  46. #define p(X) (context->arg_##X)
  47. void* zmq_thread(void* context_pointer) {
  48. acl_zmq_context* context = (acl_zmq_context*)context_pointer;
  49. char ok = 'K', err = 'X';
  50. int res;
  51. while (1) {
  52. while ((res = sem_wait(&amp;context->sem)) == EINTR);
  53. if (res) {write(context->signal_fd, &amp;err, 1); goto cleanup;}
  54. switch(p(command)) {
  55. case 0: goto cleanup;
  56. case 1: p(socket) = zmq_socket(context->context, p(int)); break;
  57. case 2: p(int) = zmq_close(p(socket)); break;
  58. case 3: p(int) = zmq_bind(p(socket), p(string)); break;
  59. case 4: p(int) = zmq_connect(p(socket), p(string)); break;
  60. case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &amp;p(len)); break;
  61. case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
  62. case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
  63. case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
  64. case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
  65. }
  66. p(command) = errno;
  67. write(context->signal_fd, &amp;ok, 1);
  68. }
  69. cleanup:
  70. close(context->signal_fd);
  71. free(context_pointer);
  72. return 0;
  73. }
  74. void* zmq_thread_init(void* zmq_context, int signal_fd) {
  75. acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
  76. pthread_t thread;
  77. context->context = zmq_context;
  78. context->signal_fd = signal_fd;
  79. sem_init(&amp;context->sem, 1, 0);
  80. pthread_create(&amp;thread, 0, &amp;zmq_thread, context);
  81. pthread_detach(thread);
  82. return context;
  83. }
  84. </textarea></div>
  85. <h2>C++ example</h2>
  86. <div><textarea id="cpp-code">
  87. #include <iostream>
  88. #include "mystuff/util.h"
  89. namespace {
  90. enum Enum {
  91. VAL1, VAL2, VAL3
  92. };
  93. char32_t unicode_string = U"\U0010FFFF";
  94. string raw_string = R"delim(anything
  95. you
  96. want)delim";
  97. int Helper(const MyType& param) {
  98. return 0;
  99. }
  100. } // namespace
  101. class ForwardDec;
  102. template <class T, class V>
  103. class Class : public BaseClass {
  104. const MyType<T, V> member_;
  105. public:
  106. const MyType<T, V>& Method() const {
  107. return member_;
  108. }
  109. void Method2(MyType<T, V>* value);
  110. }
  111. template <class T, class V>
  112. void Class::Method2(MyType<T, V>* value) {
  113. std::out << 1 >> method();
  114. value->Method3(member_);
  115. member_ = value;
  116. }
  117. </textarea></div>
  118. <h2>Objective-C example</h2>
  119. <div><textarea id="objectivec-code">
  120. /*
  121. This is a longer comment
  122. That spans two lines
  123. */
  124. #import "MyClass.h"
  125. #import <AFramework/AFramework.h>
  126. @import BFrameworkModule;
  127. NS_ENUM(SomeValues) {
  128. aValue = 1;
  129. };
  130. // A Class Extension with some properties
  131. @interface MyClass ()<AProtocol>
  132. @property(atomic, readwrite, assign) NSInteger anInt;
  133. @property(nonatomic, strong, nullable) NSString *aString;
  134. @end
  135. @implementation YourAppDelegate
  136. - (instancetype)initWithString:(NSString *)aStringVar {
  137. if ((self = [super init])) {
  138. aString = aStringVar;
  139. }
  140. return self;
  141. }
  142. - (BOOL)doSomething:(float)progress {
  143. NSString *myString = @"This is a ObjC string %f ";
  144. myString = [[NSString stringWithFormat:myString, progress] stringByAppendingString:self.aString];
  145. return myString.length > 100 ? NO : YES;
  146. }
  147. @end
  148. </textarea></div>
  149. <h2>Java example</h2>
  150. <div><textarea id="java-code">
  151. import com.demo.util.MyType;
  152. import com.demo.util.MyInterface;
  153. public enum Enum {
  154. VAL1, VAL2, VAL3
  155. }
  156. public class Class<T, V> implements MyInterface {
  157. public static final MyType<T, V> member;
  158. private class InnerClass {
  159. public int zero() {
  160. return 0;
  161. }
  162. }
  163. @Override
  164. public MyType method() {
  165. return member;
  166. }
  167. public void method2(MyType<T, V> value) {
  168. method();
  169. value.method3();
  170. member = value;
  171. }
  172. }
  173. </textarea></div>
  174. <h2>Scala example</h2>
  175. <div><textarea id="scala-code">
  176. object FilterTest extends App {
  177. def filter(xs: List[Int], threshold: Int) = {
  178. def process(ys: List[Int]): List[Int] =
  179. if (ys.isEmpty) ys
  180. else if (ys.head < threshold) ys.head :: process(ys.tail)
  181. else process(ys.tail)
  182. process(xs)
  183. }
  184. println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
  185. }
  186. </textarea></div>
  187. <h2>Kotlin mode</h2>
  188. <div><textarea id="kotlin-code">
  189. package org.wasabi.http
  190. import java.util.concurrent.Executors
  191. import java.net.InetSocketAddress
  192. import org.wasabi.app.AppConfiguration
  193. import io.netty.bootstrap.ServerBootstrap
  194. import io.netty.channel.nio.NioEventLoopGroup
  195. import io.netty.channel.socket.nio.NioServerSocketChannel
  196. import org.wasabi.app.AppServer
  197. public class HttpServer(private val appServer: AppServer) {
  198. val bootstrap: ServerBootstrap
  199. val primaryGroup: NioEventLoopGroup
  200. val workerGroup: NioEventLoopGroup
  201. init {
  202. // Define worker groups
  203. primaryGroup = NioEventLoopGroup()
  204. workerGroup = NioEventLoopGroup()
  205. // Initialize bootstrap of server
  206. bootstrap = ServerBootstrap()
  207. bootstrap.group(primaryGroup, workerGroup)
  208. bootstrap.channel(javaClass<NioServerSocketChannel>())
  209. bootstrap.childHandler(NettyPipelineInitializer(appServer))
  210. }
  211. public fun start(wait: Boolean = true) {
  212. val channel = bootstrap.bind(appServer.configuration.port)?.sync()?.channel()
  213. if (wait) {
  214. channel?.closeFuture()?.sync()
  215. }
  216. }
  217. public fun stop() {
  218. // Shutdown all event loops
  219. primaryGroup.shutdownGracefully()
  220. workerGroup.shutdownGracefully()
  221. // Wait till all threads are terminated
  222. primaryGroup.terminationFuture().sync()
  223. workerGroup.terminationFuture().sync()
  224. }
  225. }
  226. </textarea></div>
  227. <h2>Ceylon mode</h2>
  228. <div><textarea id="ceylon-code">
  229. "Produces the [[stream|Iterable]] that results from repeated
  230. application of the given [[function|next]] to the given
  231. [[first]] element of the stream, until the function first
  232. returns [[finished]]. If the given function never returns
  233. `finished`, the resulting stream is infinite.
  234. For example:
  235. loop(0)(2.plus).takeWhile(10.largerThan)
  236. produces the stream `{ 0, 2, 4, 6, 8 }`."
  237. tagged("Streams")
  238. shared {Element+} loop&lt;Element&gt;(
  239. "The first element of the resulting stream."
  240. Element first)(
  241. "The function that produces the next element of the
  242. stream, given the current element. The function may
  243. return [[finished]] to indicate the end of the
  244. stream."
  245. Element|Finished next(Element element))
  246. =&gt; let (start = first)
  247. object satisfies {Element+} {
  248. first =&gt; start;
  249. empty =&gt; false;
  250. function nextElement(Element element)
  251. =&gt; next(element);
  252. iterator()
  253. =&gt; object satisfies Iterator&lt;Element&gt; {
  254. variable Element|Finished current = start;
  255. shared actual Element|Finished next() {
  256. if (!is Finished result = current) {
  257. current = nextElement(result);
  258. return result;
  259. }
  260. else {
  261. return finished;
  262. }
  263. }
  264. };
  265. };
  266. </textarea></div>
  267. <script>
  268. var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), {
  269. lineNumbers: true,
  270. matchBrackets: true,
  271. mode: "text/x-csrc"
  272. });
  273. var cppEditor = CodeMirror.fromTextArea(document.getElementById("cpp-code"), {
  274. lineNumbers: true,
  275. matchBrackets: true,
  276. mode: "text/x-c++src"
  277. });
  278. var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
  279. lineNumbers: true,
  280. matchBrackets: true,
  281. mode: "text/x-java"
  282. });
  283. var objectivecEditor = CodeMirror.fromTextArea(document.getElementById("objectivec-code"), {
  284. lineNumbers: true,
  285. matchBrackets: true,
  286. mode: "text/x-objectivec"
  287. });
  288. var scalaEditor = CodeMirror.fromTextArea(document.getElementById("scala-code"), {
  289. lineNumbers: true,
  290. matchBrackets: true,
  291. mode: "text/x-scala"
  292. });
  293. var kotlinEditor = CodeMirror.fromTextArea(document.getElementById("kotlin-code"), {
  294. lineNumbers: true,
  295. matchBrackets: true,
  296. mode: "text/x-kotlin"
  297. });
  298. var ceylonEditor = CodeMirror.fromTextArea(document.getElementById("ceylon-code"), {
  299. lineNumbers: true,
  300. matchBrackets: true,
  301. mode: "text/x-ceylon"
  302. });
  303. var mac = CodeMirror.keyMap.default == CodeMirror.keyMap.macDefault;
  304. CodeMirror.keyMap.default[(mac ? "Cmd" : "Ctrl") + "-Space"] = "autocomplete";
  305. </script>
  306. <p>Simple mode that tries to handle C-like languages as well as it
  307. can. Takes two configuration parameters: <code>keywords</code>, an
  308. object whose property names are the keywords in the language,
  309. and <code>useCPP</code>, which determines whether C preprocessor
  310. directives are recognized.</p>
  311. <p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
  312. (C), <code>text/x-c++src</code> (C++), <code>text/x-java</code>
  313. (Java), <code>text/x-csharp</code> (C#),
  314. <code>text/x-objectivec</code> (Objective-C),
  315. <code>text/x-scala</code> (Scala), <code>text/x-vertex</code>
  316. <code>x-shader/x-fragment</code> (shader programs),
  317. <code>text/x-squirrel</code> (Squirrel) and
  318. <code>text/x-ceylon</code> (Ceylon)</p>
  319. </article>