bash.js 3.1 KB

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