lib/LaTeXML/Core/Definition/Conditional.pm

filter: Statementstmt 22.4% 22 / 98 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 7 / 14 Podpod 14.2% 1 / 7 totaltotal 14.4% 30 / 207 CC 36 SCAR 44.0
CC
36
Cov
13%
skipConditionalBody
156.0
invoke_conditional
132.0
invoke
42.0
CRAP 81.4
« lib/LaTeXML/MathParser.pm ↑ Summary lib/LaTeXML/Core/Document.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Definition::Conditional | #
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::Conditional;
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::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use base qw(LaTeXML::Core::Definition::Expandable);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Conditional control sequences; Expandable
22 # Expand enough to determine true/false, then maybe skip
23 # record a flag somewhere so that \else or \fi is recognized
24 # (otherwise, they should signal an error)
25  
26 sub new {
27 0 ▶ my ($class, $cs, $parameters, $test, %traits) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my $source = $STATE->getStomach->getGullet->getMouth;
29 0 return bless { cs => $cs, parameters => $parameters, test => $test,
30 locator => $source->getLocator,
31 isExpandable => 1,
32 %traits }, $class; }
33  
34 sub getTest {
35 0 ▶ my ($self) = @_;
Subroutine getTest: not called CC=1 SCAR=6.9
Pod undocumented
36 0 return $$self{test}; }
37  
38 # Note that although conditionals are Expandable,
39 # they do NOT defined as macros, so they don't need to handle doInvocation,
40 sub invoke {
41 0 ▶ my ($self, $gullet) = @_;
Subroutine invoke: not called CC=6 SCAR=37.4
Pod documented
42 # A real conditional must have condition_type set
43 0 ▶ if (my $cond_type = $$self{conditional_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cond_type = $self->{"conditional_type"}) 0 0 0
44 0 ▶ if ($cond_type eq 'if') {
Branch: 4 branches
BranchTrueFalseTotal
if ($cond_type eq 'if') { } 0 0 0
elsif ($cond_type eq 'else') { } 0 0 0
elsif ($cond_type eq 'or') { } 0 0 0
elsif ($cond_type eq 'fi') { } 0 0 0
45 0 return $self->invoke_conditional($gullet); }
46 elsif ($cond_type eq 'else') {
47 0 return $self->invoke_else($gullet); }
48 elsif ($cond_type eq 'or') {
49 0 return $self->invoke_else($gullet); }
50 elsif ($cond_type eq 'fi') {
51 0 return $self->invoke_fi($gullet); } }
52 0 Error('unexpected', $$self{cs}, $gullet,
53 "Unknown conditional control sequence " . Stringify($LaTeXML::CURRENT_TOKEN));
54 0 return; }
55  
56 sub invoke_conditional {
57 0 ▶ my ($self, $gullet) = @_;
Subroutine invoke_conditional: not called CC=11 SCAR=48.8
Pod undocumented
58 # Keep a stack of the conditionals we are processing.
59 0 ▶ my $ifid = $STATE->lookupValue('if_count') || 0;
Condition0%0/2
$LaTeXML::Core::Definition::Conditional::STATE->lookupValue('if_count') || 0
l!l
00
MC/DC0%0/1
$LaTeXML::Core::Definition::Conditional::STATE->lookupValue('if_count') || 0
$LaTeXML::Core::Definition::Conditional::STATE->lookupValue('if_count')
Truth table0%0/2
A
A= $LaTeXML::Core::Definition::Conditional::STATE->lookupValue('if_count')
Aresult
11
00
60 0 $STATE->assignValue(if_count => ++$ifid, 'global');
61 0 local $LaTeXML::IFFRAME = { token => $LaTeXML::CURRENT_TOKEN, start => $gullet->getLocator,
62 parsing => 1, elses => 0, ifid => $ifid };
63 0 $STATE->unshiftValue(if_stack => $LaTeXML::IFFRAME);
64  
65 0 my @args = $self->readArguments($gullet);
66 0 $$LaTeXML::IFFRAME{parsing} = 0; # Now, we're done parsing the Test clause.
67 0 my $tracing = $STATE->lookupValue('TRACINGCOMMANDS');
68 0 ▶ print STDERR '{' . $self->tracingCSName . "} [#$ifid]\n" if $tracing;
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 0 0 0
69 0 ▶ print STDERR $self->tracingArgs(@args) . "\n" if $tracing && @args;
Branch: 1 branch
BranchTrueFalseTotal
if $tracing and @args 0 0 0
Condition0%0/3
$tracing and @args
!ll&&!rl&&r
000
MC/DC0%0/2
$tracing and @args
$tracing @args
Truth table0%0/3
A and B
A= $tracingB= @args
ABresult
0X0
100
111
70 0 ▶ if (my $test = $self->getTest) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $test = $self->getTest) { } 0 0 0
71 0 my $result = &$test($gullet, @args);
72 0 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
73 0 ▶ print STDERR "{true}\n" if $tracing; }
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 0 0 0
74 else {
75 0 my $to = skipConditionalBody($gullet, -1);
76 0 ▶ print STDERR "{false} [skipped to " . ToString($to) . "]\n" if $tracing; } }
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 0 0 0
77 # If there's no test, it must be the Special Case, \ifcase
78 else {
79 0 my $num = $args[0]->valueOf;
80 0 ▶ if ($num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 0 0 0
81 0 my $to = skipConditionalBody($gullet, $num);
82 0 ▶ print STDERR "{$num} [skipped to " . ToString($to) . "]\n" if $tracing; } }
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 0 0 0
83 0 return; }
84  
85 #======================================================================
86 # Support for conditionals:
87  
88 # Skipping for conditionals
89 # 0 : skip to \fi
90 # -1 : skip to \else, if any, or \fi
91 # n : skip to n-th \or, if any, or \else, if any, or \fi.
92  
93 # NOTE that there are 2 kinds of "nested" ifs.
94 # \if's inside the body of either the true or false branch
95 # are easily skipped by tracking a level of if nesting and skipping over the
96 # same number of \fi as you find \if.
97 # \if's that get expanded while evaluating the test clause itself
98 # are considerably trickier. There's a frame on the if-stack for this \if
99 # that's above the one we're currently processing; typically the \else & \fi
100 # may still remain, but we need to either evaluate them a normal
101 # if we're continuing to follow the true branch, or skip oever them if
102 # we're trying to find the \else for the false branch.
103 # The danger is mistaking the \else that's associated with the test clause's \if
104 # and taking it for the \else that we're skipping to!
105 # Canonical example:
106 # \if\ifx AA XY junk \else blah \fi True \else False \fi
107 # The inner \ifx should expand to "XY junk", since A==A
108 # Return the token we've skipped to, and the frame that this applies to.
109 sub skipConditionalBody {
110 0 ▶ my ($gullet, $nskips) = @_;
Subroutine skipConditionalBody: not called CC=12 SCAR=50.5
Pod undocumented
111 0 my $level = 1;
112 0 my $n_ors = 0;
113 0 my $start = $gullet->getLocator;
114 # NOTE: Open-coded manipulation of if_stack!
115 # [we're only reading tokens & looking up, so State shouldn't change behind our backs]
116 0 my $stack = $STATE->lookupValue('if_stack');
117 0 while (1) {
118 0 my ($t, $cond_type) = $gullet->readNextConditional;
119 0 ▶ last unless $cond_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_type 0 0 0
120 0 ▶ if ($cond_type eq 'if') { # Found a \ifxx of some sort
Branch: 5 branches
BranchTrueFalseTotal
if ($cond_type eq 'if') { } 0 0 0
elsif ($cond_type eq 'fi') { } 0 0 0
elsif ($level > 1) { } 0 0 0
elsif ($cond_type eq 'or' and ++$n_ors == $nskips) { } 0 0 0
elsif ($cond_type eq 'else' and $nskips and $stack->[0] eq $LaTeXML::IFFRAME) { } 0 0 0
121 0 $level++; }
122 elsif ($cond_type eq 'fi') { # Found a \fi
123 0 ▶ if ($$stack[0] ne $LaTeXML::IFFRAME) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stack->[0] ne $LaTeXML::IFFRAME) { } 0 0 0
elsif (not --$level) { } 0 0 0
124 # But is it for a condition nested in the test clause?
125 0 shift(@$stack); } # then DO pop that conditional's frame; it's DONE!
126 elsif (!--$level) { # If no more nesting, we're done.
127 0 shift(@$stack); # Done with this frame
128 0 return $t; } } # AND Return the finishing token.
129 elsif ($level > 1) { # Ignore \else,\or nested in the body.
130 }
131 ▶ elsif (($cond_type eq 'or') && (++$n_ors == $nskips)) {
Condition0%0/3
$cond_type eq "or" and ++$n_ors == $nskips
!ll&&!rl&&r
000
MC/DC0%0/2
$cond_type eq "or" and ++$n_ors == $nskips
$cond_type eq "or" ++$n_ors == $nskips
Truth table0%0/3
A and B
A= $cond_type eq "or"B= ++$n_ors == $nskips
ABresult
0X0
100
111
132 0 return $t; }
133 ▶ elsif (($cond_type eq 'else') && $nskips
Condition0%0/6
$cond_type eq "else" and $nskips and $stack->[0] eq $LaTeXML::IFFRAME
!ll&&!rl&&r
000
$cond_type eq "else" and $nskips
!ll&&!rl&&r
000
MC/DC0%0/3
$cond_type eq "else" and $nskips and $stack->[0] eq $LaTeXML::IFFRAME
$cond_type eq "else" $nskips $stack->[0] eq $LaTeXML::IFFRAME
Truth table0%0/4
A and B and C
A= $cond_type eq "else"B= $nskipsC= $stack->[0] eq $LaTeXML::IFFRAME
ABCresult
0XX0
10X0
1100
1111
134 # Found \else and we're looking for one?
135 # Make sure this \else is NOT for a nested \if that is part of the test clause!
136 && ($$stack[0] eq $LaTeXML::IFFRAME)) {
137 # No need to actually call elseHandler, but note that we've seen an \else!
138 0 $$stack[0]{elses} = 1;
139 0 return $t; } } # } #}
140 0 Error('expected', '\fi', $gullet, "Missing \\fi or \\else, conditional fell off end",
141 "Conditional started at " . ToString($start));
142 0 return; }
143  
144 sub invoke_else {
145 0 ▶ my ($self, $gullet) = @_;
Subroutine invoke_else: not called CC=6 SCAR=37.4
Pod undocumented
146 0 my $stack = $STATE->lookupValue('if_stack');
147 0 ▶ if (!($stack && $$stack[0])) { # No if stack entry ?
Branch: 3 branches
BranchTrueFalseTotal
if (not $stack && $stack->[0]) { } 0 0 0
elsif ($stack->[0]{'parsing'}) { } 0 0 0
elsif ($stack->[0]{'elses'}) { } 0 0 0
Condition0%0/3
$stack && $stack->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$stack && $stack->[0]
$stack $stack->[0]
Truth table0%0/3
A && B
A= $stackB= $stack->[0]
ABresult
0X0
100
111
148 0 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
149 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
150 . " since we seem not to be in a conditional");
151 0 return; }
152 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
153 0 return [T_CS('\relax'), $LaTeXML::CURRENT_TOKEN]; }
154 elsif ($$stack[0]{elses}) { # Already seen an \else's at this level?
155 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
156 "Extra " . Stringify($LaTeXML::CURRENT_TOKEN),
157 0 "already saw \\else for " . Stringify($$stack[0]{token}) . " [" . $$stack[0]{ifid} . "] at " . ToString($$stack[0]{start}));
158 0 return; }
159 else {
160 0 local $LaTeXML::IFFRAME = $$stack[0];
161 0 my $t = skipConditionalBody($gullet, 0);
162 print STDERR '{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
163 . " [for " . ToString($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid}
164 0 ▶ . " skipping to " . ToString($t) . "]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::Core::Definition::Conditional::STATE->lookupValue("TRACINGCOMMANDS") 0 0 0
165 if $STATE->lookupValue('TRACINGCOMMANDS');
166 0 return; } }
167  
168 sub invoke_fi {
169 0 ▶ my ($self, $gullet) = @_;
Subroutine invoke_fi: not called CC=5 SCAR=34.0
Pod undocumented
170 0 my $stack = $STATE->lookupValue('if_stack');
171 0 ▶ if (!($stack && $$stack[0])) { # No if stack entry ?
Branch: 2 branches
BranchTrueFalseTotal
if (not $stack && $stack->[0]) { } 0 0 0
elsif ($stack->[0]{'parsing'}) { } 0 0 0
Condition0%0/3
$stack && $stack->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$stack && $stack->[0]
$stack $stack->[0]
Truth table0%0/3
A && B
A= $stackB= $stack->[0]
ABresult
0X0
100
111
172 0 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
173 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
174 . " since we seem not to be in a conditional");
175 0 return; }
176 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
177 0 return [T_CS('\relax'), $LaTeXML::CURRENT_TOKEN]; }
178 else { # "expand" by removing the stack entry for this level
179 0 local $LaTeXML::IFFRAME = $$stack[0];
180 0 $STATE->shiftValue('if_stack'); # Done with this frame
181 print STDERR '{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
182 0 ▶ . " [for " . Stringify($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid} . "]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::Core::Definition::Conditional::STATE->lookupValue("TRACINGCOMMANDS") 0 0 0
183 if $STATE->lookupValue('TRACINGCOMMANDS');
184 0 return; } }
185  
186 #===============================================================================
187 4 1;
188  
189 __END__
« lib/LaTeXML/MathParser.pm ↑ Summary lib/LaTeXML/Core/Document.pm »