UNPKG

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