UNPKG

2.98 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|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 = /(^|[^\w.])(?:[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(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 and parameters
33 // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
34 pattern: RegExp(classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
35 lookbehind: true,
36 inside: className.inside
37 }
38 ],
39 'keyword': keywords,
40 'function': [
41 Prism.languages.clike.function,
42 {
43 pattern: /(::\s*)[a-z_]\w*/,
44 lookbehind: true
45 }
46 ],
47 '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,
48 'operator': {
49 pattern: /(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,
50 lookbehind: true
51 }
52 });
53
54 Prism.languages.insertBefore('java', 'string', {
55 'triple-quoted-string': {
56 // http://openjdk.java.net/jeps/355#Description
57 pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,
58 greedy: true,
59 alias: 'string'
60 },
61 'char': {
62 pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/,
63 greedy: true
64 }
65 });
66
67 Prism.languages.insertBefore('java', 'class-name', {
68 'annotation': {
69 pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
70 lookbehind: true,
71 alias: 'punctuation'
72 },
73 'generics': {
74 pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
75 inside: {
76 'class-name': className,
77 'keyword': keywords,
78 'punctuation': /[<>(),.:]/,
79 'operator': /[?&|]/
80 }
81 },
82 'namespace': {
83 pattern: RegExp(
84 /(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
85 .source.replace(/<keyword>/g, function () { return keywords.source; })),
86 lookbehind: true,
87 inside: {
88 'punctuation': /\./,
89 }
90 }
91 });
92}(Prism));