UNPKG

4.09 kBJavaScriptView Raw
1(function (Prism) {
2
3 var keywords = /\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record(?!\s*[(){}[\]<>=%~.:,;?+\-*/&|^])|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/;
4
5 // full package (optional) + parent classes (optional)
6 var classNamePrefix = /(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
7
8 // based on the java naming conventions
9 var className = {
10 pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
11 lookbehind: true,
12 inside: {
13 'namespace': {
14 pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
15 inside: {
16 'punctuation': /\./
17 }
18 },
19 'punctuation': /\./
20 }
21 };
22
23 Prism.languages.java = Prism.languages.extend('clike', {
24 'string': {
25 pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"/,
26 lookbehind: true,
27 greedy: true
28 },
29 'class-name': [
30 className,
31 {
32 // variables, parameters, and constructor references
33 // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
34 pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source),
35 lookbehind: true,
36 inside: className.inside
37 },
38 {
39 // class names based on keyword
40 // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
41 pattern: RegExp(/(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/.source + classNamePrefix + /[A-Z]\w*\b/.source),
42 lookbehind: true,
43 inside: className.inside
44 }
45 ],
46 'keyword': keywords,
47 'function': [
48 Prism.languages.clike.function,
49 {
50 pattern: /(::\s*)[a-z_]\w*/,
51 lookbehind: true
52 }
53 ],
54 'number': /\b0b[01][01_]*L?\b|\b0x(?:\.[\da-f_p+-]+|[\da-f_]+(?:\.[\da-f_p+-]+)?)\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i,
55 'operator': {
56 pattern: /(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,
57 lookbehind: true
58 }
59 });
60
61 Prism.languages.insertBefore('java', 'string', {
62 'triple-quoted-string': {
63 // http://openjdk.java.net/jeps/355#Description
64 pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,
65 greedy: true,
66 alias: 'string'
67 },
68 'char': {
69 pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/,
70 greedy: true
71 }
72 });
73
74 Prism.languages.insertBefore('java', 'class-name', {
75 'annotation': {
76 pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
77 lookbehind: true,
78 alias: 'punctuation'
79 },
80 'generics': {
81 pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
82 inside: {
83 'class-name': className,
84 'keyword': keywords,
85 'punctuation': /[<>(),.:]/,
86 'operator': /[?&|]/
87 }
88 },
89 'import': [
90 {
91 pattern: RegExp(/(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
92 lookbehind: true,
93 inside: {
94 'namespace': className.inside.namespace,
95 'punctuation': /\./,
96 'operator': /\*/,
97 'class-name': /\w+/
98 }
99 },
100 {
101 pattern: RegExp(/(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
102 lookbehind: true,
103 alias: 'static',
104 inside: {
105 'namespace': className.inside.namespace,
106 'static': /\b\w+$/,
107 'punctuation': /\./,
108 'operator': /\*/,
109 'class-name': /\w+/
110 }
111 }
112 ],
113 'namespace': {
114 pattern: RegExp(
115 /(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
116 .source.replace(/<keyword>/g, function () { return keywords.source; })),
117 lookbehind: true,
118 inside: {
119 'punctuation': /\./,
120 }
121 }
122 });
123}(Prism));