forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkotlin.js
250 lines (244 loc) · 6.22 KB
/
kotlin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Language: Kotlin
Description: Kotlin is an OSS statically typed programming language that targets the JVM, Android, JavaScript and Native.
Author: Sergey Mashkov <[email protected]>
Website: https://kotlinlang.org
Category: common
*/
import { NUMERIC } from "./lib/java.js";
export default function(hljs) {
const KEYWORDS = {
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 '
+ 'tailrec where const inner suspend typealias external expect actual',
built_in:
'Byte Short Char Int Long Boolean Float Double Void Unit Nothing',
literal:
'true false null'
};
const KEYWORDS_WITH_LABEL = {
className: 'keyword',
begin: /\b(break|continue|return|this)\b/,
starts: { contains: [
{
className: 'symbol',
begin: /@\w+/
}
] }
};
const LABEL = {
className: 'symbol',
begin: hljs.UNDERSCORE_IDENT_RE + '@'
};
// for string templates
const SUBST = {
className: 'subst',
begin: /\$\{/,
end: /\}/,
contains: [ hljs.C_NUMBER_MODE ]
};
const VARIABLE = {
className: 'variable',
begin: '\\$' + hljs.UNDERSCORE_IDENT_RE
};
const STRING = {
className: 'string',
variants: [
{
begin: '"""',
end: '"""(?=[^"])',
contains: [
VARIABLE,
SUBST
]
},
// 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.
{
begin: '\'',
end: '\'',
illegal: /\n/,
contains: [ hljs.BACKSLASH_ESCAPE ]
},
{
begin: '"',
end: '"',
illegal: /\n/,
contains: [
hljs.BACKSLASH_ESCAPE,
VARIABLE,
SUBST
]
}
]
};
SUBST.contains.push(STRING);
const ANNOTATION_USE_SITE = {
className: 'meta',
begin: '@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*' + hljs.UNDERSCORE_IDENT_RE + ')?'
};
const ANNOTATION = {
className: 'meta',
begin: '@' + hljs.UNDERSCORE_IDENT_RE,
contains: [
{
begin: /\(/,
end: /\)/,
contains: [
hljs.inherit(STRING, { className: 'string' }),
"self"
]
}
]
};
// 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
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
}
] };
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 ];
return {
name: 'Kotlin',
aliases: [
'kt',
'kts'
],
keywords: KEYWORDS,
contains: [
hljs.COMMENT(
'/\\*\\*',
'\\*/',
{
relevance: 0,
contains: [
{
className: 'doctag',
begin: '@[A-Za-z]+'
}
]
}
),
hljs.C_LINE_COMMENT_MODE,
KOTLIN_NESTED_COMMENT,
KEYWORDS_WITH_LABEL,
LABEL,
ANNOTATION_USE_SITE,
ANNOTATION,
{
className: 'function',
beginKeywords: 'fun',
end: '[(]|$',
returnBegin: true,
excludeEnd: true,
keywords: KEYWORDS,
relevance: 5,
contains: [
{
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(',
returnBegin: true,
relevance: 0,
contains: [ hljs.UNDERSCORE_TITLE_MODE ]
},
{
className: 'type',
begin: /</,
end: />/,
keywords: 'reified',
relevance: 0
},
{
className: 'params',
begin: /\(/,
end: /\)/,
endsParent: true,
keywords: KEYWORDS,
relevance: 0,
contains: [
{
begin: /:/,
end: /[=,\/]/,
endsWithParent: true,
contains: [
KOTLIN_PAREN_TYPE,
hljs.C_LINE_COMMENT_MODE,
KOTLIN_NESTED_COMMENT
],
relevance: 0
},
hljs.C_LINE_COMMENT_MODE,
KOTLIN_NESTED_COMMENT,
ANNOTATION_USE_SITE,
ANNOTATION,
STRING,
hljs.C_NUMBER_MODE
]
},
KOTLIN_NESTED_COMMENT
]
},
{
begin: [
/class|interface|trait/,
/\s+/,
hljs.UNDERSCORE_IDENT_RE
],
beginScope: {
3: "title.class"
},
keywords: 'class interface trait',
end: /[:\{(]|$/,
excludeEnd: true,
illegal: 'extends implements',
contains: [
{ beginKeywords: 'public protected internal private constructor' },
hljs.UNDERSCORE_TITLE_MODE,
{
className: 'type',
begin: /</,
end: />/,
excludeBegin: true,
excludeEnd: true,
relevance: 0
},
{
className: 'type',
begin: /[,:]\s*/,
end: /[<\(,){\s]|$/,
excludeBegin: true,
returnEnd: true
},
ANNOTATION_USE_SITE,
ANNOTATION
]
},
STRING,
{
className: 'meta',
begin: "^#!/usr/bin/env",
end: '$',
illegal: '\n'
},
KOTLIN_NUMBER_MODE
]
};
}