d.js 6.7 KB

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