UNPKG

4.13 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 'constant': /\b[A-Z][A-Z_\d]+\b/
60 });
61
62 Prism.languages.insertBefore('java', 'string', {
63 'triple-quoted-string': {
64 // http://openjdk.java.net/jeps/355#Description
65 pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,
66 greedy: true,
67 alias: 'string'
68 },
69 'char': {
70 pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/,
71 greedy: true
72 }
73 });
74
75 Prism.languages.insertBefore('java', 'class-name', {
76 'annotation': {
77 pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
78 lookbehind: true,
79 alias: 'punctuation'
80 },
81 'generics': {
82 pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
83 inside: {
84 'class-name': className,
85 'keyword': keywords,
86 'punctuation': /[<>(),.:]/,
87 'operator': /[?&|]/
88 }
89 },
90 'import': [
91 {
92 pattern: RegExp(/(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
93 lookbehind: true,
94 inside: {
95 'namespace': className.inside.namespace,
96 'punctuation': /\./,
97 'operator': /\*/,
98 'class-name': /\w+/
99 }
100 },
101 {
102 pattern: RegExp(/(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
103 lookbehind: true,
104 alias: 'static',
105 inside: {
106 'namespace': className.inside.namespace,
107 'static': /\b\w+$/,
108 'punctuation': /\./,
109 'operator': /\*/,
110 'class-name': /\w+/
111 }
112 }
113 ],
114 'namespace': {
115 pattern: RegExp(
116 /(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
117 .source.replace(/<keyword>/g, function () { return keywords.source; })),
118 lookbehind: true,
119 inside: {
120 'punctuation': /\./,
121 }
122 }
123 });
124}(Prism));