Spaces:
Sleeping
Sleeping
File size: 7,603 Bytes
1d777c4 |
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 |
package TAP::Parser::YAMLish::Reader;
use strict;
use warnings;
use base 'TAP::Object';
our $VERSION = '3.43';
# TODO:
# Handle blessed object syntax
# Printable characters for escapes
my %UNESCAPES = (
z => "\x00", a => "\x07", t => "\x09",
n => "\x0a", v => "\x0b", f => "\x0c",
r => "\x0d", e => "\x1b", '\\' => '\\',
);
my $QQ_STRING = qr{ " (?:\\. | [^"])* " }x;
my $HASH_LINE = qr{ ^ ($QQ_STRING|\S+) \s* : \s* (?: (.+?) \s* )? $ }x;
my $IS_HASH_KEY = qr{ ^ [\w\'\"] }x;
my $IS_END_YAML = qr{ ^ \.\.\. \s* $ }x;
my $IS_QQ_STRING = qr{ ^ $QQ_STRING $ }x;
# new() implementation supplied by TAP::Object
sub read {
my $self = shift;
my $obj = shift;
die "Must have a code reference to read input from"
unless ref $obj eq 'CODE';
$self->{reader} = $obj;
$self->{capture} = [];
# Prime the reader
$self->_next;
return unless $self->{next};
my $doc = $self->_read;
# The terminator is mandatory otherwise we'd consume a line from the
# iterator that doesn't belong to us. If we want to remove this
# restriction we'll have to implement look-ahead in the iterators.
# Which might not be a bad idea.
my $dots = $self->_peek;
die "Missing '...' at end of YAMLish"
unless defined $dots
and $dots =~ $IS_END_YAML;
delete $self->{reader};
delete $self->{next};
return $doc;
}
sub get_raw { join( "\n", grep defined, @{ shift->{capture} || [] } ) . "\n" }
sub _peek {
my $self = shift;
return $self->{next} unless wantarray;
my $line = $self->{next};
$line =~ /^ (\s*) (.*) $ /x;
return ( $2, length $1 );
}
sub _next {
my $self = shift;
die "_next called with no reader"
unless $self->{reader};
my $line = $self->{reader}->();
$self->{next} = $line;
push @{ $self->{capture} }, $line;
}
sub _read {
my $self = shift;
my $line = $self->_peek;
# Do we have a document header?
if ( $line =~ /^ --- (?: \s* (.+?)? \s* )? $/x ) {
$self->_next;
return $self->_read_scalar($1) if defined $1; # Inline?
my ( $next, $indent ) = $self->_peek;
if ( $next =~ /^ - /x ) {
return $self->_read_array($indent);
}
elsif ( $next =~ $IS_HASH_KEY ) {
return $self->_read_hash( $next, $indent );
}
elsif ( $next =~ $IS_END_YAML ) {
die "Premature end of YAMLish";
}
else {
die "Unsupported YAMLish syntax: '$next'";
}
}
else {
die "YAMLish document header not found";
}
}
# Parse a double quoted string
sub _read_qq {
my $self = shift;
my $str = shift;
unless ( $str =~ s/^ " (.*?) " $/$1/x ) {
die "Internal: not a quoted string";
}
$str =~ s/\\"/"/gx;
$str =~ s/ \\ ( [tartan\\favez] | x([0-9a-fA-F]{2}) )
/ (length($1) > 1) ? pack("H2", $2) : $UNESCAPES{$1} /gex;
return $str;
}
# Parse a scalar string to the actual scalar
sub _read_scalar {
my $self = shift;
my $string = shift;
return undef if $string eq '~';
return {} if $string eq '{}';
return [] if $string eq '[]';
if ( $string eq '>' || $string eq '|' ) {
my ( $line, $indent ) = $self->_peek;
die "Multi-line scalar content missing" unless defined $line;
my @multiline = ($line);
while (1) {
$self->_next;
my ( $next, $ind ) = $self->_peek;
last if $ind < $indent;
my $pad = $string eq '|' ? ( ' ' x ( $ind - $indent ) ) : '';
push @multiline, $pad . $next;
}
return join( ( $string eq '>' ? ' ' : "\n" ), @multiline ) . "\n";
}
if ( $string =~ /^ ' (.*) ' $/x ) {
( my $rv = $1 ) =~ s/''/'/g;
return $rv;
}
if ( $string =~ $IS_QQ_STRING ) {
return $self->_read_qq($string);
}
if ( $string =~ /^['"]/ ) {
# A quote with folding... we don't support that
die __PACKAGE__ . " does not support multi-line quoted scalars";
}
# Regular unquoted string
return $string;
}
sub _read_nested {
my $self = shift;
my ( $line, $indent ) = $self->_peek;
if ( $line =~ /^ -/x ) {
return $self->_read_array($indent);
}
elsif ( $line =~ $IS_HASH_KEY ) {
return $self->_read_hash( $line, $indent );
}
else {
die "Unsupported YAMLish syntax: '$line'";
}
}
# Parse an array
sub _read_array {
my ( $self, $limit ) = @_;
my $ar = [];
while (1) {
my ( $line, $indent ) = $self->_peek;
last
if $indent < $limit
|| !defined $line
|| $line =~ $IS_END_YAML;
if ( $indent > $limit ) {
die "Array line over-indented";
}
if ( $line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x ) {
$indent += length $1;
$line =~ s/-\s+//;
push @$ar, $self->_read_hash( $line, $indent );
}
elsif ( $line =~ /^ - \s* (.+?) \s* $/x ) {
die "Unexpected start of YAMLish" if $line =~ /^---/;
$self->_next;
push @$ar, $self->_read_scalar($1);
}
elsif ( $line =~ /^ - \s* $/x ) {
$self->_next;
push @$ar, $self->_read_nested;
}
elsif ( $line =~ $IS_HASH_KEY ) {
$self->_next;
push @$ar, $self->_read_hash( $line, $indent, );
}
else {
die "Unsupported YAMLish syntax: '$line'";
}
}
return $ar;
}
sub _read_hash {
my ( $self, $line, $limit ) = @_;
my $indent;
my $hash = {};
while (1) {
die "Badly formed hash line: '$line'"
unless $line =~ $HASH_LINE;
my ( $key, $value ) = ( $self->_read_scalar($1), $2 );
$self->_next;
if ( defined $value ) {
$hash->{$key} = $self->_read_scalar($value);
}
else {
$hash->{$key} = $self->_read_nested;
}
( $line, $indent ) = $self->_peek;
last
if $indent < $limit
|| !defined $line
|| $line =~ $IS_END_YAML;
}
return $hash;
}
1;
__END__
=pod
=head1 NAME
TAP::Parser::YAMLish::Reader - Read YAMLish data from iterator
=head1 VERSION
Version 3.43
=head1 SYNOPSIS
=head1 DESCRIPTION
Note that parts of this code were derived from L<YAML::Tiny> with the
permission of Adam Kennedy.
=head1 METHODS
=head2 Class Methods
=head3 C<new>
The constructor C<new> creates and returns an empty
C<TAP::Parser::YAMLish::Reader> object.
my $reader = TAP::Parser::YAMLish::Reader->new;
=head2 Instance Methods
=head3 C<read>
my $got = $reader->read($iterator);
Read YAMLish from a L<TAP::Parser::Iterator> and return the data structure it
represents.
=head3 C<get_raw>
my $source = $reader->get_source;
Return the raw YAMLish source from the most recent C<read>.
=head1 AUTHOR
Andy Armstrong, <[email protected]>
Adam Kennedy wrote L<YAML::Tiny> which provided the template and many of
the YAML matching regular expressions for this module.
=head1 SEE ALSO
L<YAML::Tiny>, L<YAML>, L<YAML::Syck>, L<Config::Tiny>, L<CSS::Tiny>,
L<http://use.perl.org/~Alias/journal/29427>
=head1 COPYRIGHT
Copyright 2007-2011 Andy Armstrong.
Portions copyright 2006-2008 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|