2024-01-16 21:26:16 +08:00
|
|
|
// https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10
|
|
|
|
var decimalDigits = '[0-9](_*[0-9])*';
|
|
|
|
var frac = `\\.(${decimalDigits})`;
|
|
|
|
var hexDigits = '[0-9a-fA-F](_*[0-9a-fA-F])*';
|
|
|
|
var NUMERIC = {
|
|
|
|
className: 'number',
|
|
|
|
variants: [
|
|
|
|
// DecimalFloatingPointLiteral
|
|
|
|
// including ExponentPart
|
|
|
|
{ begin: `(\\b(${decimalDigits})((${frac})|\\.)?|(${frac}))` +
|
|
|
|
`[eE][+-]?(${decimalDigits})[fFdD]?\\b` },
|
|
|
|
// excluding ExponentPart
|
|
|
|
{ begin: `\\b(${decimalDigits})((${frac})[fFdD]?\\b|\\.([fFdD]\\b)?)` },
|
|
|
|
{ begin: `(${frac})[fFdD]?\\b` },
|
|
|
|
{ begin: `\\b(${decimalDigits})[fFdD]\\b` },
|
|
|
|
|
|
|
|
// HexadecimalFloatingPointLiteral
|
|
|
|
{ begin: `\\b0[xX]((${hexDigits})\\.?|(${hexDigits})?\\.(${hexDigits}))` +
|
|
|
|
`[pP][+-]?(${decimalDigits})[fFdD]?\\b` },
|
|
|
|
|
|
|
|
// DecimalIntegerLiteral
|
|
|
|
{ begin: '\\b(0|[1-9](_*[0-9])*)[lL]?\\b' },
|
|
|
|
|
|
|
|
// HexIntegerLiteral
|
|
|
|
{ begin: `\\b0[xX](${hexDigits})[lL]?\\b` },
|
|
|
|
|
|
|
|
// OctalIntegerLiteral
|
|
|
|
{ begin: '\\b0(_*[0-7])*[lL]?\\b' },
|
|
|
|
|
|
|
|
// BinaryIntegerLiteral
|
|
|
|
{ begin: '\\b0[bB][01](_*[01])*[lL]?\\b' },
|
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Language: Kotlin
|
|
|
|
Description: Kotlin is an OSS statically typed programming language that targets the JVM, Android, JavaScript and Native.
|
|
|
|
Author: Sergey Mashkov <cy6erGn0m@gmail.com>
|
|
|
|
Website: https://kotlinlang.org
|
|
|
|
Category: common
|
|
|
|
*/
|
|
|
|
|
|
|
|
function kotlin(hljs) {
|
|
|
|
const KEYWORDS = {
|
2023-12-18 13:12:25 +08:00
|
|
|
keyword:
|
|
|
|
'abstract as val var vararg get set class object open private protected public noinline ' +
|
|
|
|
'crossinline dynamic final enum if else do while for when throw try catch finally ' +
|
|
|
|
'import package is in fun override companion reified inline lateinit init ' +
|
|
|
|
'interface annotation data sealed internal infix operator out by constructor super ' +
|
2024-01-16 21:26:16 +08:00
|
|
|
'tailrec where const inner suspend typealias external expect actual',
|
2023-12-18 13:12:25 +08:00
|
|
|
built_in:
|
|
|
|
'Byte Short Char Int Long Boolean Float Double Void Unit Nothing',
|
|
|
|
literal:
|
|
|
|
'true false null'
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const KEYWORDS_WITH_LABEL = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'keyword',
|
|
|
|
begin: /\b(break|continue|return|this)\b/,
|
|
|
|
starts: {
|
|
|
|
contains: [
|
|
|
|
{
|
|
|
|
className: 'symbol',
|
|
|
|
begin: /@\w+/
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const LABEL = {
|
|
|
|
className: 'symbol',
|
|
|
|
begin: hljs.UNDERSCORE_IDENT_RE + '@'
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// for string templates
|
2024-01-16 21:26:16 +08:00
|
|
|
const SUBST = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'subst',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /\$\{/,
|
|
|
|
end: /\}/,
|
|
|
|
contains: [ hljs.C_NUMBER_MODE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const VARIABLE = {
|
|
|
|
className: 'variable',
|
|
|
|
begin: '\\$' + hljs.UNDERSCORE_IDENT_RE
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
|
|
|
variants: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '"""',
|
|
|
|
end: '"""(?=[^"])',
|
|
|
|
contains: [
|
|
|
|
VARIABLE,
|
|
|
|
SUBST
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
// Can't use built-in modes easily, as we want to use STRING in the meta
|
|
|
|
// context as 'meta-string' and there's no syntax to remove explicitly set
|
|
|
|
// classNames in built-in modes.
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '\'',
|
|
|
|
end: '\'',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: /\n/,
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [ hljs.BACKSLASH_ESCAPE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: '"',
|
|
|
|
end: '"',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: /\n/,
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
VARIABLE,
|
|
|
|
SUBST
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
SUBST.contains.push(STRING);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
const ANNOTATION_USE_SITE = {
|
|
|
|
className: 'meta',
|
|
|
|
begin: '@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*' + hljs.UNDERSCORE_IDENT_RE + ')?'
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const ANNOTATION = {
|
|
|
|
className: 'meta',
|
|
|
|
begin: '@' + hljs.UNDERSCORE_IDENT_RE,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /\(/,
|
|
|
|
end: /\)/,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(STRING, {
|
|
|
|
className: 'meta-string'
|
|
|
|
})
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
|
|
|
|
// According to the doc above, the number mode of kotlin is the same as java 8,
|
|
|
|
// so the code below is copied from java.js
|
2024-01-16 21:26:16 +08:00
|
|
|
const KOTLIN_NUMBER_MODE = NUMERIC;
|
|
|
|
const KOTLIN_NESTED_COMMENT = hljs.COMMENT(
|
|
|
|
'/\\*', '\\*/',
|
|
|
|
{
|
|
|
|
contains: [ hljs.C_BLOCK_COMMENT_MODE ]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const KOTLIN_PAREN_TYPE = {
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
className: 'type',
|
|
|
|
begin: hljs.UNDERSCORE_IDENT_RE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
begin: /\(/,
|
|
|
|
end: /\)/,
|
|
|
|
contains: [] // defined later
|
|
|
|
}
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const KOTLIN_PAREN_TYPE2 = KOTLIN_PAREN_TYPE;
|
|
|
|
KOTLIN_PAREN_TYPE2.variants[1].contains = [ KOTLIN_PAREN_TYPE ];
|
|
|
|
KOTLIN_PAREN_TYPE.variants[1].contains = [ KOTLIN_PAREN_TYPE2 ];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
return {
|
2024-01-16 21:26:16 +08:00
|
|
|
name: 'Kotlin',
|
|
|
|
aliases: [ 'kt', 'kts' ],
|
2023-12-18 13:12:25 +08:00
|
|
|
keywords: KEYWORDS,
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [
|
2023-12-18 13:12:25 +08:00
|
|
|
hljs.COMMENT(
|
|
|
|
'/\\*\\*',
|
|
|
|
'\\*/',
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 0,
|
|
|
|
contains: [
|
|
|
|
{
|
|
|
|
className: 'doctag',
|
|
|
|
begin: '@[A-Za-z]+'
|
|
|
|
}
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
hljs.C_LINE_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
KOTLIN_NESTED_COMMENT,
|
2023-12-18 13:12:25 +08:00
|
|
|
KEYWORDS_WITH_LABEL,
|
|
|
|
LABEL,
|
|
|
|
ANNOTATION_USE_SITE,
|
|
|
|
ANNOTATION,
|
|
|
|
{
|
|
|
|
className: 'function',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'fun',
|
|
|
|
end: '[(]|$',
|
2023-12-18 13:12:25 +08:00
|
|
|
returnBegin: true,
|
|
|
|
excludeEnd: true,
|
|
|
|
keywords: KEYWORDS,
|
|
|
|
relevance: 5,
|
|
|
|
contains: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(',
|
|
|
|
returnBegin: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0,
|
2024-01-16 21:26:16 +08:00
|
|
|
contains: [ hljs.UNDERSCORE_TITLE_MODE ]
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'type',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /</,
|
|
|
|
end: />/,
|
|
|
|
keywords: 'reified',
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'params',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /\(/,
|
|
|
|
end: /\)/,
|
2023-12-18 13:12:25 +08:00
|
|
|
endsParent: true,
|
|
|
|
keywords: KEYWORDS,
|
|
|
|
relevance: 0,
|
|
|
|
contains: [
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /:/,
|
|
|
|
end: /[=,\/]/,
|
|
|
|
endsWithParent: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
KOTLIN_PAREN_TYPE,
|
2023-12-18 13:12:25 +08:00
|
|
|
hljs.C_LINE_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
KOTLIN_NESTED_COMMENT
|
2023-12-18 13:12:25 +08:00
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
hljs.C_LINE_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
KOTLIN_NESTED_COMMENT,
|
2023-12-18 13:12:25 +08:00
|
|
|
ANNOTATION_USE_SITE,
|
|
|
|
ANNOTATION,
|
|
|
|
STRING,
|
|
|
|
hljs.C_NUMBER_MODE
|
|
|
|
]
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
KOTLIN_NESTED_COMMENT
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'class',
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'class interface trait', // remove 'trait' when removed from KEYWORDS
|
|
|
|
end: /[:\{(]|$/,
|
2023-12-18 13:12:25 +08:00
|
|
|
excludeEnd: true,
|
|
|
|
illegal: 'extends implements',
|
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
beginKeywords: 'public protected internal private constructor'
|
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
hljs.UNDERSCORE_TITLE_MODE,
|
|
|
|
{
|
|
|
|
className: 'type',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /</,
|
|
|
|
end: />/,
|
|
|
|
excludeBegin: true,
|
|
|
|
excludeEnd: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
relevance: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'type',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: /[,:]\s*/,
|
|
|
|
end: /[<\(,]|$/,
|
|
|
|
excludeBegin: true,
|
|
|
|
returnEnd: true
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
ANNOTATION_USE_SITE,
|
|
|
|
ANNOTATION
|
|
|
|
]
|
|
|
|
},
|
|
|
|
STRING,
|
|
|
|
{
|
|
|
|
className: 'meta',
|
2024-01-16 21:26:16 +08:00
|
|
|
begin: "^#!/usr/bin/env",
|
|
|
|
end: '$',
|
2023-12-18 13:12:25 +08:00
|
|
|
illegal: '\n'
|
|
|
|
},
|
|
|
|
KOTLIN_NUMBER_MODE
|
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = kotlin;
|