kotlin.js 7.3 KB

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