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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
package Tk::CodeText::Bash;
use vars qw($VERSION);
$VERSION = '0.1'; # Initial release;
use strict;
use warnings;
use base('Tk::CodeText::Template');
my $separators = '\||&|;|(|)|<|>|\s|\'|"|`|#|$';
sub new {
my ($proto, $rules) = @_;
my $class = ref($proto) || $proto;
if (not defined($rules)) {
$rules = [
['Text'],
['Comment', -foreground => 'gray'],
['Reserved', -foreground => 'brown'],
['Keyword', -foreground => 'orange'],
['String', -foreground => 'red'],
['Backticked', -foreground => 'purple'],
['String intrapolated', -foreground => 'red'],
['Escaped character', -foreground => 'magenta'],
['Operator', -foreground => 'darkblue'],
['Variable', -foreground => 'blue'],
];
};
my $self = $class->SUPER::new($rules);
$self->lists({
'Reserved' => [
'!', 'case', 'do', 'done', 'elif', 'else', 'esac', 'fi', 'for',
'function', 'if', 'in', 'select', 'then', 'until', 'while', '{',
'}', 'time', '[[', ']]',
],
'Keyword' => [
'alias', 'bind', 'bg','builtin', 'break', 'cd', 'command', 'compgen',
'complete', 'continue', 'cp', 'declare', 'disown', 'dirs', 'echo',
'enable', 'eval', 'exec', 'exit', 'export', 'false', 'fc', 'fg',
'function', 'getopts', 'hash', 'help', 'history', 'jobs', 'kill',
'let', 'local', 'logout', 'mv', 'popd', 'printf', 'pushd','pwd', 'read',
'readonly', 'return', 'rm', 'select', 'set', 'shift', 'shopt', 'source',
'suspend', 'test', 'trap', 'true', 'type', 'typeset', 'ulimit',
'umask', 'unalias', 'unset', 'variables', 'wait',
],
});
bless ($self, $class);
$self->callbacks({
'Backticked' => \&parseBackticked,
'Comment' => \&parseComment,
'Escaped character' => \&parseEscaped,
'Keyword' => \&parseKeyword,
'Operator' => \&parseOperator,
'Reserved' => \&parseReserved,
'String' => \&parseString,
'String intrapolated' => \&parseIString,
'Text' => \&parseText,
'Variable' => \&parseVariable,
});
$self->stackPush('Text');
return $self;
}
sub parseBackticked {
my ($self, $text) = @_;
if ($text =~ s/^(`)//) { #backtick stop
$self->snippetParse($1);
$self->stackPull;
return $text;
}
return $self->parseText($text);
}
sub parseComment {
my ($self, $text) = @_;
return $self->parserError($text);
}
sub parseEscaped {
my ($self, $text) = @_;
return $self->parserError($text);
}
sub parseIString {
my ($self, $text) = @_;
if ($text =~ s/^(\\.)//) { #escaped character
$self->snippetParse($1, 'Escaped character');
return $text;
}
if ($text =~ s/^(\$[^$separators]*)//) { #variable
$self->snippetParse($1, 'Variable');
return $text;
}
if ($text =~ s/^(`)//) { #backticked
$self->stackPush('Backticked');
$self->snippetParse($1);
return $text;
}
if ($text =~ s/^(")//) { #string stop
$self->snippetParse($1);
$self->stackPull;
return $text;
}
if ($text =~ s/^([^"|\$|`]+)//) { #string content
$self->snippetParse($1);
return $text;
}
return $self->parserError($text);
}
sub parseKeyword {
my ($self, $text) = @_;
return $self->parserError($text);
}
sub parseOperator {
my ($self, $text) = @_;
return $self->parserError($text);
}
sub parseReserved {
my ($self, $text) = @_;
return $self->parserError($text);
}
sub parseString {
my ($self, $text) = @_;
if ($text =~ s/^([^']+)//) { #string content
$self->snippetParse($1);
return $text;
}
if ($text =~ s/^(')//) { #string stop
$self->snippetParse($1);
$self->stackPull;
return $text;
}
return $self->parserError($text);
}
sub parseText {
my ($self, $text) = @_;
if ($text =~ s/^(^#!\/.*)//) { #launch line
$self->snippetParse($1, 'Reserved');
return $text;
}
if ($text =~ s/^(#.*)//) { #comment
$self->snippetParse($1, 'Comment');
return $text;
}
if ($text =~ s/^(\s+)//) { #spaces
$self->snippetParse($1);
return $text;
}
if ($text =~ s/^(`)//) { #backticked
$self->stackPush('Backticked');
$self->snippetParse($1);
return $text;
}
if ($text =~ s/^(")//) { #string intrapolated
$self->stackPush('String intrapolated');
$self->snippetParse($1);
return $text;
}
if ($text =~ s/^('[^']*)//) { #string start
$self->snippet($1);
if ($text) { #if there is still text to be parsed, string ends at same line
if ($text =~ s/(^')//) {
$self->snippetParse($1)
}
} else {
$self->stackPush('String');
}
return $text;
}
if ($text =~ s/^(\$[^$separators]*)//) { #variable
$self->snippetParse($1, 'Variable');
return $text;
}
if ($text =~ s/^([\|\||\||&&|&|;;|;|(|)])//) { #operator
$self->snippetParse($1, 'Operator');
return $text
}
if ($text =~ s/^([<|>])//) { #remaining separators
$self->snippetParse($1);
return $text
}
if ($text =~ s/^(\\.)//) { #escaped character
$self->snippet($1, 'Escaped character');
return $text;
}
if ($text =~ s/^([^$separators]+)//) { #fetching a bare part
if ($self->tokenTest($1, 'Reserved')) {
$self->snippetParse($1, 'Reserved');
} elsif ($self->tokenTest($1, 'Keyword')) {
$self->snippetParse($1, 'Keyword');
} else { #unrecognized text
$self->snippetParse($1);
}
return $text
}
#It shouldn't have come this far, but it has.
return $self->parserError($text);
}
sub parseVariable {
my ($self, $text) = @_;
return $self->parserError($text);
}
1;
__END__
=head1 NAME
Tk::CodeText::Bash - a Plugin for HTML syntax highlighting
=head1 SYNOPSIS
=encoding utf-8
require Tk::CodeText::Bash;
my $sh = new Tk::CodeText::Bash( [
['Text'],
['Tag', -foreground => 'brown'],
['Attr', -foreground => 'darkblue'],
['Comment', -foreground => 'lightblue'],
['Value', -foreground => 'orange'],
['String', -foreground => 'red'],
['SpChar', -foreground => 'magenta'],
]);
=head1 DESCRIPTION
Tk::CodeText::Bash is a plugin module that provides syntax highlighting
for Bash to a Tk::CodeText text widget.
It inherits Tk::CodeText::Template. See also there.
=head1 AUTHOR
Hans Jeuken (haje@toneel.demon.nl)
=cut
=head1 BUGS
Unknown
=cut
|