123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /*
- Language: Bash
- Author: vah <vahtenberg@gmail.com>
- Contributrors: Benjamin Pannell <contact@sierrasoftworks.com>
- Website: https://www.gnu.org/software/bash/
- Category: common, scripting
- */
- /** @type LanguageFn */
- function bash(hljs) {
- const regex = hljs.regex;
- const VAR = {};
- const BRACED_VAR = {
- begin: /\$\{/,
- end: /\}/,
- contains: [
- "self",
- {
- begin: /:-/,
- contains: [ VAR ]
- } // default values
- ]
- };
- Object.assign(VAR, {
- className: 'variable',
- variants: [
- { begin: regex.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
- ]
- });
- const SUBST = {
- className: 'subst',
- begin: /\$\(/,
- end: /\)/,
- contains: [ hljs.BACKSLASH_ESCAPE ]
- };
- const COMMENT = hljs.inherit(
- hljs.COMMENT(),
- {
- match: [
- /(^|\s)/,
- /#.*$/
- ],
- scope: {
- 2: 'comment'
- }
- }
- );
- const HERE_DOC = {
- begin: /<<-?\s*(?=\w+)/,
- starts: { contains: [
- hljs.END_SAME_AS_BEGIN({
- begin: /(\w+)/,
- end: /(\w+)/,
- className: 'string'
- })
- ] }
- };
- const QUOTE_STRING = {
- className: 'string',
- begin: /"/,
- end: /"/,
- contains: [
- hljs.BACKSLASH_ESCAPE,
- VAR,
- SUBST
- ]
- };
- SUBST.contains.push(QUOTE_STRING);
- const ESCAPED_QUOTE = {
- match: /\\"/
- };
- const APOS_STRING = {
- className: 'string',
- begin: /'/,
- end: /'/
- };
- const ESCAPED_APOS = {
- match: /\\'/
- };
- 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
- };
- const KEYWORDS = [
- "if",
- "then",
- "else",
- "elif",
- "fi",
- "for",
- "while",
- "until",
- "in",
- "do",
- "done",
- "case",
- "esac",
- "function",
- "select"
- ];
- const LITERALS = [
- "true",
- "false"
- ];
- // to consume paths to prevent keyword matches inside them
- const PATH_MODE = { match: /(\/[a-z._-]+)+/ };
- // http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
- const SHELL_BUILT_INS = [
- "break",
- "cd",
- "continue",
- "eval",
- "exec",
- "exit",
- "export",
- "getopts",
- "hash",
- "pwd",
- "readonly",
- "return",
- "shift",
- "test",
- "times",
- "trap",
- "umask",
- "unset"
- ];
- const BASH_BUILT_INS = [
- "alias",
- "bind",
- "builtin",
- "caller",
- "command",
- "declare",
- "echo",
- "enable",
- "help",
- "let",
- "local",
- "logout",
- "mapfile",
- "printf",
- "read",
- "readarray",
- "source",
- "sudo",
- "type",
- "typeset",
- "ulimit",
- "unalias"
- ];
- const 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",
- "zpty",
- "zregexparse",
- "zsocket",
- "zstyle",
- "ztcp"
- ];
- const GNU_CORE_UTILS = [
- "chcon",
- "chgrp",
- "chown",
- "chmod",
- "cp",
- "dd",
- "df",
- "dir",
- "dircolors",
- "ln",
- "ls",
- "mkdir",
- "mkfifo",
- "mknod",
- "mktemp",
- "mv",
- "realpath",
- "rm",
- "rmdir",
- "shred",
- "sync",
- "touch",
- "truncate",
- "vdir",
- "b2sum",
- "base32",
- "base64",
- "cat",
- "cksum",
- "comm",
- "csplit",
- "cut",
- "expand",
- "fmt",
- "fold",
- "head",
- "join",
- "md5sum",
- "nl",
- "numfmt",
- "od",
- "paste",
- "ptx",
- "pr",
- "sha1sum",
- "sha224sum",
- "sha256sum",
- "sha384sum",
- "sha512sum",
- "shuf",
- "sort",
- "split",
- "sum",
- "tac",
- "tail",
- "tr",
- "tsort",
- "unexpand",
- "uniq",
- "wc",
- "arch",
- "basename",
- "chroot",
- "date",
- "dirname",
- "du",
- "echo",
- "env",
- "expr",
- "factor",
- // "false", // keyword literal already
- "groups",
- "hostid",
- "id",
- "link",
- "logname",
- "nice",
- "nohup",
- "nproc",
- "pathchk",
- "pinky",
- "printenv",
- "printf",
- "pwd",
- "readlink",
- "runcon",
- "seq",
- "sleep",
- "stat",
- "stdbuf",
- "stty",
- "tee",
- "test",
- "timeout",
- // "true", // keyword literal already
- "tty",
- "uname",
- "unlink",
- "uptime",
- "users",
- "who",
- "whoami",
- "yes"
- ];
- return {
- name: 'Bash',
- aliases: [
- 'sh',
- 'zsh'
- ],
- keywords: {
- $pattern: /\b[a-z][a-z0-9._-]+\b/,
- keyword: KEYWORDS,
- literal: LITERALS,
- built_in: [
- ...SHELL_BUILT_INS,
- ...BASH_BUILT_INS,
- // Shell modifiers
- "set",
- "shopt",
- ...ZSH_BUILT_INS,
- ...GNU_CORE_UTILS
- ]
- },
- contains: [
- KNOWN_SHEBANG, // to catch known shells and boost relevancy
- hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
- FUNCTION,
- ARITHMETIC,
- COMMENT,
- HERE_DOC,
- PATH_MODE,
- QUOTE_STRING,
- ESCAPED_QUOTE,
- APOS_STRING,
- ESCAPED_APOS,
- VAR
- ]
- };
- }
- export { bash as default };
|