/usr/share/perl5/AtteanX/Parser/Turtle/Lexer.pm is in libattean-perl 0.012-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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | # AtteanX::Parser::Turtle::Lexer
# -----------------------------------------------------------------------------
=head1 NAME
AtteanX::Parser::Turtle::Lexer - Tokenizer for parsing Turtle, TriG, and N-Triples
=head1 VERSION
This document describes AtteanX::Parser::Turtle::Lexer version 0.012
=head1 SYNOPSIS
use AtteanX::Parser::Turtle::Lexer;
my $l = AtteanX::Parser::Turtle::Lexer->new( file => $fh );
while (my $t = $l->get_token) {
...
}
=head1 METHODS
=over 4
=cut
package AtteanX::Parser::Turtle::Lexer 0.012 {
use AtteanX::Parser::Turtle::Constants;
use v5.14;
use strict;
use warnings;
use Data::Dumper;
use Moo;
use Types::Standard qw(FileHandle Ref Str Int ArrayRef HashRef ConsumerOf InstanceOf);
use namespace::clean;
my $r_nameChar_extra = qr'[-0-9\x{B7}\x{0300}-\x{036F}\x{203F}-\x{2040}]'o;
my $r_nameStartChar_minus_underscore = qr'[A-Za-z\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{02FF}\x{0370}-\x{037D}\x{037F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{00010000}-\x{000EFFFF}]'o;
my $r_nameStartChar = qr/[A-Za-z_\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{02FF}\x{0370}-\x{037D}\x{037F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]/o;
my $r_nameChar = qr/${r_nameStartChar}|[-0-9\x{b7}\x{0300}-\x{036f}\x{203F}-\x{2040}]/o;
my $r_prefixName = qr/(?:(?!_)${r_nameStartChar})(?:$r_nameChar)*/o;
my $r_nameChar_test = qr"(?:$r_nameStartChar|$r_nameChar_extra)"o;
my $r_double = qr'[+-]?([0-9]+\.[0-9]*[eE][+-]?[0-9]+|\.[0-9]+[eE][+-]?[0-9]+|[0-9]+[eE][+-]?[0-9]+)'o;
my $r_decimal = qr'[+-]?(([0-9]+\.[0-9]+)|\.([0-9])+)'o;
my $r_integer = qr'[+-]?[0-9]+'o;
my $r_PN_CHARS_U = qr/[_A-Za-z_\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{02FF}\x{0370}-\x{037D}\x{037F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]/o;
my $r_PN_CHARS = qr"${r_PN_CHARS_U}|[-0-9\x{00B7}\x{0300}-\x{036F}\x{203F}-\x{2040}]"o;
my $r_bnode_id = qr"(?:${r_PN_CHARS_U}|[0-9])((${r_PN_CHARS}|[.])*${r_PN_CHARS})?"o;
my $r_PN_CHARS_BASE = qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/o;
my $r_PN_PREFIX = qr/(?:${r_PN_CHARS_BASE}(?:(?:${r_PN_CHARS}|[.])*${r_PN_CHARS})?)/o;
my $r_PN_LOCAL_ESCAPED = qr{(?:\\(?:[-~.!&'()*+,;=/?#@%_\$]))|%[0-9A-Fa-f]{2}}o;
our $r_PN_LOCAL = qr/(?:(?:${r_PN_CHARS_U}|[:0-9]|${r_PN_LOCAL_ESCAPED})(?:(?:${r_PN_CHARS}|${r_PN_LOCAL_ESCAPED}|[:.])*(?:${r_PN_CHARS}|[:]|${r_PN_LOCAL_ESCAPED}))?)/o;
my $r_PN_LOCAL_BNODE = qr/(?:(?:${r_PN_CHARS_U}|[0-9])(?:(?:${r_PN_CHARS}|[.])*${r_PN_CHARS})?)/o;
our $r_PNAME_NS = qr/(?:(?:${r_PN_PREFIX})?:)/o;
our $r_PNAME_LN = qr/(?:${r_PNAME_NS}${r_PN_LOCAL})/o;
with 'AtteanX::API::Lexer';
=item C<< new_token ( $type, $start_line, $start_column, @values ) >>
Returns a new token with the given type and optional values, capturing the
current line and column of the input data.
=cut
sub new_token {
my $self = shift;
my $type = shift;
my $start_line = shift;
my $start_col = shift;
my $line = $self->line;
my $col = $self->column;
return AtteanX::Parser::Turtle::Token->fast_constructor( $type, $start_line, $start_col, $line, $col, \@_ );
}
my %CHAR_TOKEN = (
'.' => DOT,
';' => SEMICOLON,
'[' => LBRACKET,
']' => RBRACKET,
'(' => LPAREN,
')' => RPAREN,
'{' => LBRACE,
'}' => RBRACE,
',' => COMMA,
'=' => EQUALS,
);
my %METHOD_TOKEN = (
# q[#] => '_get_comment',
q[@] => '_get_keyword',
q[<] => '_get_iriref',
q[_] => '_get_bnode',
q['] => '_get_single_literal',
q["] => '_get_double_literal',
q[:] => '_get_pname',
(map {$_ => '_get_number'} (0 .. 9, '-', '+'))
);
=item C<< get_token >>
Returns the next token present in the input.
=cut
sub get_token {
my $self = shift;
while (1) {
$self->fill_buffer unless (length($self->buffer));
if ($self->buffer =~ /^[ \r\n\t]+/o) {
$self->read_length($+[0]);
# we're ignoring whitespace tokens, but we could return them here instead of falling through to the 'next':
# return $self->new_token(WS);
next;
}
my $c = $self->peek_char();
return unless (defined($c));
if ($c eq '#') {
# we're ignoring comment tokens, but we could return them here instead of falling through to the 'next':
$self->_get_comment();
next;
}
my $start_column = $self->column;
my $start_line = $self->line;
$self->start_column( $start_column );
$self->start_line( $start_line );
if ($c eq '.' and $self->buffer =~ /^$r_decimal/) {
return $self->_get_number();
}
if (defined(my $name = $CHAR_TOKEN{$c})) { $self->get_char; return $self->new_token($name, $start_line, $start_column, $c); }
elsif (defined(my $method = $METHOD_TOKEN{$c})) { return $self->$method() }
elsif ($c =~ /[A-Za-z\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{02FF}\x{0370}-\x{037D}\x{037F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]/o) {
if ($self->buffer =~ /^a(?!:)\s/o) {
$self->get_char;
return $self->new_token(A, $start_line, $start_column, 'a');
} elsif ($self->buffer =~ /^(?:true|false)(?!:)\b/o) {
my $bool = $self->read_length($+[0]);
return $self->new_token(BOOLEAN, $start_line, $start_column, $bool);
} elsif ($self->buffer =~ /^BASE(?!:)\b/oi) {
$self->read_length(4);
return $self->new_token(BASE, $start_line, $start_column);
} elsif ($self->buffer =~ /^PREFIX(?!:)\b/io) {
$self->read_length(6);
return $self->new_token(PREFIX, $start_line, $start_column);
} else {
return $self->_get_pname;
}
}
elsif ($c eq '^') {
$self->read_word('^^'); return $self->new_token(HATHAT, $start_line, $start_column); }
else {
# Carp::cluck sprintf("Unexpected byte '$c' (0x%02x)", ord($c));
return $self->_throw_error(sprintf("Unexpected byte '%s' (0x%02x)", $c, ord($c)));
}
warn sprintf('byte: 0x%x', ord($c));
}
}
=begin private
=cut
sub _get_pname {
my $self = shift;
my $prefix = '';
if ($self->buffer =~ /^$r_PNAME_LN/o) {
my $ln = $self->read_length($+[0]);
my ($ns, $local) = ($ln =~ /^([^:]*:)(.*)$/);
no warnings 'uninitialized';
$local =~ s{\\([-~.!&'()*+,;=:/?#@%_\$])}{$1}g;
return $self->new_token(PREFIXNAME, $self->start_line, $self->start_column, $ns, $local);
} else {
$self->buffer =~ $r_PNAME_NS;
my $ns = $self->read_length($+[0]);
return $self->new_token(PREFIXNAME, $self->start_line, $self->start_column, $ns);
}
}
sub _get_iriref {
my $self = shift;
$self->get_char_safe(q[<]);
if ($self->buffer =~ m/^[\x23-\x3d\x3f-\x5a\x5d-\x7e]*>/o) {
my $iri .= $self->read_length($+[0]);
chop($iri);
return $self->new_token(IRI, $self->start_line, $self->start_column, $iri);
}
my $iri = '';
while (1) {
if (length($self->buffer) == 0) {
my $c = $self->peek_char;
last unless defined($c);
}
if (substr($self->buffer, 0, 1) eq '\\') {
$self->get_char_safe('\\');
my $esc = $self->get_char;
if ($esc eq '\\') {
$iri .= "\\";
} elsif ($esc eq 'U') {
my $codepoint = $self->read_length(8);
$self->_throw_error("Bad unicode escape codepoint '$codepoint'") unless ($codepoint =~ /^[0-9A-Fa-f]+$/o);
$iri .= chr(hex($codepoint));
} elsif ($esc eq 'u') {
my $codepoint = $self->read_length(4);
$self->_throw_error("Bad unicode escape codepoint '$codepoint'") unless ($codepoint =~ /^[0-9A-Fa-f]+$/o);
my $char = chr(hex($codepoint));
if ($char =~ /[<>" {}|\\^`]/o) {
$self->_throw_error(sprintf("Bad IRI character: '%s' (0x%x)", $char, ord($char)));
}
$iri .= $char;
} else {
$self->_throw_error("Unrecognized iri escape '$esc'");
}
} elsif ($self->buffer =~ /^[^<>\x00-\x20\\"{}|^`]+/o) {
$iri .= $self->read_length($+[0]);
} elsif (substr($self->buffer, 0, 1) eq '>') {
last;
} else {
my $c = $self->peek_char;
$self->_throw_error("Got '$c' while expecting IRI character");
}
}
$self->get_char_safe(q[>]);
return $self->new_token(IRI, $self->start_line, $self->start_column, $iri);
}
sub _get_bnode {
my $self = shift;
$self->read_word('_:');
$self->_throw_error("Expected: name") unless ($self->buffer =~ /^${r_bnode_id}/o);
my $name = substr($self->buffer, 0, $+[0]);
$self->read_word($name);
return $self->new_token(BNODE, $self->start_line, $self->start_column, $name);
}
sub _get_number {
my $self = shift;
if ($self->buffer =~ /^${r_double}/o) {
return $self->new_token(DOUBLE, $self->start_line, $self->start_column, $self->read_length($+[0]));
} elsif ($self->buffer =~ /^${r_decimal}/o) {
return $self->new_token(DECIMAL, $self->start_line, $self->start_column, $self->read_length($+[0]));
} elsif ($self->buffer =~ /^${r_integer}/o) {
return $self->new_token(INTEGER, $self->start_line, $self->start_column, $self->read_length($+[0]));
}
$self->_throw_error("Expected number");
}
sub _get_comment {
my $self = shift;
$self->get_char_safe('#');
my $comment = '';
my $c = $self->peek_char;
while (length($c) and $c !~ /[\r\n]/o) {
$comment .= $self->get_char;
$c = $self->peek_char;
}
if (length($c) and $c =~ /[\r\n]/o) {
$self->get_char;
}
return $self->new_token(COMMENT, $self->start_line, $self->start_column, $comment);
}
sub _get_double_literal {
my $self = shift;
# my $c = $self->peek_char();
$self->get_char_safe(q["]);
if (substr($self->buffer, 0, 2) eq q[""]) {
# #x22 #x22 #x22 lcharacter* #x22 #x22 #x22
$self->read_word(q[""]);
my $quote_count = 0;
my $string = '';
while (1) {
if (length($self->buffer) == 0) {
$self->fill_buffer;
$self->_throw_error("Found EOF in string literal") if (length($self->buffer) == 0);
}
if (substr($self->buffer, 0, 1) eq '"') {
my $c = $self->get_char;
$quote_count++;
last if ($quote_count == 3);
} else {
if ($quote_count) {
$string .= '"' foreach (1..$quote_count);
$quote_count = 0;
}
if (substr($self->buffer, 0, 1) eq '\\') {
$string .= $self->_get_escaped_char();
} else {
$self->buffer =~ /^[^"\\]+/;
$string .= $self->read_length($+[0]);
}
}
}
return $self->new_token(STRING3D, $self->start_line, $self->start_column, $string);
} else {
### #x22 scharacter* #x22
my $string = '';
while (1) {
if (substr($self->buffer, 0, 1) eq '\\') {
$string .= $self->_get_escaped_char();
} elsif ($self->buffer =~ /^[^"\\]+/o) {
$string .= $self->read_length($+[0]);
} elsif (substr($self->buffer, 0, 1) eq '"') {
last;
} else {
my $c = $self->peek_char;
$self->_throw_error("Got '$c' while expecting string character");
}
}
$self->get_char_safe(q["]);
return $self->new_token(STRING1D, $self->start_line, $self->start_column, $string);
}
}
sub _get_single_literal {
my $self = shift;
$self->get_char_safe("'");
if (substr($self->buffer, 0, 2) eq q['']) {
# #x22 #x22 #x22 lcharacter* #x22 #x22 #x22
$self->read_word(q['']);
my $quote_count = 0;
my $string = '';
while (1) {
if (length($self->buffer) == 0) {
$self->fill_buffer;
$self->_throw_error("Found EOF in string literal") if (length($self->buffer) == 0);
}
if (substr($self->buffer, 0, 1) eq "'") {
my $c = $self->get_char;
$quote_count++;
last if ($quote_count == 3);
} else {
if ($quote_count) {
$string .= "'" foreach (1..$quote_count);
$quote_count = 0;
}
if (substr($self->buffer, 0, 1) eq '\\') {
$string .= $self->_get_escaped_char();
} else {
$self->buffer =~ /^[^'\\]+/;
$string .= $self->read_length($+[0]);
}
}
}
return $self->new_token(STRING3S, $self->start_line, $self->start_column, $string);
} else {
### #x22 scharacter* #x22
my $string = '';
while (1) {
if (substr($self->buffer, 0, 1) eq '\\') {
$string .= $self->_get_escaped_char();
} elsif ($self->buffer =~ /^[^'\\]+/o) {
$string .= $self->read_length($+[0]);
} elsif (substr($self->buffer, 0, 1) eq "'") {
last;
} else {
my $c = $self->peek_char();
$self->_throw_error("Got '$c' while expecting string character");
}
}
$self->get_char_safe(q[']);
return $self->new_token(STRING1S, $self->start_line, $self->start_column, $string);
}
}
sub _get_escaped_char {
my $self = shift;
my $c = $self->peek_char;
$self->get_char_safe('\\');
my $esc = $self->get_char;
if ($esc eq '\\') { return "\\" }
elsif ($esc =~ /^['">]$/) { return $esc }
elsif ($esc eq 'r') { return "\r" }
elsif ($esc eq 't') { return "\t" }
elsif ($esc eq 'n') { return "\n" }
elsif ($esc eq 'b') { return "\b" }
elsif ($esc eq 'f') { return "\f" }
elsif ($esc eq 'U') {
my $codepoint = $self->read_length(8);
$self->_throw_error("Bad unicode escape codepoint '$codepoint'") unless ($codepoint =~ /^[0-9A-Fa-f]+$/o);
return chr(hex($codepoint));
} elsif ($esc eq 'u'){
my $codepoint = $self->read_length(4);
$self->_throw_error("Bad unicode escape codepoint '$codepoint'") unless ($codepoint =~ /^[0-9A-Fa-f]+$/o);
return chr(hex($codepoint));
}
$self->_throw_error("Unrecognized string escape '$esc'");
}
sub _get_keyword {
my $self = shift;
$self->get_char_safe('@');
if ($self->buffer =~ /^base/o) {
$self->read_word('base');
return $self->new_token(TURTLEBASE, $self->start_line, $self->start_column);
} elsif ($self->buffer =~ /^prefix/o) {
$self->read_word('prefix');
return $self->new_token(TURTLEPREFIX, $self->start_line, $self->start_column);
} else {
if ($self->buffer =~ /^[a-zA-Z]+(-[a-zA-Z0-9]+)*\b/o) {
my $lang = $self->read_length($+[0]);
return $self->new_token(LANG, $self->start_line, $self->start_column, $lang);
}
$self->_throw_error("Expected keyword or language tag");
}
}
sub _throw_error {
my $self = shift;
my $error = shift;
my $line = $self->line;
my $col = $self->column;
Carp::confess "$error at $line:$col with buffer: " . Dumper($self->buffer);
}
}
1;
__END__
=end private
=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--2016 Gregory Todd Williams. This
program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|