lib/LaTeXML/Core/Definition.pm

filter: Statementstmt 16.1% 24 / 149 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 25.0% 8 / 32 Podpod n/a totaltotal 12.6% 32 / 254 CC 30 SCAR 39.8
CC
30
Cov
11%
stopProfiling
210.0
tracingCSName
20.0
getParameters
12.0
CRAP 53.4
« lib/LaTeXML/Post/Split.pm ↑ Summary lib/LaTeXML/Common/Locator.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Definition | #
3 # | Representation of definitions of Control Sequences | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Core::Definition;
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use LaTeXML::Core::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use base qw(LaTeXML::Common::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # Make these present, but do not import.
22 require LaTeXML::Core::Definition::Expandable;
23 require LaTeXML::Core::Definition::Conditional;
24 require LaTeXML::Core::Definition::Primitive;
25 require LaTeXML::Core::Definition::Register;
26 require LaTeXML::Core::Definition::CharDef;
27 require LaTeXML::Core::Definition::Constructor;
28  
29 #**********************************************************************
30  
31 sub isaDefinition {
32 0 ▶ return 1; }
Subroutine isaDefinition: not called CC=1 SCAR=6.9
33  
34 sub getCS {
35 0 ▶ my ($self) = @_;
Subroutine getCS: not called CC=1 SCAR=6.9
36 0 return $$self{cs}; }
37  
38 sub getCSName {
39 0 ▶ my ($self) = @_;
Subroutine getCSName: not called CC=2 SCAR=17.9
40 0 ▶ return (defined $$self{alias} ? $$self{alias} : $$self{cs}->getCSName); }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'alias'} ? : 0 0 0
41  
42 # NOTE: Need to clean up alias; really should already be Token (or Tokens?)
43 # and is not always a CS!
44 sub getCSorAlias {
45 0 ▶ my ($self) = @_;
Subroutine getCSorAlias: not called CC=2 SCAR=17.9
46 0 ▶ return (defined $$self{alias} ? T_CS($$self{alias}) : $$self{cs}); }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'alias'} ? : 0 0 0
47  
48 sub isExpandable {
49 0 ▶ return 0; }
Subroutine isExpandable: not called CC=1 SCAR=6.9
50  
51 sub isRegister {
52 0 ▶ return ''; }
Subroutine isRegister: not called CC=1 SCAR=6.9
53  
54 sub isPrefix {
55 0 ▶ return 0; }
Subroutine isPrefix: not called CC=1 SCAR=6.9
56  
57 # The following come from flags, but probably(?) only make sense for Expandable (macros)
58 # and furthermore, only isProtected is used for anything significant.
59 sub isProtected {
60 0 ▶ my ($self) = @_;
Subroutine isProtected: not called CC=1 SCAR=6.9
61 0 return $$self{isProtected}; }
62  
63 sub setIsProtected {
64 0 ▶ my ($self) = @_;
Subroutine setIsProtected: not called CC=1 SCAR=6.9
65 0 $$self{isProtected} = 1;
66 0 return; }
67  
68 sub isOuter {
69 0 ▶ my ($self) = @_;
Subroutine isOuter: not called CC=1 SCAR=6.9
70 0 return $$self{isOuter}; }
71  
72 sub setIsOuter {
73 0 ▶ my ($self) = @_;
Subroutine setIsOuter: not called CC=1 SCAR=6.9
74 0 $$self{isOuter} = 1;
75 0 return; }
76  
77 sub isLong {
78 0 ▶ my ($self) = @_;
Subroutine isLong: not called CC=1 SCAR=6.9
79 0 return $$self{isLong}; }
80  
81 sub setIsLong {
82 0 ▶ my ($self) = @_;
Subroutine setIsLong: not called CC=1 SCAR=6.9
83 0 $$self{isLong} = 1;
84 0 return; }
85  
86 sub getLocator {
87 0 ▶ my ($self) = @_;
Subroutine getLocator: not called CC=1 SCAR=6.9
88 0 return $$self{locator}; }
89  
90 sub readArguments {
91 0 ▶ my ($self, $gullet) = @_;
Subroutine readArguments: not called CC=2 SCAR=17.9
92 0 my $params = $self->getParameters;
93 0 ▶ return ($params ? $params->readArguments($gullet, $self) : ()); }
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 0 0
94  
95 sub getParameters {
96 0 ▶ my ($self) = @_;
Subroutine getParameters: not called CC=3 SCAR=24.8
97 # Allow defering these until the Definition is actually used.
98 0 ▶ if ((defined $$self{parameters}) && !ref $$self{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"parameters"} and not ref $self->{"parameters"}) 0 0 0
Condition0%0/3
defined $self->{"parameters"} and not ref $self->{"parameters"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"parameters"} and not ref $self->{"parameters"}
defined $self->{"parameters"} not ref $self->{"parameters"}
Truth table0%0/3
A and not(B)
A= defined $self->{"parameters"}B= not ref $self->{"parameters"}
ABresult
0X0
100
111
99 0 require LaTeXML::Package;
100 0 $$self{parameters} = LaTeXML::Package::parseParameters($$self{parameters}, $$self{cs}); }
101 0 return $$self{parameters}; }
102  
103 #======================================================================
104 # Overriding methods
105 sub stringify {
106 0 ▶ my ($self) = @_;
Subroutine stringify: not called CC=3 SCAR=24.8
107 0 my $type = ref $self;
108 0 $type =~ s/^LaTeXML:://;
109 0 ▶ my $name = ($$self{alias} || $$self{cs}->getCSName);
Condition0%0/3
$self->{'alias'} || $self->{'cs'}->getCSName
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'alias'} || $self->{'cs'}->getCSName
$self->{'alias'} $self->{'cs'}->getCSName
Truth table0%0/3
A || B
A= $self->{'alias'}B= $self->{'cs'}->getCSName
ABresult
1X1
011
000
110 return $type . '[' . ($$self{parameters}
111 0 ▶ ? $name . ' ' . Stringify($$self{parameters}) : $name) . ']'; }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parameters'} ? : 0 0 0
112  
113 sub toString {
114 0 ▶ my ($self) = @_;
Subroutine toString: not called CC=2 SCAR=17.9
115 return ($$self{parameters}
116 0 ▶ ? ToString($$self{cs}) . ' ' . ToString($$self{parameters}) : ToString($$self{cs})); }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parameters'} ? : 0 0 0
117  
118 #======================================================================
119 # Tracing support
120 sub tracingCSName {
121 0 ▶ my ($self) = @_;
Subroutine tracingCSName: not called CC=4 SCAR=30.0
122 0 my $parameters = $$self{parameters};
123 return ToString($self->getCSName)
124 # Show parameter string too
125 . ($parameters ? ' ' . ToString($parameters) : '')
126 # And if this was \let to something show the name it was called by
127 0 ▶ . ($LaTeXML::CURRENT_TOKEN && !$$self{cs}->equals($LaTeXML::CURRENT_TOKEN)
Branch: 2 branches
BranchTrueFalseTotal
$parameters ? : 0 0 0
$LaTeXML::CURRENT_TOKEN && !$self->{'cs'}->equals($LaTeXML::CURRENT_TOKEN) ? : 0 0 0
Condition0%0/3
$LaTeXML::CURRENT_TOKEN and not $self->{"cs"}->equals($LaTeXML::CURRENT_TOKEN)
!ll&&!rl&&r
000
MC/DC0%0/2
$LaTeXML::CURRENT_TOKEN and not $self->{"cs"}->equals($LaTeXML::CURRENT_TOKEN)
$LaTeXML::CURRENT_TOKEN not $self->{"cs"}->equals($LaTeXML::CURRENT_TOKEN)
Truth table0%0/3
A and not(B)
A= $LaTeXML::CURRENT_TOKENB= not $self->{"cs"}->equals($LaTeXML::CURRENT_TOKEN)
ABresult
0X0
100
111
128 ? ' [for ' . ToString($LaTeXML::CURRENT_TOKEN) . ']' : ''); }
129  
130 sub tracingArgs {
131 0 ▶ my ($self, @args) = @_;
Subroutine tracingArgs: not called CC=1 SCAR=6.9
132 0 my $i = 1;
133 0 return join("\n", map { '#' . $i++ . '<-' . tracingArgToString($_) } @args); }
134  
135 # Annoying special handing of registers
136 sub tracingArgToString {
137 0 ▶ my ($arg) = @_;
Subroutine tracingArgToString: not called CC=2 SCAR=17.9
138 0 ▶ return (ref $arg eq 'ARRAY' ? '[' . join(',', map { ToString($_) } @$arg) . ']' : ToString($arg)); }
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq 'ARRAY' ? : 0 0 0
139  
140 #======================================================================
141 # Profiling support
142 #======================================================================
143 # If the value PROFILING is true, we'll collect some primitive profiling info.
144  
145 # Start profiling $CS (typically $LaTeXML::CURRENT_TOKEN)
146 # Call from within ->invoke.
147 # $mode = expand | digest | absorb
148 sub startProfiling {
149 0 ▶ my ($cs, $mode) = @_;
Subroutine startProfiling: not called CC=3 SCAR=24.8
150 0 my $name = $cs->getCSName;
151 0 my $entry = $STATE->lookupMapping('runtime_profile', $name);
152 # [#calls, max_depth, inclusive_time, exclusive_time, #pending
153 # starts of pending calls...]
154 0 ▶ if (!defined $entry) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 0 0 0
155 0 $entry = [0, 0, 0, 0, 0]; $STATE->assignMapping('runtime_profile', $name, $entry); }
156 0 ▶ $$entry[0]++ unless $mode eq 'absorb'; # One more call.
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "absorb" 0 0 0
157 0 $$entry[4]++; # One more pending...
158 #print STDERR "START PROFILE $mode of ".ToString($cs)."\n";
159 0 $STATE->pushValue('runtime_stack', [$name, $mode, [Time::HiRes::gettimeofday], $entry]);
160 0 return; }
161  
162 # Stop profiling $CS.
163 # Complication w/Macros: If the expansion of a macro contains CS's that read tokens,
164 # the end MARKER of macros may get read before the macro's effects have really been processed.
165 # So we need to ignore a stop on a macro that isn't at the top of the stack,
166 # and conversely, automatically stop a macro that is at the top above a CS that is being stopped.
167 sub stopProfiling {
168 0 ▶ my ($cs, $mode) = @_;
Subroutine stopProfiling: not called CC=14 SCAR=53.5
169 0 ▶ $cs = $cs->getString if $cs->getCatcode == CC_MARKER; # Special case for macros!!
Branch: 1 branch
BranchTrueFalseTotal
if $cs->getCatcode == 18 0 0 0
170 0 my $name = $cs->getCSName;
171 0 my $stack = $STATE->lookupValue('runtime_stack');
172 0 my $currdepth = scalar(@$stack);
173 0 ▶ my $prevdepth = $STATE->lookupValue('runtime_maxdepth') || '0';
Condition0%0/2
$LaTeXML::Core::Definition::STATE->lookupValue('runtime_maxdepth') || '0'
l!l
00
MC/DC0%0/1
$LaTeXML::Core::Definition::STATE->lookupValue('runtime_maxdepth') || '0'
$LaTeXML::Core::Definition::STATE->lookupValue('runtime_maxdepth')
Truth table0%0/2
A
A= $LaTeXML::Core::Definition::STATE->lookupValue('runtime_maxdepth')
Aresult
11
00
174 0 ▶ $STATE->assignValue('runtime_maxdepth' => $currdepth, 'global') if $currdepth > $prevdepth;
Branch: 1 branch
BranchTrueFalseTotal
if $currdepth > $prevdepth 0 0 0
175 0 ▶ while (@{$stack}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$stack;}) 0 0 0
176 0 my ($top, $topmode, $t0, $entry) = @{ $$stack[-1] };
177 0 ▶ if ((($top ne $name) || ($topmode ne $mode)) && ($topmode ne 'expand')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top ne $name || $topmode ne $mode and $topmode ne "expand") 0 0 0
Condition0%0/6
$top ne $name || $topmode ne $mode and $topmode ne "expand"
!ll&&!rl&&r
000
$top ne $name or $topmode ne $mode
l!l&&r!l&&!r
000
MC/DC0%0/3
$top ne $name || $topmode ne $mode and $topmode ne "expand"
$top ne $name $topmode ne $mode $topmode ne "expand"
Truth table0%0/5
A or B and C
A= $top ne $nameB= $topmode ne $modeC= $topmode ne "expand"
ABCresult
00X0
1X00
0100
1X11
0111
178 0 ▶ return if $mode eq 'expand'; # No error (yet) if this is a macro end marker.
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "expand" 0 0 0
179 print STDERR "\nPROFILE Error: ending $mode of $name but stack holds "
180 0 . join(',', map { $$_[0] . '(' . $$_[1] . ')' } @$stack) . ", $top ($topmode)\n";
181 0 return; }
182 0 pop(@$stack);
183 0 my $duration = Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday]);
184 0 my $depth = $$entry[4];
185 0 ▶ if ($depth > $$entry[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $entry->[1]) 0 0 0
186 0 $$entry[1] = $depth; }
187 0 ▶ $$entry[2] += $duration if $depth == 1; # add to inclusive time only in uppermost call
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 1 0 0 0
188 0 $$entry[3] += $duration; # add to exclusive time (see below)
189 0 $$entry[4]--;
190 0 ▶ if (my $caller = $$stack[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = $stack->[-1]) 0 0 0
191 0 my ($callername, $callermode, $callerstart, $callerentry) = @$caller;
192 0 $$callerentry[3] -= $duration; } # Remove our cost from caller's exclusive time.
193 0 ▶ return if $top eq $name; }
Branch: 1 branch
BranchTrueFalseTotal
if $top eq $name 0 0 0
194 0 ▶ print STDERR "\nPROFILE Error: ending $mode of $name but stack is empty\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "expand" 0 0 0
195 unless $mode eq 'expand';
196 0 return; }
197  
198 our $MAX_PROFILE_ENTRIES = 30; # [CONSTANT]
199  
200 # Print out profiling information, if any was collected
201 sub showProfile {
202 0 ▶ if (my $profile = $STATE->lookupValue('runtime_profile')) {
Subroutine showProfile: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (my $profile = $LaTeXML::Core::Definition::STATE->lookupValue("runtime_profile")) 0 0 0
203 0 my @cs = keys %$profile;
204 0 my $calls = 0;
205 0 map { $calls += $$profile{$_}[0] } @cs;
206 0 my $depth = $STATE->lookupValue('runtime_maxdepth');
207 0 my @frequent = sort { $$profile{$b}[0] <=> $$profile{$a}[0] } @cs;
208 0 @frequent = @frequent[0 .. $MAX_PROFILE_ENTRIES];
209 0 my @deepest = sort { $$profile{$b}[1] <=> $$profile{$a}[1] } @cs;
210 0 @deepest = @deepest[0 .. $MAX_PROFILE_ENTRIES];
211 0 my @inclusive = sort { $$profile{$b}[2] <=> $$profile{$a}[2] } @cs;
212 0 @inclusive = @inclusive[0 .. $MAX_PROFILE_ENTRIES];
213 0 my @exclusive = sort { $$profile{$b}[3] <=> $$profile{$a}[3] } @cs;
214 0 @exclusive = @exclusive[0 .. $MAX_PROFILE_ENTRIES];
215 0 print STDERR "\nProfiling results:\n";
216 0 print STDERR "Total calls: $calls; Maximum depth: $depth\n";
217 print STDERR "Most frequent:\n "
218 0 . join(', ', map { $_ . ':' . $$profile{$_}[0] } @frequent) . "\n";
219 print STDERR "Deepest :\n "
220 0 . join(', ', map { $_ . ':' . $$profile{$_}[1] } @deepest) . "\n";
221 print STDERR "Most expensive inclusive:\n "
222 0 . join(', ', map { $_ . ':' . sprintf("%.2fs", $$profile{$_}[2]) } @inclusive) . "\n";
223 print STDERR "Most expensive exclusive:\n "
224 0 . join(', ', map { $_ . ':' . sprintf("%.2fs", $$profile{$_}[3]) } @exclusive) . "\n";
225  
226 0 my $stack = $STATE->lookupValue('runtime_stack');
227 0 ▶ if (@$stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack) 0 0 0
228 print STDERR "The following were never marked as done:\n "
229 0 . join(', ', map { $$_[0] . '(' . $$_[1] . ')' } @$stack) . "\n"; } }
230 0 return; }
231  
232 #===============================================================================
233 1;
234  
235 __END__
« lib/LaTeXML/Post/Split.pm ↑ Summary lib/LaTeXML/Common/Locator.pm »