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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 115 CC 7 SCAR 27.6
CC
7
Cov
0%
invoke
42.0
new
6.0
isPrefix
2.0
CRAP 15.8
1 # /=====================================================================\ #
2 # | LaTeXML::Core::Definition::Primitive | #
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::Primitive;
13 use strict;
14 use warnings;
15 use LaTeXML::Global;
16 use LaTeXML::Common::Object;
17 use LaTeXML::Common::Error;
18 use base qw(LaTeXML::Core::Definition);
19  
20 # Known traits:
21 # isPrefix : whether this primitive is a TeX prefix, \global, etc.
22 sub new {
23 my ($class, $cs, $parameters, $replacement, %traits) = @_;
24 # Could conceivably have $replacement being a List or Box?
25 my $source = $STATE->getStomach->getGullet->getMouth;
26 Fatal('misdefined', $cs, $source, "Primitive replacement for '" . ToString($cs) . "' is not CODE",
27 "Replacement is $replacement")
28 unless ref $replacement eq 'CODE';
29 return bless { cs => $cs, parameters => $parameters, replacement => $replacement,
30 locator => $source->getLocator,
31 %traits }, $class; }
32  
33 sub isPrefix {
34 my ($self) = @_;
35 return $$self{isPrefix}; }
36  
37 sub executeBeforeDigest {
38 my ($self, $stomach) = @_;
39 local $LaTeXML::Core::State::UNLOCKED = 1;
40 my @pre = grep { defined } @{ $$self{beforeDigest} || [] };
41 return (map { &$_($stomach) } @pre); }
42  
43 sub executeAfterDigest {
44 my ($self, $stomach, @whatever) = @_;
45 local $LaTeXML::Core::State::UNLOCKED = 1;
46 my @post = grep { defined } @{ $$self{afterDigest} || [] };
47 return (map { &$_($stomach, @whatever) } @post); }
48  
49 # Digest the primitive; this should occur in the stomach.
50 sub invoke {
51 my ($self, $stomach) = @_;
52 my $profiled = $STATE->lookupValue('PROFILING') && ($LaTeXML::CURRENT_TOKEN || $$self{cs});
53 my $tracing = $STATE->lookupValue('TRACINGCOMMANDS') || $LaTeXML::DEBUG{tracing};
54 LaTeXML::Core::Definition::startProfiling($profiled, 'digest') if $profiled;
55 Debug('{' . $self->tracingCSName . '}') if $tracing;
56 my @result = ($self->executeBeforeDigest($stomach));
57 my $parms = $$self{parameters};
58 my @args = ($parms ? $parms->readArguments($stomach->getGullet, $self) : ());
59 Debug($self->tracingArgs(@args)) if $tracing && @args;
60 push(@result,
61 &{ $$self{replacement} }($stomach, @args),
62 $self->executeAfterDigest($stomach));
63  
64 LaTeXML::Core::Definition::stopProfiling($profiled, 'digest') if $profiled;
65 return @result; }
66  
67 sub equals {
68 my ($self, $other) = @_;
69 return (defined $other
70 && (ref $self) eq (ref $other)) && Equals($self->getParameters, $other->getParameters)
71 && Equals($$self{replacement}, $$other{replacement})
72 && Equals($$self{beforeDigest}, $$other{beforeDigest})
73 && Equals($$self{afterDigest}, $$other{afterDigest})
74 ; }
75  
76 #===============================================================================
77 1;
78  
79 __END__