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