4 extends 'YAML::Loader::Base';
 
   6 use YAML::Loader::Base;
 
  10 use constant LEAF       => 1;
 
  11 use constant COLLECTION => 2;
 
  12 use constant VALUE      => "\x07YAML\x07VALUE\x07";
 
  13 use constant COMMENT    => "\x07YAML\x07COMMENT\x07";
 
  15 # Common YAML character sets
 
  16 my $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]';
 
  19 my $LIT_CHAR_RX = "\\$LIT_CHAR";
 
  23     $self->stream($_[0] || '');
 
  24     return $self->_parse();
 
  27 # Top level function for parsing. Parse each document in order and
 
  28 # handle processing for YAML headers.
 
  31     my (%directives, $preface);
 
  32     $self->{stream} =~ s|\015\012|\012|g;
 
  33     $self->{stream} =~ s|\015|\012|g;
 
  35     $self->die('YAML_PARSE_ERR_BAD_CHARS')
 
  36       if $self->stream =~ /$ESCAPE_CHAR/;
 
  37 #     $self->die('YAML_PARSE_ERR_NO_FINAL_NEWLINE')
 
  38     $self->{stream} .= "\n"
 
  39       if length($self->stream) and
 
  40          $self->{stream} !~ s/(.)\n\Z/$1/s;
 
  41     $self->lines([split /\x0a/, $self->stream, -1]);
 
  43     # Throw away any comments or blanks before the header (or start of
 
  44     # content for headerless streams)
 
  45     $self->_parse_throwaway_comments();
 
  48     # Add an "assumed" header if there is no header and the stream is
 
  49     # not empty (after initial throwaways).
 
  51         if ($self->lines->[0] !~ /^---(\s|$)/) {
 
  52             unshift @{$self->lines}, '---';
 
  57     # Main Loop. Parse out all the top level nodes and return them.
 
  58     while (not $self->eos) {
 
  59         $self->anchor2node({});
 
  63         $self->offset->[0] = -1;
 
  65         if ($self->lines->[0] =~ /^---\s*(.*)$/) {
 
  66             my @words = split /\s+/, $1;
 
  68             while (@words && $words[0] =~ /^#(\w+):(\S.*)$/) {
 
  69                 my ($key, $value) = ($1, $2);
 
  71                 if (defined $directives{$key}) {
 
  72                     $self->warn('YAML_PARSE_WARN_MULTIPLE_DIRECTIVES',
 
  73                       $key, $self->document);
 
  76                 $directives{$key} = $value;
 
  78             $self->preface(join ' ', @words);
 
  81             $self->die('YAML_PARSE_ERR_NO_SEPARATOR');
 
  84         if (not $self->done) {
 
  85             $self->_parse_next_line(COLLECTION);
 
  92         $directives{YAML} ||= '1.0';
 
  93         $directives{TAB} ||= 'NONE';
 
  94         ($self->{major_version}, $self->{minor_version}) =
 
  95           split /\./, $directives{YAML}, 2;
 
  96         $self->die('YAML_PARSE_ERR_BAD_MAJOR_VERSION', $directives{YAML})
 
  97           if $self->major_version ne '1';
 
  98         $self->warn('YAML_PARSE_WARN_BAD_MINOR_VERSION', $directives{YAML})
 
  99           if $self->minor_version ne '0';
 
 100         $self->die('Unrecognized TAB policy')
 
 101           unless $directives{TAB} =~ /^(NONE|\d+)(:HARD)?$/;
 
 103         push @{$self->documents}, $self->_parse_node();
 
 105     return wantarray ? @{$self->documents} : $self->documents->[-1];
 
 108 # This function is the dispatcher for parsing each node. Every node
 
 109 # recurses back through here. (Inlines are an exception as they have
 
 110 # their own sub-parser.)
 
 113     my $preface = $self->preface;
 
 115     my ($node, $type, $indicator, $escape, $chomp) = ('') x 5;
 
 116     my ($anchor, $alias, $explicit, $implicit, $class) = ('') x 5;
 
 117     ($anchor, $alias, $explicit, $implicit, $preface) =
 
 118       $self->_parse_qualifiers($preface);
 
 120         $self->anchor2node->{$anchor} = CORE::bless [], 'YAML-anchor2node';
 
 123     while (length $preface) {
 
 124         my $line = $self->line - 1;
 
 125         if ($preface =~ s/^($FOLD_CHAR|$LIT_CHAR_RX)(-|\+)?\d*\s*//) {
 
 127             $chomp = $2 if defined($2);
 
 130             $self->die('YAML_PARSE_ERR_TEXT_AFTER_INDICATOR') if $indicator;
 
 131             $self->inline($preface);
 
 136         $self->die('YAML_PARSE_ERR_NO_ANCHOR', $alias)
 
 137           unless defined $self->anchor2node->{$alias};
 
 138         if (ref($self->anchor2node->{$alias}) ne 'YAML-anchor2node') {
 
 139             $node = $self->anchor2node->{$alias};
 
 142             $node = do {my $sv = "*$alias"};
 
 143             push @{$self->anchor2node->{$alias}}, [\$node, $self->line];
 
 146     elsif (length $self->inline) {
 
 147         $node = $self->_parse_inline(1, $implicit, $explicit);
 
 148         if (length $self->inline) {
 
 149             $self->die('YAML_PARSE_ERR_SINGLE_LINE');
 
 152     elsif ($indicator eq $LIT_CHAR) {
 
 154         $node = $self->_parse_block($chomp);
 
 155         $node = $self->_parse_implicit($node) if $implicit;
 
 158     elsif ($indicator eq $FOLD_CHAR) {
 
 160         $node = $self->_parse_unfold($chomp);
 
 161         $node = $self->_parse_implicit($node) if $implicit;
 
 166         $self->offset->[$self->level] ||= 0;
 
 167         if ($self->indent == $self->offset->[$self->level]) {
 
 168             if ($self->content =~ /^-( |$)/) {
 
 169                 $node = $self->_parse_seq($anchor);
 
 171             elsif ($self->content =~ /(^\?|\:( |$))/) {
 
 172                 $node = $self->_parse_mapping($anchor);
 
 174             elsif ($preface =~ /^\s*$/) {
 
 175                 $node = $self->_parse_implicit('');
 
 178                 $self->die('YAML_PARSE_ERR_BAD_NODE');
 
 186     $#{$self->offset} = $self->level;
 
 195             CORE::bless $node, $class;
 
 198             $node = $self->_parse_explicit($node, $explicit);
 
 202         if (ref($self->anchor2node->{$anchor}) eq 'YAML-anchor2node') {
 
 203             # XXX Can't remember what this code actually does
 
 204             for my $ref (@{$self->anchor2node->{$anchor}}) {
 
 205                 ${$ref->[0]} = $node;
 
 206                 $self->warn('YAML_LOAD_WARN_UNRESOLVED_ALIAS',
 
 210         $self->anchor2node->{$anchor} = $node;
 
 215 # Preprocess the qualifiers that may be attached to any node.
 
 216 sub _parse_qualifiers {
 
 219     my ($anchor, $alias, $explicit, $implicit, $token) = ('') x 5;
 
 221     while ($preface =~ /^[&*!]/) {
 
 222         my $line = $self->line - 1;
 
 223         if ($preface =~ s/^\!(\S+)\s*//) {
 
 224             $self->die('YAML_PARSE_ERR_MANY_EXPLICIT') if $explicit;
 
 227         elsif ($preface =~ s/^\!\s*//) {
 
 228             $self->die('YAML_PARSE_ERR_MANY_IMPLICIT') if $implicit;
 
 231         elsif ($preface =~ s/^\&([^ ,:]+)\s*//) {
 
 233             $self->die('YAML_PARSE_ERR_BAD_ANCHOR')
 
 234               unless $token =~ /^[a-zA-Z0-9]+$/;
 
 235             $self->die('YAML_PARSE_ERR_MANY_ANCHOR') if $anchor;
 
 236             $self->die('YAML_PARSE_ERR_ANCHOR_ALIAS') if $alias;
 
 239         elsif ($preface =~ s/^\*([^ ,:]+)\s*//) {
 
 241             $self->die('YAML_PARSE_ERR_BAD_ALIAS')
 
 242               unless $token =~ /^[a-zA-Z0-9]+$/;
 
 243             $self->die('YAML_PARSE_ERR_MANY_ALIAS') if $alias;
 
 244             $self->die('YAML_PARSE_ERR_ANCHOR_ALIAS') if $anchor;
 
 248     return ($anchor, $alias, $explicit, $implicit, $preface);
 
 251 # Morph a node to it's explicit type
 
 252 sub _parse_explicit {
 
 254     my ($node, $explicit) = @_;
 
 256     if ($explicit =~ /^\!?perl\/(hash|array|ref|scalar)(?:\:(\w(\w|\:\:)*)?)?$/) {
 
 257         ($type, $class) = (($1 || ''), ($2 || ''));
 
 259         # FIXME # die unless uc($type) eq ref($node) ?
 
 261         if ( $type eq "ref" ) {
 
 262             $self->die('YAML_LOAD_ERR_NO_DEFAULT_VALUE', 'XXX', $explicit)
 
 263             unless exists $node->{VALUE()} and scalar(keys %$node) == 1;
 
 265             my $value = $node->{VALUE()};
 
 269         if ( $type eq "scalar" and length($class) and !ref($node) ) {
 
 274         if ( length($class) ) {
 
 275             CORE::bless($node, $class);
 
 280     if ($explicit =~ m{^!?perl/(glob|regexp|code)(?:\:(\w(\w|\:\:)*)?)?$}) {
 
 281         ($type, $class) = (($1 || ''), ($2 || ''));
 
 282         my $type_class = "YAML::Type::$type";
 
 284         if ($type_class->can('yaml_load')) {
 
 285             return $type_class->yaml_load($node, $class, $self);
 
 288             $self->die('YAML_LOAD_ERR_NO_CONVERT', 'XXX', $explicit);
 
 291     # This !perl/@Foo and !perl/$Foo are deprecated but still parsed
 
 292     elsif ($YAML::TagClass->{$explicit} ||
 
 293            $explicit =~ m{^perl/(\@|\$)?([a-zA-Z](\w|::)+)$}
 
 295         $class = $YAML::TagClass->{$explicit} || $2;
 
 296         if ($class->can('yaml_load')) {
 
 298             return $class->yaml_load(YAML::Node->new($node, $explicit));
 
 302                 return CORE::bless $node, $class;
 
 305                 return CORE::bless \$node, $class;
 
 311         return YAML::Node->new($node, $explicit);
 
 314         # XXX This is likely wrong. Failing test:
 
 315         # --- !unknown 'scalar value'
 
 320 # Parse a YAML mapping into a Perl hash
 
 325     $self->anchor2node->{$anchor} = $mapping;
 
 327     while (not $self->done and $self->indent == $self->offset->[$self->level]) {
 
 329         if ($self->{content} =~ s/^\?\s*//) {
 
 330             $self->preface($self->content);
 
 331             $self->_parse_next_line(COLLECTION);
 
 332             $key = $self->_parse_node();
 
 335         # If "default" key (equals sign)
 
 336         elsif ($self->{content} =~ s/^\=\s*//) {
 
 339         # If "comment" key (slash slash)
 
 340         elsif ($self->{content} =~ s/^\=\s*//) {
 
 343         # Regular scalar key:
 
 345             $self->inline($self->content);
 
 346             $key = $self->_parse_inline();
 
 348             $self->content($self->inline);
 
 352         unless ($self->{content} =~ s/^:\s*//) {
 
 353             $self->die('YAML_LOAD_ERR_BAD_MAP_ELEMENT');
 
 355         $self->preface($self->content);
 
 356         my $line = $self->line;
 
 357         $self->_parse_next_line(COLLECTION);
 
 358         my $value = $self->_parse_node();
 
 359         if (exists $mapping->{$key}) {
 
 360             $self->warn('YAML_LOAD_WARN_DUPLICATE_KEY');
 
 363             $mapping->{$key} = $value;
 
 369 # Parse a YAML sequence into a Perl array
 
 374     $self->anchor2node->{$anchor} = $seq;
 
 375     while (not $self->done and $self->indent == $self->offset->[$self->level]) {
 
 376         if ($self->content =~ /^-(?: (.*))?$/) {
 
 377             $self->preface(defined($1) ? $1 : '');
 
 380             $self->die('YAML_LOAD_ERR_BAD_SEQ_ELEMENT');
 
 382         if ($self->preface =~ /^(\s*)(\w.*\:(?: |$).*)$/) {
 
 383             $self->indent($self->offset->[$self->level] + 2 + length($1));
 
 385             $self->level($self->level + 1);
 
 386             $self->offset->[$self->level] = $self->indent;
 
 388             push @$seq, $self->_parse_mapping('');
 
 390             $#{$self->offset} = $self->level;
 
 393             $self->_parse_next_line(COLLECTION);
 
 394             push @$seq, $self->_parse_node();
 
 400 # Parse an inline value. Since YAML supports inline collections, this is
 
 401 # the top level of a sub parsing.
 
 404     my ($top, $top_implicit, $top_explicit) = (@_, '', '', '');
 
 405     $self->{inline} =~ s/^\s*(.*)\s*$/$1/; # OUCH - mugwump
 
 406     my ($node, $anchor, $alias, $explicit, $implicit) = ('') x 5;
 
 407     ($anchor, $alias, $explicit, $implicit, $self->{inline}) =
 
 408       $self->_parse_qualifiers($self->inline);
 
 410         $self->anchor2node->{$anchor} = CORE::bless [], 'YAML-anchor2node';
 
 412     $implicit ||= $top_implicit;
 
 413     $explicit ||= $top_explicit;
 
 414     ($top_implicit, $top_explicit) = ('', '');
 
 416         $self->die('YAML_PARSE_ERR_NO_ANCHOR', $alias)
 
 417           unless defined $self->anchor2node->{$alias};
 
 418         if (ref($self->anchor2node->{$alias}) ne 'YAML-anchor2node') {
 
 419             $node = $self->anchor2node->{$alias};
 
 422             $node = do {my $sv = "*$alias"};
 
 423             push @{$self->anchor2node->{$alias}}, [\$node, $self->line];
 
 426     elsif ($self->inline =~ /^\{/) {
 
 427         $node = $self->_parse_inline_mapping($anchor);
 
 429     elsif ($self->inline =~ /^\[/) {
 
 430         $node = $self->_parse_inline_seq($anchor);
 
 432     elsif ($self->inline =~ /^"/) {
 
 433         $node = $self->_parse_inline_double_quoted();
 
 434         $node = $self->_unescape($node);
 
 435         $node = $self->_parse_implicit($node) if $implicit;
 
 437     elsif ($self->inline =~ /^'/) {
 
 438         $node = $self->_parse_inline_single_quoted();
 
 439         $node = $self->_parse_implicit($node) if $implicit;
 
 443             $node = $self->inline;
 
 447             $node = $self->_parse_inline_simple();
 
 449         $node = $self->_parse_implicit($node) unless $explicit;
 
 452         $node = $self->_parse_explicit($node, $explicit);
 
 455         if (ref($self->anchor2node->{$anchor}) eq 'YAML-anchor2node') {
 
 456             for my $ref (@{$self->anchor2node->{$anchor}}) {
 
 457                 ${$ref->[0]} = $node;
 
 458                 $self->warn('YAML_LOAD_WARN_UNRESOLVED_ALIAS',
 
 462         $self->anchor2node->{$anchor} = $node;
 
 467 # Parse the inline YAML mapping into a Perl hash
 
 468 sub _parse_inline_mapping {
 
 472     $self->anchor2node->{$anchor} = $node;
 
 474     $self->die('YAML_PARSE_ERR_INLINE_MAP')
 
 475       unless $self->{inline} =~ s/^\{\s*//;
 
 476     while (not $self->{inline} =~ s/^\s*\}//) {
 
 477         my $key = $self->_parse_inline();
 
 478         $self->die('YAML_PARSE_ERR_INLINE_MAP')
 
 479           unless $self->{inline} =~ s/^\: \s*//;
 
 480         my $value = $self->_parse_inline();
 
 481         if (exists $node->{$key}) {
 
 482             $self->warn('YAML_LOAD_WARN_DUPLICATE_KEY');
 
 485             $node->{$key} = $value;
 
 487         next if $self->inline =~ /^\s*\}/;
 
 488         $self->die('YAML_PARSE_ERR_INLINE_MAP')
 
 489           unless $self->{inline} =~ s/^\,\s*//;
 
 494 # Parse the inline YAML sequence into a Perl array
 
 495 sub _parse_inline_seq {
 
 499     $self->anchor2node->{$anchor} = $node;
 
 501     $self->die('YAML_PARSE_ERR_INLINE_SEQUENCE')
 
 502       unless $self->{inline} =~ s/^\[\s*//;
 
 503     while (not $self->{inline} =~ s/^\s*\]//) {
 
 504         my $value = $self->_parse_inline();
 
 506         next if $self->inline =~ /^\s*\]/;
 
 507         $self->die('YAML_PARSE_ERR_INLINE_SEQUENCE')
 
 508           unless $self->{inline} =~ s/^\,\s*//;
 
 513 # Parse the inline double quoted string.
 
 514 sub _parse_inline_double_quoted {
 
 517     # https://rt.cpan.org/Public/Bug/Display.html?id=90593
 
 518     if ($self->inline =~ /^"((?:(?:\\"|[^"]){0,32766}){0,32766})"\s*(.*)$/) {
 
 524         $self->die('YAML_PARSE_ERR_BAD_DOUBLE');
 
 530 # Parse the inline single quoted string.
 
 531 sub _parse_inline_single_quoted {
 
 534     if ($self->inline =~ /^'((?:(?:''|[^']){0,32766}){0,32766})'\s*(.*)$/) {
 
 540         $self->die('YAML_PARSE_ERR_BAD_SINGLE');
 
 545 # Parse the inline unquoted string and do implicit typing.
 
 546 sub _parse_inline_simple {
 
 549     if ($self->inline =~ /^(|[^!@#%^&*].*?)(?=[\[\]\{\},]|, |: |- |:\s*$|$)/) {
 
 551         substr($self->{inline}, 0, length($1)) = '';
 
 554         $self->die('YAML_PARSE_ERR_BAD_INLINE_IMPLICIT', $value);
 
 559 sub _parse_implicit {
 
 563     return $value if $value eq '';
 
 564     return undef if $value =~ /^~$/;
 
 566       unless $value =~ /^[\@\`]/ or
 
 567              $value =~ /^[\-\?]\s/;
 
 568     $self->die('YAML_PARSE_ERR_BAD_IMPLICIT', $value);
 
 571 # Unfold a YAML multiline scalar into a single string.
 
 577     while (not $self->done and $self->indent == $self->offset->[$self->level]) {
 
 578         $node .= $self->content. "\n";
 
 579         $self->_parse_next_line(LEAF);
 
 581     $node =~ s/^(\S.*)\n(?=\S)/$1 /gm;
 
 582     $node =~ s/^(\S.*)\n(\n+\S)/$1$2/gm;
 
 583     $node =~ s/\n*\Z// unless $chomp eq '+';
 
 584     $node .= "\n" unless $chomp;
 
 588 # Parse a YAML block style scalar. This is like a Perl here-document.
 
 593     while (not $self->done and $self->indent == $self->offset->[$self->level]) {
 
 594         $node .= $self->content . "\n";
 
 595         $self->_parse_next_line(LEAF);
 
 597     return $node if '+' eq $chomp;
 
 598     $node =~ s/\n*\Z/\n/;
 
 599     $node =~ s/\n\Z// if $chomp eq '-';
 
 603 # Handle Perl style '#' comments. Comments must be at the same indentation
 
 604 # level as the collection line following them.
 
 605 sub _parse_throwaway_comments {
 
 607     while (@{$self->lines} and
 
 608            $self->lines->[0] =~ m{^\s*(\#|$)}
 
 610         shift @{$self->lines};
 
 613     $self->eos($self->{done} = not @{$self->lines});
 
 616 # This is the routine that controls what line is being parsed. It gets called
 
 617 # once for each line in the YAML stream.
 
 620 # 1) Skip past the current line
 
 621 # 2) Determine the indentation offset for a new level
 
 622 # 3) Find the next _content_ line
 
 623 #   A) Skip over any throwaways (Comments/blanks)
 
 624 #   B) Set $self->indent, $self->content, $self->line
 
 625 # 4) Expand tabs appropriately
 
 626 sub _parse_next_line {
 
 629     my $level = $self->level;
 
 630     my $offset = $self->offset->[$level];
 
 631     $self->die('YAML_EMIT_ERR_BAD_LEVEL') unless defined $offset;
 
 632     shift @{$self->lines};
 
 633     $self->eos($self->{done} = not @{$self->lines});
 
 634     return if $self->eos;
 
 637     # Determine the offset for a new leaf node
 
 638     if ($self->preface =~
 
 639         qr/(?:^|\s)(?:$FOLD_CHAR|$LIT_CHAR_RX)(?:-|\+)?(\d*)\s*$/
 
 641         $self->die('YAML_PARSE_ERR_ZERO_INDENT')
 
 642           if length($1) and $1 == 0;
 
 645             $self->offset->[$level + 1] = $offset + $1;
 
 648             # First get rid of any comments.
 
 649             while (@{$self->lines} && ($self->lines->[0] =~ /^\s*#/)) {
 
 650                 $self->lines->[0] =~ /^( *)/;
 
 651                 last unless length($1) <= $offset;
 
 652                 shift @{$self->lines};
 
 655             $self->eos($self->{done} = not @{$self->lines});
 
 656             return if $self->eos;
 
 657             if ($self->lines->[0] =~ /^( *)\S/ and length($1) > $offset) {
 
 658                 $self->offset->[$level+1] = length($1);
 
 661                 $self->offset->[$level+1] = $offset + 1;
 
 664         $offset = $self->offset->[++$level];
 
 666     # Determine the offset for a new collection level
 
 667     elsif ($type == COLLECTION and
 
 668            $self->preface =~ /^(\s*(\!\S*|\&\S+))*\s*$/) {
 
 669         $self->_parse_throwaway_comments();
 
 671             $self->offset->[$level+1] = $offset + 1;
 
 675             $self->lines->[0] =~ /^( *)\S/ or
 
 676                 $self->die('YAML_PARSE_ERR_NONSPACE_INDENTATION');
 
 677             if (length($1) > $offset) {
 
 678                 $self->offset->[$level+1] = length($1);
 
 681                 $self->offset->[$level+1] = $offset + 1;
 
 684         $offset = $self->offset->[++$level];
 
 688         while (@{$self->lines} and
 
 689                $self->lines->[0] =~ m{^( *)(\#)} and
 
 692             shift @{$self->lines};
 
 695         $self->eos($self->{done} = not @{$self->lines});
 
 698         $self->_parse_throwaway_comments();
 
 700     return if $self->eos;
 
 702     if ($self->lines->[0] =~ /^---(\s|$)/) {
 
 706     if ($type == LEAF and
 
 707         $self->lines->[0] =~ /^ {$offset}(.*)$/
 
 709         $self->indent($offset);
 
 712     elsif ($self->lines->[0] =~ /^\s*$/) {
 
 713         $self->indent($offset);
 
 717         $self->lines->[0] =~ /^( *)(\S.*)$/;
 
 718         while ($self->offset->[$level] > length($1)) {
 
 721         $self->die('YAML_PARSE_ERR_INCONSISTENT_INDENTATION')
 
 722           if $self->offset->[$level] != length($1);
 
 723         $self->indent(length($1));
 
 726     $self->die('YAML_PARSE_ERR_INDENTATION')
 
 727       if $self->indent - $offset > 1;
 
 730 #==============================================================================
 
 731 # Utility subroutines.
 
 732 #==============================================================================
 
 734 # Printable characters for escapes
 
 740    'v' => "\x0b", # Potential v-string error on 5.6.2 if not quoted
 
 747 # Transform all the backslash style escape characters to their literal meaning
 
 751     $node =~ s/\\([never\\fart0]|x([0-9a-fA-F]{2}))/
 
 752               (length($1)>1)?pack("H2",$2):$unescapes{$1}/gex;