blib/lib/MarpaX/RFC/RFC3987.pm

filter: Statementstmt 43.6% 41 / 94 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.0% 7 / 25 Podpod 33.3% 1 / 3 totaltotal 37.9% 49 / 129 CC 3 SCAR 8.5
CC
3
Cov
41%
BUILDARGS
6.0
is_absolute
6.0
BUILD
2.0
CRAP 2.3
↑ Summary
1 package MarpaX::RFC::RFC3987;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Types::Standard -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Marpa::R2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Internationalized Resource Identifier (IRI): Generic Syntax - Marpa Parser
10  
11 # References: RFC 3987 IRI http://tools.ietf.org/html/rfc3987
12  
13 # AUTHORITY
14  
15 1 our $VERSION = '0.001'; # VERSION
16  
17  
18 1 our $DATA = do { local $/; <DATA>; };
19  
20 1 has value => (is => 'ro', isa => Str, required => 1 );
21  
22 1 ▶class_has grammar => (is => 'ro', isa => InstanceOf['Marpa::R2::Scanless:G'], default => sub { return Marpa::R2::Scanless::G->new({ source => \$DATA }) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 1 class_has bnf => (is => 'ro', isa => Str, default => $DATA );
25  
26 1 has scheme => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_scheme');
27 1 has iauthority => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_iauthority');
28 1 has ipath => (is => 'ro', isa => Str, default => '', writer => '_set_ipath'); # There is always a path in an IRI
29 1 has iquery => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_iquery');
30 1 has ifragment => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ifragment');
31  
32 1 has ihier_part => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ihier_part');
33 1 has iuserinfo => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_iuserinfo');
34 1 has ihost => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ihost');
35 1 has port => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_port');
36 1 has irelative_part => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_irelative_part');
37 1 has ip_literal => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ip_literal');
38 1 has zoneid => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_zoneid' );
39 1 has ipv4address => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ipv4address');
40 1 has ireg_name => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ireg_name');
41  
42 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my ($class, @args) = @_;
44  
45 0 ▶ unshift(@args, 'value') if (@args % 2 == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 0 0 0
46  
47 0 return { @args };
48 };
49  
50 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
51 0 my ($self) = @_;
52 #
53 # This hack just to avoid recursivity: we do not want Marpa to
54 # call another new() but operate on our instance immediately
55 #
56 0 local $MarpaX::RFC::RFC3987::SELF = $self;
57 0 $self->grammar->parse(\$self->value, { ranking_method => 'high_rule_only' });
58  
59 0 return;
60 }
61  
62 0 ▶sub is_absolute {
Subroutine is_absolute: not called CC=2 SCAR=17.9
Pod documented
63 0 my ($self) = @_;
64 #
65 ## No need to reparse. An absolute IRI is when scheme and ihier_part are defined
66 #
67 0 ▶ return Str->check($self->scheme) && Str->check($self->ihier_part);
Condition0%0/3
&Str()->check($self->scheme) && &Str()->check($self->ihier_part)
!ll&&!rl&&r
000
MC/DC0%0/2
&Str()->check($self->scheme) && &Str()->check($self->ihier_part)
&Str()->check($self->scheme) &Str()->check($self->ihier_part)
Truth table0%0/3
A && B
A= &Str()->check($self->scheme)B= &Str()->check($self->ihier_part)
ABresult
0X0
100
111
68 }
69  
70 #
71 # Grammar rules
72 #
73 0 ▶sub _marpa_concat { shift; return join('', @_); }
Subroutine _marpa_concat: not called CC=1 SCAR=6.9
74 0 ▶sub _marpa_scheme { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_scheme ($self->_marpa_concat(@_)); }
Subroutine _marpa_scheme: not called CC=1 SCAR=6.9
75 0 ▶sub _marpa_iauthority { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_iauthority ($self->_marpa_concat(@_)); }
Subroutine _marpa_iauthority: not called CC=1 SCAR=6.9
76 0 ▶sub _marpa_ipath { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ipath ($self->_marpa_concat(@_)); }
Subroutine _marpa_ipath: not called CC=1 SCAR=6.9
77 0 ▶sub _marpa_iquery { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_iquery ($self->_marpa_concat(@_)); }
Subroutine _marpa_iquery: not called CC=1 SCAR=6.9
78 0 ▶sub _marpa_ifragment { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ifragment ($self->_marpa_concat(@_)); }
Subroutine _marpa_ifragment: not called CC=1 SCAR=6.9
79  
80 0 ▶sub _marpa_ihier_part { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ihier_part ($self->_marpa_concat(@_)); }
Subroutine _marpa_ihier_part: not called CC=1 SCAR=6.9
81 0 ▶sub _marpa_iuserinfo { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_iuserinfo ($self->_marpa_concat(@_)); }
Subroutine _marpa_iuserinfo: not called CC=1 SCAR=6.9
82 0 ▶sub _marpa_ihost { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ihost ($self->_marpa_concat(@_)); }
Subroutine _marpa_ihost: not called CC=1 SCAR=6.9
83 0 ▶sub _marpa_port { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_port ($self->_marpa_concat(@_)); }
Subroutine _marpa_port: not called CC=1 SCAR=6.9
84 0 ▶sub _marpa_irelative_part { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_irelative_part ($self->_marpa_concat(@_)); }
Subroutine _marpa_irelative_part: not called CC=1 SCAR=6.9
85 0 ▶sub _marpa_ip_literal { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ip_literal ($self->_marpa_concat(@_)); }
Subroutine _marpa_ip_literal: not called CC=1 SCAR=6.9
86 0 ▶sub _marpa_zoneid { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_zoneid ($self->_marpa_concat(@_)); }
Subroutine _marpa_zoneid: not called CC=1 SCAR=6.9
87 0 ▶sub _marpa_ipv4address { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ipv4address ($self->_marpa_concat(@_)); }
Subroutine _marpa_ipv4address: not called CC=1 SCAR=6.9
88 0 ▶sub _marpa_ireg_name { shift; my $self = $MarpaX::RFC::RFC3987::SELF; return $self->_set_ireg_name ($self->_marpa_concat(@_)); }
Subroutine _marpa_ireg_name: not called CC=1 SCAR=6.9
89  
90 1 1;
91  
92 =pod
93  
94 =encoding UTF-8
95  
96 =head1 NAME
97  
98 MarpaX::RFC::RFC3987 - Internationalized Resource Identifier (IRI): Generic Syntax - Marpa Parser
99  
100 =head1 VERSION
101  
102 version 0.001
103  
104 =head1 SYNOPSIS
105  
106 use MarpaX::RFC::RFC3987;
107 use Try::Tiny;
108 use Data::Dumper;
109  
110 print Dumper(MarpaX::RFC::RFC3987->new('http://www.perl.org'));
111  
112 try {
113 print STDERR "\nThe following is an expected failure:\n";
114 MarpaX::RFC::RFC3987->new('http://invalid##');
115 } catch {
116 print STDERR "$_\n";
117 return;
118 }
119  
120 =head1 DESCRIPTION
121  
122 This module parses an IRI reference as per RFC3987. It is intended as a data validation module using a strict grammar with good error reporting.
123  
124 =head1 IRI DESCRIPTION
125  
126 Quoted from the URI RFC 3986, with which an IRI is sharing the same principle, here is the overall structure of an URI that will help understand the meaning of the methods thereafter:
127  
128 foo://example.com:8042/over/there?name=ferret#nose
129 \_/ \______________/\_________/ \_________/ \__/
130 | | | | |
131 scheme authority path query fragment
132 | _____________________|__
133 / \ / \
134 urn:example:animal:ferret:nose
135  
136 The grammar is parsing both absolute IRI and relative IRI, the corresponding start rule being named a IRI reference.
137  
138 An absolute IRI has the following structure:
139  
140 IRI = scheme ":" ihier-part [ "?" iquery ] [ "#" ifragment ]
141  
142 while a relative IRI is split into:
143  
144 irelative-ref = irelative-part [ "?" iquery ] [ "#" ifragment ]
145  
146 Back to the overall structure, the authority is:
147  
148 iauthority = [ iuserinfo "@" ] ihost [ ":" port ]
149  
150 where the host can be an IP-literal with Zone information, and IPV4 address or a registered name:
151  
152 host = IP-literal / IPv4address / ireg-name
153  
154 The Zone Identifier is an extension to original URI RFC3986, is defined in RFC6874, and has been applied into the IRI grammar (the current IRI spec just says it does not support Zone Identifiers); it is an IPv6addrz:
155  
156 IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture ) "]"
157  
158 ZoneID = 1*( iunreserved / pct-encoded )
159  
160 IPv6addrz = IPv6address "%25" ZoneID
161  
162 =head1 CLASS METHODS
163  
164 =head2 MarpaX::RFC::RFC3987->new(@options --> InstanceOf['MarpaX::RFC::RFC3987'])
165  
166 Instantiate a new object. Usage is either C<MarpaX::RFC::RFC3987-E<gt>new(value =E<gt> $iri)> or C<MarpaX::RFC::RFC3987-E<gt>new($iri)>. This method will croak if the the C<$iri> parameter cannot coerce to a string nor is a valid IRI. The variable C<$self> is used below to refer to this object instance.
167  
168 =head2 MarpaX::RFC::RFC3987->grammar( --> InstanceOf['Marpa::R2::Scanless::G'])
169  
170 A Marpa::R2::Scanless::G instance, hosting the computed grammar. This is a class variable, i.e. works also with C<$self>.
171  
172 =head2 MarpaX::RFC::RFC3987->bnf( --> Str)
173  
174 The BNF grammar used to parse an IRI. This is a class variable, i.e. works also with C<$self>.
175  
176 =head1 OBJECT METHODS
177  
178 =head2 $self->value( --> Str)
179  
180 The variable given in input to C<new()>.
181  
182 =head2 $self->scheme( --> Str|Undef)
183  
184 The IRI scheme. Can be undefined.
185  
186 =head2 $self->iauthority( --> Str|Undef)
187  
188 The IRI authority. Can be undefined.
189  
190 =head2 $self->ipath( --> Str)
191  
192 The IRI path. Note that an IRI always have a path, although it can be empty.
193  
194 =head2 $self->iquery( --> Str|Undef)
195  
196 The IRI query. Can be undefined.
197  
198 =head2 $self->ifragment( --> Str|Undef)
199  
200 The IRI fragment. Can be undefined.
201  
202 =head2 $self->ihier_part( --> Str|Undef)
203  
204 The IRI hier part. Can be undefined.
205  
206 =head2 $self->iuserinfo( --> Str|Undef)
207  
208 The IRI userinfo. Can be undefined.
209  
210 =head2 $self->ihost( --> Str|Undef)
211  
212 The IRI host. Can be undefined.
213  
214 =head2 $self->port( --> Str|Undef)
215  
216 The IRI port. Can be undefined.
217  
218 =head2 $self->irelative_part( --> Str|Undef)
219  
220 The IRI relative part. Can be undefined.
221  
222 =head2 $self->ip_literal( --> Str|Undef)
223  
224 The IRI IP literal. Can be undefined.
225  
226 =head2 $self->zoneid( --> Str|Undef)
227  
228 The IRI IP's zone id. Can be undefined.
229  
230 =head2 $self->ipv4address( --> Str|Undef)
231  
232 The IRI IP Version 4 address. Can be undefined.
233  
234 =head2 $self->ireg_name( --> Str|Undef)
235  
236 The IRI registered name. Can be undefined.
237  
238 =head2 $self->is_absolute( --> Bool)
239  
240 Returns a true value if the IRI is absolute, false otherwise.
241  
242 =head1 SEE ALSO
243  
244 L<Marpa::R2>
245  
246 L<IRI>
247  
248 L<Uniform Resource Identifier (URI): Generic Syntax|http://tools.ietf.org/html/rfc3986>
249  
250 L<Internationalized Resource Identifier (IRI): Generic Syntax|http://tools.ietf.org/html/rfc3987>
251  
252 L<Formats for IPv6 Scope Zone Identifiers in Literal Address Formats|https://tools.ietf.org/html/draft-fenner-literal-zone-02>
253  
254 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
255  
256 =head1 SUPPORT
257  
258 =head2 Bugs / Feature Requests
259  
260 Please report any bugs or feature requests through the issue tracker
261 at L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX-RFC-RFC3987>.
262 You will be notified automatically of any progress on your issue.
263  
264 =head2 Source Code
265  
266 This is open source software. The code repository is available for
267 public review and contribution under the terms of the license.
268  
269 L<https://github.com/jddurand/marpax-rfc-rfc3987>
270  
271 git clone git://github.com/jddurand/marpax-rfc-rfc3987.git
272  
273 =head1 AUTHOR
274  
275 Jean-Damien Durand <jeandamiendurand@free.fr>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is copyright (c) 2015 by Jean-Damien Durand.
280  
281 This is free software; you can redistribute it and/or modify it under
282 the same terms as the Perl 5 programming language system itself.
283  
284 =cut
285  
286 __DATA__
↑ Summary