| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Definition | # | ||
| 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 | |||
| 13 | package LaTeXML::Core::Definition::Expandable; | ||
| 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::Parameters; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use LaTeXML::Package qw(TokenizeInternal); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use base qw(LaTeXML::Core::Definition); | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ($class, $cs, $parameters, $expansion, %traits) = @_; | ||
| 27 | my $source = $STATE->getStomach->getGullet->getMouth; | ||
| 28 | my $type = ref $expansion; | ||
| 29 | # expansion must end up Tokens or CODE | ||
| 30 | if (!$type) { | ||
| 31 | $expansion = TokenizeInternal($expansion)->packParameters; } | ||
| 32 | elsif ($type eq 'LaTeXML::Core::Token') { | ||
| 33 | $expansion = TokensI($expansion); } | ||
| 34 | elsif ($type eq 'LaTeXML::Core::Tokens') { | ||
| 35 | Fatal('misdefined', $cs, $source, "Expansion of '" . ToString($cs) . "' has unbalanced {}", | ||
| 36 | "Expansion is " . ToString($expansion)) unless $expansion->isBalanced; | ||
| 37 | $expansion = $expansion->packParameters unless $traits{nopackParameters}; } | ||
| 38 | elsif ($type ne 'CODE') { | ||
| 39 | Error('misdefined', $cs, $source, | ||
| 40 | "Expansion of '" . ToString($cs) . "' cannot be of type '$type'"); | ||
| 41 | $expansion = TokensI(); } | ||
| 42 | return bless { cs => $cs, parameters => $parameters, expansion => $expansion, | ||
| 43 | locator => $source->getLocator, | ||
| 44 | isProtected => $traits{protected} || $STATE->getPrefix('protected'), | ||
| 45 | isOuter => $traits{outer} || $STATE->getPrefix('outer'), | ||
| 46 | isLong => $traits{long} || $STATE->getPrefix('long'), | ||
| 47 | hasCCARG => (($type ne 'CODE') && (grep { $$_[1] == CC_ARG; } $expansion->unlist) ? 1 : 0), | ||
| 48 | %traits }, $class; } | ||
| 49 | |||
| 50 | sub isExpandable { | ||
| 51 | return 1; } | ||
| 52 | |||
| 53 | sub getExpansion { | ||
| 54 | my ($self) = @_; | ||
| 55 | return $$self{expansion}; } | ||
| 56 | |||
| 57 | # Expand the expandable control sequence. This should be carried out by the Gullet. | ||
| 58 | # This MUST return Tokens() or undef. (NOT a Token) | ||
| 59 | sub invoke { | ||
| 60 | no warnings 'recursion'; | ||
| 61 | my ($self, $gullet, $onceonly) = @_; | ||
| 62 | # shortcut for "trivial" macros; but only if not tracing & profiling!!!! | ||
| 63 | my $_tracing = $STATE->lookupValue('TRACING') || 0; | ||
| 64 | my $tracing = ($_tracing & TRACE_MACROS); | ||
| 65 | my $profiled = ($_tracing & TRACE_PROFILE) && ($LaTeXML::CURRENT_TOKEN || $$self{cs}); | ||
| 66 | my $expansion = $$self{expansion}; | ||
| 67 | my $etype = ref $expansion; | ||
| 68 | my $result; | ||
| 69 | my $parms = $$self{parameters}; | ||
| 70 | |||
| 71 | LaTeXML::Core::Definition::startProfiling($profiled, 'expand') if $profiled; | ||
| 72 | if ($etype eq 'CODE') { | ||
| 73 | # Harder to emulate \tracingmacros here. | ||
| 74 | my @args = ($parms ? $parms->readArguments($gullet, $self) : ()); | ||
| 75 | $result = Tokens(&$expansion($gullet, @args)); | ||
| 76 | if ($tracing) { | ||
| 77 | Debug($self->tracingCSName . ' ==> ' . tracetoString($result)); | ||
| 78 | Debug($self->tracingArgs(@args)) if @args; } } | ||
| 79 | elsif (!$parms) { # Trivial macro | ||
| 80 | Debug($self->tracingCSName . ' ->' . tracetoString($expansion)) if $tracing; | ||
| 81 | # For trivial expansion, make sure we don't get \cs or \relax\cs direct recursion! | ||
| 82 | if (!$onceonly && $$self{cs}) { | ||
| 83 | my ($t0, $t1) = ($etype eq 'LaTeXML::Core::Tokens' | ||
| 84 | ? ($$expansion[0], $$expansion[1]) : ($expansion, undef)); | ||
| 85 | if ($t0 && ($t0->equals($$self{cs}) | ||
| 86 | || ($t1 && $t1->equals($$self{cs}) && $t0->equals(T_CS('\protect'))))) { | ||
| 87 | Error('recursion', $$self{cs}, $gullet, | ||
| 88 | "Token " . Stringify($$self{cs}) . " expands into itself!", | ||
| 89 | "defining as empty"); | ||
| 90 | $expansion = TokensI(); } } | ||
| 91 | $result = $expansion; } | ||
| 92 | else { | ||
| 93 | my @args = $parms->readArguments($gullet, $self); | ||
| 94 | if ($$self{hasCCARG}) { # Do we actually need to substitute the args in? | ||
| 95 | my $r; # Make sure they are actually Tokens! | ||
| 96 | @args = map { ($_ && ($r = ref $_) | ||
| 97 | && (($r eq 'LaTeXML::Core::Token') || ($r eq 'LaTeXML::Core::Tokens')) | ||
| 98 | ? $_ : Tokens(Revert($_))); } @args; | ||
| 99 | $result = $expansion->substituteParameters(@args); } | ||
| 100 | else { | ||
| 101 | $result = $expansion; } | ||
| 102 | if ($tracing) { # More involved... | ||
| 103 | Debug($self->tracingCSName . ' ->' . tracetoString($expansion)); | ||
| 104 | Debug($self->tracingArgs(@args)) if @args; } } | ||
| 105 | # Getting exclusive profiling requires dubious Gullet support! | ||
| 106 | $result = Tokens($result, T_MARKER($profiled)) if $profiled; | ||
| 107 | return $result; } | ||
| 108 | |||
| 109 | # print a string of tokens like TeX would when tracing. | ||
| 110 | sub tracetoString { | ||
| 111 | my ($tokens) = @_; | ||
| 112 | return join('', map { ($_->getCatcode == CC_CS ? $_->toString . ' ' : $_->toString) } | ||
| 113 | $tokens->unlist); } | ||
| 114 | |||
| 115 | sub equals { | ||
| 116 | my ($self, $other) = @_; | ||
| 117 | return (defined $other && (ref $self) eq (ref $other)) | ||
| 118 | && Equals($self->getParameters, $other->getParameters) | ||
| 119 | && Equals($$self{expansion}, $$other{expansion}); } | ||
| 120 | |||
| 121 | #====================================================================== | ||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ | ||