kotlin.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Language: Kotlin
  3. Description: Kotlin is an OSS statically typed programming language that targets the JVM, Android, JavaScript and Native.
  4. Author: Sergey Mashkov <cy6erGn0m@gmail.com>
  5. Website: https://kotlinlang.org
  6. Category: common
  7. */
  8. function kotlin(hljs) {
  9. var KEYWORDS = {
  10. keyword:
  11. 'abstract as val var vararg get set class object open private protected public noinline ' +
  12. 'crossinline dynamic final enum if else do while for when throw try catch finally ' +
  13. 'import package is in fun override companion reified inline lateinit init ' +
  14. 'interface annotation data sealed internal infix operator out by constructor super ' +
  15. 'tailrec where const inner suspend typealias external expect actual',
  16. built_in:
  17. 'Byte Short Char Int Long Boolean Float Double Void Unit Nothing',
  18. literal:
  19. 'true false null'
  20. };
  21. var KEYWORDS_WITH_LABEL = {
  22. className: 'keyword',
  23. begin: /\b(break|continue|return|this)\b/,
  24. starts: {
  25. contains: [
  26. {
  27. className: 'symbol',
  28. begin: /@\w+/
  29. }
  30. ]
  31. }
  32. };
  33. var LABEL = {
  34. className: 'symbol', begin: hljs.UNDERSCORE_IDENT_RE + '@'
  35. };
  36. // for string templates
  37. var SUBST = {
  38. className: 'subst',
  39. begin: '\\${', end: '}', contains: [hljs.C_NUMBER_MODE]
  40. };
  41. var VARIABLE = {
  42. className: 'variable', begin: '\\$' + hljs.UNDERSCORE_IDENT_RE
  43. };
  44. var STRING = {
  45. className: 'string',
  46. variants: [
  47. {
  48. begin: '"""', end: '"""(?=[^"])',
  49. contains: [VARIABLE, SUBST]
  50. },
  51. // Can't use built-in modes easily, as we want to use STRING in the meta
  52. // context as 'meta-string' and there's no syntax to remove explicitly set
  53. // classNames in built-in modes.
  54. {
  55. begin: '\'', end: '\'',
  56. illegal: /\n/,
  57. contains: [hljs.BACKSLASH_ESCAPE]
  58. },
  59. {
  60. begin: '"', end: '"',
  61. illegal: /\n/,
  62. contains: [hljs.BACKSLASH_ESCAPE, VARIABLE, SUBST]
  63. }
  64. ]
  65. };
  66. SUBST.contains.push(STRING);
  67. var ANNOTATION_USE_SITE = {
  68. className: 'meta', begin: '@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*' + hljs.UNDERSCORE_IDENT_RE + ')?'
  69. };
  70. var ANNOTATION = {
  71. className: 'meta', begin: '@' + hljs.UNDERSCORE_IDENT_RE,
  72. contains: [
  73. {
  74. begin: /\(/, end: /\)/,
  75. contains: [
  76. hljs.inherit(STRING, {className: 'meta-string'})
  77. ]
  78. }
  79. ]
  80. };
  81. // https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
  82. // According to the doc above, the number mode of kotlin is the same as java 8,
  83. // so the code below is copied from java.js
  84. var KOTLIN_NUMBER_RE = '\\b' +
  85. '(' +
  86. '0[bB]([01]+[01_]+[01]+|[01]+)' + // 0b...
  87. '|' +
  88. '0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)' + // 0x...
  89. '|' +
  90. '(' +
  91. '([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?' +
  92. '|' +
  93. '\\.([\\d]+[\\d_]+[\\d]+|[\\d]+)' +
  94. ')' +
  95. '([eE][-+]?\\d+)?' + // octal, decimal, float
  96. ')' +
  97. '[lLfF]?';
  98. var KOTLIN_NUMBER_MODE = {
  99. className: 'number',
  100. begin: KOTLIN_NUMBER_RE,
  101. relevance: 0
  102. };
  103. var KOTLIN_NESTED_COMMENT = hljs.COMMENT(
  104. '/\\*', '\\*/',
  105. { contains: [ hljs.C_BLOCK_COMMENT_MODE ] }
  106. );
  107. var KOTLIN_PAREN_TYPE = {
  108. variants: [
  109. { className: 'type',
  110. begin: hljs.UNDERSCORE_IDENT_RE
  111. },
  112. { begin: /\(/, end: /\)/,
  113. contains: [] //defined later
  114. }
  115. ]
  116. };
  117. var KOTLIN_PAREN_TYPE2 = KOTLIN_PAREN_TYPE;
  118. KOTLIN_PAREN_TYPE2.variants[1].contains = [ KOTLIN_PAREN_TYPE ];
  119. KOTLIN_PAREN_TYPE.variants[1].contains = [ KOTLIN_PAREN_TYPE2 ];
  120. return {
  121. name: 'Kotlin',
  122. aliases: ['kt'],
  123. keywords: KEYWORDS,
  124. contains : [
  125. hljs.COMMENT(
  126. '/\\*\\*',
  127. '\\*/',
  128. {
  129. relevance : 0,
  130. contains : [{
  131. className : 'doctag',
  132. begin : '@[A-Za-z]+'
  133. }]
  134. }
  135. ),
  136. hljs.C_LINE_COMMENT_MODE,
  137. KOTLIN_NESTED_COMMENT,
  138. KEYWORDS_WITH_LABEL,
  139. LABEL,
  140. ANNOTATION_USE_SITE,
  141. ANNOTATION,
  142. {
  143. className: 'function',
  144. beginKeywords: 'fun', end: '[(]|$',
  145. returnBegin: true,
  146. excludeEnd: true,
  147. keywords: KEYWORDS,
  148. illegal: /fun\s+(<.*>)?[^\s\(]+(\s+[^\s\(]+)\s*=/,
  149. relevance: 5,
  150. contains: [
  151. {
  152. begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
  153. relevance: 0,
  154. contains: [hljs.UNDERSCORE_TITLE_MODE]
  155. },
  156. {
  157. className: 'type',
  158. begin: /</, end: />/, keywords: 'reified',
  159. relevance: 0
  160. },
  161. {
  162. className: 'params',
  163. begin: /\(/, end: /\)/,
  164. endsParent: true,
  165. keywords: KEYWORDS,
  166. relevance: 0,
  167. contains: [
  168. {
  169. begin: /:/, end: /[=,\/]/, endsWithParent: true,
  170. contains: [
  171. KOTLIN_PAREN_TYPE,
  172. hljs.C_LINE_COMMENT_MODE,
  173. KOTLIN_NESTED_COMMENT
  174. ],
  175. relevance: 0
  176. },
  177. hljs.C_LINE_COMMENT_MODE,
  178. KOTLIN_NESTED_COMMENT,
  179. ANNOTATION_USE_SITE,
  180. ANNOTATION,
  181. STRING,
  182. hljs.C_NUMBER_MODE
  183. ]
  184. },
  185. KOTLIN_NESTED_COMMENT
  186. ]
  187. },
  188. {
  189. className: 'class',
  190. beginKeywords: 'class interface trait', end: /[:\{(]|$/, // remove 'trait' when removed from KEYWORDS
  191. excludeEnd: true,
  192. illegal: 'extends implements',
  193. contains: [
  194. {beginKeywords: 'public protected internal private constructor'},
  195. hljs.UNDERSCORE_TITLE_MODE,
  196. {
  197. className: 'type',
  198. begin: /</, end: />/, excludeBegin: true, excludeEnd: true,
  199. relevance: 0
  200. },
  201. {
  202. className: 'type',
  203. begin: /[,:]\s*/, end: /[<\(,]|$/, excludeBegin: true, returnEnd: true
  204. },
  205. ANNOTATION_USE_SITE,
  206. ANNOTATION
  207. ]
  208. },
  209. STRING,
  210. {
  211. className: 'meta',
  212. begin: "^#!/usr/bin/env", end: '$',
  213. illegal: '\n'
  214. },
  215. KOTLIN_NUMBER_MODE
  216. ]
  217. };
  218. }
  219. module.exports = kotlin;