lib/LaTeXML/Core/Definition/Expandable.pm

filter: Statementstmt 33.3% 28 / 84 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 52.9% 9 / 17 Podpod 25.0% 2 / 8 totaltotal 15.6% 39 / 249 CC 45 SCAR 56.5
CC
45
Cov
15%
doInvocation
650.0
new
72.0
recursion_check
42.0
CRAP 283.2
« lib/LaTeXML/Core/Alignment.pm ↑ Summary lib/LaTeXML/Post/LaTeXImages.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  
13 package LaTeXML::Core::Definition::Expandable;
14 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use LaTeXML::Core::Tokens;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use LaTeXML::Core::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use base qw(LaTeXML::Core::Definition);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new {
25 0 ▶ my ($class, $cs, $parameters, $expansion, %traits) = @_;
Subroutine new: not called CC=8 SCAR=42.8
Pod undocumented
26 0 ▶ $expansion = Tokens($expansion) if ref $expansion eq 'LaTeXML::Core::Token';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expansion eq "LaTeXML::Core::Token" 0 0 0
27 0 my $source = $STATE->getStomach->getGullet->getMouth;
28 0 my $trivexpansion;
29 0 ▶ if (ref $expansion eq 'LaTeXML::Core::Tokens') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expansion eq "LaTeXML::Core::Tokens") 0 0 0
30 0 ▶ Fatal('misdefined', $cs, $source, "Expansion of '" . ToString($cs) . "' has unbalanced {}",
Branch: 1 branch
BranchTrueFalseTotal
unless $expansion->isBalanced 0 0 0
31 "Expansion is " . ToString($expansion)) unless $expansion->isBalanced;
32 # If expansion is Tokens, and no arguments, we're a "trivial macro"
33 0 ▶ if (!$parameters) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parameters) 0 0 0
34 0 $trivexpansion = $expansion->substituteParameters(); }
35 }
36 return bless { cs => $cs, parameters => $parameters, expansion => $expansion,
37 trivial_expansion => $trivexpansion,
38 locator => $source->getLocator,
39 isProtected => $traits{protected} || $STATE->getPrefix('protected'),
40 isOuter => $traits{outer} || $STATE->getPrefix('outer'),
41 0 ▶ isLong => $traits{long} || $STATE->getPrefix('long'),
Condition0%0/9
$traits{'protected'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('protected')
l!l&&r!l&&!r
000
$traits{'outer'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('outer')
l!l&&r!l&&!r
000
$traits{'long'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('long')
l!l&&r!l&&!r
000
MC/DC0%0/2
$traits{'protected'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('protected')
$traits{'protected'} $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('protected')
MC/DC0%0/2
$traits{'outer'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('outer')
$traits{'outer'} $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('outer')
MC/DC0%0/2
$traits{'long'} || $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('long')
$traits{'long'} $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('long')
Truth table0%0/3
A || B
A= $traits{'protected'}B= $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('protected')
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $traits{'outer'}B= $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('outer')
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $traits{'long'}B= $LaTeXML::Core::Definition::Expandable::STATE->getPrefix('long')
ABresult
1X1
011
000
42 isExpandable => 1,
43 %traits }, $class; }
44  
45 sub isExpandable {
46 0 ▶ return 1; }
Subroutine isExpandable: not called CC=1 SCAR=6.9
Pod undocumented
47  
48 sub getExpansion {
49 0 ▶ my ($self) = @_;
Subroutine getExpansion: not called CC=2 SCAR=17.9
Pod undocumented
50 0 ▶ if (!ref $$self{expansion}) { # Tokenization DEFERRED till actually used (shaves > 5%)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"expansion"}) 0 0 0
51 0 require LaTeXML::Package; # make sure present, but no imports
52 0 $$self{expansion} = LaTeXML::Package::TokenizeInternal($$self{expansion}); }
53 0 return $$self{expansion}; }
54  
55 # Expand the expandable control sequence. This should be carried out by the Gullet.
56 sub invoke {
57 0 ▶ my ($self, $gullet) = @_;
Subroutine invoke: not called CC=4 SCAR=30.0
Pod documented
58 # shortcut for "trivial" macros; but only if not tracing & profiling!!!!
59 0 ▶ if (my $triv = (!$STATE->lookupValue('TRACINGMACROS')) && $$self{trivial_expansion}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $triv = !$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS') && $self->{'trivial_expansion'}) { } 0 0 0
Condition0%0/3
!$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS') && $self->{'trivial_expansion'}
!ll&&!rl&&r
000
MC/DC0%0/2
!$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS') && $self->{'trivial_expansion'}
!$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS') $self->{'trivial_expansion'}
Truth table0%0/3
not(A) && B
A= !$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS')B= $self->{'trivial_expansion'}
ABresult
0X0
100
111
60 0 ▶ if (recursion_check($$self{cs}, $triv->unlist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (recursion_check($self->{"cs"}, $triv->unlist)) 0 0 0
61 Error('recursion', $$self{cs}, $gullet,
62 0 "Token " . Stringify($$self{cs}) . " expands into itself!",
63 "defining as empty");
64 0 $triv = Tokens(); }
65 0 return $triv; }
66 else {
67 0 return $self->doInvocation($gullet, $self->readArguments($gullet)); } }
68  
69 sub doInvocation {
70 0 ▶ my ($self, $gullet, @args) = @_;
Subroutine doInvocation: not called CC=25 SCAR=64.8
Pod undocumented
71 0 my $expansion = $self->getExpansion;
72 0 my $r;
73 0 ▶ my $profiled = $STATE->lookupValue('PROFILING') && ($LaTeXML::CURRENT_TOKEN || $$self{cs});
Condition0%0/6
$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('PROFILING') && ($LaTeXML::CURRENT_TOKEN || $self->{'cs'})
!ll&&!rl&&r
000
$LaTeXML::CURRENT_TOKEN or $self->{"cs"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('PROFILING') && ($LaTeXML::CURRENT_TOKEN || $self->{'cs'})
$LaTeXML::Core::Definition::Expandable::STATE->lookupValue('PROFILING') $LaTeXML::CURRENT_TOKEN $self->{"cs"}
Truth table0%0/4
A && B or C
A= $LaTeXML::Core::Definition::Expandable::STATE->lookupValue('PROFILING')B= $LaTeXML::CURRENT_TOKENC= $self->{"cs"}
ABCresult
0XX0
1000
11X1
1011
74 0 ▶ LaTeXML::Core::Definition::startProfiling($profiled, 'expand') if $profiled;
Branch: 1 branch
BranchTrueFalseTotal
if $profiled 0 0 0
75 0 my @result;
76 0 ▶ if ($STATE->lookupValue('TRACINGMACROS')) { # More involved...
Branch: 1 branch
BranchTrueFalseTotal
if ($LaTeXML::Core::Definition::Expandable::STATE->lookupValue('TRACINGMACROS')) { } 0 0 0
77 0 ▶ if (ref $expansion eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expansion eq 'CODE') { } 0 0 0
78 # Harder to emulate \tracingmacros here.
79 0 @result = &$expansion($gullet, @args);
80 # CHECK @result HERE TOO!!!!
81 0 print STDERR "\n" . $self->tracingCSName . ' ==> ' . tracetoString(Tokens(@result)) . "\n";
82 0 ▶ print STDERR $self->tracingArgs(@args) . "\n" if @args; }
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
83 else {
84 # for "real" macros, make sure all args are Tokens
85 0 ▶ my @targs = map { $_ && (($r = ref $_) && ($r eq 'LaTeXML::Core::Tokens')
Branch: 3 branches
BranchTrueFalseTotal
$_ and ($r = ref $_) && $r eq "LaTeXML::Core::Tokens" ? $_ : ($r && $r eq "LaTeXML::Core::Token" ? Tokens($_) : Tokens(Revert($_))) 0 0 0
($r = ref $_) && $r eq 'LaTeXML::Core::Tokens' ? : 0 0 0
$r && $r eq 'LaTeXML::Core::Token' ? : 0 0 0
Condition0%0/6
$r = ref $_ and $r eq "LaTeXML::Core::Tokens"
!ll&&!rl&&r
000
$r and $r eq "LaTeXML::Core::Token"
!ll&&!rl&&r
000
MC/DC0%0/2
$r = ref $_ and $r eq "LaTeXML::Core::Tokens"
$r = ref $_ $r eq "LaTeXML::Core::Tokens"
MC/DC0%0/2
$r and $r eq "LaTeXML::Core::Token"
$r $r eq "LaTeXML::Core::Token"
Truth table0%0/3
A and B
A= $r = ref $_B= $r eq "LaTeXML::Core::Tokens"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rB= $r eq "LaTeXML::Core::Token"
ABresult
0X0
100
111
86 ? $_
87 : ($r && ($r eq 'LaTeXML::Core::Token')
88 ? Tokens($_)
89 : Tokens(Revert($_)))) }
90 @args;
91 0 print STDERR "\n" . $self->tracingCSName
92 . ' -> ' . tracetoString($expansion) . "\n";
93 0 ▶ print STDERR $self->tracingArgs(@targs) . "\n" if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
94 0 @result = $expansion->substituteParameters(@targs)->unlist; } }
95 else {
96 0 ▶ if (ref $expansion eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expansion eq 'CODE') { } 0 0 0
97 0 my $t;
98 # Check the result from code calls.
99 0 ▶ @result = map { (($t = ref $_) eq 'LaTeXML::Core::Token' ? $_
Branch: 2 branches
BranchTrueFalseTotal
($t = ref $_) eq 'LaTeXML::Core::Token' ? : 0 0 0
$t eq 'LaTeXML::Core::Tokens' ? : 0 0 0
100 : ($t eq 'LaTeXML::Core::Tokens' ? @$_
101 : (Error('misdefined', $self, undef,
102 "Expected a Token in expansion of " . ToString($self),
103 "got " . Stringify($_)), ()))) }
104 &$expansion($gullet, @args); }
105 else {
106 # but for tokens, make sure args are proper Tokens (lists)
107 @result = $expansion->substituteParameters(
108 0 ▶ map { $_ && (($r = ref $_) && ($r eq 'LaTeXML::Core::Tokens')
Branch: 3 branches
BranchTrueFalseTotal
$_ and ($r = ref $_) && $r eq "LaTeXML::Core::Tokens" ? $_ : ($r && $r eq "LaTeXML::Core::Token" ? Tokens($_) : Tokens(Revert($_))) 0 0 0
($r = ref $_) && $r eq 'LaTeXML::Core::Tokens' ? : 0 0 0
$r && $r eq 'LaTeXML::Core::Token' ? : 0 0 0
Condition0%0/6
$r = ref $_ and $r eq "LaTeXML::Core::Tokens"
!ll&&!rl&&r
000
$r and $r eq "LaTeXML::Core::Token"
!ll&&!rl&&r
000
MC/DC0%0/2
$r = ref $_ and $r eq "LaTeXML::Core::Tokens"
$r = ref $_ $r eq "LaTeXML::Core::Tokens"
MC/DC0%0/2
$r and $r eq "LaTeXML::Core::Token"
$r $r eq "LaTeXML::Core::Token"
Truth table0%0/3
A and B
A= $r = ref $_B= $r eq "LaTeXML::Core::Tokens"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rB= $r eq "LaTeXML::Core::Token"
ABresult
0X0
100
111
109 ? $_
110 : ($r && ($r eq 'LaTeXML::Core::Token')
111 ? Tokens($_)
112 : Tokens(Revert($_)))) }
113 @args)->unlist; } }
114 # Avoid simplest case of infinite-loop expansion.
115 0 ▶ if ((ref $expansion ne 'CODE') && !scalar(@args) && recursion_check($$self{cs}, @result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expansion ne "CODE" and not scalar @args and recursion_check($self->{"cs"}, @result)) 0 0 0
Condition0%0/6
ref $expansion ne "CODE" and not scalar @args and recursion_check($self->{"cs"}, @result)
!ll&&!rl&&r
000
ref $expansion ne "CODE" and not scalar @args
!ll&&!rl&&r
000
MC/DC0%0/3
ref $expansion ne "CODE" and not scalar @args and recursion_check($self->{"cs"}, @result)
ref $expansion ne "CODE" not scalar @args recursion_check($self->{"cs"}, @result)
Truth table0%0/4
A and not(B) and C
A= ref $expansion ne "CODE"B= not scalar @argsC= recursion_check($self->{"cs"}, @result)
ABCresult
0XX0
10X0
1100
1111
116 Error('recursion', $$self{cs}, $gullet,
117 0 "Token " . Stringify($$self{cs}) . " expands into itself!",
118 "defining as empty");
119 0 @result = (); }
120 # Getting exclusive requires dubious Gullet support!
121 0 ▶ push(@result, T_MARKER($profiled)) if $profiled;
Branch: 1 branch
BranchTrueFalseTotal
if $profiled 0 0 0
122 0 return [@result]; }
123  
124 sub recursion_check {
125 0 ▶ my ($cs, @tokens) = @_;
Subroutine recursion_check: not called CC=6 SCAR=37.4
Pod undocumented
126 # expect $expansion as Token, Tokens or [Token...] ! Argh !
127 0 ▶ return $cs &&
Condition0%0/15
$cs && ($tokens[0] && $tokens[0]->equals($cs) || $tokens[1] && $tokens[1]->equals($cs) && $tokens[0]->equals(T_CS('\\protect')))
!ll&&!rl&&r
000
$tokens[0] and $tokens[0]->equals($cs) or $tokens[1] and $tokens[1]->equals($cs) and $tokens[0]->equals(T_CS("\\protect"))
l!l&&r!l&&!r
000
$tokens[0] and $tokens[0]->equals($cs)
!ll&&!rl&&r
000
$tokens[1] and $tokens[1]->equals($cs) and $tokens[0]->equals(T_CS("\\protect"))
!ll&&!rl&&r
000
$tokens[1] and $tokens[1]->equals($cs)
!ll&&!rl&&r
000
MC/DC0%0/6
$cs && ($tokens[0] && $tokens[0]->equals($cs) || $tokens[1] && $tokens[1]->equals($cs) && $tokens[0]->equals(T_CS('\\protect')))
$cs $tokens[0] $tokens[0]->equals($cs) $tokens[1] $tokens[1]->equals($cs) $tokens[0]->equals(T_CS("\\protect"))
Truth table0%0/10
A && B and C or D and E and F
A= $csB= $tokens[0]C= $tokens[0]->equals($cs)D= $tokens[1]E= $tokens[1]->equals($cs)F= $tokens[0]->equals(T_CS("\\protect"))
ABCDEFresult
0XXXXX0
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
111XXX1
10X1111
1101111
128 (($tokens[0] && $tokens[0]->equals($cs))
129 || ($tokens[1] && $tokens[1]->equals($cs) && $tokens[0]->equals(T_CS('\protect')))); }
130  
131 # print a string of tokens like TeX would when tracing.
132 sub tracetoString {
133 0 ▶ my ($tokens) = @_;
Subroutine tracetoString: not called CC=2 SCAR=17.9
Pod undocumented
134 0 ▶ return join('', map { ($_->getCatcode == CC_CS ? $_->getString . ' ' : $_->getString) }
Branch: 1 branch
BranchTrueFalseTotal
$_->getCatcode == 16 ? : 0 0 0
135 $tokens->unlist); }
136  
137 sub equals {
138 0 ▶ my ($self, $other) = @_;
Subroutine equals: not called CC=4 SCAR=30.0
Pod documented
139 0 ▶ return (defined $other && (ref $self) eq (ref $other))
Condition0%0/9
defined $other && ref $self eq ref $other && Equals($self->getParameters, $other->getParameters) && Equals($self->getExpansion, $other->getExpansion)
!ll&&!rl&&r
000
defined $other and ref $self eq ref $other and Equals($self->getParameters, $other->getParameters)
!ll&&!rl&&r
000
defined $other and ref $self eq ref $other
!ll&&!rl&&r
000
MC/DC0%0/4
defined $other && ref $self eq ref $other && Equals($self->getParameters, $other->getParameters) && Equals($self->getExpansion, $other->getExpansion)
defined $other ref $self eq ref $other Equals($self->getParameters, $other->getParameters) Equals($self->getExpansion, $other->getExpansion)
Truth table0%0/5
A and B and C && D
A= defined $otherB= ref $self eq ref $otherC= Equals($self->getParameters, $other->getParameters)D= Equals($self->getExpansion, $other->getExpansion)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
140 && Equals($self->getParameters, $other->getParameters)
141 && Equals($self->getExpansion, $other->getExpansion); }
142  
143 #======================================================================
144 4 1;
145  
146 __END__
« lib/LaTeXML/Core/Alignment.pm ↑ Summary lib/LaTeXML/Post/LaTeXImages.pm »