bash.js 3.3 KB

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