2002-03-10 00:29:33 +01:00
|
|
|
#
|
|
|
|
# Copyright 1999, 2000, 2001 Patrik Stridvall
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
package preprocessor;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub new($) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $proto = shift;
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
my $self = {};
|
|
|
|
bless ($self, $class);
|
|
|
|
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $stack = \@{$self->{STACK}};
|
|
|
|
my $include_found = \${$self->{INCLUDE_FOUND}};
|
|
|
|
my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
|
2002-06-01 04:55:48 +02:00
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
$$include_found = shift;
|
|
|
|
$$conditional_found = shift;
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub include($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $include_found = \${$self->{INCLUDE_FOUND}};
|
|
|
|
|
|
|
|
my $argument = shift;
|
|
|
|
|
|
|
|
&$$include_found($argument);
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub define($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
|
|
|
|
|
|
|
|
my $name = shift;
|
|
|
|
|
|
|
|
$$state{$name} = "def";
|
|
|
|
|
|
|
|
&$$conditional_found($name);
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub undefine($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
|
|
|
|
|
|
|
|
my $name = shift;
|
|
|
|
|
|
|
|
$$state{$name} = "undef";
|
|
|
|
|
|
|
|
&$$conditional_found($name);
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub begin_if($$$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $stack = \@{$self->{STACK}};
|
|
|
|
|
|
|
|
my $directive = shift;
|
|
|
|
local $_ = shift;
|
|
|
|
|
|
|
|
while(!/^$/) {
|
2003-08-08 23:04:17 +02:00
|
|
|
if(/^0\s*\&\&/s) {
|
1999-10-24 23:45:39 +02:00
|
|
|
$_ = "0";
|
2003-08-08 23:04:17 +02:00
|
|
|
} elsif(/^1\s*\|\|/s) {
|
1999-10-24 23:45:39 +02:00
|
|
|
$_ = "1";
|
|
|
|
}
|
|
|
|
|
2003-08-08 23:04:17 +02:00
|
|
|
if (/^(!\s*)?defined\s*\(\s*(\w+)\s*\)\s*(?:(\&\&|\|\|)\s*)?/s ||
|
|
|
|
/^(!\s*)?defined\s*(\w+)\s*(?:(\&\&|\|\|)\s*)?/s)
|
|
|
|
{
|
1999-10-24 23:45:39 +02:00
|
|
|
$_ = $';
|
2003-08-08 23:04:17 +02:00
|
|
|
|
|
|
|
my $sign = $1;
|
|
|
|
my $var = $2;
|
|
|
|
|
|
|
|
if (defined($sign) && $sign eq "!") {
|
|
|
|
$self->undefine($var);
|
|
|
|
push @$stack, $var;
|
1999-10-24 23:45:39 +02:00
|
|
|
} else {
|
2003-08-08 23:04:17 +02:00
|
|
|
$self->define($var);
|
|
|
|
push @$stack, $var;
|
1999-10-24 23:45:39 +02:00
|
|
|
}
|
2003-08-08 23:04:17 +02:00
|
|
|
} elsif (/^(!\s*)?(\w+)\s*(?:(<|<=|==|!=|>=|>|\+|\-|\*\/)\s*(\w+)\s*)?(?:(\&\&|\|\|)\s*)?/s) {
|
1999-10-24 23:45:39 +02:00
|
|
|
$_ = $';
|
2003-08-08 23:04:17 +02:00
|
|
|
|
|
|
|
my $sign = $1;
|
|
|
|
my $var = $2;
|
|
|
|
|
|
|
|
if (defined($sign) && $sign eq "!") {
|
|
|
|
$self->undefine($var);
|
|
|
|
push @$stack, $var;
|
|
|
|
} else {
|
|
|
|
$self->define($var);
|
|
|
|
push @$stack, $var;
|
|
|
|
}
|
|
|
|
} elsif(/^(!\s*)?\(/s) {
|
|
|
|
$_ = "";
|
1999-10-24 23:45:39 +02:00
|
|
|
} else {
|
|
|
|
print "*** Can't parse '#$directive $_' ***\n";
|
|
|
|
$_ = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub else_if($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $stack = \@{$self->{STACK}};
|
|
|
|
|
|
|
|
my $argument = shift;
|
|
|
|
|
|
|
|
$self->end_if;
|
|
|
|
|
|
|
|
if(defined($argument)) {
|
|
|
|
$self->begin_if("elif", $argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub end_if($) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $stack = \@{$self->{STACK}};
|
|
|
|
|
|
|
|
my $macro = pop @$stack;
|
|
|
|
delete $$state{$macro} if defined($macro);
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub directive($$$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
my $stack = \@{$self->{STACK}};
|
|
|
|
|
|
|
|
my $directive = shift;
|
|
|
|
my $argument = shift;
|
|
|
|
|
|
|
|
local $_ = $directive;
|
|
|
|
if(/^if$/) {
|
|
|
|
$self->begin_if("if",$argument);
|
|
|
|
} elsif(/^ifdef$/) {
|
|
|
|
$self->begin_if("if", "defined($argument)");
|
|
|
|
} elsif(/^ifndef$/) {
|
|
|
|
$self->begin_if("if", "!defined($argument)");
|
|
|
|
push @$stack, $argument;
|
|
|
|
} elsif(/^elif$/) {
|
|
|
|
$self->else_if($argument);
|
|
|
|
} elsif(/^else$/) {
|
|
|
|
$self->else_if;
|
|
|
|
} elsif(/^endif$/) {
|
|
|
|
$self->end_if;
|
|
|
|
} elsif(/^include/) {
|
|
|
|
$self->include($argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub is_def($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
|
|
|
|
my $name = shift;
|
2002-06-01 04:55:48 +02:00
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
my $status = $$state{$name};
|
|
|
|
|
|
|
|
return defined($status) && $status eq "def";
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub is_undef($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
|
|
|
|
my $name = shift;
|
2002-06-01 04:55:48 +02:00
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
my $status = $$state{$name};
|
|
|
|
|
|
|
|
return defined($status) && $status eq "undef";
|
|
|
|
}
|
|
|
|
|
2004-10-07 20:53:56 +02:00
|
|
|
sub is_unknown($$) {
|
1999-10-24 23:45:39 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $state = \%{$self->{STATE}};
|
|
|
|
|
|
|
|
my $name = shift;
|
2002-06-01 04:55:48 +02:00
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
my $status = $$state{$name};
|
|
|
|
|
|
|
|
return !defined($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|