d.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. module.exports = /**
  2. * Known issues:
  3. *
  4. * - invalid hex string literals will be recognized as a double quoted strings
  5. * but 'x' at the beginning of string will not be matched
  6. *
  7. * - delimited string literals are not checked for matching end delimiter
  8. * (not possible to do with js regexp)
  9. *
  10. * - content of token string is colored as a string (i.e. no keyword coloring inside a token string)
  11. * also, content of token string is not validated to contain only valid D tokens
  12. *
  13. * - special token sequence rule is not strictly following D grammar (anything following #line
  14. * up to the end of line is matched as special token sequence)
  15. */
  16. function(hljs) {
  17. /**
  18. * Language keywords
  19. *
  20. * @type {Object}
  21. */
  22. var D_KEYWORDS = {
  23. keyword:
  24. 'abstract alias align asm assert auto body break byte case cast catch class ' +
  25. 'const continue debug default delete deprecated do else enum export extern final ' +
  26. 'finally for foreach foreach_reverse|10 goto if immutable import in inout int ' +
  27. 'interface invariant is lazy macro mixin module new nothrow out override package ' +
  28. 'pragma private protected public pure ref return scope shared static struct ' +
  29. 'super switch synchronized template this throw try typedef typeid typeof union ' +
  30. 'unittest version void volatile while with __FILE__ __LINE__ __gshared|10 ' +
  31. '__thread __traits __DATE__ __EOF__ __TIME__ __TIMESTAMP__ __VENDOR__ __VERSION__',
  32. built_in:
  33. 'bool cdouble cent cfloat char creal dchar delegate double dstring float function ' +
  34. 'idouble ifloat ireal long real short string ubyte ucent uint ulong ushort wchar ' +
  35. 'wstring',
  36. literal:
  37. 'false null true'
  38. };
  39. /**
  40. * Number literal regexps
  41. *
  42. * @type {String}
  43. */
  44. var decimal_integer_re = '(0|[1-9][\\d_]*)',
  45. decimal_integer_nosus_re = '(0|[1-9][\\d_]*|\\d[\\d_]*|[\\d_]+?\\d)',
  46. binary_integer_re = '0[bB][01_]+',
  47. hexadecimal_digits_re = '([\\da-fA-F][\\da-fA-F_]*|_[\\da-fA-F][\\da-fA-F_]*)',
  48. hexadecimal_integer_re = '0[xX]' + hexadecimal_digits_re,
  49. decimal_exponent_re = '([eE][+-]?' + decimal_integer_nosus_re + ')',
  50. decimal_float_re = '(' + decimal_integer_nosus_re + '(\\.\\d*|' + decimal_exponent_re + ')|' +
  51. '\\d+\\.' + decimal_integer_nosus_re + decimal_integer_nosus_re + '|' +
  52. '\\.' + decimal_integer_re + decimal_exponent_re + '?' +
  53. ')',
  54. hexadecimal_float_re = '(0[xX](' +
  55. hexadecimal_digits_re + '\\.' + hexadecimal_digits_re + '|'+
  56. '\\.?' + hexadecimal_digits_re +
  57. ')[pP][+-]?' + decimal_integer_nosus_re + ')',
  58. integer_re = '(' +
  59. decimal_integer_re + '|' +
  60. binary_integer_re + '|' +
  61. hexadecimal_integer_re +
  62. ')',
  63. float_re = '(' +
  64. hexadecimal_float_re + '|' +
  65. decimal_float_re +
  66. ')';
  67. /**
  68. * Escape sequence supported in D string and character literals
  69. *
  70. * @type {String}
  71. */
  72. var escape_sequence_re = '\\\\(' +
  73. '[\'"\\?\\\\abfnrtv]|' + // common escapes
  74. 'u[\\dA-Fa-f]{4}|' + // four hex digit unicode codepoint
  75. '[0-7]{1,3}|' + // one to three octal digit ascii char code
  76. 'x[\\dA-Fa-f]{2}|' + // two hex digit ascii char code
  77. 'U[\\dA-Fa-f]{8}' + // eight hex digit unicode codepoint
  78. ')|' +
  79. '&[a-zA-Z\\d]{2,};'; // named character entity
  80. /**
  81. * D integer number literals
  82. *
  83. * @type {Object}
  84. */
  85. var D_INTEGER_MODE = {
  86. className: 'number',
  87. begin: '\\b' + integer_re + '(L|u|U|Lu|LU|uL|UL)?',
  88. relevance: 0
  89. };
  90. /**
  91. * [D_FLOAT_MODE description]
  92. * @type {Object}
  93. */
  94. var D_FLOAT_MODE = {
  95. className: 'number',
  96. begin: '\\b(' +
  97. float_re + '([fF]|L|i|[fF]i|Li)?|' +
  98. integer_re + '(i|[fF]i|Li)' +
  99. ')',
  100. relevance: 0
  101. };
  102. /**
  103. * D character literal
  104. *
  105. * @type {Object}
  106. */
  107. var D_CHARACTER_MODE = {
  108. className: 'string',
  109. begin: '\'(' + escape_sequence_re + '|.)', end: '\'',
  110. illegal: '.'
  111. };
  112. /**
  113. * D string escape sequence
  114. *
  115. * @type {Object}
  116. */
  117. var D_ESCAPE_SEQUENCE = {
  118. begin: escape_sequence_re,
  119. relevance: 0
  120. };
  121. /**
  122. * D double quoted string literal
  123. *
  124. * @type {Object}
  125. */
  126. var D_STRING_MODE = {
  127. className: 'string',
  128. begin: '"',
  129. contains: [D_ESCAPE_SEQUENCE],
  130. end: '"[cwd]?'
  131. };
  132. /**
  133. * D wysiwyg and delimited string literals
  134. *
  135. * @type {Object}
  136. */
  137. var D_WYSIWYG_DELIMITED_STRING_MODE = {
  138. className: 'string',
  139. begin: '[rq]"',
  140. end: '"[cwd]?',
  141. relevance: 5
  142. };
  143. /**
  144. * D alternate wysiwyg string literal
  145. *
  146. * @type {Object}
  147. */
  148. var D_ALTERNATE_WYSIWYG_STRING_MODE = {
  149. className: 'string',
  150. begin: '`',
  151. end: '`[cwd]?'
  152. };
  153. /**
  154. * D hexadecimal string literal
  155. *
  156. * @type {Object}
  157. */
  158. var D_HEX_STRING_MODE = {
  159. className: 'string',
  160. begin: 'x"[\\da-fA-F\\s\\n\\r]*"[cwd]?',
  161. relevance: 10
  162. };
  163. /**
  164. * D delimited string literal
  165. *
  166. * @type {Object}
  167. */
  168. var D_TOKEN_STRING_MODE = {
  169. className: 'string',
  170. begin: 'q"\\{',
  171. end: '\\}"'
  172. };
  173. /**
  174. * Hashbang support
  175. *
  176. * @type {Object}
  177. */
  178. var D_HASHBANG_MODE = {
  179. className: 'meta',
  180. begin: '^#!',
  181. end: '$',
  182. relevance: 5
  183. };
  184. /**
  185. * D special token sequence
  186. *
  187. * @type {Object}
  188. */
  189. var D_SPECIAL_TOKEN_SEQUENCE_MODE = {
  190. className: 'meta',
  191. begin: '#(line)',
  192. end: '$',
  193. relevance: 5
  194. };
  195. /**
  196. * D attributes
  197. *
  198. * @type {Object}
  199. */
  200. var D_ATTRIBUTE_MODE = {
  201. className: 'keyword',
  202. begin: '@[a-zA-Z_][a-zA-Z_\\d]*'
  203. };
  204. /**
  205. * D nesting comment
  206. *
  207. * @type {Object}
  208. */
  209. var D_NESTING_COMMENT_MODE = hljs.COMMENT(
  210. '\\/\\+',
  211. '\\+\\/',
  212. {
  213. contains: ['self'],
  214. relevance: 10
  215. }
  216. );
  217. return {
  218. lexemes: hljs.UNDERSCORE_IDENT_RE,
  219. keywords: D_KEYWORDS,
  220. contains: [
  221. hljs.C_LINE_COMMENT_MODE,
  222. hljs.C_BLOCK_COMMENT_MODE,
  223. D_NESTING_COMMENT_MODE,
  224. D_HEX_STRING_MODE,
  225. D_STRING_MODE,
  226. D_WYSIWYG_DELIMITED_STRING_MODE,
  227. D_ALTERNATE_WYSIWYG_STRING_MODE,
  228. D_TOKEN_STRING_MODE,
  229. D_FLOAT_MODE,
  230. D_INTEGER_MODE,
  231. D_CHARACTER_MODE,
  232. D_HASHBANG_MODE,
  233. D_SPECIAL_TOKEN_SEQUENCE_MODE,
  234. D_ATTRIBUTE_MODE
  235. ]
  236. };
  237. };