dart.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Language: Dart
  3. Requires: markdown.js
  4. Author: Maxim Dikun <dikmax@gmail.com>
  5. Description: Dart a modern, object-oriented language developed by Google. For more information see https://www.dartlang.org/
  6. Website: https://dart.dev
  7. Category: scripting
  8. */
  9. /** @type LanguageFn */
  10. function dart(hljs) {
  11. const SUBST = {
  12. className: 'subst',
  13. variants: [{
  14. begin: '\\$[A-Za-z0-9_]+'
  15. }]
  16. };
  17. const BRACED_SUBST = {
  18. className: 'subst',
  19. variants: [{
  20. begin: /\$\{/,
  21. end: /\}/
  22. }],
  23. keywords: 'true false null this is new super'
  24. };
  25. const STRING = {
  26. className: 'string',
  27. variants: [
  28. {
  29. begin: 'r\'\'\'',
  30. end: '\'\'\''
  31. },
  32. {
  33. begin: 'r"""',
  34. end: '"""'
  35. },
  36. {
  37. begin: 'r\'',
  38. end: '\'',
  39. illegal: '\\n'
  40. },
  41. {
  42. begin: 'r"',
  43. end: '"',
  44. illegal: '\\n'
  45. },
  46. {
  47. begin: '\'\'\'',
  48. end: '\'\'\'',
  49. contains: [
  50. hljs.BACKSLASH_ESCAPE,
  51. SUBST,
  52. BRACED_SUBST
  53. ]
  54. },
  55. {
  56. begin: '"""',
  57. end: '"""',
  58. contains: [
  59. hljs.BACKSLASH_ESCAPE,
  60. SUBST,
  61. BRACED_SUBST
  62. ]
  63. },
  64. {
  65. begin: '\'',
  66. end: '\'',
  67. illegal: '\\n',
  68. contains: [
  69. hljs.BACKSLASH_ESCAPE,
  70. SUBST,
  71. BRACED_SUBST
  72. ]
  73. },
  74. {
  75. begin: '"',
  76. end: '"',
  77. illegal: '\\n',
  78. contains: [
  79. hljs.BACKSLASH_ESCAPE,
  80. SUBST,
  81. BRACED_SUBST
  82. ]
  83. }
  84. ]
  85. };
  86. BRACED_SUBST.contains = [
  87. hljs.C_NUMBER_MODE,
  88. STRING
  89. ];
  90. const BUILT_IN_TYPES = [
  91. // dart:core
  92. 'Comparable',
  93. 'DateTime',
  94. 'Duration',
  95. 'Function',
  96. 'Iterable',
  97. 'Iterator',
  98. 'List',
  99. 'Map',
  100. 'Match',
  101. 'Object',
  102. 'Pattern',
  103. 'RegExp',
  104. 'Set',
  105. 'Stopwatch',
  106. 'String',
  107. 'StringBuffer',
  108. 'StringSink',
  109. 'Symbol',
  110. 'Type',
  111. 'Uri',
  112. 'bool',
  113. 'double',
  114. 'int',
  115. 'num',
  116. // dart:html
  117. 'Element',
  118. 'ElementList'
  119. ];
  120. const NULLABLE_BUILT_IN_TYPES = BUILT_IN_TYPES.map((e) => `${e}?`);
  121. const KEYWORDS = {
  122. keyword: 'abstract as assert async await break case catch class const continue covariant default deferred do ' +
  123. 'dynamic else enum export extends extension external factory false final finally for Function get hide if ' +
  124. 'implements import in inferface is late library mixin new null on operator part required rethrow return set ' +
  125. 'show static super switch sync this throw true try typedef var void while with yield',
  126. built_in:
  127. BUILT_IN_TYPES
  128. .concat(NULLABLE_BUILT_IN_TYPES)
  129. .concat([
  130. // dart:core
  131. 'Never',
  132. 'Null',
  133. 'dynamic',
  134. 'print',
  135. // dart:html
  136. 'document',
  137. 'querySelector',
  138. 'querySelectorAll',
  139. 'window'
  140. ]),
  141. $pattern: /[A-Za-z][A-Za-z0-9_]*\??/
  142. };
  143. return {
  144. name: 'Dart',
  145. keywords: KEYWORDS,
  146. contains: [
  147. STRING,
  148. hljs.COMMENT(
  149. /\/\*\*(?!\/)/,
  150. /\*\//,
  151. {
  152. subLanguage: 'markdown',
  153. relevance: 0
  154. }
  155. ),
  156. hljs.COMMENT(
  157. /\/{3,} ?/,
  158. /$/, {
  159. contains: [{
  160. subLanguage: 'markdown',
  161. begin: '.',
  162. end: '$',
  163. relevance: 0
  164. }]
  165. }
  166. ),
  167. hljs.C_LINE_COMMENT_MODE,
  168. hljs.C_BLOCK_COMMENT_MODE,
  169. {
  170. className: 'class',
  171. beginKeywords: 'class interface',
  172. end: /\{/,
  173. excludeEnd: true,
  174. contains: [
  175. {
  176. beginKeywords: 'extends implements'
  177. },
  178. hljs.UNDERSCORE_TITLE_MODE
  179. ]
  180. },
  181. hljs.C_NUMBER_MODE,
  182. {
  183. className: 'meta',
  184. begin: '@[A-Za-z]+'
  185. },
  186. {
  187. begin: '=>' // No markup, just a relevance booster
  188. }
  189. ]
  190. };
  191. }
  192. module.exports = dart;