| 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 LaTeXML::Core::Box; | ||
| 19 | use LaTeXML::Core::Token; | ||
| 20 | use LaTeXML::Core::Tokens; | ||
| 21 | use base qw(LaTeXML::Core::Definition); | ||
| 22 | |||
| 23 | # Known traits: | ||
| 24 | # isPrefix : whether this primitive is a TeX prefix, \global, etc. | ||
| 25 | sub new { | ||
| 26 | my ($class, $cs, $parameters, $replacement, %traits) = @_; | ||
| 27 | Error('misdefined', $cs, $STATE->getStomach, | ||
| 28 | "Primitive replacement for '" . ToString($cs) . "' is not a string or CODE", | ||
| 29 | "Replacement is $replacement") | ||
| 30 | if (ref $replacement) && (ref $replacement ne 'CODE'); | ||
| 31 | return bless { cs => $cs, parameters => $parameters, replacement => $replacement, | ||
| 32 | locator => $STATE->getStomach->getGullet->getMouth->getLocator, | ||
| 33 | %traits }, $class; } | ||
| 34 | |||
| 35 | sub isPrefix { | ||
| 36 | my ($self) = @_; | ||
| 37 | return $$self{isPrefix}; } | ||
| 38 | |||
| 39 | sub executeBeforeDigest { | ||
| 40 | my ($self, $stomach) = @_; | ||
| 41 | local $LaTeXML::Core::State::UNLOCKED = 1; | ||
| 42 | my @pre = grep { defined } @{ $$self{beforeDigest} || [] }; | ||
| 43 | return (map { &$_($stomach) } @pre); } | ||
| 44 | |||
| 45 | sub executeAfterDigest { | ||
| 46 | my ($self, $stomach, @whatever) = @_; | ||
| 47 | local $LaTeXML::Core::State::UNLOCKED = 1; | ||
| 48 | my @post = grep { defined } @{ $$self{afterDigest} || [] }; | ||
| 49 | return (map { &$_($stomach, @whatever) } @post); } | ||
| 50 | |||
| 51 | # Digest the primitive; this should occur in the stomach. | ||
| 52 | sub invoke { | ||
| 53 | my ($self, $stomach) = @_; | ||
| 54 | my $_tracing = $STATE->lookupValue('TRACING') || 0; | ||
| 55 | my $tracing = ($_tracing & TRACE_COMMANDS); | ||
| 56 | my $profiled = ($_tracing & TRACE_PROFILE) && ($LaTeXML::CURRENT_TOKEN || $$self{cs}); | ||
| 57 | |||
| 58 | LaTeXML::Core::Definition::startProfiling($profiled, 'digest') if $profiled; | ||
| 59 | Debug('{' . $self->tracingCSName . '}') if $tracing; | ||
| 60 | my @result = ($self->executeBeforeDigest($stomach)); | ||
| 61 | my $parms = $$self{parameters}; | ||
| 62 | my @args = ($parms ? $parms->readArguments($stomach->getGullet, $self) : ()); | ||
| 63 | Debug($self->tracingArgs(@args)) if $tracing && @args; | ||
| 64 | my $replacement = $$self{replacement}; | ||
| 65 | |||
| 66 | if (!ref $replacement) { | ||
| 67 | my $alias = $$self{alias}; | ||
| 68 | $alias = T_CS($alias) if $alias && !ref $alias; | ||
| 69 | push(@result, Box($replacement, undef, undef, | ||
| 70 | Tokens($alias || $$self{cs}, ($parms ? $parms->revertArguments(@args) : ())), | ||
| 71 | (defined $replacement ? () : (isEmpty => 1)))); } | ||
| 72 | else { | ||
| 73 | push(@result, &{ $$self{replacement} }($stomach, @args)); } | ||
| 74 | push(@result, $self->executeAfterDigest($stomach)); | ||
| 75 | LaTeXML::Core::Definition::stopProfiling($profiled, 'digest') if $profiled; | ||
| 76 | return @result; } | ||
| 77 | |||
| 78 | sub equals { | ||
| 79 | my ($self, $other) = @_; | ||
| 80 | return (defined $other | ||
| 81 | && (ref $self) eq (ref $other)) && Equals($self->getParameters, $other->getParameters) | ||
| 82 | && Equals($$self{replacement}, $$other{replacement}) | ||
| 83 | && Equals($$self{beforeDigest}, $$other{beforeDigest}) | ||
| 84 | && Equals($$self{afterDigest}, $$other{afterDigest}) | ||
| 85 | ; } | ||
| 86 | |||
| 87 | #=============================================================================== | ||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |