index.html 6.2 KB

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