]> luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/java.js
Initial commit.
[flow-web.git] / static / highlight / languages / java.js
1 /*! `java` 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: Java
43 Author: Vsevolod Solovyov <vsevolod.solovyov@gmail.com>
44 Category: common, enterprise
45 Website: https://www.java.com/
46 */
47
48
49 /**
50 * Allows recursive regex expressions to a given depth
51 *
52 * ie: recurRegex("(abc~~~)", /~~~/g, 2) becomes:
53 * (abc(abc(abc)))
54 *
55 * @param {string} re
56 * @param {RegExp} substitution (should be a g mode regex)
57 * @param {number} depth
58 * @returns {string}``
59 */
60 function recurRegex(re, substitution, depth) {
61 if (depth === -1) return "";
62
63 return re.replace(substitution, _ => {
64 return recurRegex(re, substitution, depth - 1);
65 });
66 }
67
68 /** @type LanguageFn */
69 function java(hljs) {
70 const regex = hljs.regex;
71 const JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
72 const GENERIC_IDENT_RE = JAVA_IDENT_RE
73 + recurRegex('(?:<' + JAVA_IDENT_RE + '~~~(?:\\s*,\\s*' + JAVA_IDENT_RE + '~~~)*>)?', /~~~/g, 2);
74 const MAIN_KEYWORDS = [
75 'synchronized',
76 'abstract',
77 'private',
78 'var',
79 'static',
80 'if',
81 'const ',
82 'for',
83 'while',
84 'strictfp',
85 'finally',
86 'protected',
87 'import',
88 'native',
89 'final',
90 'void',
91 'enum',
92 'else',
93 'break',
94 'transient',
95 'catch',
96 'instanceof',
97 'volatile',
98 'case',
99 'assert',
100 'package',
101 'default',
102 'public',
103 'try',
104 'switch',
105 'continue',
106 'throws',
107 'protected',
108 'public',
109 'private',
110 'module',
111 'requires',
112 'exports',
113 'do',
114 'sealed',
115 'yield',
116 'permits',
117 'goto',
118 'when'
119 ];
120
121 const BUILT_INS = [
122 'super',
123 'this'
124 ];
125
126 const LITERALS = [
127 'false',
128 'true',
129 'null'
130 ];
131
132 const TYPES = [
133 'char',
134 'boolean',
135 'long',
136 'float',
137 'int',
138 'byte',
139 'short',
140 'double'
141 ];
142
143 const KEYWORDS = {
144 keyword: MAIN_KEYWORDS,
145 literal: LITERALS,
146 type: TYPES,
147 built_in: BUILT_INS
148 };
149
150 const ANNOTATION = {
151 className: 'meta',
152 begin: '@' + JAVA_IDENT_RE,
153 contains: [
154 {
155 begin: /\(/,
156 end: /\)/,
157 contains: [ "self" ] // allow nested () inside our annotation
158 }
159 ]
160 };
161 const PARAMS = {
162 className: 'params',
163 begin: /\(/,
164 end: /\)/,
165 keywords: KEYWORDS,
166 relevance: 0,
167 contains: [ hljs.C_BLOCK_COMMENT_MODE ],
168 endsParent: true
169 };
170
171 return {
172 name: 'Java',
173 aliases: [ 'jsp' ],
174 keywords: KEYWORDS,
175 illegal: /<\/|#/,
176 contains: [
177 hljs.COMMENT(
178 '/\\*\\*',
179 '\\*/',
180 {
181 relevance: 0,
182 contains: [
183 {
184 // eat up @'s in emails to prevent them to be recognized as doctags
185 begin: /\w+@/,
186 relevance: 0
187 },
188 {
189 className: 'doctag',
190 begin: '@[A-Za-z]+'
191 }
192 ]
193 }
194 ),
195 // relevance boost
196 {
197 begin: /import java\.[a-z]+\./,
198 keywords: "import",
199 relevance: 2
200 },
201 hljs.C_LINE_COMMENT_MODE,
202 hljs.C_BLOCK_COMMENT_MODE,
203 {
204 begin: /"""/,
205 end: /"""/,
206 className: "string",
207 contains: [ hljs.BACKSLASH_ESCAPE ]
208 },
209 hljs.APOS_STRING_MODE,
210 hljs.QUOTE_STRING_MODE,
211 {
212 match: [
213 /\b(?:class|interface|enum|extends|implements|new)/,
214 /\s+/,
215 JAVA_IDENT_RE
216 ],
217 className: {
218 1: "keyword",
219 3: "title.class"
220 }
221 },
222 {
223 // Exceptions for hyphenated keywords
224 match: /non-sealed/,
225 scope: "keyword"
226 },
227 {
228 begin: [
229 regex.concat(/(?!else)/, JAVA_IDENT_RE),
230 /\s+/,
231 JAVA_IDENT_RE,
232 /\s+/,
233 /=(?!=)/
234 ],
235 className: {
236 1: "type",
237 3: "variable",
238 5: "operator"
239 }
240 },
241 {
242 begin: [
243 /record/,
244 /\s+/,
245 JAVA_IDENT_RE
246 ],
247 className: {
248 1: "keyword",
249 3: "title.class"
250 },
251 contains: [
252 PARAMS,
253 hljs.C_LINE_COMMENT_MODE,
254 hljs.C_BLOCK_COMMENT_MODE
255 ]
256 },
257 {
258 // Expression keywords prevent 'keyword Name(...)' from being
259 // recognized as a function definition
260 beginKeywords: 'new throw return else',
261 relevance: 0
262 },
263 {
264 begin: [
265 '(?:' + GENERIC_IDENT_RE + '\\s+)',
266 hljs.UNDERSCORE_IDENT_RE,
267 /\s*(?=\()/
268 ],
269 className: { 2: "title.function" },
270 keywords: KEYWORDS,
271 contains: [
272 {
273 className: 'params',
274 begin: /\(/,
275 end: /\)/,
276 keywords: KEYWORDS,
277 relevance: 0,
278 contains: [
279 ANNOTATION,
280 hljs.APOS_STRING_MODE,
281 hljs.QUOTE_STRING_MODE,
282 NUMERIC,
283 hljs.C_BLOCK_COMMENT_MODE
284 ]
285 },
286 hljs.C_LINE_COMMENT_MODE,
287 hljs.C_BLOCK_COMMENT_MODE
288 ]
289 },
290 NUMERIC,
291 ANNOTATION
292 ]
293 };
294 }
295
296 return java;
297
298 })();
299
300 hljs.registerLanguage('java', hljsGrammar);
301 })();