/usr/share/perl5/TOML/Parser.pm is in libtoml-parser-perl 0.06-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 | package TOML::Parser;
use 5.008005;
use strict;
use warnings;
use Encode;
our $VERSION = "0.06";
use TOML::Parser::Tokenizer qw/:constant/;
use TOML::Parser::Tokenizer::Strict;
use TOML::Parser::Util qw/unescape_str/;
use Types::Serialiser;
sub new {
my $class = shift;
my $args = (@_ == 1 and ref $_[0] eq 'HASH') ? +shift : +{ @_ };
return bless +{
inflate_datetime => sub { $_[0] },
inflate_boolean => sub { $_[0] eq 'true' ? Types::Serialiser::true : Types::Serialiser::false },
strict_mode => 0,
%$args,
} => $class;
}
sub parse_file {
my ($self, $file) = @_;
open my $fh, '<:encoding(utf-8)', $file or die $!;
return $self->parse_fh($fh);
}
sub parse_fh {
my ($self, $fh) = @_;
my $src = do { local $/; <$fh> };
return $self->parse($src);
}
sub _tokenizer_class {
my $self = shift;
return $self->{strict_mode} ? 'TOML::Parser::Tokenizer::Strict' : 'TOML::Parser::Tokenizer';
}
our @TOKENS;
our $ROOT;
our $CONTEXT;
sub parse {
my ($self, $src) = @_;
local $ROOT = {};
local $CONTEXT = $ROOT;
local @TOKENS = $self->_tokenizer_class->tokenize($src);
return $self->_parse_tokens();
}
sub _parse_tokens {
my $self = shift;
while (my $token = shift @TOKENS) {
$self->_parse_token($token);
}
return $CONTEXT;
}
sub _parse_token {
my ($self, $token) = @_;
my ($type, $val) = @$token;
if ($type eq TOKEN_TABLE) {
$self->_parse_table($val);
}
elsif ($type eq TOKEN_ARRAY_OF_TABLE) {
$self->_parse_array_of_table($val);
}
elsif (my ($key, $value) = $self->_parse_key_and_value($token)) {
die "Duplicate key. key:$key" if exists $CONTEXT->{$key};
$CONTEXT->{$key} = $value;
}
elsif ($type eq TOKEN_COMMENT) {
# pass through
}
else {
die "Unknown case. type:$type";
}
}
sub _parse_key_and_value {
my ($self, $token) = @_;
my ($type, $val) = @$token;
if ($type eq TOKEN_KEY) {
my $token = shift @TOKENS;
my $key = $val;
my $value = $self->_parse_value_token($token);
return ($key, $value);
}
return;
}
sub _parse_table {
my ($self, $keys) = @_;
my @keys = @$keys;
local $CONTEXT = $ROOT;
for my $k (@keys) {
if (exists $CONTEXT->{$k}) {
$CONTEXT = ref $CONTEXT->{$k} eq 'ARRAY' ? $CONTEXT->{$k}->[-1] :
ref $CONTEXT->{$k} eq 'HASH' ? $CONTEXT->{$k} :
die "invalid structure. @{[ join '.', @keys ]} cannot be `Table`";
}
else {
$CONTEXT = $CONTEXT->{$k} ||= +{};
}
}
$self->_parse_tokens();
}
sub _parse_array_of_table {
my ($self, $keys) = @_;
my @keys = @$keys;
my $last_key = pop @keys;
local $CONTEXT = $ROOT;
for my $k (@keys) {
if (exists $CONTEXT->{$k}) {
$CONTEXT = ref $CONTEXT->{$k} eq 'ARRAY' ? $CONTEXT->{$k}->[-1] :
ref $CONTEXT->{$k} eq 'HASH' ? $CONTEXT->{$k} :
die "invalid structure. @{[ join '.', @keys ]} cannot be `Array of table`.";
}
else {
$CONTEXT = $CONTEXT->{$k} ||= +{};
}
}
$CONTEXT->{$last_key} = [] unless exists $CONTEXT->{$last_key};
die "invalid structure. @{[ join '.', @keys ]} cannot be `Array of table`" unless ref $CONTEXT->{$last_key} eq 'ARRAY';
push @{ $CONTEXT->{$last_key} } => $CONTEXT = {};
$self->_parse_tokens();
}
sub _parse_value_token {
my $self = shift;
my $token = shift;
my ($type, $val) = @$token;
if ($type eq TOKEN_COMMENT) {
return; # pass through
}
elsif ($type eq TOKEN_INTEGER || $type eq TOKEN_FLOAT) {
$val =~ tr/_//d;
return 0+$val;
}
elsif ($type eq TOKEN_BOOLEAN) {
return $self->inflate_boolean($val);
}
elsif ($type eq TOKEN_DATETIME) {
return $self->inflate_datetime($val);
}
elsif ($type eq TOKEN_STRING) {
return unescape_str($val);
}
elsif ($type eq TOKEN_MULTI_LINE_STRING_BEGIN) {
my $value = $self->_parse_value_token(shift @TOKENS);
$value =~ s/\A\s+//msg;
$value =~ s/\\\s+//msg;
if (my $token = shift @TOKENS) {
my ($type) = @$token;
return $value if $type eq TOKEN_MULTI_LINE_STRING_END;
die "Unexpected token: $type";
}
}
elsif ($type eq TOKEN_INLINE_TABLE_BEGIN) {
my %data;
while (my $token = shift @TOKENS) {
last if $token->[0] eq TOKEN_INLINE_TABLE_END;
my ($key, $value) = $self->_parse_key_and_value($token);
die "Duplicate key. key:$key" if exists $data{$key};
$data{$key} = $value;
}
return \%data;
}
elsif ($type eq TOKEN_ARRAY_BEGIN) {
my @data;
my $last_token;
while (my $token = shift @TOKENS) {
last if $token->[0] eq TOKEN_ARRAY_END;
if ($self->{strict_mode} && $token->[0] ne TOKEN_COMMENT) {
die "Unexpected token: $token->[0]" if defined $last_token && $token->[0] ne $last_token->[0];
}
push @data => $self->_parse_value_token($token);
$last_token = $token;
}
return \@data;
}
die "Unexpected token: $type";
}
sub inflate_datetime {
my $self = shift;
return $self->{inflate_datetime}->(@_);
}
sub inflate_boolean {
my $self = shift;
return $self->{inflate_boolean}->(@_);
}
1;
__END__
=encoding utf-8
=for stopwords versa
=head1 NAME
TOML::Parser - simple toml parser
=head1 SYNOPSIS
use TOML::Parser;
my $parser = TOML::Parser->new;
my $data = $parser->parse($toml);
=head1 DESCRIPTION
TOML::Parser is a simple toml parser.
This data structure complies with the tests
provided at L<https://github.com/toml-lang/toml/tree/v0.4.0/tests>.
The v0.4.0 specification is supported.
=head1 METHODS
=over
=item my $parser = TOML::Parser->new(\%args)
Creates a new TOML::Parser instance.
use TOML::Parser;
# create new parser
my $parser = TOML::Parser->new();
Arguments can be:
=over
=item * C<inflate_datetime>
If use it, You can replace inflate C<datetime> process.
The subroutine of default is C<identity>. C<e.g.) sub { $_[0] }>
use TOML::Parser;
use DateTime;
use DateTime::Format::ISO8601;
# create new parser
my $parser = TOML::Parser->new(
inflate_datetime => sub {
my $dt = shift;
return DateTime::Format::ISO8601->parse_datetime($dt);
},
);
=item * C<inflate_boolean>
If use it, You can replace inflate boolean process.
The return value of default subroutine is C<Types::Serialiser::true> or C<Types::Serialiser::false>.
use TOML::Parser;
# create new parser
my $parser = TOML::Parser->new(
inflate_boolean => sub {
my $boolean = shift;
return $boolean eq 'true' ? 1 : 0;
},
);
=item * C<strict_mode>
TOML::Parser is using a more flexible rule for compatibility with old TOML of default.
If make this option true value, You can parse a toml with strict rule.
use TOML::Parser;
# create new parser
my $parser = TOML::Parser->new(
strict_mode => 1
);
=back
=item my $data = $parser->parse_file($path)
=item my $data = $parser->parse_fh($fh)
=item my $data = $parser->parse($src)
Transforms a string containing toml to a perl data structure or vice versa.
=back
=head1 SEE ALSO
L<TOML>
=head1 LICENSE
Copyright (C) karupanerura.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
karupanerura E<lt>karupa@cpan.orgE<gt>
=head1 CONTRIBUTOR
Olivier Mengué E<lt>dolmen@cpan.orgE<gt>
=cut
|