import { toArray } from '../util/iterables.js'; import clike from './clike.js'; /** @type {import('../types.d.ts').LanguageProto<'java'>} */ export default { id: 'java', base: clike, grammar ({ base }) { const 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/; // full package (optional) + parent classes (optional) const classNamePrefix = /(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source; // based on the java naming conventions const className = { pattern: RegExp( /(^|[^\w.])/.source + classNamePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source ), lookbehind: true, inside: { 'namespace': { pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/, inside: { 'punctuation': /\./, }, }, 'punctuation': /\./, }, }; return { 'string': { pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"/, lookbehind: true, greedy: true, }, 'class-name': [ className, { // variables, parameters, and constructor references // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods) pattern: RegExp( /(^|[^\w.])/.source + classNamePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source ), lookbehind: true, inside: className.inside, }, { // class names based on keyword // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods) pattern: RegExp( /(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/ .source + classNamePrefix + /[A-Z]\w*\b/.source ), lookbehind: true, inside: className.inside, }, ], 'keyword': keywords, 'function': [ ...toArray(/** @type {import('../types.d.ts').GrammarTokens} */ (base).function), { pattern: /(::\s*)[a-z_]\w*/, lookbehind: true, }, ], '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, 'operator': { pattern: /(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m, lookbehind: true, }, 'constant': /\b[A-Z][A-Z_\d]+\b/, $insertBefore: { 'comment': { 'doc-comment': { pattern: /\/\*\*(?!\/)[\s\S]*?(?:\*\/|$)/, greedy: true, alias: 'comment', inside: 'javadoc', }, }, 'string': { 'triple-quoted-string': { // http://openjdk.java.net/jeps/355#Description pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/, greedy: true, alias: 'string', }, 'char': { pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/, greedy: true, }, }, 'class-name': { 'annotation': { pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/, lookbehind: true, alias: 'punctuation', }, 'generics': { pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/, inside: { 'class-name': className, 'keyword': keywords, 'punctuation': /[<>(),.:]/, 'operator': /[?&|]/, }, }, 'import': [ { pattern: RegExp( /(\bimport\s+)/.source + classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source ), lookbehind: true, inside: { 'namespace': className.inside.namespace, 'punctuation': /\./, 'operator': /\*/, 'class-name': /\w+/, }, }, { pattern: RegExp( /(\bimport\s+static\s+)/.source + classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source ), lookbehind: true, alias: 'static', inside: { 'namespace': className.inside.namespace, 'static': /\b\w+$/, 'punctuation': /\./, 'operator': /\*/, 'class-name': /\w+/, }, }, ], 'namespace': { pattern: RegExp( /(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!)[a-z]\w*(?:\.[a-z]\w*)*\.?/.source.replace( //g, () => keywords.source ) ), lookbehind: true, inside: { 'punctuation': /\./, }, }, }, }, }; }, };