Literal.pm

filter: Statementstmt 95.3% 62 / 65 Branchbran 83.3% 35 / 42 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 87.9% 117 / 133 CC 26 SCAR 20.0
CC
26
Cov
89%
_convert
14.0
_convert_nodes
5.2
_prune
4.0
CRAP 7.4
↑ Summary Literal/Unknown.pm »
1  
2 package PPIx::Literal;
3 3 $PPIx::Literal::VERSION = '0.1.0';
4 # ABSTRACT: Convert PPI nodes into literal values
5  
6 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶sub convert {
Subroutine convert: called CC=1 SCAR=0.0
Pod documented
12 13 my $self = shift;
13 13 my @nodes = _prune( map { $_->clone } @_ );
14 13 return $self->_convert_nodes(@nodes);
15 }
16  
17 13 ▶sub _prune {
Subroutine _prune: called CC=4 SCAR=13.9
18 13 my @nodes;
19 13 for my $node (@_) {
20 15 ▶ next if $node->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("PPI::Token::Whitespace") 0 15 15
21 15 ▶ $node->prune('PPI::Token::Whitespace') if $node->can('prune');
Branch: 1 branch
BranchTrueFalseTotal
if $node->can("prune") 12 3 15
22 15 push @nodes, $node;
23 }
24 13 return @nodes;
25 }
26  
27 56 ▶sub _convert {
Subroutine _convert: called CC=14 SCAR=26.4
28 56 my ( $self, $node ) = ( shift, shift );
29  
30 56 ▶ if ( $node->isa('PPI::Token::Quote') && $node->can('literal') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Token::Quote") and $node->can("literal")) 5 51 56
Condition100%3/3
$node->isa("PPI::Token::Quote") and $node->can("literal")
!ll&&!rl&&r
4925
MC/DC100%2/2
$node->isa("PPI::Token::Quote") and $node->can("literal")
$node->isa("PPI::Token::Quote") $node->can("literal")
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Quote")B= $node->can("literal")
ABresult
0X0
100
111
31 5 return $node->literal;
32 }
33 51 ▶ if ( $node->isa('PPI::Token::Quote::Double') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("PPI::Token::Quote::Double")) 2 49 51
34 2 $node->simplify;
35 2 ▶ return $node->literal if $node->can('literal');
Branch: 1 branch
BranchTrueFalseTotal
if $node->can("literal") 2 0 2
36 }
37 49 ▶ if ( $node->isa('PPI::Token::Number') ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Token::Number')) { } 10 39 49
elsif ($node->isa('PPI::Token::QuoteLike::Words')) { } 2 37 39
elsif ($node->isa('PPI::Token::Word')) { } 2 35 37
elsif ($node->isa('PPI::Structure::List')) { } 2 33 35
elsif ($node->isa('PPI::Structure::Constructor')) { } 4 29 33
elsif ($node->isa('PPI::Statement::Expression')) { } 4 25 29
elsif ($node->isa('PPI::Statement')) { } 12 13 25
elsif ($node->isa('PPI::Document')) { } 12 1 13
38 10 return $node->literal;
39 }
40 elsif ( $node->isa('PPI::Token::QuoteLike::Words') ) {
41 2 return $node->literal;
42 }
43 elsif ( $node->isa('PPI::Token::Word') ) {
44 2 return $node->literal;
45 }
46 elsif ( $node->isa('PPI::Structure::List') ) {
47 2 return map { $self->_convert($_) } $node->children;
48 }
49 elsif ( $node->isa('PPI::Structure::Constructor') ) {
50 4 my @v = map { $self->_convert($_) } $node->children;
51 4 return _build_struct( $node->start->content, @v );
52 }
53 elsif ( $node->isa('PPI::Statement::Expression') ) {
54 4 return $self->_convert_nodes( $node->children );
55 }
56 elsif ( $node->isa('PPI::Statement') ) {
57 12 ▶ return _unknown($node) if $node->specialized;
Branch: 1 branch
BranchTrueFalseTotal
if $node->specialized 0 12 12
58 12 return $self->_convert_nodes( $node->children );
59 }
60 elsif ( $node->isa('PPI::Document') ) {
61 12 return map { $self->_convert($_) } $node->children;
62 }
63 else {
64 1 return _unknown($node);
65 }
66 }
67  
68 29 ▶sub _convert_nodes {
Subroutine _convert_nodes: called CC=5 SCAR=16.5
69 29 my ( $self, @nodes ) = @_;
70  
71 29 my @v;
72 29 my $expect = 'value';
73 29 ▶ while ( my $node = shift @nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = shift @nodes) 51 29 80
74 51 ▶ if ( $expect eq 'value' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expect eq 'value') { } 40 11 51
elsif ($expect eq 'comma') { } 11 0 11
75 40 push @v, $self->_convert($node);
76 40 $expect = 'comma';
77 }
78 elsif ( $expect eq 'comma' ) {
79 11 ▶ if ( _is_comma($node) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_comma($node)) { } 11 0 11
80 11 $expect = 'value';
81 }
82 else {
83 # This and the rest are considered unknowns
84 0 push @v, _unknown( $node, @nodes );
85 0 last;
86 }
87 }
88 }
89 29 return @v;
90 }
91  
92 4 ▶sub _build_struct {
Subroutine _build_struct: called CC=3 SCAR=11.3
93 4 my ( $start, @values ) = @_;
94 4 ▶ if ( $start eq '{' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($start eq '{') { } 2 2 4
elsif ($start eq '[') { } 2 0 2
95 2 return +{@values};
96 }
97 elsif ( $start eq '[' ) {
98 2 return [@values];
99 }
100 0 Carp::croak(qq{Can't build structure with start "$start"});
101 }
102  
103 11 ▶sub _is_comma {
Subroutine _is_comma: called CC=3 SCAR=12.5
104 11 my $node = shift;
105 11 ▶ return $node->isa('PPI::Token::Operator')
Condition50%3/6
$node->isa('PPI::Token::Operator') && ($node->content eq ',' || $node->content eq '=>')
!ll&&!rl&&r
0011
$node->content eq "," or $node->content eq "=>"
l!l&&r!l&&!r
830
MC/DC0%0/3
$node->isa('PPI::Token::Operator') && ($node->content eq ',' || $node->content eq '=>')
$node->isa('PPI::Token::Operator') $node->content eq "," $node->content eq "=>"
Truth table50%2/4
A && B or C
A= $node->isa('PPI::Token::Operator')B= $node->content eq ","C= $node->content eq "=>"
ABCresult
0XX0
1000
11X1
1011
106 && ( $node->content eq ',' || $node->content eq '=>' );
107 }
108  
109 1 ▶sub _unknown {
Subroutine _unknown: called CC=2 SCAR=7.2
110 1 ▶ my $content = ( @_ == 1 ) ? { node => shift } : { nodes => [@_] };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 0 1
111 1 return bless $content, 'PPIx::Literal::Unknown';
112 }
113  
114 3 1;
115  
116 #pod =encoding utf8
117 #pod
118 #pod =head1 SYNOPSIS
119 #pod
120 #pod use PPI;
121 #pod use PPIx::Literal;
122 #pod
123 #pod my $doc = PPI::Document->new( \q{(1, "one", 'two')} );
124 #pod my @values = PPIx::Literal->convert($doc);
125 #pod # (1, "one", "two")
126 #pod
127 #pod my $doc = PPI::Document->new( \q{ [ 3.14, 'exp', { one => 1 } ] } );
128 #pod my @values = PPIx::Literal->convert($doc);
129 #pod # [3.14, "exp", { one => 1 }]
130 #pod
131 #pod my $doc = PPI::Document->new( \q{use zim 'Carp' => qw(carp croak)} );
132 #pod my ($use) = $doc->children;
133 #pod my @values = PPIx::Literal->convert( $use->arguments );
134 #pod # ("Carp", "carp", "croak")
135 #pod
136 #pod =head1 DESCRIPTION
137 #pod
138 #pod This code is alpha quality. It is an early release.
139 #pod Interface may change. There may be serious bugs.
140 #pod
141 #pod This module implements the conversion of a small subset of Perl
142 #pod into their literal values. The perl code to be converted
143 #pod is represented as a list of PPI nodes.
144 #pod
145 #pod The conversion works for pieces which gets built from literal tokens
146 #pod and which don't require any kind of compilation.
147 #pod
148 #pod Some examples are:
149 #pod
150 #pod 42 # number
151 #pod "a + b" # plain strings
152 #pod qw(a b c) # quoted words
153 #pod
154 #pod [] # anon array refs
155 #pod { -version => '0.3.2' }, # anon hash refs
156 #pod (2, 3, 4) # literal lists
157 #pod
158 #pod The result of the conversion is a list of Perl data structures
159 #pod which contain plain scalars and "unknowns" as leafs.
160 #pod The "unknowns" are used to represent PPI nodes which
161 #pod can't be converted to literals.
162 #pod
163 #pod =head1 METHODS
164 #pod
165 #pod L<PPIx::Literal> implements the following methods.
166 #pod
167 #pod =head2 convert
168 #pod
169 #pod @values = PPIx::Literal->convert(@nodes);
170 #pod
171 #pod Convert C<@nodes> into their literal values or into "unknowns".
172 #pod
173 #pod =head1 SEE ALSO
174 #pod
175 #pod L<PPI>
176 #pod
177 #pod =cut
178  
179 __END__
↑ Summary Literal/Unknown.pm »