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