| 1 | # | ||
| 2 | # $Id: State.pm,v 0.1 2001/04/25 10:41:48 ram Exp $ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2000-2001, Raphael Manfredi | ||
| 5 | # | ||
| 6 | # You may redistribute only under the terms of the Artistic License, | ||
| 7 | # as specified in the README file that comes with the distribution. | ||
| 8 | # | ||
| 9 | # HISTORY | ||
| 10 | # $Log: State.pm,v $ | ||
| 11 | # Revision 0.1 2001/04/25 10:41:48 ram | ||
| 12 | # Baseline for first Alpha release. | ||
| 13 | # | ||
| 14 | # $EndLog$ | ||
| 15 | # | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | package Pod::PP::State; | ||
| 20 | |||
| 21 | use Carp::Datum; | ||
| 22 | use Log::Agent; | ||
| 23 | |||
| 24 | use Pod::PP::State::Info; | ||
| 25 | |||
| 26 | # | ||
| 27 | # ->make | ||
| 28 | # | ||
| 29 | # Creation routine | ||
| 30 | # | ||
| 31 | sub make { | ||
| 32 | DFEATURE my $f_; | ||
| 33 | my $self = bless [], shift; | ||
| 34 | return DVAL $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | # | ||
| 38 | # ->push | ||
| 39 | # | ||
| 40 | # Add nested branch. | ||
| 41 | # | ||
| 42 | sub push { | ||
| 43 | DFEATURE my $f_; | ||
| 44 | my $self = shift; | ||
| 45 | my ($cmd, $state, $podinfo) = @_; | ||
| 46 | |||
| 47 | my $sinfo = Pod::PP::State::Info->make($cmd, $state, $podinfo); | ||
| 48 | push(@$self, $sinfo); | ||
| 49 | |||
| 50 | return DVOID; | ||
| 51 | } | ||
| 52 | |||
| 53 | # | ||
| 54 | # ->pop | ||
| 55 | # | ||
| 56 | # Leaving nested branch. | ||
| 57 | # | ||
| 58 | sub pop { | ||
| 59 | DFEATURE my $f_; | ||
| 60 | my $self = shift; | ||
| 61 | my ($podinfo) = @_; | ||
| 62 | |||
| 63 | unless (@$self) { | ||
| 64 | my ($file, $line) = $podinfo->file_line(); | ||
| 65 | logwarn "improper Pod::PP 'endif' ('if' missing?) at \"%s\", line %d", | ||
| 66 | $file, $line; | ||
| 67 | return DVOID; | ||
| 68 | } | ||
| 69 | |||
| 70 | pop @$self; | ||
| 71 | return DVOID; | ||
| 72 | } | ||
| 73 | |||
| 74 | # | ||
| 75 | # ->state | ||
| 76 | # | ||
| 77 | # Return current state | ||
| 78 | # | ||
| 79 | sub state { | ||
| 80 | DFEATURE my $f_; | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | return DVAL POD_PP_STATE_OK unless @$self; | ||
| 84 | return DVAL $self->[-1]->state; # State of latest item | ||
| 85 | } | ||
| 86 | |||
| 87 | # | ||
| 88 | # ->replace | ||
| 89 | # | ||
| 90 | # Return state of topmost element. | ||
| 91 | # | ||
| 92 | sub replace { | ||
| 93 | DFEATURE my $f_; | ||
| 94 | my $self = shift; | ||
| 95 | my ($cmd, $state, $podinfo) = @_; | ||
| 96 | |||
| 97 | unless (@$self) { | ||
| 98 | my ($file, $line) = $podinfo->file_line(); | ||
| 99 | logwarn "improper Pod::PP '%s' ('if' missing?) at \"%s\", line %d", | ||
| 100 | $cmd, $file, $line; | ||
| 101 | return DVOID; | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->[-1]->replace($cmd, $state, $podinfo); | ||
| 105 | |||
| 106 | return DVOID; | ||
| 107 | } | ||
| 108 | |||
| 109 | # | ||
| 110 | # ->pending | ||
| 111 | # | ||
| 112 | # Returns ($cmd, $podinfo) of last branch taken. | ||
| 113 | # Returns () when stack is empty, indicating no pending branch. | ||
| 114 | # | ||
| 115 | sub pending { | ||
| 116 | DFEATURE my $f_; | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | return DARY () unless @$self; | ||
| 120 | |||
| 121 | my $top = $self->[-1]; | ||
| 122 | return DARY ($top->cmd, $top->podinfo); | ||
| 123 | } | ||
| 124 | |||
| 125 | # | ||
| 126 | # ->reset | ||
| 127 | # | ||
| 128 | # Empty stack, warning them if there are pending branches. | ||
| 129 | # | ||
| 130 | sub reset { | ||
| 131 | DFEATURE my $f_; | ||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | my ($cmd, $podinfo) = $self->pending; | ||
| 135 | if (defined $cmd) { | ||
| 136 | my ($file, $line) = $podinfo->file_line; | ||
| 137 | logwarn "unclosed Pod::PP '%s' directive (started at \"%s\", line %d)", | ||
| 138 | $cmd, $file, $line; | ||
| 139 | } | ||
| 140 | |||
| 141 | @$self = (); | ||
| 142 | |||
| 143 | return DVOID; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 |