bash.js (3335B)
1 /* 2 Language: Bash 3 Author: vah <vahtenberg@gmail.com> 4 Contributrors: Benjamin Pannell <contact@sierrasoftworks.com> 5 Website: https://www.gnu.org/software/bash/ 6 Category: common 7 */ 8 9 /** @type LanguageFn */ 10 function bash(hljs) { 11 const VAR = {}; 12 const BRACED_VAR = { 13 begin: /\$\{/, end:/\}/, 14 contains: [ 15 { begin: /:-/, contains: [VAR] } // default values 16 ] 17 }; 18 Object.assign(VAR,{ 19 className: 'variable', 20 variants: [ 21 {begin: /\$[\w\d#@][\w\d_]*/}, 22 BRACED_VAR 23 ] 24 }); 25 26 const SUBST = { 27 className: 'subst', 28 begin: /\$\(/, end: /\)/, 29 contains: [hljs.BACKSLASH_ESCAPE] 30 }; 31 const QUOTE_STRING = { 32 className: 'string', 33 begin: /"/, end: /"/, 34 contains: [ 35 hljs.BACKSLASH_ESCAPE, 36 VAR, 37 SUBST 38 ] 39 }; 40 SUBST.contains.push(QUOTE_STRING); 41 const ESCAPED_QUOTE = { 42 className: '', 43 begin: /\\"/ 44 45 }; 46 const APOS_STRING = { 47 className: 'string', 48 begin: /'/, end: /'/ 49 }; 50 const ARITHMETIC = { 51 begin: /\$\(\(/, 52 end: /\)\)/, 53 contains: [ 54 { begin: /\d+#[0-9a-f]+/, className: "number" }, 55 hljs.NUMBER_MODE, 56 VAR 57 ] 58 }; 59 const SH_LIKE_SHELLS = [ 60 "fish", 61 "bash", 62 "zsh", 63 "sh", 64 "csh", 65 "ksh", 66 "tcsh", 67 "dash", 68 "scsh", 69 ]; 70 const KNOWN_SHEBANG = hljs.SHEBANG({ 71 binary: `(${SH_LIKE_SHELLS.join("|")})`, 72 relevance: 10 73 }); 74 const FUNCTION = { 75 className: 'function', 76 begin: /\w[\w\d_]*\s*\(\s*\)\s*\{/, 77 returnBegin: true, 78 contains: [hljs.inherit(hljs.TITLE_MODE, {begin: /\w[\w\d_]*/})], 79 relevance: 0 80 }; 81 82 return { 83 name: 'Bash', 84 aliases: ['sh', 'zsh'], 85 keywords: { 86 $pattern: /\b-?[a-z\._-]+\b/, 87 keyword: 88 'if then else elif fi for while in do done case esac function', 89 literal: 90 'true false', 91 built_in: 92 // Shell built-ins 93 // http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html 94 'break cd continue eval exec exit export getopts hash pwd readonly return shift test times ' + 95 'trap umask unset ' + 96 // Bash built-ins 97 'alias bind builtin caller command declare echo enable help let local logout mapfile printf ' + 98 'read readarray source type typeset ulimit unalias ' + 99 // Shell modifiers 100 'set shopt ' + 101 // Zsh built-ins 102 'autoload bg bindkey bye cap chdir clone comparguments compcall compctl compdescribe compfiles ' + 103 'compgroups compquote comptags comptry compvalues dirs disable disown echotc echoti emulate ' + 104 'fc fg float functions getcap getln history integer jobs kill limit log noglob popd print ' + 105 'pushd pushln rehash sched setcap setopt stat suspend ttyctl unfunction unhash unlimit ' + 106 'unsetopt vared wait whence where which zcompile zformat zftp zle zmodload zparseopts zprof ' + 107 'zpty zregexparse zsocket zstyle ztcp', 108 _: 109 '-ne -eq -lt -gt -f -d -e -s -l -a' // relevance booster 110 }, 111 contains: [ 112 KNOWN_SHEBANG, // to catch known shells and boost relevancy 113 hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang 114 FUNCTION, 115 ARITHMETIC, 116 hljs.HASH_COMMENT_MODE, 117 QUOTE_STRING, 118 ESCAPED_QUOTE, 119 APOS_STRING, 120 VAR 121 ] 122 }; 123 } 124 125 module.exports = bash;