openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (6332B)


      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="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></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 /**
     33 Templates with which to do compile-time manipulation of strings.
     34 
     35 Macros:
     36  WIKI = Phobos/StdMetastrings
     37 
     38 Copyright: Copyright Digital Mars 2007 - 2009.
     39 License:   <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
     40 Authors:   $(WEB digitalmars.com, Walter Bright),
     41            Don Clugston
     42 Source:    $(PHOBOSSRC std/_metastrings.d)
     43 */
     44 /*
     45          Copyright Digital Mars 2007 - 2009.
     46 Distributed 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  */
     50 module std.metastrings;
     51 
     52 /**
     53 Formats constants into a string at compile time.  Analogous to $(XREF
     54 string,format).
     55 
     56 Parameters:
     57 
     58 A = tuple of constants, which can be strings, characters, or integral
     59     values.
     60 
     61 Formats:
     62  *    The formats supported are %s for strings, and %%
     63  *    for the % character.
     64 Example:
     65 ---
     66 import std.metastrings;
     67 import std.stdio;
     68 
     69 void main()
     70 {
     71   string s = Format!("Arg %s = %s", "foo", 27);
     72   writefln(s); // "Arg foo = 27"
     73 }
     74  * ---
     75  */
     76 
     77 template 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 
     87 template 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 
    105 unittest
    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 
    115 template 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 
    123 unittest
    124 {
    125     static assert(toStringNow!(1uL << 62) == "4611686018427387904");
    126 }
    127 
    128 /// ditto
    129 template 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 
    137 unittest
    138 {
    139     static assert(toStringNow!(0x100000000) == "4294967296");
    140     static assert(toStringNow!(-138L) == "-138");
    141 }
    142 
    143 /// ditto
    144 template toStringNow(uint U)
    145 {
    146     enum toStringNow = toStringNow!(cast(ulong)U);
    147 }
    148 
    149 /// ditto
    150 template toStringNow(int I)
    151 {
    152     enum toStringNow = toStringNow!(cast(long)I);
    153 }
    154 
    155 /// ditto
    156 template toStringNow(bool B)
    157 {
    158     enum toStringNow = B ? "true" : "false";
    159 }
    160 
    161 /// ditto
    162 template toStringNow(string S)
    163 {
    164     enum toStringNow = S;
    165 }
    166 
    167 /// ditto
    168 template 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 
    184 template 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 /********
    204 Parse integer literal optionally preceded by $(D '-') from the start
    205 of string $(D s).
    206 
    207 Returns:
    208    .value = the integer literal as a string,
    209    .rest = the string following the integer literal
    210 
    211 Otherwise:
    212    .value = null,
    213    .rest = s
    214 */
    215 
    216 template 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 
    241 unittest
    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 /**
    250 Deprecated aliases held for backward compatibility.
    251 */
    252 deprecated alias toStringNow ToString;
    253 /// Ditto
    254 deprecated alias parseUinteger ParseUinteger;
    255 /// Ditto
    256 deprecated 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>