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