| 776 |
lars |
1 |
<!doctype html>
|
|
|
2 |
<title>CodeMirror: D 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 |
<script src="d.js"></script>
|
|
|
9 |
<style>
|
|
|
10 |
.CodeMirror {
|
|
|
11 |
border: 2px inset #dee;
|
|
|
12 |
}
|
|
|
13 |
</style>
|
|
|
14 |
<div id=nav>
|
|
|
15 |
<a href="http://codemirror.net">
|
|
|
16 |
<h1>CodeMirror</h1>
|
|
|
17 |
<img id=logo src="../../doc/logo.png">
|
|
|
18 |
</a>
|
|
|
19 |
<ul>
|
|
|
20 |
<li>
|
|
|
21 |
<a href="../../index.html">Home</a>
|
|
|
22 |
<li>
|
|
|
23 |
<a href="../../doc/manual.html">Manual</a>
|
|
|
24 |
<li>
|
|
|
25 |
<a href="https://github.com/codemirror/codemirror">Code</a>
|
|
|
26 |
</ul>
|
|
|
27 |
<ul>
|
|
|
28 |
<li>
|
|
|
29 |
<a href="../index.html">Language modes</a>
|
|
|
30 |
<li>
|
|
|
31 |
<a class=active href="#">D</a>
|
|
|
32 |
</ul>
|
|
|
33 |
</div>
|
|
|
34 |
<article>
|
|
|
35 |
<h2>D mode</h2>
|
|
|
36 |
<form>
|
|
|
37 |
<textarea id="code" name="code"> /* D demo code // copied from phobos/sd/metastrings.d */ // Written in the D programming language. /** Templates with which to do compile-time manipulation of strings. Macros: WIKI = Phobos/StdMetastrings Copyright: Copyright Digital Mars 2007
|
|
|
38 |
- 2009. License:
|
|
|
39 |
<a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>. Authors: $(WEB digitalmars.com, Walter Bright), Don Clugston Source: $(PHOBOSSRC std/_metastrings.d) */ /* Copyright Digital Mars 2007 - 2009. Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
40 |
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ module std.metastrings; /** Formats constants into a string at compile time. Analogous to $(XREF string,format). Parameters: A = tuple of constants, which can be strings,
|
|
|
41 |
characters, or integral values. Formats: * The formats supported are %s for strings, and %% * for the % character. Example: --- import std.metastrings; import std.stdio; void main() { string s = Format!("Arg %s = %s", "foo", 27); writefln(s);
|
|
|
42 |
// "Arg foo = 27" } * --- */ template Format(A...) { static if (A.length == 0) enum Format = ""; else static if (is(typeof(A[0]) : const(char)[])) enum Format = FormatString!(A[0], A[1..$]); else enum Format = toStringNow!(A[0]) ~ Format!(A[1..$]);
|
|
|
43 |
} template FormatString(const(char)[] F, A...) { static if (F.length == 0) enum FormatString = Format!(A); else static if (F.length == 1) enum FormatString = F[0] ~ Format!(A); else static if (F[0..2] == "%s") enum FormatString = toStringNow!(A[0])
|
|
|
44 |
~ FormatString!(F[2..$],A[1..$]); else static if (F[0..2] == "%%") enum FormatString = "%" ~ FormatString!(F[2..$],A); else { static assert(F[0] != '%', "unrecognized format %" ~ F[1]); enum FormatString = F[0] ~ FormatString!(F[1..$],A);
|
|
|
45 |
} } unittest { auto s = Format!("hel%slo", "world", -138, 'c', true); assert(s == "helworldlo-138ctrue", "[" ~ s ~ "]"); } /** * Convert constant argument to a string. */ template toStringNow(ulong v) { static if (v
|
|
|
46 |
< 10) enum toStringNow=""
|
|
|
47 |
~ cast(char)(v + '0'); else enum toStringNow=t oStringNow!(v / 10) ~ toStringNow!(v % 10); } unittest { static assert(toStringNow!(1uL << 62)=="4611686018427387904" ); } /// ditto template toStringNow(long v) { static if (v < 0) enum toStringNow="-"
|
|
|
48 |
~ toStringNow!(cast(ulong) -v); else enum toStringNow=t oStringNow!(cast(ulong) v); } unittest { static assert(toStringNow!(0x100000000)=="4294967296" ); static assert(toStringNow!(-138L)=="-138" ); } /// ditto template toStringNow(uint U)
|
|
|
49 |
{ enum toStringNow=t oStringNow!(cast(ulong)U); } /// ditto template toStringNow(int I) { enum toStringNow=t oStringNow!(cast(long)I); } /// ditto template toStringNow(bool B) { enum toStringNow=B ? "true" : "false"; } /// ditto template toStringNow(string
|
|
|
50 |
S) { enum toStringNow=S ; } /// ditto template toStringNow(char C) { enum toStringNow="" ~ C; } /******** * Parse unsigned integer literal from the start of string s. * returns: * .value=t he integer literal as a string, * .rest=t he string
|
|
|
51 |
following the integer literal * Otherwise: * .value=n ull, * .rest=s */ template parseUinteger(const(char)[] s) { static if (s.length==0 ) { enum value="" ; enum rest="" ; } else static if (s[0]>= '0' && s[0]
|
|
|
52 |
<='9' ) { enum value=s [0] ~ parseUinteger!(s[1..$]).value; enum rest=p arseUinteger!(s[1..$]).rest; } else { enum value="" ; enum rest=s ; } } /******** Parse integer literal optionally preceded by $(D '-') from the start of
|
|
|
53 |
string $(D s). Returns: .value=t he integer literal as a string, .rest=t he string following the integer literal Otherwise: .value=n ull, .rest=s */ template parseInteger(const(char)[] s) { static if (s.length==0 ) { enum value="" ; enum
|
|
|
54 |
rest="" ; } else static if (s[0]>= '0' && s[0]
|
|
|
55 |
<='9' ) { enum value=s [0] ~ parseUinteger!(s[1..$]).value; enum rest=p arseUinteger!(s[1..$]).rest; } else static if (s.length>= 2 && s[0] == '-' && s[1] >= '0' && s[1]
|
|
|
56 |
<='9' ) { enum value=s [0..2] ~ parseUinteger!(s[2..$]).value; enum rest=p arseUinteger!(s[2..$]).rest; } else { enum value="" ; enum rest=s ; } } unittest { assert(parseUinteger!(
|
|
|
57 |
"1234abc").value=="1234" ); assert(parseUinteger!( "1234abc").rest=="abc" ); assert(parseInteger!( "-1234abc").value=="-1234" ); assert(parseInteger!( "-1234abc").rest=="abc" ); } /** Deprecated aliases held for backward compatibility.
|
|
|
58 |
*/ deprecated alias toStringNow ToString; /// Ditto deprecated alias parseUinteger ParseUinteger; /// Ditto deprecated alias parseUinteger ParseInteger; </textarea>
|
|
|
59 |
</form>
|
|
|
60 |
<script>
|
|
|
61 |
var editor = CodeMirror.fromTextArea(document.getElementById("code"),
|
|
|
62 |
{
|
|
|
63 |
lineNumbers: true,
|
|
|
64 |
matchBrackets: true,
|
|
|
65 |
indentUnit: 4,
|
|
|
66 |
mode: "text/x-d"
|
|
|
67 |
});
|
|
|
68 |
</script>
|
|
|
69 |
<p>Simple mode that handle D-Syntax (
|
|
|
70 |
<a href="http://www.dlang.org">DLang Homepage</a>).</p>
|
|
|
71 |
<p>
|
|
|
72 |
<strong>MIME types defined:</strong> <code>text/x-d</code> .</p>
|
|
|
73 |
</article>
|