d.js 6.7 KB

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