winapi_c_parser.pm 7.03 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#

package winapi_c_parser;

use strict;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw();

use options qw($options);
use output qw($output);

use c_function;
use c_type;
use function;
use winapi_function;
use winapi_parser;

########################################################################
# new
#
42
sub new($$) {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);

    my $file = \${$self->{FILE}};
    my $create_function = \${$self->{CREATE_FUNCTION}};
    my $create_type = \${$self->{CREATE_TYPE}};
    my $found_comment = \${$self->{FOUND_COMMENT}};
    my $found_declaration = \${$self->{FOUND_DECLARATION}};
    my $found_function = \${$self->{FOUND_FUNCTION}};
    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
    my $found_line = \${$self->{FOUND_LINE}};
    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
    my $found_statement = \${$self->{FOUND_STATEMENT}};
    my $found_type = \${$self->{FOUND_TYPE}};
    my $found_variable = \${$self->{FOUND_VARIABLE}};

    $$file = shift;

    $$create_function = sub { return new c_function; };
    $$create_type = sub { return new c_type; };
    $$found_comment = sub { return 1; };
    $$found_declaration = sub { return 1; };
    $$found_function = sub { return 1; };
    $$found_function_call = sub { return 1; };
    $$found_line = sub { return 1; };
    $$found_preprocessor = sub { return 1; };
    $$found_statement = sub { return 1; };
    $$found_type = sub { return 1; };
    $$found_variable = sub { return 1; };

    return $self;
}

########################################################################
# set_found_comment_callback
#
81
sub set_found_comment_callback($$) {
82 83 84 85 86 87 88 89 90 91
    my $self = shift;

    my $found_comment = \${$self->{FOUND_COMMENT}};

    $$found_comment = shift;
}

########################################################################
# set_found_declaration_callback
#
92
sub set_found_declaration_callback($$) {
93 94 95 96 97 98 99 100 101 102
    my $self = shift;

    my $found_declaration = \${$self->{FOUND_DECLARATION}};

    $$found_declaration = shift;
}

########################################################################
# set_found_function_callback
#
103
sub set_found_function_callback($$) {
104 105 106 107 108 109 110 111 112 113
    my $self = shift;

    my $found_function = \${$self->{FOUND_FUNCTION}};

    $$found_function = shift;
}

########################################################################
# set_found_function_call_callback
#
114
sub set_found_function_call_callback($$) {
115 116 117 118 119 120 121 122 123 124
    my $self = shift;

    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};

    $$found_function_call = shift;
}

########################################################################
# set_found_line_callback
#
125
sub set_found_line_callback($$) {
126 127 128 129 130 131 132 133 134 135
    my $self = shift;

    my $found_line = \${$self->{FOUND_LINE}};

    $$found_line = shift;
}

########################################################################
# set_found_preprocessor_callback
#
136
sub set_found_preprocessor_callback($$) {
137 138 139 140 141 142 143 144 145 146
    my $self = shift;

    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};

    $$found_preprocessor = shift;
}

########################################################################
# set_found_statement_callback
#
147
sub set_found_statement_callback($$) {
148 149 150 151 152 153 154 155 156 157
    my $self = shift;

    my $found_statement = \${$self->{FOUND_STATEMENT}};

    $$found_statement = shift;
}

########################################################################
# set_found_type_callback
#
158
sub set_found_type_callback($$) {
159 160 161 162 163 164 165 166 167 168
    my $self = shift;

    my $found_type = \${$self->{FOUND_TYPE}};

    $$found_type = shift;
}

########################################################################
# set_found_variable_callback
#
169
sub set_found_variable_callback($$) {
170 171 172 173 174 175 176 177 178 179
    my $self = shift;

    my $found_variable = \${$self->{FOUND_VARIABLE}};

    $$found_variable = shift;
}

########################################################################
# parse_c_file

180
sub parse_c_file($$$$) {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    my $self = shift;

    my $file = \${$self->{FILE}};

    my $found_comment = \${$self->{FOUND_COMMENT}};
    my $found_declaration = \${$self->{FOUND_DECLARATION}};
    my $create_function = \${$self->{CREATE_FUNCTION}};
    my $found_function = \${$self->{FOUND_FUNCTION}};
    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
    my $found_line = \${$self->{FOUND_LINE}};
    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
    my $found_statement = \${$self->{FOUND_STATEMENT}};
    my $found_variable = \${$self->{FOUND_VARIABLE}};

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $_create_function = sub {
	return 'function'->new;
    };

    my $_create_type = sub {
	return 'type'->new;
    };

    my $_found_function = sub {
	my $old_function = shift;

	my $function = new c_function;
211 212

	$function->file($old_function->file);
213 214 215 216 217 218 219 220 221 222 223
	
	$function->begin_line($old_function->function_line);
	$function->begin_column(0);
	$function->end_line($old_function->function_line);
	$function->end_column(0);

	$function->linkage($old_function->linkage);
	$function->return_type($old_function->return_type);
	$function->calling_convention($old_function->calling_convention);
	$function->name($old_function->internal_name);

224 225 226 227 228 229 230 231 232 233 234
	if(defined($old_function->argument_types)) {
	    $function->argument_types([@{$old_function->argument_types}]);
	}
	if(defined($old_function->argument_names)) {
	    $function->argument_names([@{$old_function->argument_names}]);
	}

	$function->statements_line($old_function->statements_line);
	$function->statements_column(0);
	$function->statements($old_function->statements);
	
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	&$$found_function($function);
    };

    my $_found_preprocessor = sub {
	my $directive = shift;
	my $argument = shift;

	my $begin_line = 0;
	my $begin_column = 0;
	my $preprocessor = "#$directive $argument";

	&$$found_preprocessor($begin_line, $begin_column, $preprocessor);
    };

    my $_found_type = sub {
	my $type = shift;
    };

253
    winapi_parser::parse_c_file($$file, {
254 255 256 257 258 259 260 261 262 263 264
	# c_comment_found => $found_c_comment,
	# cplusplus_comment_found => $found_cplusplus_comment,
	function_create => $_create_function,
	function_found => $_found_function,
	preprocessor_found => $_found_preprocessor,
	type_create => $_create_type,
	type_found => $_found_type,
    });
}

1;