Skip to content

Commit

Permalink
added "isa" syntax to the import. allows modules to import the macros…
Browse files Browse the repository at this point in the history
… and constants of another class
  • Loading branch information
sungo committed Sep 10, 2001
1 parent 5a618cb commit 5866505
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions lib/POE/Preprocessor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ sub COND_INDENT () { 2 }
#sub DEBUG_INVOKE () { 1 }
#sub DEBUG_DEFINE () { 1 }

#sub WARN_DEFINE () { 1 }

BEGIN {
defined &DEBUG or eval 'sub DEBUG () { 0 }'; # preprocessor
defined &DEBUG_INVOKE or eval 'sub DEBUG_INVOKE () { 0 }'; # macro invocs
defined &DEBUG_DEFINE or eval 'sub DEBUG_DEFINE () { 0 }'; # macro defines
defined &WARN_DEFINE or eval 'sub WARN_DEFINE () { 0 }'; # macro/const redefinition warning
};

# text_trie_trie is virtually identical to code in Ilya Zakharevich's
Expand Down Expand Up @@ -126,20 +129,53 @@ sub fix_exclude {
my (%constants, %macros, %const_regexp, %macro);

sub import {
# Outer closure to define a unique scope.
{ my $macro_name = '';

my $self = shift;
my %args;
if(@_ > 1) {
%args = @_;
}

# Outer closure to define a unique scope.
{ my $macro_name = '';
my ($macro_line, $enum_index);
my ($package_name, $file_name, $line_number) = (caller)[0,1,2];
my $const_regexp_dirty = 0;
my $state = STATE_PLAIN;


my @isas;

if($args{isa}) {
if(ref $args{isa} eq 'ARRAY') {
foreach my $isa (@{$args{isa}}) {
push @isas, $isa;
}
} else {
push @isas, $args{isa};
}
foreach my $isa (@isas) {
eval "use $isa";
croak "Unable to load $isa : $@" if $@;

foreach my $const (keys %{$constants{$isa}}) {
$constants{$package_name}->{$const} = $constants{$isa}->{$const};
$const_regexp_dirty = 1;
}

foreach my $macro (keys %{$macros{$isa}}) {
$macros{$package_name}->{$macro} = $macros{$isa}->{$macro};
}
}
}

$conditional_stacks{$package_name} = [ ];
$excluding_code{$package_name} = 0;

my $set_const = sub {
my ($name, $value) = @_;

if (exists $constants{$package_name}->{$name}) {
if (exists $constants{$package_name}->{$name} && WARN_DEFINE) {
warn "const $name redefined at $file_name line $line_number\n"
unless $constants{$package_name}->{$name} eq $value;
}
Expand Down Expand Up @@ -336,7 +372,7 @@ sub import {
$macro{$package_name}->[MAC_CODE] =~ s/^\s*//;
$macro{$package_name}->[MAC_CODE] =~ s/\s*$//;

if (exists $macros{$package_name}->{$macro_name}) {
if (exists $macros{$package_name}->{$macro_name} && WARN_DEFINE) {
warn( "macro $macro_name redefined at ",
"$file_name line $line_number\n"
)
Expand Down Expand Up @@ -556,6 +592,8 @@ POE::Preprocessor - a macro/const/enum preprocessor
use POE::Preprocessor;
# use POE::Preprocessor ( isa => 'POE::SomeModule' );
macro max (one,two) {
((one) > (two) ? (one) : (two))
}
Expand Down Expand Up @@ -676,6 +714,19 @@ previous example.
Conditional includes are experimental pending a decision on how useful
they are.
=head1 IMPORTING MACROS/CONSTANTS
use POE::Preprocessor ( isa => 'POE::SomeModule' );
This method of calling Preprocessor causes the macros and constants of
C<POE::SomeModule> to be imported for use in the current namespace.
These macros and constants can be overriden simply by defining items
in the current namespace of the same name.
Note: if the macros in C<POE::SomeModule> require additional perl
modules, any code which imports these macros will need to C<use>
those modules as well.
=head1 DEBUGGING
POE::Preprocessor has three debugging constants which may be defined
Expand All @@ -694,6 +745,10 @@ To see macro, constant, and enum definitions:
sub POE::Preprocessor::DEBUG_DEFINE () { 1 }
To see warnings when a macro or constant is redefined:
sub POE::Preprocessor::WARN_DEFINE () { 1 }
=head1 BUGS
Source filters are line-based, and so is the macro language. The only
Expand Down

0 comments on commit 5866505

Please sign in to comment.