Removed some more trailing whitespace.
[wine/multimedia.git] / tools / winapi_check / preprocessor.pm
blob6689214d521424e313fb202b00cd1edc06a0bfa1
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package preprocessor;
21 use strict;
23 sub new {
24 my $proto = shift;
25 my $class = ref($proto) || $proto;
26 my $self = {};
27 bless ($self, $class);
29 my $state = \%{$self->{STATE}};
30 my $stack = \@{$self->{STACK}};
31 my $include_found = \${$self->{INCLUDE_FOUND}};
32 my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
34 $$include_found = shift;
35 $$conditional_found = shift;
37 return $self;
40 sub include {
41 my $self = shift;
42 my $include_found = \${$self->{INCLUDE_FOUND}};
44 my $argument = shift;
46 &$$include_found($argument);
49 sub define {
50 my $self = shift;
51 my $state = \%{$self->{STATE}};
52 my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
54 my $name = shift;
56 $$state{$name} = "def";
58 &$$conditional_found($name);
61 sub undefine {
62 my $self = shift;
63 my $state = \%{$self->{STATE}};
64 my $conditional_found = \${$self->{CONDITIONAL_FOUND}};
66 my $name = shift;
68 $$state{$name} = "undef";
70 &$$conditional_found($name);
73 sub begin_if {
74 my $self = shift;
75 my $state = \%{$self->{STATE}};
76 my $stack = \@{$self->{STACK}};
78 my $directive = shift;
79 local $_ = shift;
81 while(!/^$/) {
82 if(/^0\s*\&\&/) {
83 $_ = "0";
84 } elsif(/^1\s*\|\|/) {
85 $_ = "1";
88 if(/^(!)?defined\s*\(\s*(.+?)\s*\)\s*((\&\&|\|\|)\s*)?/){
89 $_ = $';
90 if(defined($1) && $1 eq "!") {
91 $self->undefine($2);
92 push @$stack, $2;
93 } else {
94 $self->define($2);
95 push @$stack, $2;
97 } elsif(/^(\w+)\s*(<|<=|==|!=|>=|>)\s*(\w+)\s*((\&\&|\|\|)\s*)?/) {
98 $_ = $';
99 } elsif(/^(!)?(\w+)\s*$/) {
100 $_ = $';
101 } elsif(/^\(|\)/) {
102 $_ = $';
103 } else {
104 print "*** Can't parse '#$directive $_' ***\n";
105 $_ = "";
110 sub else_if {
111 my $self = shift;
112 my $state = \%{$self->{STATE}};
113 my $stack = \@{$self->{STACK}};
115 my $argument = shift;
117 $self->end_if;
119 if(defined($argument)) {
120 $self->begin_if("elif", $argument);
124 sub end_if {
125 my $self = shift;
126 my $state = \%{$self->{STATE}};
127 my $stack = \@{$self->{STACK}};
129 my $macro = pop @$stack;
130 delete $$state{$macro} if defined($macro);
133 sub directive {
134 my $self = shift;
135 my $state = \%{$self->{STATE}};
136 my $stack = \@{$self->{STACK}};
138 my $directive = shift;
139 my $argument = shift;
141 local $_ = $directive;
142 if(/^if$/) {
143 $self->begin_if("if",$argument);
144 } elsif(/^ifdef$/) {
145 $self->begin_if("if", "defined($argument)");
146 } elsif(/^ifndef$/) {
147 $self->begin_if("if", "!defined($argument)");
148 push @$stack, $argument;
149 } elsif(/^elif$/) {
150 $self->else_if($argument);
151 } elsif(/^else$/) {
152 $self->else_if;
153 } elsif(/^endif$/) {
154 $self->end_if;
155 } elsif(/^include/) {
156 $self->include($argument);
160 sub is_def {
161 my $self = shift;
162 my $state = \%{$self->{STATE}};
164 my $name = shift;
166 my $status = $$state{$name};
168 return defined($status) && $status eq "def";
171 sub is_undef {
172 my $self = shift;
173 my $state = \%{$self->{STATE}};
175 my $name = shift;
177 my $status = $$state{$name};
179 return defined($status) && $status eq "undef";
182 sub is_unknown {
183 my $self = shift;
184 my $state = \%{$self->{STATE}};
186 my $name = shift;
188 my $status = $$state{$name};
190 return !defined($status);