d.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const 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. const decimal_integer_re = '(0|[1-9][\\d_]*)';
  55. const decimal_integer_nosus_re = '(0|[1-9][\\d_]*|\\d[\\d_]*|[\\d_]+?\\d)';
  56. const binary_integer_re = '0[bB][01_]+';
  57. const hexadecimal_digits_re = '([\\da-fA-F][\\da-fA-F_]*|_[\\da-fA-F][\\da-fA-F_]*)';
  58. const hexadecimal_integer_re = '0[xX]' + hexadecimal_digits_re;
  59. const decimal_exponent_re = '([eE][+-]?' + decimal_integer_nosus_re + ')';
  60. const decimal_float_re = '(' + decimal_integer_nosus_re + '(\\.\\d*|' + decimal_exponent_re + ')|' +
  61. '\\d+\\.' + decimal_integer_nosus_re + '|' +
  62. '\\.' + decimal_integer_re + decimal_exponent_re + '?' +
  63. ')';
  64. const hexadecimal_float_re = '(0[xX](' +
  65. hexadecimal_digits_re + '\\.' + hexadecimal_digits_re + '|' +
  66. '\\.?' + hexadecimal_digits_re +
  67. ')[pP][+-]?' + decimal_integer_nosus_re + ')';
  68. const integer_re = '(' +
  69. decimal_integer_re + '|' +
  70. binary_integer_re + '|' +
  71. hexadecimal_integer_re +
  72. ')';
  73. const 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. const 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. const 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. const 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. const D_CHARACTER_MODE = {
  118. className: 'string',
  119. begin: '\'(' + escape_sequence_re + '|.)',
  120. end: '\'',
  121. illegal: '.'
  122. };
  123. /**
  124. * D string escape sequence
  125. *
  126. * @type {Object}
  127. */
  128. const D_ESCAPE_SEQUENCE = {
  129. begin: escape_sequence_re,
  130. relevance: 0
  131. };
  132. /**
  133. * D double quoted string literal
  134. *
  135. * @type {Object}
  136. */
  137. const D_STRING_MODE = {
  138. className: 'string',
  139. begin: '"',
  140. contains: [D_ESCAPE_SEQUENCE],
  141. end: '"[cwd]?'
  142. };
  143. /**
  144. * D wysiwyg and delimited string literals
  145. *
  146. * @type {Object}
  147. */
  148. const D_WYSIWYG_DELIMITED_STRING_MODE = {
  149. className: 'string',
  150. begin: '[rq]"',
  151. end: '"[cwd]?',
  152. relevance: 5
  153. };
  154. /**
  155. * D alternate wysiwyg string literal
  156. *
  157. * @type {Object}
  158. */
  159. const D_ALTERNATE_WYSIWYG_STRING_MODE = {
  160. className: 'string',
  161. begin: '`',
  162. end: '`[cwd]?'
  163. };
  164. /**
  165. * D hexadecimal string literal
  166. *
  167. * @type {Object}
  168. */
  169. const D_HEX_STRING_MODE = {
  170. className: 'string',
  171. begin: 'x"[\\da-fA-F\\s\\n\\r]*"[cwd]?',
  172. relevance: 10
  173. };
  174. /**
  175. * D delimited string literal
  176. *
  177. * @type {Object}
  178. */
  179. const D_TOKEN_STRING_MODE = {
  180. className: 'string',
  181. begin: 'q"\\{',
  182. end: '\\}"'
  183. };
  184. /**
  185. * Hashbang support
  186. *
  187. * @type {Object}
  188. */
  189. const D_HASHBANG_MODE = {
  190. className: 'meta',
  191. begin: '^#!',
  192. end: '$',
  193. relevance: 5
  194. };
  195. /**
  196. * D special token sequence
  197. *
  198. * @type {Object}
  199. */
  200. const D_SPECIAL_TOKEN_SEQUENCE_MODE = {
  201. className: 'meta',
  202. begin: '#(line)',
  203. end: '$',
  204. relevance: 5
  205. };
  206. /**
  207. * D attributes
  208. *
  209. * @type {Object}
  210. */
  211. const D_ATTRIBUTE_MODE = {
  212. className: 'keyword',
  213. begin: '@[a-zA-Z_][a-zA-Z_\\d]*'
  214. };
  215. /**
  216. * D nesting comment
  217. *
  218. * @type {Object}
  219. */
  220. const D_NESTING_COMMENT_MODE = hljs.COMMENT(
  221. '\\/\\+',
  222. '\\+\\/',
  223. {
  224. contains: ['self'],
  225. relevance: 10
  226. }
  227. );
  228. return {
  229. name: 'D',
  230. keywords: D_KEYWORDS,
  231. contains: [
  232. hljs.C_LINE_COMMENT_MODE,
  233. hljs.C_BLOCK_COMMENT_MODE,
  234. D_NESTING_COMMENT_MODE,
  235. D_HEX_STRING_MODE,
  236. D_STRING_MODE,
  237. D_WYSIWYG_DELIMITED_STRING_MODE,
  238. D_ALTERNATE_WYSIWYG_STRING_MODE,
  239. D_TOKEN_STRING_MODE,
  240. D_FLOAT_MODE,
  241. D_INTEGER_MODE,
  242. D_CHARACTER_MODE,
  243. D_HASHBANG_MODE,
  244. D_SPECIAL_TOKEN_SEQUENCE_MODE,
  245. D_ATTRIBUTE_MODE
  246. ]
  247. };
  248. }
  249. module.exports = d;