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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
package Tk::CodeText::Template;
use vars qw($VERSION);
$VERSION = '0.3';
use strict;
use Data::Dumper;
sub new {
my ($proto, $rules) = @_;
my $class = ref($proto) || $proto;
if (not defined($rules)) {
$rules = [];
};
my $self = {};
$self->{'lists'} = {};
$self->{'out'} = [];
$self->{'rules'} = $rules,
$self->{'stack'} = [];
$self->{'snippet'} = '';
$self->{'callbacks'} = {};
$self->{'oneliners'} = [],
bless ($self, $class);
return $self;
}
sub callbacks {
my $hlt = shift;
if (@_) { $hlt->{'callbacks'} = shift; };
return $hlt->{'callbacks'};
}
sub highlight {
my ($hlt, $text) = @_;
$hlt->snippetParse;
my $out = $hlt->out;
@$out = ();
while ($text) {
# print "highlighting '$text'\n";
# print "mode is", $hlt->stackTop, "\n";
my $sub = $hlt->callbacks->{$hlt->stackTop};
$text = &$sub($hlt, $text);
}
$hlt->snippetParse;
return @$out;
}
sub lists {
my $hlt = shift;
if (@_) { $hlt->{'lists'} = shift; };
return $hlt->{'lists'};
}
sub listAdd {
my $hlt = shift;
my $listname = shift;
# print "listname $listname\n";
my $lst = $hlt->lists;
if (@_) {
$lst->{$listname} = [@_];
} else {
$lst->{$listname} = [];
}
my $r = $hlt->lists->{$listname};
# print "added tokens\n"; foreach my $f (@$r) { print " $f\n"; };
}
sub rules {
my $hlt = shift;
if (@_) { $hlt->{'rules'} = shift; }
return $hlt->{'rules'};
}
sub out {
my $hlt = shift;
if (@_) { $hlt->{'out'} = shift; }
return $hlt->{'out'};
}
sub parserError {
my ($hlt, $text) = @_;
my $s = $hlt->stack;
if (@$s eq 1) { #we cannot dump this mode because it's the lowest.
warn "Parser error\n\tmode: '" . $hlt->stackTop . "'\n" .
"text: '$text'\nparsing as plain text";
$hlt->snippetParse($text);
$text =''; #Let's call it a day;
} else {
warn "Parser error\n\tmode: '" . $hlt->stackTop . "'\n" .
"text: '$text'\nexiting mode";
$hlt->stackPull;
};
return $text;
}
sub snippet {
my $hlt = shift;
if (@_) { $hlt->{'snippet'} = shift; }
return $hlt->{'snippet'};
}
sub snippetAppend {
my ($hlt, $ch) = @_;
$hlt->{'snippet'} = $hlt->{'snippet'} . $ch;
}
sub snippetParse {
my $hlt = shift;
my $snip = shift;
my $attr = shift;
unless (defined($snip)) { $snip = $hlt->snippet }
unless (defined($attr)) { $attr = $hlt->stackTop }
my $out = $hlt->{'out'};
# print "parsing '$snip' with attribute '$attr'\n";
if ($snip) {
push(@$out, length($snip), $attr);
$hlt->snippet('');
}
}
sub stack {
my $hlt = shift;
return $hlt->{'stack'};
}
sub stackPush {
my ($hlt, $val) = @_;
# print "pushing $val\n";
my $stack = $hlt->stack;
unshift(@$stack, $val);
}
sub stackPull {
my ($hlt, $val) = @_;
my $stack = $hlt->stack;
return shift(@$stack);
}
sub stackTop {
my $hlt = shift;
return $hlt->stack->[0];
}
sub stateCompare {
my ($hlt, $state) = @_;
my $h = [ $hlt->stateGet ];
my $equal = 1;
if (Dumper($h) ne Dumper($state)) { $equal = 0 };
return $equal;
}
sub stateGet {
my $hlt = shift;
my $s = $hlt->stack;
return @$s;
}
sub stateSet {
my $hlt = shift;
my $s = $hlt->stack;
@$s = (@_);
}
sub syntax {
my $hlt = shift;
my $class = ref $hlt;
$class =~ /Tk::CodeText::(.*)/;
return $1;
}
sub tokenParse {
my $hlt = shift;
my $tkn = shift;
$hlt->stackPush($tkn);
$hlt->snippetParse(@_);
$hlt->stackPull;
}
sub tokenTest {
my ($hlt, $test, $list) = @_;
# print "tokenTest $test\n";
my $l = $hlt->lists->{$list};
my @list = reverse sort @$l;
# return grep { ($test =~ /^$_/) } @$l;
my @rl = grep { (substr($test, 0, length($_)) eq $_) } @list;
# foreach my $r (@rl) { print "$r\n" }
if (@rl) {
return $rl[0]
} else {
return undef;
}
}
1;
__END__
=head1 NAME
Tk::CodeText::Template - a template for syntax highlighting plugins
=head1 SYNOPSIS
=head1 DESCRIPTION
Tk::CodeText::Template is a framework to assist authors of plugin modules.
All methods to provide highlighting in a Tk::CodeText widget are there, Just
no syntax definitions and callbacks. An instance of Tk::CodeText::Template
should never be created, it's meant to be sub classed only.
=head1 METHODS
=over 4
=item B<callbacks>({I<'Tagname'> => I<\&callback>, ...});
sets and returns the instance variable 'callbacks'
=item B<highlight>(I<$text>);
highlights I<$text>. It does so by selecting the proper callback
from the B<commands> hash and invoke it. It will do so untill
$text has been reduced to an empty string.
=item B<listAdd>(I<'listname'>, I<$item1>, I<$item2> ...);
Adds a list to the 'lists' hash.
=item B<lists>(I<?\%lists?>);
sets and returns the instance variable 'lists'.
=item B<out>(I<?\@highlightedlist?>);
sets and returns the instance variable 'out'.
=item B<parserError>(I<'text'>);
Error trapping method. Tries to escape the current mode. If that is not
possible, it will parse the text with the default tag. Furthermore it
complains about being called at all. Usefull for debugging when writing
a new plugin.
=item B<rules>(I<?\@rules?>)
sets and returns a reference to a list of tagnames and options.
By default it is set to [].
=item B<snippetAppend>(I<$string>)
appends I<$string> to the current snippet.
=item B<snippetParse>(I<?$text?>, I<?$tagname?>)
parses $text to the 'out' list, and assigns $tagname to it. If $tagname is
not specified it will look for the tagname by calling B<stackTop>. If I<$text>
is also not specified it will look for text by calling B<snippet>.
=item B<stack>
sets and returns the instance variable 'stack', a reference to an array.
=item B<stackPull>
retrieves the element that is on top of the stack, decrements stacksize by 1.
=item B<stackPush>(I<$tagname>)
puts I<$tagname> on top of the stack, increments stacksize by 1
=item B<stackTop>
retrieves the element that is on top of the stack.
=item B<stateCompare>(I<\@state>);
Compares two lists, \@state and the stack. returns true if they
match.
=item B<stateGet>
Returns a list containing the entire stack.
=item B<stateSet>(I<@list>)
Accepts I<@list> as the current stack.
=item B<tokenParse>(I<'Tagname'>);
Parses the currently build snippet and tags it with 'Tagname'
=item B<tokenTest>(I<$value>, I<'Listname'>);
returns true if $value is and element of 'Listname' in the 'lists' hash
=back
=cut
=head1 AUTHOR
Hans Jeuken (haje@toneel.demon.nl)
=cut
=head1 BUGS
Unknown.
=cut
|