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