File Coverage

lib/Perl/PrereqScanner/NotQuiteLite/Parser/Core.pm
Criterion Covered Total %
statement 85 92 92.3
branch 51 62 82.2
condition 34 50 68.0
subroutine 10 10 100.0
pod 0 7 0.0
total 180 221 81.4


line stmt bran cond sub pod time code
1             package Perl::PrereqScanner::NotQuiteLite::Parser::Core;
2              
3 83     83   1134 use strict;
  83         150  
  83         1945  
4 83     83   338 use warnings;
  83         148  
  83         1647  
5 83     83   392 use Perl::PrereqScanner::NotQuiteLite::Util;
  83         134  
  83         79462  
6              
7             my %feature_since = (
8             say => '5.010',
9             state => '5.010',
10             switch => '5.010',
11             unicode_strings => '5.012',
12             current_sub => '5.016',
13             evalbytes => '5.016',
14             fc => '5.016',
15             arybase => '5.016',
16             unicode_eval => '5.016',
17             lexical_subs => '5.018',
18             postderef => '5.020',
19             postderef_qq => '5.020',
20             signatures => '5.020',
21             bitwise => '5.022',
22             refaliasing => '5.022',
23             declared_refs => '5.026',
24             );
25              
26             sub register { return {
27 82     82 0 688 use => {
28             if => 'parse_if_args',
29             base => 'parse_base_args',
30             parent => 'parse_parent_args',
31             feature => 'parse_feature_args',
32             },
33             keyword => {
34             package => 'parse_package',
35             exit => 'parse_begin_exit',
36             },
37             }}
38              
39             sub parse_if_args {
40 9     9 0 24 my ($class, $c, $used_module, $raw_tokens) = @_;
41              
42 9         34 while(my $token = shift @$raw_tokens) {
43 20 100       71 last if $token->[1] eq 'COMMA';
44             }
45              
46 9         34 my $tokens = convert_string_tokens($raw_tokens);
47 9         23 my $module = shift @$tokens;
48 9 50 66     41 if (ref $module and ($module->[1] eq 'WORD' or $module->[1] eq 'KEYWORD')) {
      66        
49 4         9 $module = $module->[0];
50             }
51 9 50       23 if (is_module_name($module)) {
52 9 50       33 if (is_version($tokens->[0])) {
53 0         0 my $version = shift @$tokens;
54 0         0 $c->add_recommendation($module => $version);
55             } else {
56 9         30 $c->add_recommendation($module => 0);
57             }
58             } else {
59 0         0 push @{$c->{errors}}, "use if module not found";
  0         0  
60             }
61             }
62              
63             sub parse_base_args {
64 21     21 0 62 my ($class, $c, $used_module, $raw_tokens) = @_;
65              
66 21         60 my $tokens = convert_string_tokens($raw_tokens);
67 21 50       74 if (is_version($tokens->[0])) {
68 0         0 $c->add($used_module => shift @$tokens);
69             }
70 21         62 while(my $token = shift @$tokens) {
71 51         662 my $module = $token;
72 51 100 100     186 if (ref $module and ($module->[1] || '') eq 'WORD') {
      100        
73             # allow bareword, but disallow function()
74 2         4 $module = $module->[0];
75 2 100 33     17 next if @$tokens and ref $tokens->[0] and ($tokens->[0][1] || '') eq '()';
      50        
      66        
76             }
77             # bareword in parentheses
78 50 100 100     131 if (ref $module and ref $module->[0]) {
79 3         6 $module = $module->[0][0];
80             }
81 50 100       97 if (is_module_name($module)) {
82 25         81 $c->add($module => 0);
83             }
84             }
85             }
86              
87             sub parse_parent_args {
88 28     28 0 64 my ($class, $c, $used_module, $raw_tokens) = @_;
89              
90 28         75 my $tokens = convert_string_tokens($raw_tokens);
91 28 50       76 if (is_version($tokens->[0])) {
92 0         0 $c->add($used_module => shift @$tokens);
93             }
94 28         66 while(my $token = shift @$tokens) {
95 54 100       599 last if $token eq '-norequire';
96 52         69 my $module = $token;
97 52 100       100 if (ref $token) {
98 31 100       79 last if $token->[0] eq '-norequire';
99             }
100 47 100 100     160 if (ref $module and ($module->[1] || '') eq 'WORD') {
      100        
101             # allow bareword, but disallow function()
102 2         4 $module = $module->[0];
103 2 100 33     18 next if @$tokens and ref $tokens->[0] and ($tokens->[0][1] || '') eq '()';
      50        
      66        
104             }
105             # bareword in parentheses
106 46 100 100     114 if (ref $module and ref $module->[0]) {
107 3         4 $module = $module->[0][0];
108             }
109 46 100       85 $c->add($module => 0) if is_module_name($module);
110             }
111             }
112              
113             sub parse_feature_args {
114 10     10 0 24 my ($class, $c, $used_module, $raw_tokens) = @_;
115              
116 10         30 $c->add_perl('5.010', 'feature');
117 10         29 my $tokens = convert_string_tokens($raw_tokens);
118 10 50       27 if (is_version($tokens->[0])) {
119 0         0 $c->add($used_module => shift @$tokens);
120             }
121 10         27 while(my $token = shift @$tokens) {
122 19 100       61 next if ref $token;
123 9 100       24 if (exists $feature_since{$token}) {
124 5         19 $c->add_perl($feature_since{$token} => "feature $token");
125 5         16 next;
126             }
127 4 50       17 if ($token =~ /^:5\.([0-9]+)(\.\[0-9]+)?/) {
128 4         23 my $version = sprintf '5.%03d', $1;
129 4         13 $c->add_perl($version, $token);
130 4         12 next;
131             }
132             }
133             }
134              
135             sub parse_begin_exit {
136 8     8 0 20 my ($class, $c, $raw_tokens) = @_;
137              
138 8 50       14 my @stack = @{$c->{stack} || []};
  8         332  
139 8 50       22 if (grep {$_->[0] eq '{' and $_->[2] eq 'BEGIN'} @stack) {
  7 100       59  
140 4 100       8 if (grep {$c->token_is_conditional($_->[0])} @$raw_tokens) {
  12 100       28  
141 1         4 $c->{force_cond} = 1;
142 4 50       16 } elsif (grep {$_->[0] eq '{' and $c->token_is_conditional($_->[2])} @stack) {
143 1         4 $c->{force_cond} = 1;
144             } else {
145 2         4 $c->{ended} = 1;
146 2         3 @{$c->{stack}} = ();
  2         7  
147             }
148             }
149             }
150              
151             sub parse_package {
152 32     32 0 90 my ($class, $c, $raw_tokens) = @_;
153              
154 32         124 my $tokens = convert_string_tokens($raw_tokens);
155 32         55 shift @$tokens; # drop "package"
156 32         54 my $token = shift @$tokens;
157 32 100 33     253 if (ref $token && $token->[1] && $token->[1] eq 'WORD') {
      66        
158 31         113 $c->add_package($token->[0]);
159             }
160 32 50       105 if (@$tokens) {
161 32         64 $token = shift @$tokens;
162 32 100       94 if (is_version($token)) {
163 5         15 $c->add_perl("5.012", "package PACKAGE VERSION");
164 5         11 $token = shift @$tokens;
165             }
166 32 100 33     290 if (ref $token && $token->[1] && $token->[1] =~ /^\{/) {
      66        
167 6         15 $c->add_perl("5.014", "package PACKAGE (VERSION) {}");
168             }
169             }
170             }
171              
172             1;
173              
174             __END__