/usr/lib/ruby/vendor_ruby/rugments/lexers/shell.rb is in ruby-rugments 1.0.0~beta8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | module Rugments
module Lexers
class Shell < RegexLexer
title 'shell'
desc 'Various shell languages, including sh and bash'
tag 'shell'
aliases 'bash', 'zsh', 'ksh', 'sh'
filenames '*.sh', '*.bash', '*.zsh', '*.ksh',
'.bashrc', '.zshrc', '.kshrc', '.profile', 'PKGBUILD'
mimetypes 'application/x-sh', 'application/x-shellscript'
def self.analyze_text(text)
text.shebang?(/(ba|z|k)?sh/) ? 1 : 0
end
KEYWORDS = %w(
if fi else while do done for then return function
select continue until esac elif in
).join('|')
BUILTINS = %w(
alias bg bind break builtin caller cd command compgen
complete declare dirs disown echo enable eval exec exit
export false fc fg getopts hash help history jobs kill let
local logout popd printf pushd pwd read readonly set shift
shopt source suspend test time times trap true type typeset
ulimit umask unalias unset wait
).join('|')
state :basic do
rule /#.*$/, Comment
rule /\b(#{KEYWORDS})\s*\b/, Keyword
rule /\bcase\b/, Keyword, :case
rule /\b(#{BUILTINS})\s*\b(?!\.)/, Name::Builtin
rule /^\S*[\$%>#] +/, Generic::Prompt
rule /(\b\w+)(=)/ do |_m|
groups Name::Variable, Operator
end
rule /[\[\]{}()=]/, Operator
rule /&&|\|\|/, Operator
# rule /\|\|/, Operator
rule /<<</, Operator # here-string
rule /<<-?\s*(\'?)\\?(\w+)\1/ do |m|
lsh = Str::Heredoc
token lsh
heredocstr = Regexp.escape(m[2])
push do
rule /\s*#{heredocstr}\s*\n/, lsh, :pop!
rule /.*?\n/, lsh
end
end
end
state :double_quotes do
# NB: "abc$" is literally the string abc$.
# Here we prevent :interp from interpreting $" as a variable.
rule /(?:\$#?)?"/, Str::Double, :pop!
mixin :interp
rule /[^"`\\$]+/, Str::Double
end
state :single_quotes do
rule /'/, Str::Single, :pop!
rule /[^']+/, Str::Single
end
state :data do
rule /\s+/, Text
rule /\\./, Str::Escape
rule /\$?"/, Str::Double, :double_quotes
# single quotes are much easier than double quotes - we can
# literally just scan until the next single quote.
# POSIX: Enclosing characters in single-quotes ( '' )
# shall preserve the literal value of each character within the
# single-quotes. A single-quote cannot occur within single-quotes.
rule /$?'/, Str::Single, :single_quotes
rule /\*/, Keyword
rule /;/, Text
rule /[^=\*\s{}()$"'`\\<]+/, Text
rule /\d+(?= |\Z)/, Num
rule /</, Text
mixin :interp
end
state :curly do
rule /}/, Keyword, :pop!
rule /:-/, Keyword
rule /[a-zA-Z0-9_]+/, Name::Variable
rule /[^}:"`'$]+/, Punctuation
mixin :root
end
state :paren do
rule /\)/, Keyword, :pop!
mixin :root
end
state :math do
rule /\)\)/, Keyword, :pop!
rule %r{[-+*/%^|&]|\*\*|\|\|}, Operator
rule /\d+/, Num
mixin :root
end
state :case do
rule /\besac\b/, Keyword, :pop!
rule /\|/, Punctuation
rule /\)/, Punctuation, :case_stanza
mixin :root
end
state :case_stanza do
rule /;;/, Punctuation, :pop!
mixin :root
end
state :backticks do
rule /`/, Str::Backtick, :pop!
mixin :root
end
state :interp do
rule /\\$/, Str::Escape # line continuation
rule /\\./, Str::Escape
rule /\$\(\(/, Keyword, :math
rule /\$\(/, Keyword, :paren
rule /\${#?/, Keyword, :curly
rule /`/, Str::Backtick, :backticks
rule /\$#?(\w+|.)/, Name::Variable
end
state :root do
mixin :basic
mixin :data
end
end
end
end
|