bash.js 4.0 KB

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