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

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 282 CC 34 SCAR 43.4
CC
34
Cov
0%
skipConditionalBody
156.0
invoke_conditional
132.0
invoke
42.0
CRAP 76.6
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 %traits }, $class; }
33  
34 sub isExpandable {
35 return 1; }
36  
37 sub getTest {
38 my ($self) = @_;
39 return $$self{test}; }
40  
41 # This MUST return Tokens() or undef. (NOT a Token)
42 sub invoke {
43 my ($self, $gullet) = @_;
44 # A real conditional must have condition_type set
45 if (my $cond_type = $$self{conditional_type}) {
46 if (($cond_type eq 'if') || ($cond_type eq 'unless')) {
47 return $self->invoke_conditional($gullet); }
48 elsif ($cond_type eq 'else') {
49 return $self->invoke_else($gullet); }
50 elsif ($cond_type eq 'or') {
51 return $self->invoke_else($gullet); }
52 elsif ($cond_type eq 'fi') {
53 return $self->invoke_fi($gullet); } }
54 Error('unexpected', $$self{cs}, $gullet,
55 "Unknown conditional control sequence " . Stringify($LaTeXML::CURRENT_TOKEN));
56 return; }
57  
58 sub invoke_conditional {
59 my ($self, $gullet) = @_;
60 # Keep a stack of the conditionals we are processing.
61 my $ifid = $STATE->lookupValue('if_count') || 0;
62 $STATE->assignValue(if_count => ++$ifid, 'global');
63 if ($LaTeXML::IF_LIMIT and $ifid > $LaTeXML::IF_LIMIT) {
64 Fatal('timeout', 'if_limit', $self,
65 "Conditional limit of $LaTeXML::IF_LIMIT exceeded, infinite loop?"); }
66 local $LaTeXML::IFFRAME = { token => $LaTeXML::CURRENT_TOKEN, start => $gullet->getLocator,
67 parsing => 1, elses => 0, ifid => $ifid };
68 $STATE->unshiftValue(if_stack => $LaTeXML::IFFRAME);
69 my $parms = $$self{parameters};
70 my @args = ($parms ? $parms->readArguments($gullet) : ());
71 $$LaTeXML::IFFRAME{parsing} = 0; # Now, we're done parsing the Test clause.
72 my $tracing = ($STATE->lookupValue('TRACING') || 0) & TRACE_COMMANDS;
73 if ($tracing) {
74 Debug('{' . $self->tracingCSName . "} [#$ifid]");
75 Debug($self->tracingArgs(@args)) if @args; }
76 if (my $test = $self->getTest) {
77 my $result = &$test($gullet, @args);
78 if ($result) {
79 Debug("{true}") if $tracing; }
80 else {
81 my $to = skipConditionalBody($gullet, -1);
82 Debug("{false} [skipped to " . ToString($to) . "]") if $tracing; } }
83 # If there's no test, it must be the Special Case, \ifcase
84 else {
85 my $num = $args[0]->valueOf;
86 if ($num > 0) {
87 my $to = skipConditionalBody($gullet, $num);
88 Debug("{$num} [skipped to " . ToString($to) . "]") if $tracing; } }
89 return; }
90  
91 #======================================================================
92 # Support for conditionals:
93  
94 # Skipping for conditionals
95 # 0 : skip to \fi
96 # -1 : skip to \else, if any, or \fi
97 # n : skip to n-th \or, if any, or \else, if any, or \fi.
98  
99 # NOTE that there are 2 kinds of "nested" ifs.
100 # \if's inside the body of either the true or false branch
101 # are easily skipped by tracking a level of if nesting and skipping over the
102 # same number of \fi as you find \if.
103 # \if's that get expanded while evaluating the test clause itself
104 # are considerably trickier. There's a frame on the if-stack for this \if
105 # that's above the one we're currently processing; typically the \else & \fi
106 # may still remain, but we need to either evaluate them a normal
107 # if we're continuing to follow the true branch, or skip oever them if
108 # we're trying to find the \else for the false branch.
109 # The danger is mistaking the \else that's associated with the test clause's \if
110 # and taking it for the \else that we're skipping to!
111 # Canonical example:
112 # \if\ifx AA XY junk \else blah \fi True \else False \fi
113 # The inner \ifx should expand to "XY junk", since A==A
114 # Return the token we've skipped to, and the frame that this applies to.
115 sub skipConditionalBody {
116 my ($gullet, $nskips) = @_;
117 my $level = 1;
118 my $n_ors = 0;
119 my $start = $gullet->getLocator;
120 # NOTE: Open-coded manipulation of if_stack!, Gullet and Token's; Must be fast!
121 # [we're only reading tokens & looking up, so State shouldn't change behind our backs]
122 my $stack = $STATE->lookupValue('if_stack');
123 while (1) {
124 my ($t, $cond_type);
125 while ($t = shift(@{ $$gullet{pushback} }) || $$gullet{mouth}->readToken()) {
126 my $cc = $$t[1];
127 if ($cc == CC_BEGIN) { $LaTeXML::ALIGN_STATE++; }
128 elsif ($cc == CC_END) { $LaTeXML::ALIGN_STATE--; }
129 elsif ($LaTeXML::Core::State::CATCODE_ACTIVE_OR_CS[$cc]
130 && ($cond_type = $STATE->lookupConditional($t))) {
131 last; } }
132 last unless $cond_type;
133 if ($cond_type eq 'if') { # Found a \ifxx of some sort
134 $level++; }
135 elsif ($cond_type eq 'fi') { # Found a \fi
136 if ($$stack[0] ne $LaTeXML::IFFRAME) {
137 # But is it for a condition nested in the test clause?
138 shift(@$stack); } # then DO pop that conditional's frame; it's DONE!
139 elsif (!--$level) { # If no more nesting, we're done.
140 shift(@$stack); # Done with this frame
141 return TokensI($t); } } # AND Return the finishing token.
142 elsif ($level > 1) { } # Ignore \else,\or nested in the body.
143 elsif (($cond_type eq 'or') && (++$n_ors == $nskips)) {
144 return TokensI($t); }
145 elsif (($cond_type eq 'else') && $nskips
146 # Found \else and we're looking for one?
147 # Make sure this \else is NOT for a nested \if that is part of the test clause!
148 && ($$stack[0] eq $LaTeXML::IFFRAME)) {
149 # No need to actually call elseHandler, but note that we've seen an \else!
150 $$stack[0]{elses} = 1;
151 return TokensI($t); } } # } #}
152 Error('expected', '\fi', $gullet, "Missing \\fi or \\else, conditional fell off end",
153 "Conditional started at " . ToString($start));
154 return; }
155  
156 sub invoke_else {
157 my ($self, $gullet) = @_;
158 my $stack = $STATE->lookupValue('if_stack');
159 if (!($stack && $$stack[0])) { # No if stack entry ?
160 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
161 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
162 . " since we seem not to be in a conditional");
163 return; }
164 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
165 return TokensI(T_CS('\relax'), $LaTeXML::CURRENT_TOKEN); }
166 elsif ($$stack[0]{elses}) { # Already seen an \else's at this level?
167 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
168 "Extra " . Stringify($LaTeXML::CURRENT_TOKEN),
169 "already saw \\else for " . Stringify($$stack[0]{token}) . " [" . $$stack[0]{ifid} . "] at " . ToString($$stack[0]{start}));
170 return; }
171 else {
172 local $LaTeXML::IFFRAME = $$stack[0];
173 my $t = skipConditionalBody($gullet, 0);
174 Debug('{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
175 . " [for " . ToString($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid}
176 . " skipping to " . ToString($t) . "]")
177 if ($STATE->lookupValue('TRACING') || 0) & TRACE_COMMANDS;
178 return; } }
179  
180 sub invoke_fi {
181 my ($self, $gullet) = @_;
182 my $stack = $STATE->lookupValue('if_stack');
183 if (!($stack && $$stack[0])) { # No if stack entry ?
184 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $gullet,
185 "Didn't expect a " . Stringify($LaTeXML::CURRENT_TOKEN)
186 . " since we seem not to be in a conditional");
187 return; }
188 elsif ($$stack[0]{parsing}) { # Defer expanding the \else if we're still parsing the test
189 return TokensI(T_CS('\relax'), $LaTeXML::CURRENT_TOKEN); }
190 else { # "expand" by removing the stack entry for this level
191 local $LaTeXML::IFFRAME = $$stack[0];
192 $STATE->shiftValue('if_stack'); # Done with this frame
193 Debug('{' . ToString($LaTeXML::CURRENT_TOKEN) . '}'
194 . " [for " . Stringify($$LaTeXML::IFFRAME{token}) . " #" . $$LaTeXML::IFFRAME{ifid} . "]")
195 if ($STATE->lookupValue('TRACING') || 0) & TRACE_COMMANDS;
196 return; } }
197  
198 sub equals {
199 my ($self, $other) = @_;
200 return (defined $other && (ref $self) eq (ref $other))
201 && Equals($self->getParameters, $other->getParameters)
202 && Equals($self->getTest, $other->getTest); }
203  
204 #===============================================================================
205 1;
206  
207 __END__