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