2
0

index.html 5.9 KB

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