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