lib/LaTeXML/Core/Definition/Conditional.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 35 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 232 CC 32 SCAR 42.0
CC
32
Cov
0%
skipConditionalBody
132.0
invoke_conditional
110.0
invoke
42.0
CRAP 66.4
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 use strict;
14 use warnings;
15 use LaTeXML::Global;
16 use LaTeXML::Common::Object;
17 use LaTeXML::Common::Error;
18 use LaTeXML::Core::Token;
19 use LaTeXML::Core::Tokens;
20 use base qw(LaTeXML::Core::Definition::Expandable);
21  
22 # Conditional control sequences; Expandable
23 # Expand enough to determine true/false, then maybe skip
24 # record a flag somewhere so that \else or \fi is recognized
25 # (otherwise, they should signal an error)
26  
27 sub new {
28 my ($class, $cs, $parameters, $test, %traits) = @_;
29 my $source = $STATE->getStomach->getGullet->getMouth;
30 return bless { cs => $cs, parameters => $parameters, test => $test,
31 locator => $source->getLocator,
32 isExpandable => 1,
33 %traits }, $class; }
34  
35 sub getTest {
36 my ($self) = @_;
37 return $$self{test}; }
38  
39 sub invoke {
40 my ($self, $gullet) = @_;
41 # A real conditional must have condition_type set
42 if (my $cond_type = $$self{conditional_type}) {
43 if (($cond_type eq 'if') || ($cond_type eq 'unless')) {
44 return $self->invoke_conditional($gullet); }
45 elsif ($cond_type eq 'else') {
46 return $self->invoke_else($gullet); }
47 elsif ($cond_type eq 'or') {
48 return $self->invoke_else($gullet); }
49 elsif ($cond_type eq 'fi') {
50 return $self->invoke_fi($gullet); } }
51 Error('unexpected', $$self{cs}, $gullet,
52 "Unknown conditional control sequence " . Stringify($LaTeXML::CURRENT_TOKEN));
53 return; }
54  
55 sub invoke_conditional {
56 my ($self, $gullet) = @_;
57 # Keep a stack of the conditionals we are processing.
58 my $ifid = $STATE->lookupValue('if_count') || 0;
59 $STATE->assignValue(if_count => ++$ifid, 'global');
60 local $LaTeXML::IFFRAME = { token => $LaTeXML::CURRENT_TOKEN, start => $gullet->getLocator,
61 parsing => 1, elses => 0, ifid => $ifid };
62 $STATE->unshiftValue(if_stack => $LaTeXML::IFFRAME);
63 my $parms = $$self{parameters};
64 my @args = ($parms ? $parms->readArguments($gullet) : ());
65 $$LaTeXML::IFFRAME{parsing} = 0; # Now, we're done parsing the Test clause.
66 my $tracing = $STATE->lookupValue('TRACINGCOMMANDS') || $LaTeXML::DEBUG{tracing};
67 if ($tracing) {
68 Debug('{' . $self->tracingCSName . "} [#$ifid]");
69 Debug($self->tracingArgs(@args)) if @args; }
70 if (my $test = $self->getTest) {
71 my $result = &$test($gullet, @args);
72 if ($result) {
73 Debug("{true}") if $tracing; }
74 else {
75 my $to = skipConditionalBody($gullet, -1);
76 Debug("{false} [skipped to " . ToString($to) . "]") if $tracing; } }
77 # If there's no test, it must be the Special Case, \ifcase
78 else {
79 my $num = $args[0]->valueOf;
80 if ($num > 0) {
81 my $to = skipConditionalBody($gullet, $num);
82 Debug("{$num} [skipped to " . ToString($to) . "]") if $tracing; } }
83 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 my ($gullet, $nskips) = @_;
111 my $level = 1;
112 my $n_ors = 0;
113 my $start = $gullet->getLocator;
114 # NOTE: Open-coded manipulation of if_stack!, Gullet and Token's
115 # [we're only reading tokens & looking up, so State shouldn't change behind our backs]
116 my $stack = $STATE->lookupValue('if_stack');
117 while (1) {
118 my ($t, $cond_type);
119 while ($t = shift(@{ $$gullet{pushback} }) || $$gullet{mouth}->readToken()) {
120 $t = $$t[2] if $$t[1] == CC_SMUGGLE_THE;
121 if ($LaTeXML::Core::State::CATCODE_ACTIVE_OR_CS[$$t[1]]
122 && ($cond_type = $STATE->lookupConditional($t))) {
123 last; } }
124 last unless $cond_type;
125 if ($cond_type eq 'if') { # Found a \ifxx of some sort
126 $level++; }
127 elsif ($cond_type eq 'fi') { # Found a \fi
128 if ($$stack[0] ne $LaTeXML::IFFRAME) {
129 # But is it for a condition nested in the test clause?
130 shift(@$stack); } # then DO pop that conditional's frame; it's DONE!
131 elsif (!--$level) { # If no more nesting, we're done.
132 shift(@$stack); # Done with this frame
133 return $t; } } # AND Return the finishing token.
134 elsif ($level > 1) { } # Ignore \else,\or nested in the body.
135 elsif (($cond_type eq 'or') && (++$n_ors == $nskips)) {
136 return $t; }
137 elsif (($cond_type eq 'else') && $nskips
138 # Found \else and we're looking for one?
139 # Make sure this \else is NOT for a nested \if that is part of the test clause!
140 && ($$stack[0] eq $LaTeXML::IFFRAME)) {
141 # No need to actually call elseHandler, but note that we've seen an \else!
142 $$stack[0]{elses} = 1;
143 return $t; } } # } #}
144 Error('expected', '\fi', $gullet, "Missing \\fi or \\else, conditional fell off end",
145 "Conditional started at " . ToString($start));
146 return; }
147  
148 sub invoke_else {
149 my ($self, $gullet) = @_;
150 my $stack = $STATE->lookupValue('if_stack');
151 if (!($stack && $$stack[0])) { # No if stack entry ?
152 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
153 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
154 . " since we seem not to be in a conditional");
155 return; }
156 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
157 return Tokens(T_CS('\relax'), $LaTeXML::CURRENT_TOKEN); }
158 elsif ($$stack[0]{elses}) { # Already seen an \else's at this level?
159 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
160 "Extra " . Stringify($LaTeXML::CURRENT_TOKEN),
161 "already saw \\else for " . Stringify($$stack[0]{token}) . " [" . $$stack[0]{ifid} . "] at " . ToString($$stack[0]{start}));
162 return; }
163 else {
164 local $LaTeXML::IFFRAME = $$stack[0];
165 my $t = skipConditionalBody($gullet, 0);
166 Debug('{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
167 . " [for " . ToString($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid}
168 . " skipping to " . ToString($t) . "]")
169 if $STATE->lookupValue('TRACINGCOMMANDS') || $LaTeXML::DEBUG{tracing};
170 return; } }
171  
172 sub invoke_fi {
173 my ($self, $gullet) = @_;
174 my $stack = $STATE->lookupValue('if_stack');
175 if (!($stack && $$stack[0])) { # No if stack entry ?
176 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
177 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
178 . " since we seem not to be in a conditional");
179 return; }
180 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
181 return Tokens(T_CS('\relax'), $LaTeXML::CURRENT_TOKEN); }
182 else { # "expand" by removing the stack entry for this level
183 local $LaTeXML::IFFRAME = $$stack[0];
184 $STATE->shiftValue('if_stack'); # Done with this frame
185 Debug('{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
186 . " [for " . Stringify($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid} . "]")
187 if $STATE->lookupValue('TRACINGCOMMANDS') || $LaTeXML::DEBUG{tracing};
188 return; } }
189  
190 #===============================================================================
191 1;
192  
193 __END__