bash.js
1 /*! `bash` grammar compiled for Highlight.js 11.10.0 */ 2 (function(){ 3 var hljsGrammar = (function () { 4 'use strict'; 5 6 /* 7 Language: Bash 8 Author: vah <vahtenberg@gmail.com> 9 Contributrors: Benjamin Pannell <contact@sierrasoftworks.com> 10 Website: https://www.gnu.org/software/bash/ 11 Category: common, scripting 12 */ 13 14 /** @type LanguageFn */ 15 function bash(hljs) { 16 const regex = hljs.regex; 17 const VAR = {}; 18 const BRACED_VAR = { 19 begin: /\$\{/, 20 end: /\}/, 21 contains: [ 22 "self", 23 { 24 begin: /:-/, 25 contains: [ VAR ] 26 } // default values 27 ] 28 }; 29 Object.assign(VAR, { 30 className: 'variable', 31 variants: [ 32 { begin: regex.concat(/\$[\w\d#@][\w\d_]*/, 33 // negative look-ahead tries to avoid matching patterns that are not 34 // Perl at all like $ident$, @ident@, etc. 35 `(?![\\w\\d])(?![$])`) }, 36 BRACED_VAR 37 ] 38 }); 39 40 const SUBST = { 41 className: 'subst', 42 begin: /\$\(/, 43 end: /\)/, 44 contains: [ hljs.BACKSLASH_ESCAPE ] 45 }; 46 const COMMENT = hljs.inherit( 47 hljs.COMMENT(), 48 { 49 match: [ 50 /(^|\s)/, 51 /#.*$/ 52 ], 53 scope: { 54 2: 'comment' 55 } 56 } 57 ); 58 const HERE_DOC = { 59 begin: /<<-?\s*(?=\w+)/, 60 starts: { contains: [ 61 hljs.END_SAME_AS_BEGIN({ 62 begin: /(\w+)/, 63 end: /(\w+)/, 64 className: 'string' 65 }) 66 ] } 67 }; 68 const QUOTE_STRING = { 69 className: 'string', 70 begin: /"/, 71 end: /"/, 72 contains: [ 73 hljs.BACKSLASH_ESCAPE, 74 VAR, 75 SUBST 76 ] 77 }; 78 SUBST.contains.push(QUOTE_STRING); 79 const ESCAPED_QUOTE = { 80 match: /\\"/ 81 }; 82 const APOS_STRING = { 83 className: 'string', 84 begin: /'/, 85 end: /'/ 86 }; 87 const ESCAPED_APOS = { 88 match: /\\'/ 89 }; 90 const ARITHMETIC = { 91 begin: /\$?\(\(/, 92 end: /\)\)/, 93 contains: [ 94 { 95 begin: /\d+#[0-9a-f]+/, 96 className: "number" 97 }, 98 hljs.NUMBER_MODE, 99 VAR 100 ] 101 }; 102 const SH_LIKE_SHELLS = [ 103 "fish", 104 "bash", 105 "zsh", 106 "sh", 107 "csh", 108 "ksh", 109 "tcsh", 110 "dash", 111 "scsh", 112 ]; 113 const KNOWN_SHEBANG = hljs.SHEBANG({ 114 binary: `(${SH_LIKE_SHELLS.join("|")})`, 115 relevance: 10 116 }); 117 const FUNCTION = { 118 className: 'function', 119 begin: /\w[\w\d_]*\s*\(\s*\)\s*\{/, 120 returnBegin: true, 121 contains: [ hljs.inherit(hljs.TITLE_MODE, { begin: /\w[\w\d_]*/ }) ], 122 relevance: 0 123 }; 124 125 const KEYWORDS = [ 126 "if", 127 "then", 128 "else", 129 "elif", 130 "fi", 131 "for", 132 "while", 133 "until", 134 "in", 135 "do", 136 "done", 137 "case", 138 "esac", 139 "function", 140 "select" 141 ]; 142 143 const LITERALS = [ 144 "true", 145 "false" 146 ]; 147 148 // to consume paths to prevent keyword matches inside them 149 const PATH_MODE = { match: /(\/[a-z._-]+)+/ }; 150 151 // http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html 152 const SHELL_BUILT_INS = [ 153 "break", 154 "cd", 155 "continue", 156 "eval", 157 "exec", 158 "exit", 159 "export", 160 "getopts", 161 "hash", 162 "pwd", 163 "readonly", 164 "return", 165 "shift", 166 "test", 167 "times", 168 "trap", 169 "umask", 170 "unset" 171 ]; 172 173 const BASH_BUILT_INS = [ 174 "alias", 175 "bind", 176 "builtin", 177 "caller", 178 "command", 179 "declare", 180 "echo", 181 "enable", 182 "help", 183 "let", 184 "local", 185 "logout", 186 "mapfile", 187 "printf", 188 "read", 189 "readarray", 190 "source", 191 "sudo", 192 "type", 193 "typeset", 194 "ulimit", 195 "unalias" 196 ]; 197 198 const ZSH_BUILT_INS = [ 199 "autoload", 200 "bg", 201 "bindkey", 202 "bye", 203 "cap", 204 "chdir", 205 "clone", 206 "comparguments", 207 "compcall", 208 "compctl", 209 "compdescribe", 210 "compfiles", 211 "compgroups", 212 "compquote", 213 "comptags", 214 "comptry", 215 "compvalues", 216 "dirs", 217 "disable", 218 "disown", 219 "echotc", 220 "echoti", 221 "emulate", 222 "fc", 223 "fg", 224 "float", 225 "functions", 226 "getcap", 227 "getln", 228 "history", 229 "integer", 230 "jobs", 231 "kill", 232 "limit", 233 "log", 234 "noglob", 235 "popd", 236 "print", 237 "pushd", 238 "pushln", 239 "rehash", 240 "sched", 241 "setcap", 242 "setopt", 243 "stat", 244 "suspend", 245 "ttyctl", 246 "unfunction", 247 "unhash", 248 "unlimit", 249 "unsetopt", 250 "vared", 251 "wait", 252 "whence", 253 "where", 254 "which", 255 "zcompile", 256 "zformat", 257 "zftp", 258 "zle", 259 "zmodload", 260 "zparseopts", 261 "zprof", 262 "zpty", 263 "zregexparse", 264 "zsocket", 265 "zstyle", 266 "ztcp" 267 ]; 268 269 const GNU_CORE_UTILS = [ 270 "chcon", 271 "chgrp", 272 "chown", 273 "chmod", 274 "cp", 275 "dd", 276 "df", 277 "dir", 278 "dircolors", 279 "ln", 280 "ls", 281 "mkdir", 282 "mkfifo", 283 "mknod", 284 "mktemp", 285 "mv", 286 "realpath", 287 "rm", 288 "rmdir", 289 "shred", 290 "sync", 291 "touch", 292 "truncate", 293 "vdir", 294 "b2sum", 295 "base32", 296 "base64", 297 "cat", 298 "cksum", 299 "comm", 300 "csplit", 301 "cut", 302 "expand", 303 "fmt", 304 "fold", 305 "head", 306 "join", 307 "md5sum", 308 "nl", 309 "numfmt", 310 "od", 311 "paste", 312 "ptx", 313 "pr", 314 "sha1sum", 315 "sha224sum", 316 "sha256sum", 317 "sha384sum", 318 "sha512sum", 319 "shuf", 320 "sort", 321 "split", 322 "sum", 323 "tac", 324 "tail", 325 "tr", 326 "tsort", 327 "unexpand", 328 "uniq", 329 "wc", 330 "arch", 331 "basename", 332 "chroot", 333 "date", 334 "dirname", 335 "du", 336 "echo", 337 "env", 338 "expr", 339 "factor", 340 // "false", // keyword literal already 341 "groups", 342 "hostid", 343 "id", 344 "link", 345 "logname", 346 "nice", 347 "nohup", 348 "nproc", 349 "pathchk", 350 "pinky", 351 "printenv", 352 "printf", 353 "pwd", 354 "readlink", 355 "runcon", 356 "seq", 357 "sleep", 358 "stat", 359 "stdbuf", 360 "stty", 361 "tee", 362 "test", 363 "timeout", 364 // "true", // keyword literal already 365 "tty", 366 "uname", 367 "unlink", 368 "uptime", 369 "users", 370 "who", 371 "whoami", 372 "yes" 373 ]; 374 375 return { 376 name: 'Bash', 377 aliases: [ 378 'sh', 379 'zsh' 380 ], 381 keywords: { 382 $pattern: /\b[a-z][a-z0-9._-]+\b/, 383 keyword: KEYWORDS, 384 literal: LITERALS, 385 built_in: [ 386 ...SHELL_BUILT_INS, 387 ...BASH_BUILT_INS, 388 // Shell modifiers 389 "set", 390 "shopt", 391 ...ZSH_BUILT_INS, 392 ...GNU_CORE_UTILS 393 ] 394 }, 395 contains: [ 396 KNOWN_SHEBANG, // to catch known shells and boost relevancy 397 hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang 398 FUNCTION, 399 ARITHMETIC, 400 COMMENT, 401 HERE_DOC, 402 PATH_MODE, 403 QUOTE_STRING, 404 ESCAPED_QUOTE, 405 APOS_STRING, 406 ESCAPED_APOS, 407 VAR 408 ] 409 }; 410 } 411 412 return bash; 413 414 })(); 415 416 hljs.registerLanguage('bash', hljsGrammar); 417 })();