| 1 | package Inline::C::ParseRegExp; | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | { | ||
| 7 | extends => [qw(C)], | ||
| 8 | overrides => [qw(get_parser)], | ||
| 9 | } | ||
| 10 | } | ||
| 11 | |||
| 12 | sub get_parser { | ||
| 13 | Inline::C::_parser_test("Inline::C::ParseRegExp::get_parser called\n") if $_[0]->{CONFIG}{_TESTING}; | ||
| 14 | bless {}, 'Inline::C::ParseRegExp' | ||
| 15 | } | ||
| 16 | |||
| 17 | sub code { | ||
| 18 | my($self,$code) = @_; | ||
| 19 | |||
| 20 | # These regular expressions were derived from Regexp::Common v0.01. | ||
| 21 | my $RE_comment_C = q{(?:(?:\/\*)(?:(?:(?!\*\/)[\s\S])*)(?:\*\/))}; | ||
| 22 | my $RE_comment_Cpp = q{(?:\/\*(?:(?!\*\/)[\s\S])*\*\/|\/\/[^\n]*\n)}; | ||
| 23 | my $RE_quoted = (q{(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")} | ||
| 24 | .q{|(?:\')(?:[^\\\']*(?:\\.[^\\\']*)*)(?:\'))}); | ||
| 25 | our $RE_balanced_brackets; $RE_balanced_brackets = | ||
| 26 | qr'(?:[{]((?:(?>[^{}]+)|(??{$RE_balanced_brackets}))*)[}])'; | ||
| 27 | our $RE_balanced_parens; $RE_balanced_parens = | ||
| 28 | qr'(?:[(]((?:(?>[^()]+)|(??{$RE_balanced_parens}))*)[)])'; | ||
| 29 | |||
| 30 | # First, we crush out anything potentially confusing. | ||
| 31 | # The order of these _does_ matter. | ||
| 32 | $code =~ s/$RE_comment_C/ /go; | ||
| 33 | $code =~ s/$RE_comment_Cpp/ /go; | ||
| 34 | $code =~ s/^\#.*(\\\n.*)*//mgo; | ||
| 35 | #$code =~ s/$RE_quoted/\"\"/go; # Buggy, if included. | ||
| 36 | $code =~ s/$RE_balanced_brackets/{ }/go; | ||
| 37 | |||
| 38 | $self->{_the_code_most_recently_parsed} = $code; # Simplifies debugging. | ||
| 39 | |||
| 40 | my $normalize_type = sub { | ||
| 41 | # Normalize a type for lookup in a typemap. | ||
| 42 | my($type) = @_; | ||
| 43 | |||
| 44 | # Remove "extern". | ||
| 45 | # But keep "static", "inline", "typedef", etc, | ||
| 46 | # to cause desirable typemap misses. | ||
| 47 | $type =~ s/\bextern\b//g; | ||
| 48 | |||
| 49 | # Whitespace: only single spaces, none leading or trailing. | ||
| 50 | $type =~ s/\s+/ /g; | ||
| 51 | $type =~ s/^\s//; $type =~ s/\s$//; | ||
| 52 | |||
| 53 | # Adjacent "derivative characters" are not separated by whitespace, | ||
| 54 | # but _are_ separated from the adjoining text. | ||
| 55 | # [ Is really only * (and not ()[]) needed??? ] | ||
| 56 | $type =~ s/\*\s\*/\*\*/g; | ||
| 57 | $type =~ s/(?<=[^ \*])\*/ \*/g; | ||
| 58 | |||
| 59 | return $type; | ||
| 60 | }; | ||
| 61 | |||
| 62 | # The decision of what is an acceptable declaration was originally | ||
| 63 | # derived from Inline::C::grammar.pm version 0.30 (Inline 0.43). | ||
| 64 | |||
| 65 | my $re_plausible_place_to_begin_a_declaration = qr { | ||
| 66 | # The beginning of a line, possibly indented. | ||
| 67 | # (Accepting indentation allows for C code to be aligned with | ||
| 68 | # its surrounding perl, and for backwards compatibility with | ||
| 69 | # Inline 0.43). | ||
| 70 | (?m: ^ ) \s* | ||
| 71 | }xo; | ||
| 72 | |||
| 73 | # Instead of using \s , we dont tolerate blank lines. | ||
| 74 | # This matches user expectation better than allowing arbitrary | ||
| 75 | # vertical whitespace. | ||
| 76 | my $sp = qr{[ \t]|\n(?![ \t]*\n)}; | ||
| 77 | |||
| 78 | my $re_type = qr {( | ||
| 79 | (?: \w+ $sp* )+? # words | ||
| 80 | (?: \* $sp* )* # stars | ||
| 81 | )}xo; | ||
| 82 | |||
| 83 | my $re_identifier = qr{ (\w+) $sp* }xo; | ||
| 84 | |||
| 85 | while($code =~ m{ | ||
| 86 | $re_plausible_place_to_begin_a_declaration | ||
| 87 | ( $re_type $re_identifier $RE_balanced_parens $sp* (\;|\{) ) | ||
| 88 | }xgo) | ||
| 89 | { | ||
| 90 | my($type, $identifier, $args, $what) = ($2,$3,$4,$5); | ||
| 91 | $args = "" if $args =~ /^\s+$/; | ||
| 92 | |||
| 93 | my $is_decl = $what eq ';'; | ||
| 94 | my $function = $identifier; | ||
| 95 | my $return_type = &$normalize_type($type); | ||
| 96 | my @arguments = split ',', $args; | ||
| 97 | |||
| 98 | goto RESYNC if $is_decl && !$self->{data}{AUTOWRAP}; | ||
| 99 | goto RESYNC if $self->{data}{done}{$function}; | ||
| 100 | goto RESYNC if !defined | ||
| 101 | $self->{data}{typeconv}{valid_rtypes}{$return_type}; | ||
| 102 | |||
| 103 | my(@arg_names,@arg_types); | ||
| 104 | my $dummy_name = 'arg1'; | ||
| 105 | |||
| 106 | foreach my $arg (@arguments) { | ||
| 107 | my $arg_no_space = $arg; | ||
| 108 | $arg_no_space =~ s/\s//g; | ||
| 109 | # If $arg_no_space is 'void', there will be no identifier. | ||
| 110 | if(my($type, $identifier) = | ||
| 111 | $arg =~ /^\s*$re_type(?:$re_identifier)?\s*$/o) | ||
| 112 | { | ||
| 113 | my $arg_name = $identifier; | ||
| 114 | my $arg_type = &$normalize_type($type); | ||
| 115 | |||
| 116 | if((!defined $arg_name) && ($arg_no_space ne 'void')) { | ||
| 117 | goto RESYNC if !$is_decl; | ||
| 118 | $arg_name = $dummy_name++; | ||
| 119 | } | ||
| 120 | goto RESYNC if ((!defined | ||
| 121 | $self->{data}{typeconv}{valid_types}{$arg_type}) && ($arg_no_space ne 'void')); | ||
| 122 | |||
| 123 | # Push $arg_name onto @arg_names iff it's defined. Otherwise ($arg_no_space | ||
| 124 | # was 'void'), push the empty string onto @arg_names (to avoid uninitialized | ||
| 125 | # warnings emanating from C.pm). | ||
| 126 | defined($arg_name) ? push(@arg_names,$arg_name) | ||
| 127 | : push(@arg_names, ''); | ||
| 128 | if($arg_name) {push(@arg_types,$arg_type)} | ||
| 129 | else {push(@arg_types,'')} # $arg_no_space was 'void' - this push() avoids 'uninitialized' warnings from C.pm | ||
| 130 | } | ||
| 131 | elsif($arg =~ /^\s*\.\.\.\s*$/) { | ||
| 132 | push(@arg_names,'...'); | ||
| 133 | push(@arg_types,'...'); | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | goto RESYNC; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # Commit. | ||
| 141 | push @{$self->{data}{functions}}, $function; | ||
| 142 | $self->{data}{function}{$function}{return_type}= $return_type; | ||
| 143 | $self->{data}{function}{$function}{arg_names} = [@arg_names]; | ||
| 144 | $self->{data}{function}{$function}{arg_types} = [@arg_types]; | ||
| 145 | $self->{data}{done}{$function} = 1; | ||
| 146 | |||
| 147 | next; | ||
| 148 | |||
| 149 | RESYNC: # Skip the rest of the current line, and continue. | ||
| 150 | $code =~ /\G[^\n]*\n/gc; | ||
| 151 | } | ||
| 152 | |||
| 153 | return 1; # We never fail. | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | __DATA__ |