Skip to content

Commit

Permalink
enh(bash) added gnu core utilities (#3344)
Browse files Browse the repository at this point in the history
* enh(bash) add gnu core utilities

Co-authored-by: Josh Goebel <me@joshgoebel.com>

* prevent false positive on paths
* just forget about install for now, far too common an argument
* (chore) reorganization keywords
  • Loading branch information
katzeprior committed Oct 5, 2021
1 parent 934f2e7 commit 7d10f7f
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -24,6 +24,7 @@ Grammars:
- enh(scala) add `inline` soft keyword (#3329) [Nicolas Stucki][]
- enh(scala) add `using` soft keyword (#3330) [Nicolas Stucki][]
- enh(fsharp) added `f#` alias (#3337) [Bahnschrift][]
- enh(bash) added gnu core utilities (#3342) [katzeprior][]

[Austin Schick]: https://github.com/austin-schick
[Josh Goebel]: https://github.com/joshgoebel
Expand All @@ -32,6 +33,7 @@ Grammars:
[Teletha]: https://github.com/teletha
[Nicolas Stucki]: https://github.com/nicolasstucki
[Bahnschrift]: https://github.com/Bahnschrift
[katzeprior]: https://github.com/katzeprior


## Version 11.2.0
Expand Down
253 changes: 237 additions & 16 deletions src/languages/bash.js
Expand Up @@ -122,30 +122,250 @@ export default function(hljs) {
"false"
];

// to consume paths to prevent keyword matches inside them
const PATH_MODE = {
match: /(\/[a-z._-]+)+/
};

// http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
const SHELL_BUILT_INS = [
"break",
"cd",
"continue",
"eval",
"exec",
"exit",
"export",
"getopts",
"hash",
"pwd",
"readonly",
"return",
"shift",
"test",
"times",
"trap",
"umask",
"unset"
];

const BASH_BUILT_INS = [
"alias",
"bind",
"builtin",
"caller",
"command",
"declare",
"echo",
"enable",
"help",
"let",
"local",
"logout",
"mapfile",
"printf",
"read",
"readarray",
"source",
"type",
"typeset",
"ulimit",
"unalias"
];

const ZSH_BUILT_INS = [
"autoload",
"bg",
"bindkey",
"bye",
"cap",
"chdir",
"clone",
"comparguments",
"compcall",
"compctl",
"compdescribe",
"compfiles",
"compgroups",
"compquote",
"comptags",
"comptry",
"compvalues",
"dirs",
"disable",
"disown",
"echotc",
"echoti",
"emulate",
"fc",
"fg",
"float",
"functions",
"getcap",
"getln",
"history",
"integer",
"jobs",
"kill",
"limit",
"log",
"noglob",
"popd",
"print",
"pushd",
"pushln",
"rehash",
"sched",
"setcap",
"setopt",
"stat",
"suspend",
"ttyctl",
"unfunction",
"unhash",
"unlimit",
"unsetopt",
"vared",
"wait",
"whence",
"where",
"which",
"zcompile",
"zformat",
"zftp",
"zle",
"zmodload",
"zparseopts",
"zprof",
"zpty",
"zregexparse",
"zsocket",
"zstyle",
"ztcp"
];

const GNU_CORE_UTILS = [
"chcon",
"chgrp",
"chown",
"chmod",
"cp",
"dd",
"df",
"dir",
"dircolors",
"ln",
"ls",
"mkdir",
"mkfifo",
"mknod",
"mktemp",
"mv",
"realpath",
"rm",
"rmdir",
"shred",
"sync",
"touch",
"truncate",
"vdir",
"b2sum",
"base32",
"base64",
"cat",
"cksum",
"comm",
"csplit",
"cut",
"expand",
"fmt",
"fold",
"head",
"join",
"md5sum",
"nl",
"numfmt",
"od",
"paste",
"ptx",
"pr",
"sha1sum",
"sha224sum",
"sha256sum",
"sha384sum",
"sha512sum",
"shuf",
"sort",
"split",
"sum",
"tac",
"tail",
"tr",
"tsort",
"unexpand",
"uniq",
"wc",
"arch",
"basename",
"chroot",
"date",
"dirname",
"du",
"echo",
"env",
"expr",
"factor",
// "false", // keyword literal already
"groups",
"hostid",
"id",
"link",
"logname",
"nice",
"nohup",
"nproc",
"pathchk",
"pinky",
"printenv",
"printf",
"pwd",
"readlink",
"runcon",
"seq",
"sleep",
"stat",
"stdbuf",
"stty",
"tee",
"test",
"timeout",
// "true", // keyword literal already
"tty",
"uname",
"unlink",
"uptime",
"users",
"who",
"whoami",
"yes"
];

return {
name: 'Bash',
aliases: ['sh'],
keywords: {
$pattern: /\b[a-z._-]+\b/,
keyword: KEYWORDS,
literal: LITERALS,
built_in:
// Shell built-ins
// http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
'break cd continue eval exec exit export getopts hash pwd readonly return shift test times ' +
'trap umask unset ' +
// Bash built-ins
'alias bind builtin caller command declare echo enable help let local logout mapfile printf ' +
'read readarray source type typeset ulimit unalias ' +
built_in:[
...SHELL_BUILT_INS,
...BASH_BUILT_INS,
// Shell modifiers
'set shopt ' +
// Zsh built-ins
'autoload bg bindkey bye cap chdir clone comparguments compcall compctl compdescribe compfiles ' +
'compgroups compquote comptags comptry compvalues dirs disable disown echotc echoti emulate ' +
'fc fg float functions getcap getln history integer jobs kill limit log noglob popd print ' +
'pushd pushln rehash sched setcap setopt stat suspend ttyctl unfunction unhash unlimit ' +
'unsetopt vared wait whence where which zcompile zformat zftp zle zmodload zparseopts zprof ' +
'zpty zregexparse zsocket zstyle ztcp'
"set",
"shopt",
...ZSH_BUILT_INS,
...GNU_CORE_UTILS
]
},
contains: [
KNOWN_SHEBANG, // to catch known shells and boost relevancy
Expand All @@ -154,6 +374,7 @@ export default function(hljs) {
ARITHMETIC,
hljs.HASH_COMMENT_MODE,
HERE_DOC,
PATH_MODE,
QUOTE_STRING,
ESCAPED_QUOTE,
APOS_STRING,
Expand Down
2 changes: 1 addition & 1 deletion test/markup/bash/escaped-quote.expect.txt
@@ -1,2 +1,2 @@
<span class="hljs-comment"># Escaped double-quote is not a string</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;&quot;quoted&quot;&#x27;</span> | tr -d \&quot; &gt; text.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;&quot;quoted&quot;&#x27;</span> | <span class="hljs-built_in">tr</span> -d \&quot; &gt; text.txt
2 changes: 1 addition & 1 deletion test/markup/bash/no-numbers.expect.txt
@@ -1,3 +1,3 @@
<span class="hljs-comment"># numbers aren&#x27;t highlighted in bash as their semantics is</span>
<span class="hljs-comment"># not strictly defined for command line parameters</span>
$ tail -10 access.log
$ <span class="hljs-built_in">tail</span> -10 access.log
8 changes: 4 additions & 4 deletions test/markup/bash/strings.expect.txt
Expand Up @@ -8,7 +8,7 @@ EOF</span>

jshell -s - &lt;&lt;&lt;<span class="hljs-string">&#x27;System.out.printf(&quot;Procs: %s%n&quot;, getdata())&#x27;</span>

cat &lt;&lt;&lt; <span class="hljs-string">&#x27;$VARIABLE&#x27;</span>
cat &lt;&lt;&lt; <span class="hljs-string">&quot;<span class="hljs-variable">$VARIABLE</span>&quot;</span>
cat &lt;&lt;&lt; <span class="hljs-variable">$VARIABLE</span>
cat &lt;&lt;&lt; `<span class="hljs-variable">$VARIABLE</span>`
<span class="hljs-built_in">cat</span> &lt;&lt;&lt; <span class="hljs-string">&#x27;$VARIABLE&#x27;</span>
<span class="hljs-built_in">cat</span> &lt;&lt;&lt; <span class="hljs-string">&quot;<span class="hljs-variable">$VARIABLE</span>&quot;</span>
<span class="hljs-built_in">cat</span> &lt;&lt;&lt; <span class="hljs-variable">$VARIABLE</span>
<span class="hljs-built_in">cat</span> &lt;&lt;&lt; `<span class="hljs-variable">$VARIABLE</span>`
2 changes: 1 addition & 1 deletion test/markup/dockerfile/default.expect.txt
Expand Up @@ -8,7 +8,7 @@

<span class="hljs-keyword">RUN</span><span class="language-bash"> apt-get update \
&amp;&amp; apt-get install -y php5-fpm php-apc php5-curl php5-gd php5-intl php5-mysql</span>
<span class="hljs-keyword">RUN</span><span class="language-bash"> mkdir /tmp/sessions</span>
<span class="hljs-keyword">RUN</span><span class="language-bash"> <span class="hljs-built_in">mkdir</span> /tmp/sessions</span>

<span class="hljs-keyword">ENV</span> APPLICATION_ENV dev

Expand Down
2 changes: 1 addition & 1 deletion test/markup/shell/command-continuation.expect.txt
Expand Up @@ -8,7 +8,7 @@
--group-add=<span class="hljs-variable">$groups</span> \
neo4j:3.4</span>
<span class="hljs-meta">&gt; </span><span class="language-bash">/bin/cat \.travis.yml\
-b | head -n1</span>
-b | <span class="hljs-built_in">head</span> -n1</span>
1 language: node_js
<span class="hljs-meta">&gt; </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">&#x27;hello&#x27;</span></span>
hello

0 comments on commit 7d10f7f

Please sign in to comment.