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