UNPKG

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