bash.js 6.4 KB

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