dart.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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: [ { begin: '\\$[A-Za-z0-9_]+' } ]
  14. };
  15. const BRACED_SUBST = {
  16. className: 'subst',
  17. variants: [
  18. {
  19. begin: /\$\{/,
  20. end: /\}/
  21. }
  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 BASIC_KEYWORDS = [
  122. "abstract",
  123. "as",
  124. "assert",
  125. "async",
  126. "await",
  127. "base",
  128. "break",
  129. "case",
  130. "catch",
  131. "class",
  132. "const",
  133. "continue",
  134. "covariant",
  135. "default",
  136. "deferred",
  137. "do",
  138. "dynamic",
  139. "else",
  140. "enum",
  141. "export",
  142. "extends",
  143. "extension",
  144. "external",
  145. "factory",
  146. "false",
  147. "final",
  148. "finally",
  149. "for",
  150. "Function",
  151. "get",
  152. "hide",
  153. "if",
  154. "implements",
  155. "import",
  156. "in",
  157. "interface",
  158. "is",
  159. "late",
  160. "library",
  161. "mixin",
  162. "new",
  163. "null",
  164. "on",
  165. "operator",
  166. "part",
  167. "required",
  168. "rethrow",
  169. "return",
  170. "sealed",
  171. "set",
  172. "show",
  173. "static",
  174. "super",
  175. "switch",
  176. "sync",
  177. "this",
  178. "throw",
  179. "true",
  180. "try",
  181. "typedef",
  182. "var",
  183. "void",
  184. "when",
  185. "while",
  186. "with",
  187. "yield"
  188. ];
  189. const KEYWORDS = {
  190. keyword: BASIC_KEYWORDS,
  191. built_in:
  192. BUILT_IN_TYPES
  193. .concat(NULLABLE_BUILT_IN_TYPES)
  194. .concat([
  195. // dart:core
  196. 'Never',
  197. 'Null',
  198. 'dynamic',
  199. 'print',
  200. // dart:html
  201. 'document',
  202. 'querySelector',
  203. 'querySelectorAll',
  204. 'window'
  205. ]),
  206. $pattern: /[A-Za-z][A-Za-z0-9_]*\??/
  207. };
  208. return {
  209. name: 'Dart',
  210. keywords: KEYWORDS,
  211. contains: [
  212. STRING,
  213. hljs.COMMENT(
  214. /\/\*\*(?!\/)/,
  215. /\*\//,
  216. {
  217. subLanguage: 'markdown',
  218. relevance: 0
  219. }
  220. ),
  221. hljs.COMMENT(
  222. /\/{3,} ?/,
  223. /$/, { contains: [
  224. {
  225. subLanguage: 'markdown',
  226. begin: '.',
  227. end: '$',
  228. relevance: 0
  229. }
  230. ] }
  231. ),
  232. hljs.C_LINE_COMMENT_MODE,
  233. hljs.C_BLOCK_COMMENT_MODE,
  234. {
  235. className: 'class',
  236. beginKeywords: 'class interface',
  237. end: /\{/,
  238. excludeEnd: true,
  239. contains: [
  240. { beginKeywords: 'extends implements' },
  241. hljs.UNDERSCORE_TITLE_MODE
  242. ]
  243. },
  244. hljs.C_NUMBER_MODE,
  245. {
  246. className: 'meta',
  247. begin: '@[A-Za-z]+'
  248. },
  249. { begin: '=>' // No markup, just a relevance booster
  250. }
  251. ]
  252. };
  253. }
  254. export { dart as default };