lib/LaTeXML/Core/Stomach.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 96.6% 58 / 60 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/LaTeXML.pm ↑ Summary lib/LaTeXML/Package.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Stomach | #
3 # | Analog of TeX's Stomach: digests tokens, stores state | #
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::Stomach;
14 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use LaTeXML::Core::Tokens;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use LaTeXML::Core::Gullet;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use LaTeXML::Core::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use LaTeXML::Core::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use LaTeXML::Core::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use LaTeXML::Core::Mouth;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use LaTeXML::Common::Font;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # Silly place to import these....?
28 5 ▶use LaTeXML::Common::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 ▶use LaTeXML::Core::Definition;
Subroutine BEGIN: called CC=1 SCAR=2.6
30 use Scalar::Util qw(blessed);
31 use base qw(LaTeXML::Common::Object);
32  
33 #**********************************************************************
34 sub new {
35 my ($class, %options) = @_;
36 return bless { gullet => LaTeXML::Core::Gullet->new(%options),
37 boxing => [], token_stack => [] }, $class; }
38  
39 #**********************************************************************
40 # Initialize various parameters, preload, etc.
41 sub initialize {
42 my ($self) = @_;
43 $$self{boxing} = [];
44 $$self{token_stack} = [];
45 $$self{progress} = 0;
46 $STATE->assignValue(MODE => 'text', 'global');
47 $STATE->assignValue(IN_MATH => 0, 'global');
48 $STATE->assignValue(PRESERVE_NEWLINES => 1, 'global');
49 $STATE->assignValue(afterGroup => [], 'global');
50 $STATE->assignValue(afterAssignment => undef, 'global');
51 $STATE->assignValue(groupInitiator => 'Initialization', 'global');
52 # Setup default fonts.
53 $STATE->assignValue(font => LaTeXML::Common::Font->textDefault(), 'global');
54 $STATE->assignValue(mathfont => LaTeXML::Common::Font->mathDefault(), 'global');
55 return; }
56  
57 our $DIGESTION_PROGRESS_QUANTUM = 30000;
58  
59 #**********************************************************************
60 sub getGullet {
61 my ($self) = @_;
62 return $$self{gullet}; }
63  
64 sub getLocator {
65 my ($self) = @_;
66 return $$self{gullet}->getLocator; }
67  
68 sub getBoxingLevel {
69 my ($self) = @_;
70 return scalar(@{ $$self{boxing} }); }
71  
72 # ScriptLevel is similar to boxing level, but relative to current Math mode's level
73 # This is used for the scriptpos attribute to recognize overlapping sccripts.
74 # Making it relative to the math's level avoids unnecessary changes
75 sub getScriptLevel {
76 my ($self) = @_;
77 my $boxlevel = scalar(@{ $$self{boxing} });
78 if (my $prevlevel = $STATE->lookupValue('script_base_level')) {
79 return $boxlevel - $prevlevel + 1; }
80 else {
81 return $boxlevel; } }
82  
83 #**********************************************************************
84 # Digestion
85 #**********************************************************************
86 # NOTE: Worry about whether the $autoflush thing is right?
87 # It puts a lot of cruft in Gullet; Should we just create a new Gullet?
88  
89 sub digestNextBody {
90 my ($self, $terminal) = @_;
91 no warnings 'recursion';
92 my $startloc = getLocator($self);
93 my $initdepth = scalar(@{ $$self{boxing} });
94 my $token;
95 local @LaTeXML::LIST = ();
96 my $alignment = $STATE->lookupValue('Alignment');
97 my @aug = ();
98  
99 while (defined($token = $$self{gullet}->getPendingComment || $$self{gullet}->readXToken(1))) {
100 if ($alignment && scalar(@LaTeXML::LIST) && (Equals($token, T_ALIGN) ||
101 Equals($token, T_CS('\cr')) || Equals($token, T_CS('\hidden@cr')) ||
102 Equals($token, T_CS('\hidden@crcr')))) {
103 # at least \over calls in here without the intent to passing through the alignment.
104 # So if we already have some digested boxes available, return them here.
105 $$self{gullet}->unread($token);
106 return @LaTeXML::LIST; }
107 my @r = invokeToken($self, $token);
108 push(@LaTeXML::LIST, @r);
109 push(@aug, $token, @r);
110 last if $terminal and Equals($token, $terminal);
111 last if $initdepth > scalar(@{ $$self{boxing} }); } # if we've closed the initial mode.
112 Warn('expected', $terminal, $self, "body should have ended with '" . ToString($terminal) . "'",
113 "current body started at " . ToString($startloc),
114 "Got " . join("\n -- ", map { Stringify($_) } @aug))
115 if $terminal && !Equals($token, $terminal);
116 push(@LaTeXML::LIST, Box()) unless $token; # Dummy `trailer' if none explicit.
117 return @LaTeXML::LIST; }
118  
119 # Digest a list of tokens independent from any current Gullet.
120 # Typically used to digest arguments to primitives or constructors.
121 # Returns a List containing the digested material.
122 sub digest {
123 my ($self, $tokens) = @_;
124 no warnings 'recursion';
125 return unless defined $tokens;
126 return
127 $$self{gullet}->readingFromMouth(LaTeXML::Core::Mouth->new(), sub {
128 my ($gullet) = @_;
129 $gullet->unread($tokens);
130 $STATE->clearPrefixes; # prefixes shouldn't apply here.
131 my $ismath = $STATE->lookupValue('IN_MATH');
132 my $initdepth = scalar(@{ $$self{boxing} });
133 local @LaTeXML::LIST = ();
134 while (defined(my $token =
135 $$self{gullet}->getPendingComment || $$self{gullet}->readXToken(1))) {
136 push(@LaTeXML::LIST, invokeToken($self, $token));
137 last if $initdepth > scalar(@{ $$self{boxing} }); } # if we've closed the initial mode.
138 List(@LaTeXML::LIST, mode => ($ismath ? 'math' : 'text'));
139 }); }
140  
141 # Invoke a token;
142 # If it is a primitive or constructor, the definition will be invoked,
143 # possibly arguments will be parsed from the Gullet.
144 # Otherwise, the token is simply digested: turned into an appropriate box.
145 # Returns a list of boxes/whatsits.
146 my $MAXSTACK = 200; # [CONSTANT]
147  
148 # Overly complex, but want to avoid recursion/stack
149 our @CATCODE_ABSORBABLE = ( # [CONSTANT]
150 0, 0, 0, 0,
151 0, 0, 0, 0,
152 0, 0, 1, 1,
153 1, 0, 1, 0,
154 0, 0, 0, 0);
155  
156 sub invokeToken {
157 no warnings 'recursion';
158 my ($self, $token) = @_;
159 INVOKE:
160 ProgressStep() if ($$self{progress}++ % $DIGESTION_PROGRESS_QUANTUM) == 0;
161 push(@{ $$self{token_stack} }, $token);
162 if (scalar(@{ $$self{token_stack} }) > $MAXSTACK) {
163 Fatal('internal', '<recursion>', $self,
164 "Excessive recursion(?): ",
165 "Tokens on stack: " . join(', ', map { ToString($_) } @{ $$self{token_stack} })); }
166 local $LaTeXML::CURRENT_TOKEN = $token;
167 my @result = ();
168 my $meaning = $STATE->lookupDigestableDefinition($token);
169  
170 if (!$meaning) {
171 @result = invokeToken_undefined($self, $token); }
172 elsif ($meaning->isaToken) { # Common case
173 my $cc = $meaning->getCatcode;
174 if ($cc == CC_CS) {
175 @result = invokeToken_undefined($self, $token); }
176 elsif ($CATCODE_ABSORBABLE[$cc]) {
177 @result = invokeToken_simple($self, $token, $meaning); }
178 else {
179 # Special error guard for the align char "&":
180 # Locally deactivate to avoid a flurry of errors in the same table.
181 # Alert the end user once per table, allowing longer documents to not
182 # hit 100 errors too quickly.
183 $STATE->assignMeaning($token,
184 $STATE->lookupMeaning(T_CS('\relax')), 'local')
185 if Equals($token, T_ALIGN);
186 Error('misdefined', $token, $self,
187 "The token " . Stringify($token) . " should never reach Stomach!");
188 @result = invokeToken_simple($self, $token, $meaning); } }
189 # A math-active character will (typically) be a macro,
190 # but it isn't expanded in the gullet, but later when digesting, in math mode (? I think)
191 elsif ($meaning->isExpandable) {
192 my $gullet = $$self{gullet};
193 $gullet->unread($meaning->invoke($gullet));
194 $token = $gullet->readXToken(); # replace the token by it's expansion!!!
195 pop(@{ $$self{token_stack} });
196 goto INVOKE if $token; }
197 elsif ($meaning->isaDefinition) { # Otherwise, a normal primitive or constructor
198 @result = $meaning->invoke($self);
199 $STATE->clearPrefixes unless $meaning->isPrefix; } # Clear prefixes unless we just set one.
200 else {
201 Error('misdefined', $meaning, $self,
202 "The object " . Stringify($meaning) . " should never reach Stomach!");
203 @result = (makeMisdefinedError($meaning)); }
204 if ((scalar(@result) == 1) && (!defined $result[0])) {
205 @result = (); } # Just paper over the obvious thing.
206 if (my @bad = grep { (!blessed $_) || (!$_->isaBox) } @result) {
207 Error('misdefined', $token, $self,
208 "Execution yielded non boxes",
209 "Returned " . join(',', map { "'" . Stringify($_) . "'" } @bad));
210 @result = (makeMisdefinedError(@result)); }
211 pop(@{ $$self{token_stack} });
212 return @result; }
213  
214 sub makeMisdefinedError {
215 my (@objects) = @_;
216 return LaTeXML::Core::Whatsit->new($STATE->lookupDefinition(T_CS('\@ERROR')),
217 ['misdefined', join('', map { ToString($_); } @objects)],
218 font => $STATE->lookupValue('font'),
219 ); }
220  
221 sub invokeToken_undefined {
222 my ($self, $token) = @_;
223 $STATE->generateErrorStub($self, $token);
224 $$self{gullet}->unread($token); # Retry
225 return; }
226  
227 sub invokeToken_simple {
228 my ($self, $token, $meaning) = @_;
229 my $cc = $meaning->getCatcode;
230 my $font = $STATE->lookupValue('font');
231 if ($cc == CC_SPACE) {
232 $STATE->clearPrefixes; # prefixes shouldn't apply here.
233 if ($STATE->lookupValue('IN_MATH')) { # (but in Preamble, OK ?)
234 return (); }
235 else {
236 return Box($meaning->toString, $font, $$self{gullet}->getLocator, $meaning); } }
237 elsif ($cc == CC_COMMENT) { # Note: Comments need char decoding as well!
238 my $comment = LaTeXML::Package::FontDecodeString($meaning->toString, undef, 1);
239 # However, spaces normally would have be digested away as positioning...
240 my $badspace = pack('U', 0xA0) . "\x{0335}"; # This is at space's pos in OT1
241 $comment =~ s/\Q$badspace\E/ /g;
242 return LaTeXML::Core::Comment->new($comment); }
243 else {
244 $STATE->clearPrefixes; # prefixes shouldn't apply here.
245 return Box(LaTeXML::Package::FontDecodeString($meaning->toString, undef, 1),
246 undef, undef, $meaning); } }
247  
248 # Regurgitate: steal the previously digested boxes from the current level.
249 sub regurgitate {
250 my ($self) = @_;
251 my @stuff = @LaTeXML::LIST;
252 @LaTeXML::LIST = ();
253 return @stuff; }
254  
255 #**********************************************************************
256 # Maintaining State.
257 #**********************************************************************
258 # State changes that the Stomach needs to moderate and know about (?)
259  
260 #======================================================================
261 # Dealing with TeX's bindings & grouping.
262 # Note that lookups happen more often than bgroup/egroup (which open/close frames).
263  
264 sub pushStackFrame {
265 my ($self, $nobox) = @_;
266 $STATE->pushFrame;
267 $STATE->assignValue(beforeAfterGroup => [], 'local'); # ALWAYS bind this!
268 $STATE->assignValue(afterGroup => [], 'local'); # ALWAYS bind this!
269 $STATE->assignValue(afterAssignment => undef, 'local'); # ALWAYS bind this!
270 $STATE->assignValue(groupNonBoxing => $nobox, 'local'); # ALWAYS bind this!
271 $STATE->assignValue(groupInitiator => $LaTeXML::CURRENT_TOKEN, 'local');
272 $STATE->assignValue(groupInitiatorLocator => getLocator($self), 'local');
273 push(@{ $$self{boxing} }, $LaTeXML::CURRENT_TOKEN) unless $nobox; # For begingroup/endgroup
274 return; }
275  
276 sub popStackFrame {
277 my ($self, $nobox) = @_;
278 if (my $beforeafter = $STATE->lookupValue('beforeAfterGroup')) {
279 if (@$beforeafter) {
280 my @result = map { $_->beDigested($self) } @$beforeafter;
281 if (my ($x) = grep { !$_->isaBox } @result) {
282 Error('misdefined', $x, $self, "Expected a Box|List|Whatsit, but got '" . Stringify($x) . "'");
283 @result = (makeMisdefinedError(@result)); }
284 push(@LaTeXML::LIST, @result); } }
285 my $after = $STATE->lookupValue('afterGroup');
286 $STATE->popFrame;
287 pop(@{ $$self{boxing} }) unless $nobox; # For begingroup/endgroup
288 $$self{gullet}->unread(@$after) if $after;
289 return; }
290  
291 sub currentFrameMessage {
292 my ($self) = @_;
293 return "current frame is "
294 . ($STATE->isValueBound('MODE', 0) # SET mode in CURRENT frame ?
295 ? "mode-switch to " . $STATE->lookupValue('MODE')
296 : ($STATE->lookupValue('groupNonBoxing') # Current frame is a non-boxing group?
297 ? "non-boxing" : "boxing") . " group")
298 . " due to " . Stringify($STATE->lookupValue('groupInitiator'))
299 . " " . ToString($STATE->lookupValue('groupInitiatorLocator')); }
300  
301 #======================================================================
302 # Grouping pushes a new stack frame for binding definitions, etc.
303 #======================================================================
304 # Originally, we only treated math vs text "modes", which are correlated
305 # to grouping (somehow). But we'll gradually need to incorporate all
306 # the horizontal/vertical modes, which are NOT correlated to grouping,
307 # although they do operate on a stack.
308 # So, we should NOT generate errors when the grouping clashes with modes
309 # (until we can get it properly sorted).
310  
311 # if $nobox is true, inhibit incrementing the boxingLevel
312 sub bgroup {
313 my ($self) = @_;
314 pushStackFrame($self, 0);
315 return; }
316  
317 sub egroup {
318 my ($self) = @_;
319 if ( ##$STATE->isValueBound('MODE', 0) || # Last stack frame was a mode switch!?!?!
320 $STATE->lookupValue('groupNonBoxing')) { # or group was opened with \begingroup
321 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $self, "Attempt to close boxing group",
322 currentFrameMessage($self)); }
323 else { # Don't pop if there's an error; maybe we'll recover?
324 popStackFrame($self, 0); }
325 return; }
326  
327 sub begingroup {
328 my ($self) = @_;
329 pushStackFrame($self, 1);
330 return; }
331  
332 sub endgroup {
333 my ($self) = @_;
334 if ( ##$STATE->isValueBound('MODE', 0) || # Last stack frame was a mode switch!?!?!
335 !$STATE->lookupValue('groupNonBoxing')) { # or group was opened with \bgroup
336 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $self, "Attempt to close non-boxing group",
337 currentFrameMessage($self)); }
338 else { # Don't pop if there's an error; maybe we'll recover?
339 popStackFrame($self, 1); }
340 return; }
341  
342 #======================================================================
343 # Mode (minimal so far; math vs text)
344 # Could (should?) be taken up by Stomach by building horizontal, vertical or math lists ?
345  
346 # This sets the mode without doing any grouping (NOR does it stack the modes!!)
347 # Useful for environments, where the group has already been established.
348 # (presumably, in the long run, modes & groups should be much less coupled)
349 sub setMode {
350 my ($self, $mode) = @_;
351 my $prevmode = $STATE->lookupValue('MODE');
352 my $ismath = $mode =~ /math$/;
353 $STATE->assignValue(MODE => $mode, 'local');
354 $STATE->assignValue(IN_MATH => $ismath, 'local');
355 my $curfont = $STATE->lookupValue('font');
356 if ($mode eq $prevmode) { }
357 elsif ($ismath) {
358 # When entering math mode, we set the font to the default math font,
359 # and save the text font for any embedded text.
360 $STATE->assignValue(savedfont => $curfont, 'local');
361 $STATE->assignValue(script_base_level => scalar(@{ $$self{boxing} })); # See getScriptLevel
362 $STATE->assignValue(font => $STATE->lookupValue('mathfont')->merge(
363 color => $curfont->getColor, background => $curfont->getBackground,
364 size => $curfont->getSize,
365 mathstyle => ($mode =~ /^display/ ? 'display' : 'text')), 'local'); }
366 else {
367 # When entering text mode, we should set the font to the text font in use before the math
368 # but inherit color and size
369 $STATE->assignValue(font => $STATE->lookupValue('savedfont')->merge(
370 color => $curfont->getColor, background => $curfont->getBackground,
371 size => $curfont->getSize), 'local'); }
372 return; }
373  
374 sub beginMode {
375 my ($self, $mode) = @_;
376 pushStackFrame($self); # Effectively bgroup
377 setMode($self, $mode);
378 return; }
379  
380 sub endMode {
381 my ($self, $mode) = @_;
382 if ((!$STATE->isValueBound('MODE', 0)) # Last stack frame was NOT a mode switch!?!?!
383 || ($STATE->lookupValue('MODE') ne $mode)) { # Or was a mode switch to a different mode
384 Error('unexpected', $LaTeXML::CURRENT_TOKEN, $self, "Attempt to end mode $mode",
385 currentFrameMessage($self)); }
386 else { # Don't pop if there's an error; maybe we'll recover?
387 popStackFrame($self); } # Effectively egroup.
388 return; }
389  
390 #**********************************************************************
391 1;
392  
393 __END__
« lib/LaTeXML.pm ↑ Summary lib/LaTeXML/Package.pm »