| 1 |
package FFI::Platypus::TypeParser::Version1; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use Carp qw( croak ); |
| 7 |
use parent qw( FFI::Platypus::TypeParser ); |
| 8 |
use constant _version => 1; |
| 9 |
|
| 10 |
# ABSTRACT: FFI Type Parser Version One |
| 11 |
our $VERSION = '2.12'; # VERSION |
| 12 |
|
| 13 |
|
| 14 |
our @CARP_NOT = qw( FFI::Platypus FFI::Platypus::TypeParser ); |
| 15 |
|
| 16 |
my %reserved = map { $_ => 1 } qw( |
| 17 |
string |
| 18 |
object |
| 19 |
type |
| 20 |
role |
| 21 |
union |
| 22 |
class |
| 23 |
struct |
| 24 |
record |
| 25 |
array |
| 26 |
senum |
| 27 |
enum |
| 28 |
); |
| 29 |
|
| 30 |
# The type parser is responsible for deciding if something is a legal |
| 31 |
# alias name. Since this needs to be checked before the type is parsed |
| 32 |
# it is separate from set_alias below. |
| 33 |
sub check_alias |
| 34 |
{ |
| 35 |
my($self, $alias) = @_; |
| 36 |
croak "spaces not allowed in alias" if $alias =~ /\s/; |
| 37 |
croak "allowed characters for alias: [A-Za-z0-9_]" if $alias !~ /^[A-Za-z0-9_]+$/; |
| 38 |
croak "reserved world \"$alias\" cannot be used as an alias" |
| 39 |
if $reserved{$alias}; |
| 40 |
croak "alias \"$alias\" conflicts with existing type" |
| 41 |
if defined $self->type_map->{$alias} |
| 42 |
|| $self->types->{$alias} |
| 43 |
|| $self->global_types->{basic}->{$alias}; |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
sub set_alias |
| 48 |
{ |
| 49 |
my($self, $alias, $type) = @_; |
| 50 |
$self->types->{$alias} = $type; |
| 51 |
} |
| 52 |
|
| 53 |
use constant type_regex => |
| 54 |
|
| 55 |
qr/^ # |
| 56 |
# |
| 57 |
\s* # prefix white space |
| 58 |
# |
| 59 |
(?: # |
| 60 |
# |
| 61 |
\( ([^)]*) \) -> (.*) # closure, argument types $1, return type $2 |
| 62 |
| # |
| 63 |
( string | record ) \s* \( \s* ([0-9]+) \s* \) (?: \s* (\*) | ) # fixed record $3, fixed string $4, ponter $5 |
| 64 |
| # |
| 65 |
record \s* \( ( \s* (?: [A-Za-z_] [A-Za-z_0-9]* :: )* [A-Za-z_] [A-Za-z_0-9]* ) \s* \) (?: \s* (\*) | ) # record class $6, pointer $7 |
| 66 |
| # |
| 67 |
( (?: [A-Za-z_] [A-Za-z_0-9]* \s+ )* [A-Za-z_] [A-Za-z_0-9]* ) \s* # unit type name $8 |
| 68 |
# |
| 69 |
(?: (\*) | \[ ([0-9]*) \] | ) # pointer $9, array $10 |
| 70 |
| # |
| 71 |
object \s* \( \s* ( (?: [A-Za-z_] [A-Za-z_0-9]* :: )* [A-Za-z_] [A-Za-z_0-9]* ) # object class $11 |
| 72 |
(?: \s*,\s* ( (?: [A-Za-z_] [A-Za-z_0-9]* \s+ )* [A-Za-z_] [A-Za-z_0-9]* ) )? # type $12 |
| 73 |
\s* \) # |
| 74 |
) # |
| 75 |
# |
| 76 |
\s* # trailing white space |
| 77 |
# |
| 78 |
$/x; # |
| 79 |
|
| 80 |
sub parse |
| 81 |
{ |
| 82 |
my($self, $name, $opt) = @_; |
| 83 |
|
| 84 |
$opt ||= {}; |
| 85 |
|
| 86 |
return $self->types->{$name} if $self->types->{$name}; |
| 87 |
|
| 88 |
$name =~ type_regex or croak "bad type name: $name"; |
| 89 |
|
| 90 |
if(defined (my $at = $1)) # closure |
| 91 |
{ |
| 92 |
my $rt = $2; |
| 93 |
return $self->types->{$name} = $self->create_type_closure( |
| 94 |
$self->abi, |
| 95 |
$self->parse($rt, $opt), |
| 96 |
map { $self->parse($_, $opt) } map { my $t = $_; $t =~ s/^\s+//; $t =~ s/\s+$//; $t } split /,/, $at, |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
if(defined (my $size = $4)) # fixed record / fixed string |
| 101 |
{ |
| 102 |
croak "fixed record / fixed string size must be larger than 0" |
| 103 |
unless $size > 0; |
| 104 |
|
| 105 |
if(my $pointer = $5) |
| 106 |
{ |
| 107 |
return $self->types->{$name} = $self->create_type_record( |
| 108 |
0, |
| 109 |
$size, |
| 110 |
); |
| 111 |
} |
| 112 |
elsif($opt->{member} || ($3 eq 'string' && $self->_version > 1)) |
| 113 |
{ |
| 114 |
return $self->types->{"$name *"} = $self->create_type_record( |
| 115 |
0, |
| 116 |
$size, |
| 117 |
); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
if($self->_version > 1) |
| 122 |
{ |
| 123 |
croak "classless record not allowed as value type"; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
croak "fixed string / classless record not allowed as value type"; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if(defined (my $class = $6)) # class record |
| 133 |
{ |
| 134 |
my $size_method = $class->can('ffi_record_size') || $class->can('_ffi_record_size') || croak "$class has no ffi_record_size or _ffi_record_size method"; |
| 135 |
if(my $pointer = $7) |
| 136 |
{ |
| 137 |
return $self->types->{$name} = $self->create_type_record( |
| 138 |
0, |
| 139 |
$class->$size_method, |
| 140 |
$class, |
| 141 |
); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
return $self->types->{$name} = $self->create_type_record( |
| 146 |
1, |
| 147 |
$class->$size_method, |
| 148 |
$class, |
| 149 |
$class->_ffi_meta->ptr, |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if(defined (my $unit_name = $8)) # basic type |
| 155 |
{ |
| 156 |
if($self->global_types->{basic}->{$unit_name}) |
| 157 |
{ |
| 158 |
if(my $pointer = $9) |
| 159 |
{ |
| 160 |
croak "void pointer not allowed" if $unit_name eq 'void'; |
| 161 |
return $self->types->{$name} = $self->global_types->{ptr}->{$unit_name}; |
| 162 |
} |
| 163 |
|
| 164 |
if(defined (my $size = $10)) # array |
| 165 |
{ |
| 166 |
croak "void array not allowed" if $unit_name eq 'void'; |
| 167 |
if($size ne '') |
| 168 |
{ |
| 169 |
croak "array size must be larger than 0" if $size < 1; |
| 170 |
return $self->types->{$name} = $self->create_type_array( |
| 171 |
$self->global_types->{basic}->{$unit_name}->type_code, |
| 172 |
$size, |
| 173 |
); |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
return $self->global_types->{array}->{$unit_name} ||= $self->create_type_array( |
| 178 |
$self->global_types->{basic}->{$unit_name}->type_code, |
| 179 |
0, |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
# basic type with no decorations |
| 185 |
return $self->global_types->{basic}->{$unit_name}; |
| 186 |
} |
| 187 |
|
| 188 |
if(my $map_name = $self->type_map->{$unit_name}) |
| 189 |
{ |
| 190 |
if(my $pointer = $9) |
| 191 |
{ |
| 192 |
return $self->types->{$name} = $self->parse("$map_name *", $opt); |
| 193 |
} |
| 194 |
if(defined (my $size = $10)) |
| 195 |
{ |
| 196 |
if($size ne '') |
| 197 |
{ |
| 198 |
croak "array size must be larger than 0" if $size < 1; |
| 199 |
return $self->types->{$name} = $self->parse("$map_name [$size]", $opt); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
return $self->types->{$name} = $self->parse("$map_name []", $opt); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return $self->types->{$name} = $self->parse("$map_name", $opt); |
| 208 |
} |
| 209 |
|
| 210 |
if(my $pointer = $9) |
| 211 |
{ |
| 212 |
my $unit_type = $self->parse($unit_name, $opt); |
| 213 |
|
| 214 |
if($unit_type->is_record_value) |
| 215 |
{ |
| 216 |
my $meta = $unit_type->meta; |
| 217 |
return $self->types->{$name} = $self->create_type_record( |
| 218 |
0, |
| 219 |
$meta->{size}, |
| 220 |
$meta->{class}, |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
my $basic_name = $self->global_types->{rev}->{$unit_type->type_code}; |
| 225 |
if($basic_name) |
| 226 |
{ |
| 227 |
return $self->types->{$name} = $self->parse("$basic_name *", $opt); |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
croak "cannot make a pointer to $unit_name"; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if(defined (my $size = $10)) |
| 236 |
{ |
| 237 |
my $unit_type = $self->parse($unit_name, $opt); |
| 238 |
my $basic_name = $self->global_types->{rev}->{$unit_type->type_code}; |
| 239 |
if($basic_name) |
| 240 |
{ |
| 241 |
if($size ne '') |
| 242 |
{ |
| 243 |
croak "array size must be larger than 0" if $size < 1; |
| 244 |
return $self->types->{$name} = $self->parse("$basic_name [$size]", $opt); |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
return $self->types->{$name} = $self->parse("$basic_name []", $opt); |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
croak "cannot make an array of $unit_name"; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if($name eq 'string ro') |
| 258 |
{ |
| 259 |
return $self->global_types->{basic}->{string}; |
| 260 |
} |
| 261 |
elsif($name eq 'string rw') |
| 262 |
{ |
| 263 |
return $self->global_types->{v2}->{string_rw} ||= $self->create_type_string(1); |
| 264 |
} |
| 265 |
|
| 266 |
return $self->types->{$name} || croak "unknown type: $unit_name"; |
| 267 |
} |
| 268 |
|
| 269 |
if(defined (my $class = $11)) # object type |
| 270 |
{ |
| 271 |
my $basic_name = $12 || 'opaque'; |
| 272 |
my $basic_type = $self->parse($basic_name); |
| 273 |
if($basic_type->is_object_ok) |
| 274 |
{ |
| 275 |
return $self->types->{$name} = $self->create_type_object( |
| 276 |
$basic_type->type_code, |
| 277 |
$class, |
| 278 |
); |
| 279 |
} |
| 280 |
else |
| 281 |
{ |
| 282 |
croak "cannot make an object of $basic_name"; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
croak "internal error parsing: $name"; |
| 287 |
} |
| 288 |
|
| 289 |
1; |
| 290 |
|
| 291 |
__END__ |
| 292 |
|
| 293 |
=pod |
| 294 |
|
| 295 |
=encoding UTF-8 |
| 296 |
|
| 297 |
=head1 NAME |
| 298 |
|
| 299 |
FFI::Platypus::TypeParser::Version1 - FFI Type Parser Version One |
| 300 |
|
| 301 |
=head1 VERSION |
| 302 |
|
| 303 |
version 2.12 |
| 304 |
|
| 305 |
=head1 SYNOPSIS |
| 306 |
|
| 307 |
use FFI::Platypus 1.00; |
| 308 |
my $ffi = FFI::Platypus->new( api => 1 ); |
| 309 |
$ffi->type('record(Foo::Bar)' => 'foo_bar_t'); |
| 310 |
$ffi->type('record(Foo::Bar)*' => 'foo_bar_ptr'); |
| 311 |
$ffi->type('opaque' => 'baz_t'); |
| 312 |
$ffi->type('bar_t*' => 'baz_ptr'); |
| 313 |
|
| 314 |
=head1 DESCRIPTION |
| 315 |
|
| 316 |
This documents the second (version 1) type parser for L<FFI::Platypus>. |
| 317 |
This type parser was included with L<FFI::Platypus> starting with version |
| 318 |
C<0.91> in an experimental capability, and C<1.00> as a stable interface. |
| 319 |
Starting with version C<1.00> the main L<FFI::Platypus> documentation |
| 320 |
describes the version 1 API and you can refer to |
| 321 |
L<FFI::Platypus::TypeParser::Version0> for details on the version0 API. |
| 322 |
|
| 323 |
=head1 SEE ALSO |
| 324 |
|
| 325 |
=over 4 |
| 326 |
|
| 327 |
=item L<FFI::Platypus> |
| 328 |
|
| 329 |
The core L<FFI::Platypus> documentation. |
| 330 |
|
| 331 |
=item L<FFI::Platypus::TypeParser::Version0> |
| 332 |
|
| 333 |
The API C<0.02> type parser. |
| 334 |
|
| 335 |
=back |
| 336 |
|
| 337 |
=head1 AUTHOR |
| 338 |
|
| 339 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 340 |
|
| 341 |
Contributors: |
| 342 |
|
| 343 |
Bakkiaraj Murugesan (bakkiaraj) |
| 344 |
|
| 345 |
Dylan Cali (calid) |
| 346 |
|
| 347 |
pipcet |
| 348 |
|
| 349 |
Zaki Mughal (zmughal) |
| 350 |
|
| 351 |
Fitz Elliott (felliott) |
| 352 |
|
| 353 |
Vickenty Fesunov (vyf) |
| 354 |
|
| 355 |
Gregor Herrmann (gregoa) |
| 356 |
|
| 357 |
Shlomi Fish (shlomif) |
| 358 |
|
| 359 |
Damyan Ivanov |
| 360 |
|
| 361 |
Ilya Pavlov (Ilya33) |
| 362 |
|
| 363 |
Petr Písař (ppisar) |
| 364 |
|
| 365 |
Mohammad S Anwar (MANWAR) |
| 366 |
|
| 367 |
Håkon Hægland (hakonhagland, HAKONH) |
| 368 |
|
| 369 |
Meredith (merrilymeredith, MHOWARD) |
| 370 |
|
| 371 |
Diab Jerius (DJERIUS) |
| 372 |
|
| 373 |
Eric Brine (IKEGAMI) |
| 374 |
|
| 375 |
szTheory |
| 376 |
|
| 377 |
José Joaquín Atria (JJATRIA) |
| 378 |
|
| 379 |
Pete Houston (openstrike, HOUSTON) |
| 380 |
|
| 381 |
Lukas Mai (MAUKE) |
| 382 |
|
| 383 |
=head1 COPYRIGHT AND LICENSE |
| 384 |
|
| 385 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 386 |
|
| 387 |
This is free software; you can redistribute it and/or modify it under |
| 388 |
the same terms as the Perl 5 programming language system itself. |
| 389 |
|
| 390 |
=cut |