blib/lib/Inline/C/Parser/RegExp.pm

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