/usr/share/perl5/AtteanX/Parser/NTuples.pm is in libattean-perl 0.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | use v5.14;
use warnings;
=head1 NAME
AtteanX::Parser::NTuples - Shared functionality for N-Triples and N-Quads parsers
=head1 VERSION
This document describes AtteanX::Parser::NTuples version 0.019
=head1 SYNOPSIS
use Attean;
=head1 DESCRIPTION
...
=head1 METHODS
=over 4
=cut
package AtteanX::Parser::NTuples 0.019 {
use utf8;
use Moo;
use Attean;
use Carp qw(carp);
use Encode qw(decode);
use namespace::clean;
=item C<< parse_term_from_string( $bytes ) >>
This method is deprecated, as the name was misleading.
Calls should be replaced with C<< parse_term_from_bytes >>.
=item C<< parse_term_from_bytes( $bytes ) >>
Parses the given C<< $bytes >> and returns a corresponding L<Attean::API::Term> object.
=cut
sub parse_term_from_string {
carp "parse_term_from_string is deprecated and will be removed in Attean 0.019 (use parse_term_from_bytes instead)";
return shift->parse_term_from_bytes(@_);
}
sub parse_term_from_bytes {
my $self = shift;
unless (ref($self)) {
$self = $self->new();
}
my $string = shift;
my $n = $self->_eat_node( 0, $string );
return $n;
}
=item C<< parse_iter_from_bytes( $data ) >>
Returns an iterator of L<Attean::API::Binding> objects that result from parsing
the data read from the UTF-8 encoded byte string C<< $data >>.
=cut
sub parse_iter_from_bytes {
my $self = shift;
my $data = shift;
$data = Encode::encode("utf-8", $data);
open(my $fh, '<:encoding(UTF-8)', \$data);
return $self->parse_iter_from_io($fh);
}
=item C<< parse_iter_from_io( $fh ) >>
Returns an iterator of L<Attean::API::Binding> objects that result from parsing
the data read from the L<IO::Handle> object C<< $fh >>.
=cut
sub parse_iter_from_io {
my $self = shift;
my $fh = shift;
my $lineno = 0;
my $line;
my $gen = sub {
while (defined($line = <$fh>)) {
($line, my @extra) = split(/\r\n|\r|\n/, $line, 2);
$lineno++;
next unless (defined($line) and length($line));
next unless ($line =~ /\S/);
chomp($line);
$line =~ s/^\s*//;
$line =~ s/\s*$//;
next if ($line =~ /^#/);
my @nodes = ();
while (my $n = $self->_eat_node( $lineno, $line )) {
push(@nodes, $n);
$line =~ s/^\s*//;
}
$line =~ s/^\s//g;
unless ($line eq '.') {
die "Missing expected '.' at line $lineno";
}
my $binding = $self->_binding( \@nodes, $lineno );
if (@extra and $extra[0] ne '') {
$line = shift(@extra);
goto LINE;
}
return $binding;
}
return;
};
return Attean::CodeIterator->new(
generator => $gen,
item_type => $self->handled_type->role,
);
}
sub _eat_node {
my $self = shift;
my $lineno = shift;
$_[0] =~ s/^\s*//;
return unless length($_[0]);
my $char = substr($_[0], 0, 1);
return if ($char eq '.');
if ($char eq '<') {
my ($uri) = $_[0] =~ m/^<([^>]*)>/;
substr($_[0], 0, length($uri)+2) = '';
state %cache;
if (my $i = $cache{$uri}) {
return $i;
} else {
if (rand() < 0.02) {
# clear out the cache roughly every 50 IRIs
%cache = ();
}
my $iri = $self->new_iri( value => _unescape($uri, $lineno) );
$cache{$uri} = $iri;
return $iri;
}
} elsif ($char eq '_') {
my ($name) = $_[0] =~ m/^_:([A-Za-z][A-Za-z0-9]*)/;
substr($_[0], 0, length($name)+2) = '';
return Attean::Blank->new( $name );
} elsif ($char eq '"') {
substr($_[0], 0, 1) = '';
my $value = decode('utf8', '');
while (length($_[0]) and substr($_[0], 0, 1) ne '"') {
while ($_[0] =~ m/^([^"\\]+)/) {
$value .= $1;
substr($_[0],0,length($1)) = '';
}
if (substr($_[0],0,1) eq '\\') {
while ($_[0] =~ m/^\\(.)/) {
if ($1 eq 't') {
$value .= "\t";
substr($_[0],0,2) = '';
} elsif ($1 eq 'r') {
$value .= "\r";
substr($_[0],0,2) = '';
} elsif ($1 eq 'n') {
$value .= "\n";
substr($_[0],0,2) = '';
} elsif ($1 eq '"') {
$value .= '"';
substr($_[0],0,2) = '';
} elsif ($1 eq '\\') {
$value .= "\\";
substr($_[0],0,2) = '';
} elsif ($1 eq 'u') {
$_[0] =~ m/^\\u([0-9A-Fa-f]{4})/ or die qq[Bad N-Triples \\u escape at line $lineno, near "$_[0]"];
$value .= chr(oct('0x' . $1));
substr($_[0],0,6) = '';
} elsif ($1 eq 'U') {
$_[0] =~ m/^\\U([0-9A-Fa-f]{8})/ or die qq[Bad N-Triples \\U escape at line $lineno, near "$_[0]"];
$value .= chr(oct('0x' . $1));
substr($_[0],0,10) = '';
} else {
die qq[Not valid N-Triples escape character '\\$1' at line $lineno, near "$_[0]"];
}
}
}
}
if (substr($_[0],0,1) eq '"') {
substr($_[0],0,1) = '';
} else {
die qq[Ending double quote not found at line $lineno];
}
if ($_[0] =~ m/^@([a-z]+(-[a-zA-Z0-9]+)*)/) {
my $lang = $1;
substr($_[0],0,1+length($lang)) = '';
return Attean::Literal->new( value => $value, language => $lang );
} elsif (substr($_[0],0,3) eq '^^<') {
substr($_[0],0,3) = '';
my ($uri) = $_[0] =~ m/^([^>]*)>/;
substr($_[0], 0, length($uri)+1) = '';
my $dt = $self->new_iri(value => $uri);
return Attean::Literal->new( value => $value, datatype => $dt);
} else {
return Attean::Literal->new($value);
}
} else {
die qq[Not valid N-Triples node start character '$char' at line $lineno, near "$_[0]"];
}
}
sub _unescape {
my $string = shift;
my $lineno = shift;
my $value = '';
while (length($string)) {
while ($string =~ m/^([^\\]+)/) {
$value .= $1;
substr($string,0,length($1)) = '';
}
if (length($string)) {
if ($string eq '\\') {
die qq[Backslash in N-Triples node without escaped character at line $lineno];
}
if ($string =~ m/^\\([tbnrf"'uU])/) {
while ($string =~ m/^\\([tbnrf"'uU])/) {
if ($1 eq 't') {
$value .= "\t";
substr($string,0,2) = '';
} elsif ($1 eq 'b') {
$value .= "\b";
substr($string,0,2) = '';
} elsif ($1 eq 'n') {
$value .= "\n";
substr($string,0,2) = '';
} elsif ($1 eq 'r') {
$value .= "\r";
substr($string,0,2) = '';
} elsif ($1 eq 'f') {
$value .= "\f";
substr($string,0,2) = '';
} elsif ($1 eq '"') {
$value .= '"';
substr($string,0,2) = '';
} elsif ($1 eq '\\') {
$value .= "\\";
substr($string,0,2) = '';
} elsif ($1 eq 'u') {
$string =~ m/^\\u([0-9A-F]{4})/ or die qq[Bad N-Triples \\u escape at line $lineno, near "$string"];
$value .= chr(oct('0x' . $1));
substr($string,0,6) = '';
} elsif ($1 eq 'U') {
$string =~ m/^\\U([0-9A-F]{8})/ or die qq[Bad N-Triples \\U escape at line $lineno, near "$string"];
$value .= chr(oct('0x' . $1));
substr($string,0,10) = '';
}
}
} else {
my $esc = substr($string, 0, 2);
die qq[Not a valid N-Triples escape sequence '$esc' at line $lineno, near "$string"];
}
}
}
return $value;
}
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/perlrdf/issues>.
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2018 Gregory Todd Williams. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|