bash.js 6.1 KB

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