<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package TAP::Parser::YAMLish::Reader;

use strict;
use warnings;

use base 'TAP::Object';

our $VERSION = '3.44';

                             # No EBCDIC support on early perls
*to_native = (ord "A" == 65 || $] &lt; 5.008)
             ? sub { return shift }
             : sub { utf8::unicode_to_native(shift) };

# TODO:
#   Handle blessed object syntax

# Printable characters for escapes
my %UNESCAPES = (
    z =&gt; "\x00", a =&gt; "\a",  t    =&gt; "\t",
    n =&gt; "\n",   v =&gt; "\cK", f    =&gt; "\f",
    r =&gt; "\r",   e =&gt; "\e",  '\\' =&gt; '\\',
);

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;
my $IS_ARRAY_LINE = qr{ ^ - \s* ($QQ_STRING|\S+) }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-&gt;{reader}  = $obj;
    $self-&gt;{capture} = [];

    #Â&nbsp;Prime the reader
    $self-&gt;_next;
    return unless $self-&gt;{next};

    my $doc = $self-&gt;_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-&gt;_peek;
    die "Missing '...' at end of YAMLish"
      unless defined $dots
          and $dots =~ $IS_END_YAML;

    delete $self-&gt;{reader};
    delete $self-&gt;{next};

    return $doc;
}

sub get_raw { join( "\n", grep defined, @{ shift-&gt;{capture} || [] } ) . "\n" }

sub _peek {
    my $self = shift;
    return $self-&gt;{next} unless wantarray;
    my $line = $self-&gt;{next};
    $line =~ /^ (\s*) (.*) $ /x;
    return ( $2, length $1 );
}

sub _next {
    my $self = shift;
    die "_next called with no reader"
      unless $self-&gt;{reader};
    my $line = $self-&gt;{reader}-&gt;();
    $self-&gt;{next} = $line;
    push @{ $self-&gt;{capture} }, $line;
}

sub _read {
    my $self = shift;

    my $line = $self-&gt;_peek;

    # Do we have a document header?
    if ( $line =~ /^ --- (?: \s* (.+?)? \s* )? $/x ) {
        $self-&gt;_next;

        return $self-&gt;_read_scalar($1) if defined $1;    # Inline?

        my ( $next, $indent ) = $self-&gt;_peek;

        if ( $next =~ /^ - /x ) {
            return $self-&gt;_read_array($indent);
        }
        elsif ( $next =~ $IS_HASH_KEY ) {
            return $self-&gt;_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) &gt; 1) ? pack("H2", to_native($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 '&gt;' || $string eq '|' ) {

        my ( $line, $indent ) = $self-&gt;_peek;
        die "Multi-line scalar content missing" unless defined $line;

        my @multiline = ($line);

        while (1) {
            $self-&gt;_next;
            my ( $next, $ind ) = $self-&gt;_peek;
            last if $ind &lt; $indent;

            my $pad = $string eq '|' ? ( ' ' x ( $ind - $indent ) ) : '';
            push @multiline, $pad . $next;
        }

        return join( ( $string eq '&gt;' ? ' ' : "\n" ), @multiline ) . "\n";
    }

    if ( $string =~ /^ ' (.*) ' $/x ) {
        ( my $rv = $1 ) =~ s/''/'/g;
        return $rv;
    }

    if ( $string =~ $IS_QQ_STRING ) {
        return $self-&gt;_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-&gt;_peek;

    if ( $line =~ /^ -/x ) {
        return $self-&gt;_read_array($indent);
    }
    elsif ( $line =~ $IS_HASH_KEY ) {
        return $self-&gt;_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-&gt;_peek;
        last
          if $indent &lt; $limit
              || !defined $line
              || $line =~ $IS_END_YAML;

        if ( $indent &gt; $limit ) {
            die "Array line over-indented";
        }

        if ( $line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x ) {
            $indent += length $1;
            $line =~ s/-\s+//;
            push @$ar, $self-&gt;_read_hash( $line, $indent );
        }
        elsif ( $line =~ /^ - \s* (.+?) \s* $/x ) {
            die "Unexpected start of YAMLish" if $line =~ /^---/;
            $self-&gt;_next;
            push @$ar, $self-&gt;_read_scalar($1);
        }
        elsif ( $line =~ /^ - \s* $/x ) {
            $self-&gt;_next;
            push @$ar, $self-&gt;_read_nested;
        }
        elsif ( $line =~ $IS_HASH_KEY ) {
            $self-&gt;_next;
            push @$ar, $self-&gt;_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-&gt;_read_scalar($1), $2 );
        $self-&gt;_next;

        my ( $next_line, $next_indent ) = $self-&gt;_peek;

        if ( defined $value ) {
            $hash-&gt;{$key} = $self-&gt;_read_scalar($value);
        }
        elsif (not defined $value                  # no explicit undef ("~") given
               and $next_indent &lt;= $limit          # next line is same or less indentation
               and $next_line !~ $IS_ARRAY_LINE)   # arrays can start at same indent
        {
            $hash-&gt;{$key} = undef;
        }
        else {
            $hash-&gt;{$key} = $self-&gt;_read_nested;
        }

        ( $line, $indent ) = $self-&gt;_peek;
        last
          if $indent &lt; $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.44

=head1 SYNOPSIS

=head1 DESCRIPTION

Note that parts of this code were derived from L&lt;YAML::Tiny&gt; with the
permission of Adam Kennedy.

=head1 METHODS

=head2 Class Methods

=head3 C&lt;new&gt;

The constructor C&lt;new&gt; creates and returns an empty
C&lt;TAP::Parser::YAMLish::Reader&gt; object.

 my $reader = TAP::Parser::YAMLish::Reader-&gt;new; 

=head2 Instance Methods

=head3 C&lt;read&gt;

 my $got = $reader-&gt;read($iterator);

Read YAMLish from a L&lt;TAP::Parser::Iterator&gt; and return the data structure it
represents.

=head3 C&lt;get_raw&gt;

 my $source = $reader-&gt;get_source;

Return the raw YAMLish source from the most recent C&lt;read&gt;.

=head1 AUTHOR

Andy Armstrong, &lt;andy@hexten.net&gt;

Adam Kennedy wrote L&lt;YAML::Tiny&gt; which provided the template and many of
the YAML matching regular expressions for this module.

=head1 SEE ALSO

L&lt;YAML::Tiny&gt;, L&lt;YAML&gt;, L&lt;YAML::Syck&gt;, L&lt;Config::Tiny&gt;, L&lt;CSS::Tiny&gt;,
L&lt;http://use.perl.org/~Alias/journal/29427&gt;

=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

</pre></body></html>