kotlin.js 6.3 KB

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