Kamelon/Debugger.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Kamelon/Builder.pm ↑ Summary
1 package Syntax::Kamelon::Debugger;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw(Syntax::Kamelon);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 my $VERSION = '0.15';
9  
10 my %functions = (
11 step => [\&StepPre, \&StepPost],
12 timer => [\&TimerPre, \&TimerPost],
13 watch => [\&WatchPre, \&WatchPost],
14 );
15  
16 sub new {
17 my $class = shift;
18 my %args = (@_);
19
20 my $self = $class->SUPER::new(%args);
21 $self->{CURPOINT} = [1, 0];
22 $self->{CURRULE} = '';
23 $self->{DEBUGSTACK} = [];
24 $self->{ELAPSED} = 0;
25 $self->{LASTPATH} = "";
26 $self->{LASTPOINT} = [];
27 $self->{LINE} = 1;
28 $self->{STARTIME} = '';
29 $self->{STEPCALL} = sub {
30 my ($text, $inf, $result) = @_;
31 print "Line: '$$text'\n";
32 print "Result: $result\n";
33 for (sort keys %$inf) {
34 print "$_: '" , $inf->{$_}, "'\n"
35 }
36 print "\nPress a Enter to continue\n";
37 my $key = <STDIN>;
38 };
39 $self->{TASKS} = [];
40 $self->{WATCHINFO} = {};
41 $self->{WATCHPOINT} = [];
42 $self->{WATCHRESULT} = "";
43 return $self;
44 }
45  
46 sub CurRule {
47 my $self = shift;
48 return $self->{CURRULE};
49 }
50  
51 sub DebugStackPull {
52 my $self = shift;
53 my $stack = $self->{DEBUGSTACK};
54 my $i = pop @$stack;
55 return @$i;
56 }
57  
58 sub DebugStackPush {
59 my $self = shift;
60 my $stack = $self->{DEBUGSTACK};
61 push @$stack, \@_;
62 }
63  
64 sub GetStackImage {
65 my $self = shift;
66 my $stack = $self->{STACK};
67 my @o = ();
68 foreach my $item (@$stack) {
69 my @i = ($item->[0]->{syntax}, $item->[1]);
70 if (defined $item->[2]) {
71 my $i2 = $item->[2];
72 for (@$i2) { push @i, $_ }
73 }
74 push @o, \@i
75 }
76 return \@o
77 }
78  
79 sub NewLine {
80 my $self = shift;
81 my $l = $self->{LINE};
82 $l ++;
83 $self->{LINE} = $l;
84 }
85  
86 sub ParseContext {
87 my ($self, $text, $callbacklist, $debuginfo) = @_;
88 my $r = 0;
89 my $num = 0;
90 for (@$callbacklist) {
91 my @i = @$_;
92 my $inf = $debuginfo->[$num];
93 $self->{CURRULE} = $inf->{path};
94 $self->PreTask($$text, $inf);
95 my $call = shift @i;
96 $r = &$call($self, $text, @i);
97 $self->PostTask($text, $inf, $r);
98 $num ++;
99 last if $r;
100 }
101 return $r;
102 }
103  
104 sub ParseLine {
105 my ($self, $text) = @_;
106 while ($text ne '') {
107 my $top = $self->{STACK}->[0];
108 my ($hl, $context) = @$top;
109 my $ctd = $hl->{contexts}->{$context};
110 if ($text =~ s/^(\n)//) {
111 if ($self->LineStart) {
112 my $m = $ctd->{emptycontext};
113 &$m;
114 }
115 $self->LineEndContext($ctd->{endcontext});
116 $self->SnippetForce;
117 # my $attr = $ctd->{attribute};
118 # $self->SnippetParse($1, $attr);
119 $self->SnippetForce;
120 $self->{LINESEGMENT} = '';
121 $self->NewLine;
122 } else {
123 my $callbacklist = $ctd->{callbacks};
124 my $debuginfo = $ctd->{debug};
125 my $result = $self->ParseContext(\$text, $callbacklist, $debuginfo);
126 unless($result) {
127 my $f = $ctd->{fallthroughcontext};
128 if (defined($f)) {
129 &$f;
130 } else {
131 if ($text =~ s/^([^\n])//) {
132 my $attr = $self->{USEATTRIBSTACK}->[0];
133 unless (defined $attr) {
134 $attr = $ctd->{attribute};
135 }
136 $self->SnippetParse($1, $attr);
137 }
138 }
139 }
140 }
141 }
142 }
143  
144 sub ParseResult {
145 my ($self, $text, $string, $context, $attr) = @_;
146 if ($string =~ s/\n$//) {
147 $self->LogWarning($self->{CURRULE} ."Attempted to parse a newline in line " . $self->{LINE} . "\n");
148 }
149 return $self->SUPER::ParseResult($text, $string, $context, $attr)
150 }
151  
152 sub ParseResultLookAhead {
153 my ($self, $text, $string, $context, $attr) = @_;
154 if ($string =~ s/\n$//) {
155 $self->LogWarning($self->{CURRULE} ."Attempted to parse a newline in line " . $self->{LINE} . "\n");
156 }
157 return $self->SUPER::ParseResultLookAhead($text, $string, $context, $attr)
158 }
159  
160 sub PreTask {
161 my ($self, $text, $rule) = @_;
162 my $t = $self->{TASKS};
163 foreach my $task (@$t) {
164 my $call = $functions{$task}->[0];
165 &$call($self, $text, $rule)
166 }
167 my $time = time;
168 $self->DebugStackPush($text, $rule, $time);
169 }
170  
171 sub PostTask {
172 my $time = time;
173 my ($self, $text, $rule, $result) = @_;
174 my ($ptext, $prule, $ptime) = $self->DebugStackPull;
175 $self->{ELAPSED} = $time - $ptime;
176 if ($result) {
177 $self->{PARSED} = substr($ptext, 0, length($ptext) - length($text));
178 }
179 my $t = $self->{TASKS};
180 foreach my $task (@$t) {
181 my $call = $functions{$task}->[1];
182 &$call($self, $text, $prule, $result);
183 }
184 }
185  
186 sub Reset {
187 my $self = shift;
188 $self->SUPER::Reset;
189 $self->{DEBUGSTACK} = [];
190 $self->{ELAPSED} = 0;
191 $self->{LASTPATH} = "";
192 $self->{LINE} = 1;
193 $self->{WATCHRESULT} = "";
194 $self->{WATCHINFO} = {};
195 }
196  
197 sub SetTasks {
198 my $self = shift;
199 my @tasks = ();
200 while (@_) {
201 my $t = shift;
202 if ( $t =~ /^step|timer|watch$/) {
203 push @tasks, $t
204 } else {
205 warn "invalid task $t";
206 }
207 }
208 $self->{TASKS} = \@tasks;
209 }
210  
211 sub SetWatch {
212 my $self = shift;
213 my @point = ();
214 while (@_) {
215 my $t = shift;
216 if ( $t =~ /^\d+$/) {
217 push @point, $t
218 } else {
219 warn "Value should be a positive integer: $t";
220 }
221 }
222 $self->{WATCHPOINT} = \@point;
223 }
224  
225 sub StepPost {
226 my $self = shift;
227 my $call = $self->{STEPCALL};
228 &$call(@_);
229 }
230  
231 sub StepPre {
232 my ($self, $text, $inf) = @_;
233 }
234  
235 sub TimerPost {
236 my ($self, $text, $inf, $result) = @_;
237 }
238  
239 sub TimerPre {
240 my ($self, $text, $inf) = @_;
241 }
242  
243 sub WatchPosition {
244 my ($self, $line, $pos) = @_;
245 unless (defined $line) { $line = $self->{LINE} }
246 unless (defined $pos) { $pos = $self->Column }
247 my $wpoint = $self->{WATCHPOINT};
248 if (@$wpoint == 2) {
249 my ($wline, $wpos) = @$wpoint;
250 if ($line < $wline) {
251 return 0
252 } elsif ($line == $wline) {
253 if ($pos < $wpos) {
254 return 0
255 } elsif ($pos == $wpos) {
256 return 1
257 } else {
258 return 2
259 }
260 } else {
261 return 2
262 }
263 } else {
264 warn "Watchpoint not properly set"
265 }
266 }
267  
268 sub WatchPost {
269 my ($self, $text, $inf, $result) = @_;
270 my $r = $self->WatchPosition($self->{LINE}, $self->Column);
271 my $status = $self->{WATCHRESULT};
272 my $path = $inf->{path};
273 if (($r eq 2) and ($status eq '')) {
274 $self->{WATCHINFO} = [ $inf, $self->{PARSED}, $self->GetStackImage ];
275 if ($result) {
276 $self->{WATCHRESULT} = "Matched";
277 }
278 } else {
279 $self->{LASTPOINT} = [ $self->{LINE}, $self->Column ];
280 $self->{LASTPATH} = $path;
281 }
282 }
283  
284 sub WatchPre {
285 my ($self, $text, $inf) = @_;
286 # print "parsing ", $inf->{path}, "\n";
287 my $r = $self->WatchPosition($self->{LINE}, $self->Column);
288 my $status = $self->{WATCHRESULT};
289 if (($r eq 2) and ($status eq '')){
290 my $lp = $self->{LASTPOINT};
291 if ($self->WatchPosition(@$lp) < 2) {
292 $self->{WATCHRESULT} = "Failed";
293 $self->{WATCHINFO} = [ $inf, undef, $self->GetStackImage];
294 }
295 }
296 }
297  
298 sub WatchResult {
299 my $self = shift;
300 return ($self->{WATCHRESULT}, $self->{WATCHINFO})
301 }
302  
303 1;
304 __END__
« Kamelon/Builder.pm ↑ Summary