UNPKG

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