scripts/CodeMirror/mode/d/index.html (view raw)
1<!doctype html>
2
3<title>CodeMirror: D 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<script src="d.js"></script>
11<style>.CodeMirror {border: 2px inset #dee;}</style>
12<div id=nav>
13 <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
14
15 <ul>
16 <li><a href="../../index.html">Home</a>
17 <li><a href="../../doc/manual.html">Manual</a>
18 <li><a href="https://github.com/codemirror/codemirror">Code</a>
19 </ul>
20 <ul>
21 <li><a href="../index.html">Language modes</a>
22 <li><a class=active href="#">D</a>
23 </ul>
24</div>
25
26<article>
27<h2>D mode</h2>
28<form><textarea id="code" name="code">
29/* D demo code // copied from phobos/sd/metastrings.d */
30// Written in the D programming language.
31
32/**
33Templates with which to do compile-time manipulation of strings.
34
35Macros:
36 WIKI = Phobos/StdMetastrings
37
38Copyright: Copyright Digital Mars 2007 - 2009.
39License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
40Authors: $(WEB digitalmars.com, Walter Bright),
41 Don Clugston
42Source: $(PHOBOSSRC std/_metastrings.d)
43*/
44/*
45 Copyright Digital Mars 2007 - 2009.
46Distributed under the Boost Software License, Version 1.0.
47 (See accompanying file LICENSE_1_0.txt or copy at
48 http://www.boost.org/LICENSE_1_0.txt)
49 */
50module std.metastrings;
51
52/**
53Formats constants into a string at compile time. Analogous to $(XREF
54string,format).
55
56Parameters:
57
58A = tuple of constants, which can be strings, characters, or integral
59 values.
60
61Formats:
62 * The formats supported are %s for strings, and %%
63 * for the % character.
64Example:
65---
66import std.metastrings;
67import std.stdio;
68
69void main()
70{
71 string s = Format!("Arg %s = %s", "foo", 27);
72 writefln(s); // "Arg foo = 27"
73}
74 * ---
75 */
76
77template Format(A...)
78{
79 static if (A.length == 0)
80 enum Format = "";
81 else static if (is(typeof(A[0]) : const(char)[]))
82 enum Format = FormatString!(A[0], A[1..$]);
83 else
84 enum Format = toStringNow!(A[0]) ~ Format!(A[1..$]);
85}
86
87template FormatString(const(char)[] F, A...)
88{
89 static if (F.length == 0)
90 enum FormatString = Format!(A);
91 else static if (F.length == 1)
92 enum FormatString = F[0] ~ Format!(A);
93 else static if (F[0..2] == "%s")
94 enum FormatString
95 = toStringNow!(A[0]) ~ FormatString!(F[2..$],A[1..$]);
96 else static if (F[0..2] == "%%")
97 enum FormatString = "%" ~ FormatString!(F[2..$],A);
98 else
99 {
100 static assert(F[0] != '%', "unrecognized format %" ~ F[1]);
101 enum FormatString = F[0] ~ FormatString!(F[1..$],A);
102 }
103}
104
105unittest
106{
107 auto s = Format!("hel%slo", "world", -138, 'c', true);
108 assert(s == "helworldlo-138ctrue", "[" ~ s ~ "]");
109}
110
111/**
112 * Convert constant argument to a string.
113 */
114
115template toStringNow(ulong v)
116{
117 static if (v < 10)
118 enum toStringNow = "" ~ cast(char)(v + '0');
119 else
120 enum toStringNow = toStringNow!(v / 10) ~ toStringNow!(v % 10);
121}
122
123unittest
124{
125 static assert(toStringNow!(1uL << 62) == "4611686018427387904");
126}
127
128/// ditto
129template toStringNow(long v)
130{
131 static if (v < 0)
132 enum toStringNow = "-" ~ toStringNow!(cast(ulong) -v);
133 else
134 enum toStringNow = toStringNow!(cast(ulong) v);
135}
136
137unittest
138{
139 static assert(toStringNow!(0x100000000) == "4294967296");
140 static assert(toStringNow!(-138L) == "-138");
141}
142
143/// ditto
144template toStringNow(uint U)
145{
146 enum toStringNow = toStringNow!(cast(ulong)U);
147}
148
149/// ditto
150template toStringNow(int I)
151{
152 enum toStringNow = toStringNow!(cast(long)I);
153}
154
155/// ditto
156template toStringNow(bool B)
157{
158 enum toStringNow = B ? "true" : "false";
159}
160
161/// ditto
162template toStringNow(string S)
163{
164 enum toStringNow = S;
165}
166
167/// ditto
168template toStringNow(char C)
169{
170 enum toStringNow = "" ~ C;
171}
172
173
174/********
175 * Parse unsigned integer literal from the start of string s.
176 * returns:
177 * .value = the integer literal as a string,
178 * .rest = the string following the integer literal
179 * Otherwise:
180 * .value = null,
181 * .rest = s
182 */
183
184template parseUinteger(const(char)[] s)
185{
186 static if (s.length == 0)
187 {
188 enum value = "";
189 enum rest = "";
190 }
191 else static if (s[0] >= '0' && s[0] <= '9')
192 {
193 enum value = s[0] ~ parseUinteger!(s[1..$]).value;
194 enum rest = parseUinteger!(s[1..$]).rest;
195 }
196 else
197 {
198 enum value = "";
199 enum rest = s;
200 }
201}
202
203/********
204Parse integer literal optionally preceded by $(D '-') from the start
205of string $(D s).
206
207Returns:
208 .value = the integer literal as a string,
209 .rest = the string following the integer literal
210
211Otherwise:
212 .value = null,
213 .rest = s
214*/
215
216template parseInteger(const(char)[] s)
217{
218 static if (s.length == 0)
219 {
220 enum value = "";
221 enum rest = "";
222 }
223 else static if (s[0] >= '0' && s[0] <= '9')
224 {
225 enum value = s[0] ~ parseUinteger!(s[1..$]).value;
226 enum rest = parseUinteger!(s[1..$]).rest;
227 }
228 else static if (s.length >= 2 &&
229 s[0] == '-' && s[1] >= '0' && s[1] <= '9')
230 {
231 enum value = s[0..2] ~ parseUinteger!(s[2..$]).value;
232 enum rest = parseUinteger!(s[2..$]).rest;
233 }
234 else
235 {
236 enum value = "";
237 enum rest = s;
238 }
239}
240
241unittest
242{
243 assert(parseUinteger!("1234abc").value == "1234");
244 assert(parseUinteger!("1234abc").rest == "abc");
245 assert(parseInteger!("-1234abc").value == "-1234");
246 assert(parseInteger!("-1234abc").rest == "abc");
247}
248
249/**
250Deprecated aliases held for backward compatibility.
251*/
252deprecated alias toStringNow ToString;
253/// Ditto
254deprecated alias parseUinteger ParseUinteger;
255/// Ditto
256deprecated alias parseUinteger ParseInteger;
257
258</textarea></form>
259
260 <script>
261 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
262 lineNumbers: true,
263 matchBrackets: true,
264 indentUnit: 4,
265 mode: "text/x-d"
266 });
267 </script>
268
269 <p>Simple mode that handle D-Syntax (<a href="http://www.dlang.org">DLang Homepage</a>).</p>
270
271 <p><strong>MIME types defined:</strong> <code>text/x-d</code>
272 .</p>
273 </article>