国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

361 lines
9.9KB

  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="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></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/codemirror">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 <Test/Test.h>
  125. @implementation YourAppDelegate
  126. // This is a one-line comment
  127. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  128. char myString[] = "This is a C character array";
  129. int test = 5;
  130. return YES;
  131. }
  132. </textarea></div>
  133. <h2>Java example</h2>
  134. <div><textarea id="java-code">
  135. import com.demo.util.MyType;
  136. import com.demo.util.MyInterface;
  137. public enum Enum {
  138. VAL1, VAL2, VAL3
  139. }
  140. public class Class<T, V> implements MyInterface {
  141. public static final MyType<T, V> member;
  142. private class InnerClass {
  143. public int zero() {
  144. return 0;
  145. }
  146. }
  147. @Override
  148. public MyType method() {
  149. return member;
  150. }
  151. public void method2(MyType<T, V> value) {
  152. method();
  153. value.method3();
  154. member = value;
  155. }
  156. }
  157. </textarea></div>
  158. <h2>Scala example</h2>
  159. <div><textarea id="scala-code">
  160. object FilterTest extends App {
  161. def filter(xs: List[Int], threshold: Int) = {
  162. def process(ys: List[Int]): List[Int] =
  163. if (ys.isEmpty) ys
  164. else if (ys.head < threshold) ys.head :: process(ys.tail)
  165. else process(ys.tail)
  166. process(xs)
  167. }
  168. println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
  169. }
  170. </textarea></div>
  171. <h2>Kotlin mode</h2>
  172. <div><textarea id="kotlin-code">
  173. package org.wasabi.http
  174. import java.util.concurrent.Executors
  175. import java.net.InetSocketAddress
  176. import org.wasabi.app.AppConfiguration
  177. import io.netty.bootstrap.ServerBootstrap
  178. import io.netty.channel.nio.NioEventLoopGroup
  179. import io.netty.channel.socket.nio.NioServerSocketChannel
  180. import org.wasabi.app.AppServer
  181. public class HttpServer(private val appServer: AppServer) {
  182. val bootstrap: ServerBootstrap
  183. val primaryGroup: NioEventLoopGroup
  184. val workerGroup: NioEventLoopGroup
  185. init {
  186. // Define worker groups
  187. primaryGroup = NioEventLoopGroup()
  188. workerGroup = NioEventLoopGroup()
  189. // Initialize bootstrap of server
  190. bootstrap = ServerBootstrap()
  191. bootstrap.group(primaryGroup, workerGroup)
  192. bootstrap.channel(javaClass<NioServerSocketChannel>())
  193. bootstrap.childHandler(NettyPipelineInitializer(appServer))
  194. }
  195. public fun start(wait: Boolean = true) {
  196. val channel = bootstrap.bind(appServer.configuration.port)?.sync()?.channel()
  197. if (wait) {
  198. channel?.closeFuture()?.sync()
  199. }
  200. }
  201. public fun stop() {
  202. // Shutdown all event loops
  203. primaryGroup.shutdownGracefully()
  204. workerGroup.shutdownGracefully()
  205. // Wait till all threads are terminated
  206. primaryGroup.terminationFuture().sync()
  207. workerGroup.terminationFuture().sync()
  208. }
  209. }
  210. </textarea></div>
  211. <h2>Ceylon mode</h2>
  212. <div><textarea id="ceylon-code">
  213. "Produces the [[stream|Iterable]] that results from repeated
  214. application of the given [[function|next]] to the given
  215. [[first]] element of the stream, until the function first
  216. returns [[finished]]. If the given function never returns
  217. `finished`, the resulting stream is infinite.
  218. For example:
  219. loop(0)(2.plus).takeWhile(10.largerThan)
  220. produces the stream `{ 0, 2, 4, 6, 8 }`."
  221. tagged("Streams")
  222. shared {Element+} loop&lt;Element&gt;(
  223. "The first element of the resulting stream."
  224. Element first)(
  225. "The function that produces the next element of the
  226. stream, given the current element. The function may
  227. return [[finished]] to indicate the end of the
  228. stream."
  229. Element|Finished next(Element element))
  230. =&gt; let (start = first)
  231. object satisfies {Element+} {
  232. first =&gt; start;
  233. empty =&gt; false;
  234. function nextElement(Element element)
  235. =&gt; next(element);
  236. iterator()
  237. =&gt; object satisfies Iterator&lt;Element&gt; {
  238. variable Element|Finished current = start;
  239. shared actual Element|Finished next() {
  240. if (!is Finished result = current) {
  241. current = nextElement(result);
  242. return result;
  243. }
  244. else {
  245. return finished;
  246. }
  247. }
  248. };
  249. };
  250. </textarea></div>
  251. <script>
  252. var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), {
  253. lineNumbers: true,
  254. matchBrackets: true,
  255. mode: "text/x-csrc"
  256. });
  257. var cppEditor = CodeMirror.fromTextArea(document.getElementById("cpp-code"), {
  258. lineNumbers: true,
  259. matchBrackets: true,
  260. mode: "text/x-c++src"
  261. });
  262. var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
  263. lineNumbers: true,
  264. matchBrackets: true,
  265. mode: "text/x-java"
  266. });
  267. var objectivecEditor = CodeMirror.fromTextArea(document.getElementById("objectivec-code"), {
  268. lineNumbers: true,
  269. matchBrackets: true,
  270. mode: "text/x-objectivec"
  271. });
  272. var scalaEditor = CodeMirror.fromTextArea(document.getElementById("scala-code"), {
  273. lineNumbers: true,
  274. matchBrackets: true,
  275. mode: "text/x-scala"
  276. });
  277. var kotlinEditor = CodeMirror.fromTextArea(document.getElementById("kotlin-code"), {
  278. lineNumbers: true,
  279. matchBrackets: true,
  280. mode: "text/x-kotlin"
  281. });
  282. var ceylonEditor = CodeMirror.fromTextArea(document.getElementById("ceylon-code"), {
  283. lineNumbers: true,
  284. matchBrackets: true,
  285. mode: "text/x-ceylon"
  286. });
  287. var mac = CodeMirror.keyMap.default == CodeMirror.keyMap.macDefault;
  288. CodeMirror.keyMap.default[(mac ? "Cmd" : "Ctrl") + "-Space"] = "autocomplete";
  289. </script>
  290. <p>Simple mode that tries to handle C-like languages as well as it
  291. can. Takes two configuration parameters: <code>keywords</code>, an
  292. object whose property names are the keywords in the language,
  293. and <code>useCPP</code>, which determines whether C preprocessor
  294. directives are recognized.</p>
  295. <p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
  296. (C), <code>text/x-c++src</code> (C++), <code>text/x-java</code>
  297. (Java), <code>text/x-csharp</code> (C#),
  298. <code>text/x-objectivec</code> (Objective-C),
  299. <code>text/x-scala</code> (Scala), <code>text/x-vertex</code>
  300. <code>x-shader/x-fragment</code> (shader programs),
  301. <code>text/x-squirrel</code> (Squirrel) and
  302. <code>text/x-ceylon</code> (Ceylon)</p>
  303. </article>