| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Definition::Register | # | ||
| 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::Register; | ||
| 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::Primitive); | ||
| 19 | |||
| 20 | # Known Traits: | ||
| 21 | # address : address to store in State's value table; defaults to the cs | ||
| 22 | # getter : a sub to get the value; otherwise stores in State value table under address | ||
| 23 | # setter : a sub to set the value; ditto | ||
| 24 | # registerType : the type of register value (a LaTeXML class) | ||
| 25 | # beforeDigest, afterDigest : code for before/after digestion daemons | ||
| 26 | # readonly : whether this register can only be read | ||
| 27 | # default : default value; safety for when no value assigned | ||
| 28 | sub new { | ||
| 29 | my ($class, $cs, $parameters, %traits) = @_; | ||
| 30 | $traits{address} = ToString($cs) unless defined $traits{address}; | ||
| 31 | return bless { cs => $cs, parameters => $parameters, | ||
| 32 | locator => $STATE->getStomach->getGullet->getMouth->getLocator, | ||
| 33 | %traits }, $class; } | ||
| 34 | |||
| 35 | sub isPrefix { | ||
| 36 | return 0; } | ||
| 37 | |||
| 38 | sub isRegister { | ||
| 39 | my ($self) = @_; | ||
| 40 | return $$self{registerType}; } | ||
| 41 | |||
| 42 | sub getAddress { | ||
| 43 | my ($self) = @_; | ||
| 44 | return $$self{address} || $self->getCSName; } | ||
| 45 | |||
| 46 | sub isReadonly { | ||
| 47 | my ($self) = @_; | ||
| 48 | return $$self{readonly}; } | ||
| 49 | |||
| 50 | sub valueOf { | ||
| 51 | my ($self, @args) = @_; | ||
| 52 | if (my $getter = $$self{getter}) { | ||
| 53 | return &{ $$self{getter} }(@args); } | ||
| 54 | else { | ||
| 55 | my $loc = (@args ? join('', $$self{address}, map { ToString($_) } @args) : $$self{address}); | ||
| 56 | return $STATE->lookupValue($loc) || $$self{default}; } } | ||
| 57 | |||
| 58 | sub setValue { | ||
| 59 | my ($self, $value, $scope, @args) = @_; | ||
| 60 | my $tracing = (($STATE->lookupValue('TRACING') || 0) & TRACE_COMMANDS); | ||
| 61 | if ($tracing) { | ||
| 62 | my $scope = $STATE->getPrefix('global') ? 'globally ' : ''; | ||
| 63 | my $csname = ToString($$self{cs}); | ||
| 64 | Debug("{$scope" . "changing " . $csname . "=" . ToString($self->valueOf(@args)) . "}"); } | ||
| 65 | if (my $setter = $$self{setter}) { | ||
| 66 | &{ $$self{setter} }($value, $scope, @args); } | ||
| 67 | elsif ($$self{readonly}) { | ||
| 68 | Warn('unexpected', $$self{cs}, $STATE->getStomach, | ||
| 69 | "Can't assign readonly register " . ToString($$self{cs}) . " to " . ToString($value)); } | ||
| 70 | else { | ||
| 71 | my $loc = (@args ? join('', $$self{address}, map { ToString($_) } @args) : $$self{address}); | ||
| 72 | $STATE->assignValue($loc => $value, $scope); } | ||
| 73 | Debug("{into " . ToString($$self{cs}) . "=" . ToString($self->valueOf(@args)) . "}") if $tracing; | ||
| 74 | return; } | ||
| 75 | |||
| 76 | sub addValue { | ||
| 77 | my ($self, $value, $scope, @args) = @_; | ||
| 78 | my $oldvalue; | ||
| 79 | if (my $getter = $$self{getter}) { | ||
| 80 | $oldvalue = &{ $$self{getter} }(@args); } | ||
| 81 | else { | ||
| 82 | my $loc = (@args ? join('', $$self{address}, map { ToString($_) } @args) : $$self{address}); | ||
| 83 | $oldvalue = $STATE->lookupValue($loc) || $$self{default}; } | ||
| 84 | my $newvalue = $oldvalue->add($value); | ||
| 85 | if (my $setter = $$self{setter}) { | ||
| 86 | &{ $$self{setter} }($newvalue, $scope, @args); } | ||
| 87 | elsif ($$self{readonly}) { | ||
| 88 | Warn('unexpected', $$self{cs}, $STATE->getStomach, | ||
| 89 | "Can't assign readonly register $$self{address} to " . ToString($value)); } | ||
| 90 | else { | ||
| 91 | my $loc = (@args ? join('', $$self{address}, map { ToString($_) } @args) : $$self{address}); | ||
| 92 | $STATE->assignValue($loc => $newvalue, $scope); } | ||
| 93 | return; } | ||
| 94 | |||
| 95 | # No before/after daemons ??? | ||
| 96 | # (other than afterassign) | ||
| 97 | sub invoke { | ||
| 98 | my ($self, $stomach) = @_; | ||
| 99 | my $_tracing = $STATE->lookupValue('TRACING') || 0; | ||
| 100 | my $profiled = ($_tracing & TRACE_PROFILE) && ($LaTeXML::CURRENT_TOKEN || $$self{cs}); | ||
| 101 | LaTeXML::Core::Definition::startProfiling($profiled, 'digest') if $profiled; | ||
| 102 | |||
| 103 | my $gullet = $stomach->getGullet; | ||
| 104 | my $parms = $$self{parameters}; | ||
| 105 | my @args = ($parms ? $parms->readArguments($gullet) : ()); | ||
| 106 | $gullet->readKeyword('='); # Ignore | ||
| 107 | my $value = $gullet->readValue($self->isRegister); | ||
| 108 | $self->setValue($value, undef, @args); | ||
| 109 | |||
| 110 | # Tracing ? | ||
| 111 | if (my $after = $STATE->lookupValue('afterAssignment')) { | ||
| 112 | $STATE->assignValue(afterAssignment => undef, 'global'); | ||
| 113 | $gullet->unread($after); } # primitive returns boxes, so these need to be digested! | ||
| 114 | LaTeXML::Core::Definition::stopProfiling($profiled, 'digest') if $profiled; | ||
| 115 | return; } | ||
| 116 | |||
| 117 | sub equals { | ||
| 118 | my ($self, $other) = @_; | ||
| 119 | return (defined $other | ||
| 120 | && (ref $self) eq (ref $other)) && Equals($self->getParameters, $other->getParameters) | ||
| 121 | && Equals($$self{address}, $$other{address}); } | ||
| 122 | |||
| 123 | #=============================================================================== | ||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ |