bash.js 6.3 KB

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