lib/Type/Parser.pm

filter: Statementstmt 99.1% 237 / 241 (2 uncoverable) Branchbran 90.0% 99 / 110 Conditioncond 85.5% 59 / 69 MC/DCmcdc 72.5% 29 / 40 Subroutinesub 100% 31 / 31 Podpod 100% 3 / 3 totaltotal 93.1% 458 / 494 (2 uncoverable) CC 80 SCAR 24.3
CC
80
Cov
95%
_eval_type
26.7
_parse_primary
14.0
_read_token
11.0
CRAP 11.3
« lib/Types/Standard/Dict.pm ↑ Summary lib/Types/Standard/ArrayRef.pm »
1 package Type::Parser;
2  
3 125 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 125 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 125 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
8  
9 125 our $AUTHORITY = 'cpan:TOBYINK';
10 125 our $VERSION = '2.010001';
11  
12 125 $VERSION =~ tr/_//d;
13  
14 # Token types
15 #
16 sub TYPE () { "TYPE" }
17 sub QUOTELIKE () { "QUOTELIKE" }
18 sub STRING () { "STRING" }
19 sub HEXNUM () { "HEXNUM" }
20 sub CLASS () { "CLASS" }
21 sub L_BRACKET () { "L_BRACKET" }
22 sub R_BRACKET () { "R_BRACKET" }
23 sub COMMA () { "COMMA" }
24 sub SLURPY () { "SLURPY" }
25 sub UNION () { "UNION" }
26 sub INTERSECT () { "INTERSECT" }
27 sub SLASH () { "SLASH" }
28 sub NOT () { "NOT" }
29 sub L_PAREN () { "L_PAREN" }
30 sub R_PAREN () { "R_PAREN" }
31 sub MYSTERY () { "MYSTERY" }
32  
33 125 our @EXPORT_OK = qw( eval_type _std_eval parse extract_type );
34  
35 125 require Exporter::Tiny;
36 125 our @ISA = 'Exporter::Tiny';
37  
38 Evaluate: {
39  
40 331 ▶ sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
41 331 my $str = $_[0];
42 331 my $parser = "Type::Parser::AstBuilder"->new( input => $str );
43 331 $parser->build;
44 328 ▶ wantarray ? ( $parser->ast, $parser->remainder ) : $parser->ast;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 69 259 328
45 }
46
47 1 ▶ sub extract_type {
Subroutine extract_type: called CC=2 SCAR=7.1
Pod documented
48 1 my ( $str, $reg ) = @_;
49 1 my ( $parsed, $tail ) = parse( $str );
50 wantarray
51 1 ▶ ? ( _eval_type( $parsed, $reg ), $tail )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
52 : _eval_type( $parsed, $reg );
53 }
54
55 69 ▶ sub eval_type {
Subroutine eval_type: called CC=2 SCAR=6.9
Pod documented
56 69 my ( $str, $reg ) = @_;
57 69 my ( $parsed, $tail ) = parse( $str );
58 66 ▶ _croak( "Unexpected tail on type expression: $tail" ) if $tail =~ /\S/sm;
Branch: 1 branch
BranchTrueFalseTotal
if $tail =~ /\S/ms 2 64 66
59 64 return _eval_type( $parsed, $reg );
60 }
61
62 125 my $std;
63
64 52 ▶ sub _std_eval {
Subroutine _std_eval: called CC=2 SCAR=6.9
65 52 require Type::Registry;
66 52 ▶ unless ( $std ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($std) 1 51 52
67 1 $std = "Type::Registry"->new;
68 1 $std->add_types( -Standard );
69 }
70 52 eval_type( $_[0], $std );
71 }
72
73 312 ▶ sub _eval_type {
Subroutine _eval_type: called CC=26 SCAR=32.8
74 312 my ( $node, $reg ) = @_;
75
76 312 $node = _simplify_expression( $node );
77
78 312 ▶ if ( $node->{type} eq "list" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "list") 59 253 312
79 59 return map _eval_type( $_, $reg ), @{ $node->{list} };
80 }
81
82 253 ▶ if ( $node->{type} eq "union" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "union") 18 235 253
83 18 return $reg->_make_union_by_overload( map _eval_type( $_, $reg ), @{ $node->{union} } );
84 }
85
86 235 ▶ if ( $node->{type} eq "intersect" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "intersect") 8 227 235
87 return $reg->_make_intersection_by_overload(
88 map _eval_type( $_, $reg ),
89 8 @{ $node->{intersect} }
90 );
91 }
92
93 227 ▶ if ( $node->{type} eq "slash" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "slash") 2 225 227
94 2 my @types = map _eval_type( $_, $reg ), @{ $node->{slash} };
95 2 ▶ _croak( "Expected exactly two types joined with slash operator" )
Branch: 1 branch
BranchTrueFalseTotal
unless @types == 2 0 2 2
96 unless @types == 2;
97 2 return $types[0] / $types[1];
98 }
99
100 225 ▶ if ( $node->{type} eq "slurpy" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "slurpy") 2 223 225
101 2 require Types::Standard;
102 2 return Types::Standard::Slurpy()->of( _eval_type( $node->{of}, $reg ) );
103 }
104
105 223 ▶ if ( $node->{type} eq "complement" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "complement") 4 219 223
106 4 return _eval_type( $node->{of}, $reg )->complementary_type;
107 }
108
109 219 ▶ if ( $node->{type} eq "parameterized" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "parameterized") 53 166 219
110 53 my $base = _eval_type( $node->{base}, $reg );
111
112 53 ▶ return $base unless $base->is_parameterizable || $node->{params};
Branch: 1 branch
BranchTrueFalseTotal
unless $base->is_parameterizable or $node->{"params"} 2 51 53
Condition66%2/3
$base->is_parameterizable or $node->{"params"}
l!l&&r!l&&!r
5102
MC/DC50%1/2
$base->is_parameterizable or $node->{"params"}
$base->is_parameterizable $node->{"params"}
Truth table66%2/3
A or B
A= $base->is_parameterizableB= $node->{"params"}
ABresult
1X1
011
000
113 return $base->parameterize(
114 51 ▶ $node->{params} ? _eval_type( $node->{params}, $reg ) : () );
Branch: 1 branch
BranchTrueFalseTotal
$node->{'params'} ? : 49 2 51
115 }
116
117 166 ▶ if ( $node->{type} eq "primary" and $node->{token}->type eq CLASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "primary" and $node->{"token"}->type eq "CLASS") 8 158 166
Condition66%2/3
$node->{"type"} eq "primary" and $node->{"token"}->type eq "CLASS"
!ll&&!rl&&r
01588
MC/DC50%1/2
$node->{"type"} eq "primary" and $node->{"token"}->type eq "CLASS"
$node->{"type"} eq "primary" $node->{"token"}->type eq "CLASS"
Truth table66%2/3
A and B
A= $node->{"type"} eq "primary"B= $node->{"token"}->type eq "CLASS"
ABresult
0X0
100
111
118 my $class = substr(
119 $node->{token}->spelling,
120 0,
121 8 length( $node->{token}->spelling ) - 2
122 );
123 8 return $reg->make_class_type( $class );
124 }
125
126 158 ▶ if ( $node->{type} eq "primary" and $node->{token}->type eq QUOTELIKE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "primary" and $node->{"token"}->type eq "QUOTELIKE") 12 146 158
Condition66%2/3
$node->{"type"} eq "primary" and $node->{"token"}->type eq "QUOTELIKE"
!ll&&!rl&&r
014612
MC/DC50%1/2
$node->{"type"} eq "primary" and $node->{"token"}->type eq "QUOTELIKE"
$node->{"type"} eq "primary" $node->{"token"}->type eq "QUOTELIKE"
Truth table66%2/3
A and B
A= $node->{"type"} eq "primary"B= $node->{"token"}->type eq "QUOTELIKE"
ABresult
0X0
100
111
127 12 return eval( $node->{token}->spelling ); #ARGH
128 }
129
130 146 ▶ if ( $node->{type} eq "primary" and $node->{token}->type eq STRING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "primary" and $node->{"token"}->type eq "STRING") 6 140 146
Condition66%2/3
$node->{"type"} eq "primary" and $node->{"token"}->type eq "STRING"
!ll&&!rl&&r
01406
MC/DC50%1/2
$node->{"type"} eq "primary" and $node->{"token"}->type eq "STRING"
$node->{"type"} eq "primary" $node->{"token"}->type eq "STRING"
Truth table66%2/3
A and B
A= $node->{"type"} eq "primary"B= $node->{"token"}->type eq "STRING"
ABresult
0X0
100
111
131 6 return $node->{token}->spelling;
132 }
133
134 140 ▶ if ( $node->{type} eq "primary" and $node->{token}->type eq HEXNUM ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "primary" and $node->{"token"}->type eq "HEXNUM") 3 137 140
Condition66%2/3
$node->{"type"} eq "primary" and $node->{"token"}->type eq "HEXNUM"
!ll&&!rl&&r
01373
MC/DC50%1/2
$node->{"type"} eq "primary" and $node->{"token"}->type eq "HEXNUM"
$node->{"type"} eq "primary" $node->{"token"}->type eq "HEXNUM"
Truth table66%2/3
A and B
A= $node->{"type"} eq "primary"B= $node->{"token"}->type eq "HEXNUM"
ABresult
0X0
100
111
135 3 my $sign = '+';
136 3 my $spelling = $node->{token}->spelling;
137 3 ▶ if ( $spelling =~ /^[+-]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spelling =~ /^[+-]/) 1 2 3
138 1 $sign = substr( $spelling, 0, 1);
139 1 $spelling = substr( $spelling, 1 );
140 }
141 return (
142 3 ▶ ( $sign eq '-' ) ? ( 0 - hex($spelling) ) : hex($spelling)
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 1 2 3
143 );
144 }
145
146 137 ▶ if ( $node->{type} eq "primary" and $node->{token}->type eq TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"type"} eq "primary" and $node->{"token"}->type eq "TYPE") 137 0 137
Condition33%1/3
$node->{"type"} eq "primary" and $node->{"token"}->type eq "TYPE"
!ll&&!rl&&r
00137
MC/DC0%0/2
$node->{"type"} eq "primary" and $node->{"token"}->type eq "TYPE"
$node->{"type"} eq "primary" $node->{"token"}->type eq "TYPE"
Truth table33%1/3
A and B
A= $node->{"type"} eq "primary"B= $node->{"token"}->type eq "TYPE"
ABresult
0X0
100
111
147 137 my $t = $node->{token}->spelling;
148 137 ▶ my $r =
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /^(.+)::(\w+)$/ ? : 0 137 137
149 ( $t =~ /^(.+)::(\w+)$/ )
150 ? $reg->foreign_lookup( $t, 1 )
151 : $reg->simple_lookup( $t, 1 );
152 137 ▶ $r or _croak( "%s is not a known type constraint", $node->{token}->spelling );
Branch: 1 branch
BranchTrueFalseTotal
$r or &_croak("%s is not a known type constraint", $node->{"token"}->spelling) 133 4 137
153 133 return $r;
154 }
155 } #/ sub _eval_type
156
157 312 ▶ sub _simplify_expression {
Subroutine _simplify_expression: called CC=9 SCAR=22.0
158 312 my $expr = shift;
159
160 312 ▶ if ( $expr->{type} eq "expression" and $expr->{op}[0] eq COMMA ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "COMMA") 10 302 312
Condition100%3/3
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "COMMA"
!ll&&!rl&&r
2742810
MC/DC100%2/2
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "COMMA"
$expr->{"type"} eq "expression" $expr->{"op"}[0] eq "COMMA"
Truth table100%3/3
A and B
A= $expr->{"type"} eq "expression"B= $expr->{"op"}[0] eq "COMMA"
ABresult
0X0
100
111
161 10 return _simplify( "list", COMMA, $expr );
162 }
163
164 302 ▶ if ( $expr->{type} eq "expression" and $expr->{op}[0] eq UNION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "UNION") 18 284 302
Condition100%3/3
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "UNION"
!ll&&!rl&&r
2741018
MC/DC100%2/2
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "UNION"
$expr->{"type"} eq "expression" $expr->{"op"}[0] eq "UNION"
Truth table100%3/3
A and B
A= $expr->{"type"} eq "expression"B= $expr->{"op"}[0] eq "UNION"
ABresult
0X0
100
111
165 18 return _simplify( "union", UNION, $expr );
166 }
167
168 284 ▶ if ( $expr->{type} eq "expression" and $expr->{op}[0] eq INTERSECT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "INTERSECT") 8 276 284
Condition100%3/3
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "INTERSECT"
!ll&&!rl&&r
27428
MC/DC100%2/2
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "INTERSECT"
$expr->{"type"} eq "expression" $expr->{"op"}[0] eq "INTERSECT"
Truth table100%3/3
A and B
A= $expr->{"type"} eq "expression"B= $expr->{"op"}[0] eq "INTERSECT"
ABresult
0X0
100
111
169 8 return _simplify( "intersect", INTERSECT, $expr );
170 }
171
172 276 ▶ if ( $expr->{type} eq "expression" and $expr->{op}[0] eq SLASH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "SLASH") 2 274 276
Condition66%2/3
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "SLASH"
!ll&&!rl&&r
27402
MC/DC50%1/2
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq "SLASH"
$expr->{"type"} eq "expression" $expr->{"op"}[0] eq "SLASH"
Truth table66%2/3
A and B
A= $expr->{"type"} eq "expression"B= $expr->{"op"}[0] eq "SLASH"
ABresult
0X0
100
111
173 2 return _simplify( "slash", SLASH, $expr );
174 }
175
176 274 return $expr;
177 } #/ sub _simplify_expression
178
179 52 ▶ sub _simplify {
Subroutine _simplify: called CC=4 SCAR=13.9
180 125 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
181 52 my $type = shift;
182 52 my $op = shift;
183
184 52 my @list;
185 52 for my $expr ( $_[0]{lhs}, $_[0]{rhs} ) {
186 104 ▶ if ( $expr->{type} eq "expression" and $expr->{op}[0] eq $op ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{'type'} eq 'expression' and $expr->{'op'}[0] eq $op) { } 14 90 104
Condition100%3/3
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq $op
!ll&&!rl&&r
83714
MC/DC100%2/2
$expr->{"type"} eq "expression" and $expr->{"op"}[0] eq $op
$expr->{"type"} eq "expression" $expr->{"op"}[0] eq $op
Truth table100%3/3
A and B
A= $expr->{"type"} eq "expression"B= $expr->{"op"}[0] eq $op
ABresult
0X0
100
111
187 14 my $simple = _simplify( $type, $op, $expr );
188 14 push @list, @{ $simple->{$type} };
189 }
190 else {
191 90 push @list, $expr;
192 }
193 }
194
195 52 return { type => $type, $type => \@list };
196 } #/ sub _simplify
197 } #/ Evaluate:
198  
199 {
200 125 package Type::Parser::AstBuilder;
201
202 125 our $AUTHORITY = 'cpan:TOBYINK';
203 125 our $VERSION = '2.010001';
204
205 125 $VERSION =~ tr/_//d;
206
207 331 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
208 331 my $class = shift;
209 331 bless {@_}, $class;
210 }
211
212 125 our %precedence = (
213
214 # Type::Parser::COMMA() , 1 ,
215 Type::Parser::SLASH(), 1,
216 Type::Parser::UNION(), 2,
217 Type::Parser::INTERSECT(), 3,
218 Type::Parser::NOT(), 4,
219 );
220
221 4096 ▶ sub _parse_primary {
Subroutine _parse_primary: called CC=14 SCAR=26.4
222 4096 my $self = shift;
223 4096 my $tokens = $self->{tokens};
224
225 4096 $tokens->assert_not_empty;
226
227 4096 ▶ if ( $tokens->peek( 0 )->type eq Type::Parser::NOT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->peek(0)->type eq "NOT") 4 4092 4096
228 4 $tokens->eat( Type::Parser::NOT );
229 4 $tokens->assert_not_empty;
230 return {
231 4 type => "complement",
232 of => $self->_parse_primary,
233 };
234 }
235
236 4092 ▶ if ( $tokens->peek( 0 )->type eq Type::Parser::SLURPY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->peek(0)->type eq "SLURPY") 2 4090 4092
237 2 $tokens->eat( Type::Parser::SLURPY );
238 2 $tokens->assert_not_empty;
239 return {
240 2 type => "slurpy",
241 of => $self->_parse_primary,
242 };
243 }
244
245 4090 ▶ if ( $tokens->peek( 0 )->type eq Type::Parser::L_PAREN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->peek(0)->type eq "L_PAREN") 9 4081 4090
246 9 $tokens->eat( Type::Parser::L_PAREN );
247 9 my $r = $self->_parse_expression;
248 9 $tokens->eat( Type::Parser::R_PAREN );
249 9 return $r;
250 }
251
252 4081 ▶ if ( $tokens->peek( 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->peek(1) and $tokens->peek(0)->type eq "TYPE" and $tokens->peek(1)->type eq "L_BRACKET") 365 3716 4081
Condition100%6/6
$tokens->peek(1) and $tokens->peek(0)->type eq "TYPE" and $tokens->peek(1)->type eq "L_BRACKET"
!ll&&!rl&&r
3502214365
$tokens->peek(1) and $tokens->peek(0)->type eq "TYPE"
!ll&&!rl&&r
233479579
MC/DC100%3/3
$tokens->peek(1) and $tokens->peek(0)->type eq "TYPE" and $tokens->peek(1)->type eq "L_BRACKET"
$tokens->peek(1) $tokens->peek(0)->type eq "TYPE" $tokens->peek(1)->type eq "L_BRACKET"
Truth table100%4/4
A and B and C
A= $tokens->peek(1)B= $tokens->peek(0)->type eq "TYPE"C= $tokens->peek(1)->type eq "L_BRACKET"
ABCresult
0XX0
10X0
1100
1111
253 and $tokens->peek( 0 )->type eq Type::Parser::TYPE
254 and $tokens->peek( 1 )->type eq Type::Parser::L_BRACKET )
255 {
256 365 my $base = { type => "primary", token => $tokens->eat( Type::Parser::TYPE ) };
257 365 $tokens->eat( Type::Parser::L_BRACKET );
258 365 $tokens->assert_not_empty;
259
260 365 local $precedence{ Type::Parser::COMMA() } = 1;
261
262 365 my $params = undef;
263 365 ▶ if ( $tokens->peek( 0 )->type eq Type::Parser::R_BRACKET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->peek(0)->type eq 'R_BRACKET') { } 4 361 365
264 4 $tokens->eat( Type::Parser::R_BRACKET );
265 }
266 else {
267 361 $params = $self->_parse_expression;
268 $params = { type => "list", list => [$params] }
269 361 ▶ unless $params->{type} eq "list";
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"type"} eq "list" 361 0 361
270 361 $tokens->eat( Type::Parser::R_BRACKET );
271 }
272 return {
273 365 type => "parameterized",
274 base => $base,
275 params => $params,
276 };
277 } #/ if ( $tokens->peek( 1 ...))
278
279 3716 my $type = $tokens->peek( 0 )->type;
280 3716 ▶ if ( $type eq Type::Parser::TYPE
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "TYPE" or $type eq "QUOTELIKE" or $type eq "STRING" or $type eq "HEXNUM" or $type eq "CLASS") 3713 3 3716
Condition100%12/12
$type eq "TYPE" or $type eq "QUOTELIKE" or $type eq "STRING" or $type eq "HEXNUM" or $type eq "CLASS"
l!l&&r!l&&!r
3703103
$type eq "TYPE" or $type eq "QUOTELIKE" or $type eq "STRING" or $type eq "HEXNUM"
l!l&&r!l&&!r
3700313
$type eq "TYPE" or $type eq "QUOTELIKE" or $type eq "STRING"
l!l&&r!l&&!r
3691916
$type eq "TYPE" or $type eq "QUOTELIKE"
l!l&&r!l&&!r
232345925
MC/DC100%5/5
$type eq "TYPE" or $type eq "QUOTELIKE" or $type eq "STRING" or $type eq "HEXNUM" or $type eq "CLASS"
$type eq "TYPE" $type eq "QUOTELIKE" $type eq "STRING" $type eq "HEXNUM" $type eq "CLASS"
Truth table100%6/6
A or B or C or D or E
A= $type eq "TYPE"B= $type eq "QUOTELIKE"C= $type eq "STRING"D= $type eq "HEXNUM"E= $type eq "CLASS"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
281 or $type eq Type::Parser::QUOTELIKE
282 or $type eq Type::Parser::STRING
283 or $type eq Type::Parser::HEXNUM
284 or $type eq Type::Parser::CLASS )
285 {
286 3713 return { type => "primary", token => $tokens->eat };
287 }
288
289 Type::Parser::_croak(
290 3 "Unexpected token in primary type expression; got '%s'",
291 $tokens->peek( 0 )->spelling
292 );
293 } #/ sub _parse_primary
294
295 701 ▶ sub _parse_expression_1 {
Subroutine _parse_expression_1: called CC=7 SCAR=19.5
296 701 my $self = shift;
297 701 my $tokens = $self->{tokens};
298
299 701 my ( $lhs, $min_p ) = @_;
300 701 ▶ while ( !$tokens->empty
Branch: 1 branch
BranchTrueFalseTotal
if (not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} >= $min_p) 3389 701 4090
Condition83%5/6
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} >= $min_p
!ll&&!rl&&r
70103389
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type}
!ll&&!rl&&r
3243773389
MC/DC66%2/3
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} >= $min_p
not $tokens->empty defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} >= $min_p
Truth table75%3/4
not(A) and B and C
A= not $tokens->emptyB= defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type}C= $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} >= $min_p
ABCresult
0XX0
10X0
1100
1111
301 and defined( $precedence{ $tokens->peek( 0 )->type } )
302 and $precedence{ $tokens->peek( 0 )->type } >= $min_p )
303 {
304 3389 my $op = $tokens->eat;
305 3389 my $rhs = $self->_parse_primary;
306
307 3389 ▶ while ( !$tokens->empty
Branch: 1 branch
BranchTrueFalseTotal
if (not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} > $Type::Parser::AstBuilder::precedence{$op->type}) 3 3389 3392
Condition100%6/6
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} > $Type::Parser::AstBuilder::precedence{$op->type}
!ll&&!rl&&r
27331163
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type}
!ll&&!rl&&r
132603119
MC/DC100%3/3
not $tokens->empty and defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} and $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} > $Type::Parser::AstBuilder::precedence{$op->type}
not $tokens->empty defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} > $Type::Parser::AstBuilder::precedence{$op->type}
Truth table100%4/4
not(A) and B and C
A= not $tokens->emptyB= defined $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type}C= $Type::Parser::AstBuilder::precedence{$tokens->peek(0)->type} > $Type::Parser::AstBuilder::precedence{$op->type}
ABCresult
0XX0
10X0
1100
1111
308 and defined( $precedence{ $tokens->peek( 0 )->type } )
309 and $precedence{ $tokens->peek( 0 )->type } > $precedence{ $op->type } )
310 {
311 3 my $lookahead = $tokens->peek( 0 );
312 3 $rhs = $self->_parse_expression_1( $rhs, $precedence{ $lookahead->type } );
313 }
314
315 $lhs = {
316 3389 type => "expression",
317 op => $op,
318 lhs => $lhs,
319 rhs => $rhs,
320 };
321 } #/ while ( !$tokens->empty and...)
322 701 return $lhs;
323 } #/ sub _parse_expression_1
324
325 701 ▶ sub _parse_expression {
Subroutine _parse_expression: called CC=1 SCAR=0.0
326 701 my $self = shift;
327 701 my $tokens = $self->{tokens};
328
329 701 return $self->_parse_expression_1( $self->_parse_primary, 0 );
330 }
331
332 331 ▶ sub build {
Subroutine build: called CC=1 SCAR=0.0
333 331 my $self = shift;
334 $self->{tokens} =
335 331 "Type::Parser::TokenStream"->new( remaining => $self->{input} );
336 331 $self->{ast} = $self->_parse_expression;
337 }
338
339 328 ▶ sub ast {
Subroutine ast: called CC=1 SCAR=0.0
340 328 $_[0]{ast};
341 }
342
343 69 ▶ sub remainder {
Subroutine remainder: called CC=1 SCAR=0.0
344 69 $_[0]{tokens}->remainder;
345 }
346 }
347  
348 {
349 125 package Type::Parser::Token;
350
351 125 our $AUTHORITY = 'cpan:TOBYINK';
352 125 our $VERSION = '2.010001';
353
354 125 $VERSION =~ tr/_//d;
355
356 46406 ▶ sub type { $_[0][0] }
Subroutine type: called CC=1 SCAR=0.0
357 3825 ▶ sub spelling { $_[0][1] }
Subroutine spelling: called CC=1 SCAR=0.0
358 }
359  
360 {
361 125 package Type::Parser::TokenStream;
362
363 125 our $AUTHORITY = 'cpan:TOBYINK';
364 125 our $VERSION = '2.010001';
365
366 125 $VERSION =~ tr/_//d;
367
368 125 ▶ use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
369
370 331 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
371 331 my $class = shift;
372 331 bless { stack => [], done => [], @_ }, $class;
373 }
374
375 50685 ▶ sub peek {
Subroutine peek: called CC=3 SCAR=11.0
376 50685 my $self = shift;
377 50685 my $ahead = $_[0];
378
379 50685 ▶ while ( $self->_stack_size <= $ahead and length $self->{remaining} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_stack_size <= $ahead and length $self->{"remaining"}) 8229 50685 58914
Condition100%3/3
$self->_stack_size <= $ahead and length $self->{"remaining"}
!ll&&!rl&&r
503253608229
MC/DC100%2/2
$self->_stack_size <= $ahead and length $self->{"remaining"}
$self->_stack_size <= $ahead length $self->{"remaining"}
Truth table100%3/3
A and B
A= $self->_stack_size <= $aheadB= length $self->{"remaining"}
ABresult
0X0
100
111
380 8229 $self->_stack_extend;
381 }
382
383 50685 my @tokens = grep ref, @{ $self->{stack} };
384 50685 return $tokens[$ahead];
385 } #/ sub peek
386
387 11949 ▶ sub empty {
Subroutine empty: called CC=1 SCAR=0.0
388 11949 my $self = shift;
389 11949 not $self->peek( 0 );
390 }
391
392 8221 ▶ sub eat {
Subroutine eat: called CC=6 SCAR=18.6
393 8221 my $self = shift;
394 8221 ▶ $self->_stack_extend unless $self->_stack_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_stack_size 0 8221 8221
395 8221 my $r;
396 8221 ▶ while ( defined( my $item = shift @{ $self->{stack} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = shift @{$$self{"stack"};})) 8247 0 8247
397 8247 push @{ $self->{done} }, $item;
398 8247 ▶ if ( ref $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) 8221 26 8247
399 8221 $r = $item;
400 8221 last;
401 }
402 }
403
404 8221 ▶ if ( @_ and $_[0] ne $r->type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] ne $r->type) 0 8221 8221
Condition66%2/3
@_ and $_[0] ne $r->type
!ll&&!rl&&r
710211190
MC/DC0%0/2
@_ and $_[0] ne $r->type
@_ $_[0] ne $r->type
Truth table66%2/3
A and B
A= @_B= $_[0] ne $r->type
ABresult
0X0
100
111
405 0 unshift @{ $self->{stack} }, pop @{ $self->{done} }; # uncoverable statement
406 0 Type::Parser::_croak( "Expected $_[0]; got " . $r->type ); # uncoverable statement
407 }
408
409 8221 return $r;
410 } #/ sub eat
411
412 4467 ▶ sub assert_not_empty {
Subroutine assert_not_empty: called CC=2 SCAR=7.2
413 4467 my $self = shift;
414 4467 ▶ Type::Parser::_croak( "Expected token; got empty string" ) if $self->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty 0 4467 4467
415 }
416
417 67135 ▶ sub _stack_size {
Subroutine _stack_size: called CC=1 SCAR=0.0
418 67135 my $self = shift;
419 67135 scalar grep ref, @{ $self->{stack} };
420 }
421
422 8229 ▶ sub _stack_extend {
Subroutine _stack_extend: called CC=2 SCAR=6.9
423 8229 my $self = shift;
424 8229 push @{ $self->{stack} }, $self->_read_token;
425 8229 my ( $space ) = ( $self->{remaining} =~ m/^([\s\n\r]*)/sm );
426 8229 ▶ return unless length $space;
Branch: 1 branch
BranchTrueFalseTotal
unless length $space 8197 32 8229
427 32 push @{ $self->{stack} }, $space;
428 32 substr( $self->{remaining}, 0, length $space ) = "";
429 }
430
431 69 ▶ sub remainder {
Subroutine remainder: called CC=2 SCAR=6.9
432 69 my $self = shift;
433 return join "",
434 80 ▶ map { ref( $_ ) ? $_->spelling : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 5 75 80
435 69 ( @{ $self->{stack} }, $self->{remaining} );
436 }
437
438 125 my %punctuation = (
439 '[' => bless( [ Type::Parser::L_BRACKET, "[" ], "Type::Parser::Token" ),
440 ']' => bless( [ Type::Parser::R_BRACKET, "]" ], "Type::Parser::Token" ),
441 '(' => bless( [ Type::Parser::L_PAREN, "[" ], "Type::Parser::Token" ),
442 ')' => bless( [ Type::Parser::R_PAREN, "]" ], "Type::Parser::Token" ),
443 ',' => bless( [ Type::Parser::COMMA, "," ], "Type::Parser::Token" ),
444 '=>' => bless( [ Type::Parser::COMMA, "=>" ], "Type::Parser::Token" ),
445 'slurpy' => bless( [ Type::Parser::SLURPY, "slurpy" ], "Type::Parser::Token" ),
446 '|' => bless( [ Type::Parser::UNION, "|" ], "Type::Parser::Token" ),
447 '&' => bless( [ Type::Parser::INTERSECT, "&" ], "Type::Parser::Token" ),
448 '/' => bless( [ Type::Parser::SLASH, "/" ], "Type::Parser::Token" ),
449 '~' => bless( [ Type::Parser::NOT, "~" ], "Type::Parser::Token" ),
450 );
451
452 8229 ▶ sub _read_token {
Subroutine _read_token: called CC=11 SCAR=24.0
453 8229 my $self = shift;
454
455 8229 ▶ return if $self->{remaining} eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"remaining"} eq "" 0 8229 8229
456
457 # Punctuation
458 #
459
460 8229 ▶ if ( $self->{remaining} =~ /^( => | [()\]\[|&~,\/] )/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} =~ m(^( => | [()\]\[|&~,/] ))msx) 4143 4086 8229
461 4143 my $spelling = $1;
462 4143 substr( $self->{remaining}, 0, length $spelling ) = "";
463 4143 return $punctuation{$spelling};
464 }
465
466 4086 ▶ if ( $self->{remaining} =~ /\A\s*[q'"]/sm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} =~ /\A\s*[q'"]/ms) 3459 627 4086
467 3459 require Text::Balanced;
468 3459 ▶ if ( my $quotelike = Text::Balanced::extract_quotelike( $self->{remaining} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quotelike = &Text::Balanced::extract_quotelike($self->{"remaining"})) 3459 0 3459
469 3459 return bless( [ Type::Parser::QUOTELIKE, $quotelike ], "Type::Parser::Token" );
470 }
471 }
472
473 627 ▶ if ( $self->{remaining} =~ /^([+-]?[\w:.+]+)/sm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} =~ /^([+-]?[\w:.+]+)/ms) 624 3 627
474 624 my $spelling = $1;
475 624 substr( $self->{remaining}, 0, length $spelling ) = "";
476
477 624 ▶ if ( $spelling =~ /::$/sm ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($spelling =~ /::$/ms) { } 10 614 624
elsif ($spelling =~ /^[+-]?0x[0-9A-Fa-f]+$/ms) { } 3 611 614
elsif (&looks_like_number($spelling)) { } 5 606 611
elsif ($self->{'remaining'} =~ /^\s*=>/ms) { } 4 602 606
elsif ($spelling eq 'slurpy') { } 2 600 602
478 10 return bless( [ Type::Parser::CLASS, $spelling ], "Type::Parser::Token" );
479 }
480 elsif ( $spelling =~ /^[+-]?0x[0-9A-Fa-f]+$/sm ) {
481 3 return bless( [ Type::Parser::HEXNUM, $spelling ], "Type::Parser::Token" );
482 }
483 elsif ( looks_like_number( $spelling ) ) {
484 5 return bless( [ Type::Parser::STRING, $spelling ], "Type::Parser::Token" );
485 }
486 elsif ( $self->{remaining} =~ /^\s*=>/sm ) # peek ahead
487 {
488 4 return bless( [ Type::Parser::STRING, $spelling ], "Type::Parser::Token" );
489 }
490 elsif ( $spelling eq "slurpy" ) {
491 2 return $punctuation{$spelling};
492 }
493
494 600 return bless( [ Type::Parser::TYPE, $spelling ], "Type::Parser::Token" );
495 } #/ if ( $self->{remaining...})
496
497 3 my $rest = $self->{remaining};
498 3 $self->{remaining} = "";
499 3 return bless( [ Type::Parser::MYSTERY, $rest ], "Type::Parser::Token" );
500 } #/ sub _read_token
501 }
502  
503 125 1;
504  
505 __END__
« lib/Types/Standard/Dict.pm ↑ Summary lib/Types/Standard/ArrayRef.pm »