bash.js 3.3 KB

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