preprocessor.pm 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18
#

19 20 21 22
package preprocessor;

use strict;

23
sub new($) {
24 25 26 27 28 29 30 31 32
    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}};
33

34 35 36 37 38 39
    $$include_found = shift;
    $$conditional_found = shift;

    return $self;
}

40
sub include($$) {
41 42 43 44 45 46 47 48
    my $self = shift;
    my $include_found = \${$self->{INCLUDE_FOUND}};

    my $argument = shift;

    &$$include_found($argument);
}

49
sub define($$) {
50 51 52 53 54 55 56 57 58 59 60
    my $self = shift;
    my $state = \%{$self->{STATE}};
    my $conditional_found = \${$self->{CONDITIONAL_FOUND}};

    my $name = shift;

    $$state{$name} = "def";

    &$$conditional_found($name);
}

61
sub undefine($$) {
62 63 64 65 66 67 68 69 70 71 72
    my $self = shift;
    my $state = \%{$self->{STATE}};
    my $conditional_found = \${$self->{CONDITIONAL_FOUND}};

    my $name = shift;

    $$state{$name} = "undef";

    &$$conditional_found($name);
}

73
sub begin_if($$$) {
74 75 76 77 78 79 80 81
    my $self = shift;
    my $state = \%{$self->{STATE}};
    my $stack = \@{$self->{STACK}};

    my $directive = shift;
    local $_ = shift;

    while(!/^$/) {
Patrik Stridvall's avatar
Patrik Stridvall committed
82
	if(/^0\s*\&\&/s) {
83
	    $_ = "0";
Patrik Stridvall's avatar
Patrik Stridvall committed
84
	} elsif(/^1\s*\|\|/s) {
85 86 87
	    $_ = "1";
	}

Patrik Stridvall's avatar
Patrik Stridvall committed
88 89 90
	if (/^(!\s*)?defined\s*\(\s*(\w+)\s*\)\s*(?:(\&\&|\|\|)\s*)?/s ||
	    /^(!\s*)?defined\s*(\w+)\s*(?:(\&\&|\|\|)\s*)?/s)
	{
91
	    $_ = $';
Patrik Stridvall's avatar
Patrik Stridvall committed
92 93 94 95 96 97 98

	    my $sign = $1;
	    my $var = $2;

	    if (defined($sign) && $sign eq "!") {
		$self->undefine($var);
		push @$stack, $var;
99
	    } else {
Patrik Stridvall's avatar
Patrik Stridvall committed
100 101
		$self->define($var);
		push @$stack, $var;
102
	    }
Patrik Stridvall's avatar
Patrik Stridvall committed
103
	} elsif (/^(!\s*)?(\w+)\s*(?:(<|<=|==|!=|>=|>|\+|\-|\*\/)\s*(\w+)\s*)?(?:(\&\&|\|\|)\s*)?/s) {
104
	    $_ = $';
Patrik Stridvall's avatar
Patrik Stridvall committed
105 106 107 108 109 110 111 112 113 114 115 116 117

	    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) {
	    $_ = "";
118 119 120 121 122 123 124
	} else {
	    print "*** Can't parse '#$directive $_' ***\n";
	    $_ = "";
	}
    }
}

125
sub else_if($$) {
126 127 128 129 130 131 132 133 134 135 136 137 138
    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);
    }
}

139
sub end_if($) {
140 141 142 143 144 145 146 147
    my $self = shift;
    my $state = \%{$self->{STATE}};
    my $stack = \@{$self->{STACK}};

    my $macro = pop @$stack;
    delete $$state{$macro} if defined($macro);
}

148
sub directive($$$) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    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);
    }
}

175
sub is_def($$) {
176 177 178 179
    my $self = shift;
    my $state = \%{$self->{STATE}};

    my $name = shift;
180

181 182 183 184 185
    my $status = $$state{$name};

    return defined($status) && $status eq "def";
}

186
sub is_undef($$) {
187 188 189 190
    my $self = shift;
    my $state = \%{$self->{STATE}};

    my $name = shift;
191

192 193 194 195 196
    my $status = $$state{$name};

    return defined($status) && $status eq "undef";
}

197
sub is_unknown($$) {
198 199 200 201
    my $self = shift;
    my $state = \%{$self->{STATE}};

    my $name = shift;
202

203 204 205 206 207 208
    my $status = $$state{$name};

    return !defined($status);
}

1;