d.js 6.6 KB

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