2024-01-16 21:26:16 +08:00
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @returns {RegExp}
|
|
|
|
* */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {RegExp | string } re
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function source(re) {
|
|
|
|
if (!re) return null;
|
|
|
|
if (typeof re === "string") return re;
|
|
|
|
|
|
|
|
return re.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {...(RegExp | string) } args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function concat(...args) {
|
|
|
|
const joined = args.map((x) => source(x)).join("");
|
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Language: Bash
|
|
|
|
Author: vah <vahtenberg@gmail.com>
|
|
|
|
Contributrors: Benjamin Pannell <contact@sierrasoftworks.com>
|
|
|
|
Website: https://www.gnu.org/software/bash/
|
|
|
|
Category: common
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type LanguageFn */
|
|
|
|
function bash(hljs) {
|
|
|
|
const VAR = {};
|
|
|
|
const BRACED_VAR = {
|
|
|
|
begin: /\$\{/,
|
|
|
|
end:/\}/,
|
|
|
|
contains: [
|
|
|
|
"self",
|
|
|
|
{
|
|
|
|
begin: /:-/,
|
|
|
|
contains: [ VAR ]
|
|
|
|
} // default values
|
|
|
|
]
|
|
|
|
};
|
|
|
|
Object.assign(VAR,{
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'variable',
|
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{begin: concat(/\$[\w\d#@][\w\d_]*/,
|
|
|
|
// negative look-ahead tries to avoid matching patterns that are not
|
|
|
|
// Perl at all like $ident$, @ident@, etc.
|
|
|
|
`(?![\\w\\d])(?![$])`) },
|
|
|
|
BRACED_VAR
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
2024-01-16 21:26:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const SUBST = {
|
|
|
|
className: 'subst',
|
|
|
|
begin: /\$\(/, end: /\)/,
|
|
|
|
contains: [hljs.BACKSLASH_ESCAPE]
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const HERE_DOC = {
|
|
|
|
begin: /<<-?\s*(?=\w+)/,
|
|
|
|
starts: {
|
|
|
|
contains: [
|
|
|
|
hljs.END_SAME_AS_BEGIN({
|
|
|
|
begin: /(\w+)/,
|
|
|
|
end: /(\w+)/,
|
|
|
|
className: 'string'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const QUOTE_STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
|
|
|
begin: /"/, end: /"/,
|
|
|
|
contains: [
|
|
|
|
hljs.BACKSLASH_ESCAPE,
|
|
|
|
VAR,
|
2024-01-16 21:26:16 +08:00
|
|
|
SUBST
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
SUBST.contains.push(QUOTE_STRING);
|
|
|
|
const ESCAPED_QUOTE = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: '',
|
|
|
|
begin: /\\"/
|
|
|
|
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const APOS_STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
|
|
|
begin: /'/, end: /'/
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const ARITHMETIC = {
|
|
|
|
begin: /\$\(\(/,
|
|
|
|
end: /\)\)/,
|
|
|
|
contains: [
|
|
|
|
{ begin: /\d+#[0-9a-f]+/, className: "number" },
|
|
|
|
hljs.NUMBER_MODE,
|
|
|
|
VAR
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const SH_LIKE_SHELLS = [
|
|
|
|
"fish",
|
|
|
|
"bash",
|
|
|
|
"zsh",
|
|
|
|
"sh",
|
|
|
|
"csh",
|
|
|
|
"ksh",
|
|
|
|
"tcsh",
|
|
|
|
"dash",
|
|
|
|
"scsh",
|
|
|
|
];
|
|
|
|
const KNOWN_SHEBANG = hljs.SHEBANG({
|
|
|
|
binary: `(${SH_LIKE_SHELLS.join("|")})`,
|
|
|
|
relevance: 10
|
|
|
|
});
|
|
|
|
const FUNCTION = {
|
|
|
|
className: 'function',
|
|
|
|
begin: /\w[\w\d_]*\s*\(\s*\)\s*\{/,
|
|
|
|
returnBegin: true,
|
|
|
|
contains: [hljs.inherit(hljs.TITLE_MODE, {begin: /\w[\w\d_]*/})],
|
|
|
|
relevance: 0
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
return {
|
2024-01-16 21:26:16 +08:00
|
|
|
name: 'Bash',
|
2023-12-18 13:12:25 +08:00
|
|
|
aliases: ['sh', 'zsh'],
|
|
|
|
keywords: {
|
2024-01-16 21:26:16 +08:00
|
|
|
$pattern: /\b[a-z._-]+\b/,
|
2023-12-18 13:12:25 +08:00
|
|
|
keyword:
|
|
|
|
'if then else elif fi for while in do done case esac function',
|
|
|
|
literal:
|
|
|
|
'true false',
|
|
|
|
built_in:
|
|
|
|
// Shell built-ins
|
|
|
|
// http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
|
|
|
|
'break cd continue eval exec exit export getopts hash pwd readonly return shift test times ' +
|
|
|
|
'trap umask unset ' +
|
|
|
|
// Bash built-ins
|
|
|
|
'alias bind builtin caller command declare echo enable help let local logout mapfile printf ' +
|
|
|
|
'read readarray source type typeset ulimit unalias ' +
|
|
|
|
// Shell modifiers
|
|
|
|
'set shopt ' +
|
|
|
|
// Zsh built-ins
|
|
|
|
'autoload bg bindkey bye cap chdir clone comparguments compcall compctl compdescribe compfiles ' +
|
|
|
|
'compgroups compquote comptags comptry compvalues dirs disable disown echotc echoti emulate ' +
|
|
|
|
'fc fg float functions getcap getln history integer jobs kill limit log noglob popd print ' +
|
|
|
|
'pushd pushln rehash sched setcap setopt stat suspend ttyctl unfunction unhash unlimit ' +
|
|
|
|
'unsetopt vared wait whence where which zcompile zformat zftp zle zmodload zparseopts zprof ' +
|
2024-01-16 21:26:16 +08:00
|
|
|
'zpty zregexparse zsocket zstyle ztcp'
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
KNOWN_SHEBANG, // to catch known shells and boost relevancy
|
|
|
|
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
|
|
|
|
FUNCTION,
|
|
|
|
ARITHMETIC,
|
2023-12-18 13:12:25 +08:00
|
|
|
hljs.HASH_COMMENT_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
HERE_DOC,
|
2023-12-18 13:12:25 +08:00
|
|
|
QUOTE_STRING,
|
|
|
|
ESCAPED_QUOTE,
|
|
|
|
APOS_STRING,
|
|
|
|
VAR
|
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = bash;
|