Skip to content

Commit

Permalink
Merge pull request #793 from zeitgeist87/BashString
Browse files Browse the repository at this point in the history
Substantially reduce wrongly highlighted stuff for Bash
  • Loading branch information
Golmote committed Oct 6, 2015
2 parents 29643f4 + c628ce2 commit ac6fe2e
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 25 deletions.
67 changes: 53 additions & 14 deletions components/prism-bash.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
(function(Prism) {
var varsRegex = /\$(?:[a-z0-9_#\?\-\*!@]+|\{[^}]+\})/i,
bashVars = { variable: varsRegex };
var insideString = {
variable: [
// Arithmetic Environment
{
pattern: /\$?\(\([\w\W]+?\)\)/,
inside: {
// If there is a $ sign at the beginning highlight $(( and )) as variable
variable: [{
pattern: /(^\$\(\([\w\W]+)\)\)/,
lookbehind: true
},
/^\$\(\(/,
],
number: /\b-?(?:0x[\dA-Fa-f]+|\d*\.?\d+(?:[Ee]-?\d+)?)\b/,
// Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic
operator: /--?|-=|\+\+?|\+=|!=?|~|\*\*?|\*=|\/=?|%=?|<<=?|>>=?|<=?|>=?|==?|&&?|&=|\^=?|\|\|?|\|=|\?|:/,
// If there is no $ sign at the beginning highlight (( and )) as punctuation
punctuation: /\(\(?|\)\)?|,|;/
}
},
// Command Substitution
{
pattern: /\$\([^)]+\)|`[^`]+`/,
inside: {
variable: /^\$\(|^`|\)$|`$/
}
},
/\$(?:[a-z0-9_#\?\*!@]+|\{[^}]+\})/i
],
};

Prism.languages.bash = {
'shebang': {
Expand All @@ -16,24 +44,35 @@
{
pattern: /((?:^|[^<])<<\s*)(?:"|')?(\w+?)(?:"|')?\s*\r?\n(?:[\s\S])*?\r?\n\2/g,
lookbehind: true,
inside: bashVars
inside: insideString
},
{
pattern: /("|')(?:\\?[\s\S])*?\1/g,
inside: bashVars
inside: insideString
}
],
// Redefined to prevent highlighting of numbers in filenames
'number': {
pattern: /([^\w\.])-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
'variable': insideString.variable,
// Originally based on http://ss64.com/bash/
'function': {
pattern: /(^|\s|;|\||&)(?:alias|apropos|apt-get|aptitude|aspell|awk|basename|bash|bc|bg|builtin|bzip2|cal|cat|cd|cfdisk|chgrp|chmod|chown|chroot|chkconfig|cksum|clear|cmp|comm|command|cp|cron|crontab|csplit|cut|date|dc|dd|ddrescue|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|du|egrep|eject|enable|env|ethtool|eval|exec|expand|expect|export|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|getopts|git|grep|groupadd|groupdel|groupmod|groups|gzip|hash|head|help|hg|history|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|jobs|join|kill|killall|less|link|ln|locate|logname|logout|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|make|man|mkdir|mkfifo|mkisofs|mknod|more|most|mount|mtools|mtr|mv|mmv|nano|netstat|nice|nl|nohup|notify-send|nslookup|open|op|passwd|paste|pathchk|ping|pkill|popd|pr|printcap|printenv|printf|ps|pushd|pv|pwd|quota|quotacheck|quotactl|ram|rar|rcp|read|readarray|readonly|reboot|rename|renice|remsync|rev|rm|rmdir|rsync|screen|scp|sdiff|sed|seq|service|sftp|shift|shopt|shutdown|sleep|slocate|sort|source|split|ssh|stat|strace|su|sudo|sum|suspend|sync|tail|tar|tee|test|time|timeout|times|touch|top|traceroute|trap|tr|tsort|tty|type|ulimit|umask|umount|unalias|uname|unexpand|uniq|units|unrar|unshar|uptime|useradd|userdel|usermod|users|uuencode|uudecode|v|vdir|vi|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yes|zip)(?=$|\s|;|\||&)/,
lookbehind: true
},
'variable': varsRegex,
// Originally based on http://ss64.com/bash/
'function': /\b(?:alias|apropos|apt-get|aptitude|aspell|awk|basename|bash|bc|bg|builtin|bzip2|cal|cat|cd|cfdisk|chgrp|chmod|chown|chroot|chkconfig|cksum|clear|cmp|comm|command|cp|cron|crontab|csplit|cut|date|dc|dd|ddrescue|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|du|egrep|eject|enable|env|ethtool|eval|exec|expand|expect|export|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|getopts|git|grep|groupadd|groupdel|groupmod|groups|gzip|hash|head|help|hg|history|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|jobs|join|kill|killall|less|link|ln|locate|logname|logout|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|make|man|mkdir|mkfifo|mkisofs|mknod|more|most|mount|mtools|mtr|mv|mmv|nano|netstat|nice|nl|nohup|notify-send|nslookup|open|op|passwd|paste|pathchk|ping|pkill|popd|pr|printcap|printenv|printf|ps|pushd|pv|pwd|quota|quotacheck|quotactl|ram|rar|rcp|read|readarray|readonly|reboot|rename|renice|remsync|rev|rm|rmdir|rsync|screen|scp|sdiff|sed|seq|service|sftp|shift|shopt|shutdown|sleep|slocate|sort|source|split|ssh|stat|strace|su|sudo|sum|suspend|sync|tail|tar|tee|test|time|timeout|times|touch|top|traceroute|trap|tr|tsort|tty|type|ulimit|umask|umount|unalias|uname|unexpand|uniq|units|unrar|unshar|uptime|useradd|userdel|usermod|users|uuencode|uudecode|v|vdir|vi|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yes|zip)\b/,
'keyword': /\b(let|if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)\b/,
'boolean': /\b(?:true|false)\b/,
'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,
'punctuation': /[{}[\];(),.:]/
'keyword': {
pattern: /(^|\s|;|\||&)(?:let|:|\.|if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)(?=$|\s|;|\||&)/,
lookbehind: true
},
'boolean': {
pattern: /(^|\s|;|\||&)(?:true|false)(?=$|\s|;|\||&)/,
lookbehind: true
},
'operator': /&&?|\|\|?|==?|!=?|<<<?|>>|<=?|>=?|=~/,
'punctuation': /\$?\(\(?|\)\)?|\.\.|[{}[\];]/
};

var inside = insideString.variable[1].inside;
inside['function'] = Prism.languages.bash['function'];
inside.keyword = Prism.languages.bash.keyword;
inside.boolean = Prism.languages.bash.boolean;
inside.operator = Prism.languages.bash.operator;
inside.punctuation = Prism.languages.bash.punctuation;
})(Prism);
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions tests/languages/bash/arithmetic_environment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(( 4 + 5 ))
$((5 * 7))
"foo $((5 * 7)) bar"
for (( NUM=1 ; NUM<=1000 ; NUM++ ))

----------------------------------------------------

[
["variable", [
["punctuation", "(("],
["number", "4"],
["operator", "+"],
["number", "5"],
["punctuation", "))"]
]],
["variable", [
["variable", "$(("],
["number", "5"],
["operator", "*"],
["number", "7"],
["variable", "))"]
]],
["string", [
"\"foo ",
["variable", [
["variable", "$(("],
["number", "5"],
["operator", "*"],
["number", "7"],
["variable", "))"]
]],
" bar\""
]],
["keyword", "for"],
["variable", [
["punctuation", "(("],
" NUM",
["operator", "="],
["number", "1"],
["punctuation", ";"],
" NUM",
["operator", "<="],
["number", "1000"],
["punctuation", ";"],
" NUM",
["operator", "++"],
["punctuation", "))"]
]]
]

----------------------------------------------------

Checks arithmetic environments
34 changes: 34 additions & 0 deletions tests/languages/bash/command_substitution_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
$(echo foo)
`echo foo`
"$(echo foo) bar"

----------------------------------------------------

[
["variable", [
["variable", "$("],
["keyword", "echo"],
" foo",
["variable", ")"]
]],
["variable", [
["variable", "`"],
["keyword", "echo"],
" foo",
["variable", "`"]
]],
["string", [
"\"",
["variable", [
["variable", "$("],
["keyword", "echo"],
" foo",
["variable", ")"]
]],
" bar\""
]]
]

----------------------------------------------------

Checks for command substitution.
5 changes: 3 additions & 2 deletions tests/languages/bash/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ for break continue while
in case function select
do done until echo exit
return set declare

. :
----------------------------------------------------

[
["keyword", "if"], ["keyword", "then"], ["keyword", "else"], ["keyword", "elif"], ["keyword", "fi"],
["keyword", "for"], ["keyword", "break"], ["keyword", "continue"], ["keyword", "while"],
["keyword", "in"], ["keyword", "case"], ["keyword", "function"], ["keyword", "select"],
["keyword", "do"], ["keyword", "done"], ["keyword", "until"], ["keyword", "echo"], ["keyword", "exit"],
["keyword", "return"], ["keyword", "set"], ["keyword", "declare"]
["keyword", "return"], ["keyword", "set"], ["keyword", "declare"],
["keyword", "."], ["keyword", ":"]
]

----------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions tests/languages/bash/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ EOF
["string", [
"\"", ["variable", "${foo}"], "\""
]],
["operator", "<"],
["operator", "<"],
["operator", "<<"],
["string", ["STRING_END\r\nfoo\r\nbar\r\nSTRING_END"]],
["operator", "<"],
["operator", "<"],
["operator", "<<"],
["string", ["EOF\r\nfoo ", ["variable", "$@"], "\r\nbar\r\nEOF"]],
["operator", "<"],
["operator", "<"],
["operator", "<<"],
["string", ["'EOF'\r\n'single quoted string'\r\n\"double quoted string\"\r\nEOF"]],
["operator", "<"],
["operator", "<"],
["operator", "<<"],
["string", ["\"EOF\"\r\nfoo\r\nbar\r\nEOF"]]
]

Expand Down

0 comments on commit ac6fe2e

Please sign in to comment.