line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package TAP::Parser::YAMLish::Reader; |
2
|
|
|
|
|
|
|
|
3
|
37
|
|
|
37
|
|
44732
|
use strict; |
|
37
|
|
|
|
|
49
|
|
|
37
|
|
|
|
|
910
|
|
4
|
37
|
|
|
37
|
|
124
|
use warnings; |
|
37
|
|
|
|
|
44
|
|
|
37
|
|
|
|
|
910
|
|
5
|
|
|
|
|
|
|
|
6
|
37
|
|
|
37
|
|
136
|
use base 'TAP::Object'; |
|
37
|
|
|
|
|
45
|
|
|
37
|
|
|
|
|
49669
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '3.39'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# TODO: |
11
|
|
|
|
|
|
|
# Handle blessed object syntax |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Printable characters for escapes |
14
|
|
|
|
|
|
|
my %UNESCAPES = ( |
15
|
|
|
|
|
|
|
z => "\x00", a => "\x07", t => "\x09", |
16
|
|
|
|
|
|
|
n => "\x0a", v => "\x0b", f => "\x0c", |
17
|
|
|
|
|
|
|
r => "\x0d", e => "\x1b", '\\' => '\\', |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $QQ_STRING = qr{ " (?:\\. | [^"])* " }x; |
21
|
|
|
|
|
|
|
my $HASH_LINE = qr{ ^ ($QQ_STRING|\S+) \s* : \s* (?: (.+?) \s* )? $ }x; |
22
|
|
|
|
|
|
|
my $IS_HASH_KEY = qr{ ^ [\w\'\"] }x; |
23
|
|
|
|
|
|
|
my $IS_END_YAML = qr{ ^ \.\.\. \s* $ }x; |
24
|
|
|
|
|
|
|
my $IS_QQ_STRING = qr{ ^ $QQ_STRING $ }x; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# new() implementation supplied by TAP::Object |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub read { |
29
|
70
|
|
|
70
|
1
|
175
|
my $self = shift; |
30
|
70
|
|
|
|
|
73
|
my $obj = shift; |
31
|
|
|
|
|
|
|
|
32
|
70
|
50
|
|
|
|
190
|
die "Must have a code reference to read input from" |
33
|
|
|
|
|
|
|
unless ref $obj eq 'CODE'; |
34
|
|
|
|
|
|
|
|
35
|
70
|
|
|
|
|
131
|
$self->{reader} = $obj; |
36
|
70
|
|
|
|
|
94
|
$self->{capture} = []; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Prime the reader |
39
|
70
|
|
|
|
|
159
|
$self->_next; |
40
|
70
|
100
|
|
|
|
131
|
return unless $self->{next}; |
41
|
|
|
|
|
|
|
|
42
|
69
|
|
|
|
|
115
|
my $doc = $self->_read; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# The terminator is mandatory otherwise we'd consume a line from the |
45
|
|
|
|
|
|
|
# iterator that doesn't belong to us. If we want to remove this |
46
|
|
|
|
|
|
|
# restriction we'll have to implement look-ahead in the iterators. |
47
|
|
|
|
|
|
|
# Which might not be a bad idea. |
48
|
65
|
|
|
|
|
111
|
my $dots = $self->_peek; |
49
|
65
|
100
|
66
|
|
|
428
|
die "Missing '...' at end of YAMLish" |
50
|
|
|
|
|
|
|
unless defined $dots |
51
|
|
|
|
|
|
|
and $dots =~ $IS_END_YAML; |
52
|
|
|
|
|
|
|
|
53
|
62
|
|
|
|
|
87
|
delete $self->{reader}; |
54
|
62
|
|
|
|
|
62
|
delete $self->{next}; |
55
|
|
|
|
|
|
|
|
56
|
62
|
|
|
|
|
130
|
return $doc; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
53
|
50
|
|
53
|
1
|
127
|
sub get_raw { join( "\n", grep defined, @{ shift->{capture} || [] } ) . "\n" } |
|
53
|
|
|
|
|
318
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _peek { |
62
|
452
|
|
|
452
|
|
341
|
my $self = shift; |
63
|
452
|
100
|
|
|
|
771
|
return $self->{next} unless wantarray; |
64
|
318
|
|
|
|
|
299
|
my $line = $self->{next}; |
65
|
318
|
|
|
|
|
615
|
$line =~ /^ (\s*) (.*) $ /x; |
66
|
318
|
|
|
|
|
640
|
return ( $2, length $1 ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _next { |
70
|
337
|
|
|
337
|
|
247
|
my $self = shift; |
71
|
|
|
|
|
|
|
die "_next called with no reader" |
72
|
337
|
50
|
|
|
|
519
|
unless $self->{reader}; |
73
|
337
|
|
|
|
|
485
|
my $line = $self->{reader}->(); |
74
|
337
|
|
|
|
|
650
|
$self->{next} = $line; |
75
|
337
|
|
|
|
|
239
|
push @{ $self->{capture} }, $line; |
|
337
|
|
|
|
|
485
|
|
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _read { |
79
|
69
|
|
|
69
|
|
56
|
my $self = shift; |
80
|
|
|
|
|
|
|
|
81
|
69
|
|
|
|
|
115
|
my $line = $self->_peek; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Do we have a document header? |
84
|
69
|
100
|
|
|
|
403
|
if ( $line =~ /^ --- (?: \s* (.+?)? \s* )? $/x ) { |
85
|
67
|
|
|
|
|
92
|
$self->_next; |
86
|
|
|
|
|
|
|
|
87
|
67
|
100
|
|
|
|
200
|
return $self->_read_scalar($1) if defined $1; # Inline? |
88
|
|
|
|
|
|
|
|
89
|
44
|
|
|
|
|
61
|
my ( $next, $indent ) = $self->_peek; |
90
|
|
|
|
|
|
|
|
91
|
44
|
100
|
|
|
|
234
|
if ( $next =~ /^ - /x ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
92
|
22
|
|
|
|
|
75
|
return $self->_read_array($indent); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif ( $next =~ $IS_HASH_KEY ) { |
95
|
21
|
|
|
|
|
54
|
return $self->_read_hash( $next, $indent ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
elsif ( $next =~ $IS_END_YAML ) { |
98
|
1
|
|
|
|
|
11
|
die "Premature end of YAMLish"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
else { |
101
|
0
|
|
|
|
|
0
|
die "Unsupported YAMLish syntax: '$next'"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
2
|
|
|
|
|
19
|
die "YAMLish document header not found"; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Parse a double quoted string |
110
|
|
|
|
|
|
|
sub _read_qq { |
111
|
16
|
|
|
16
|
|
17
|
my $self = shift; |
112
|
16
|
|
|
|
|
15
|
my $str = shift; |
113
|
|
|
|
|
|
|
|
114
|
16
|
50
|
|
|
|
80
|
unless ( $str =~ s/^ " (.*?) " $/$1/x ) { |
115
|
0
|
|
|
|
|
0
|
die "Internal: not a quoted string"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
16
|
|
|
|
|
21
|
$str =~ s/\\"/"/gx; |
119
|
16
|
|
|
|
|
39
|
$str =~ s/ \\ ( [tartan\\favez] | x([0-9a-fA-F]{2}) ) |
120
|
42
|
100
|
|
|
|
118
|
/ (length($1) > 1) ? pack("H2", $2) : $UNESCAPES{$1} /gex; |
121
|
16
|
|
|
|
|
42
|
return $str; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Parse a scalar string to the actual scalar |
125
|
|
|
|
|
|
|
sub _read_scalar { |
126
|
287
|
|
|
287
|
|
216
|
my $self = shift; |
127
|
287
|
|
|
|
|
283
|
my $string = shift; |
128
|
|
|
|
|
|
|
|
129
|
287
|
100
|
|
|
|
436
|
return undef if $string eq '~'; |
130
|
274
|
100
|
|
|
|
357
|
return {} if $string eq '{}'; |
131
|
272
|
100
|
|
|
|
343
|
return [] if $string eq '[]'; |
132
|
|
|
|
|
|
|
|
133
|
270
|
100
|
100
|
|
|
784
|
if ( $string eq '>' || $string eq '|' ) { |
134
|
|
|
|
|
|
|
|
135
|
5
|
|
|
|
|
11
|
my ( $line, $indent ) = $self->_peek; |
136
|
5
|
50
|
|
|
|
11
|
die "Multi-line scalar content missing" unless defined $line; |
137
|
|
|
|
|
|
|
|
138
|
5
|
|
|
|
|
11
|
my @multiline = ($line); |
139
|
|
|
|
|
|
|
|
140
|
5
|
|
|
|
|
6
|
while (1) { |
141
|
10
|
|
|
|
|
16
|
$self->_next; |
142
|
10
|
|
|
|
|
16
|
my ( $next, $ind ) = $self->_peek; |
143
|
10
|
100
|
|
|
|
24
|
last if $ind < $indent; |
144
|
|
|
|
|
|
|
|
145
|
5
|
100
|
|
|
|
14
|
my $pad = $string eq '|' ? ( ' ' x ( $ind - $indent ) ) : ''; |
146
|
5
|
|
|
|
|
9
|
push @multiline, $pad . $next; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
5
|
100
|
|
|
|
28
|
return join( ( $string eq '>' ? ' ' : "\n" ), @multiline ) . "\n"; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
265
|
100
|
|
|
|
381
|
if ( $string =~ /^ ' (.*) ' $/x ) { |
153
|
12
|
|
|
|
|
28
|
( my $rv = $1 ) =~ s/''/'/g; |
154
|
12
|
|
|
|
|
30
|
return $rv; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
253
|
100
|
|
|
|
718
|
if ( $string =~ $IS_QQ_STRING ) { |
158
|
16
|
|
|
|
|
34
|
return $self->_read_qq($string); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
237
|
50
|
|
|
|
403
|
if ( $string =~ /^['"]/ ) { |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# A quote with folding... we don't support that |
164
|
0
|
|
|
|
|
0
|
die __PACKAGE__ . " does not support multi-line quoted scalars"; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Regular unquoted string |
168
|
237
|
|
|
|
|
433
|
return $string; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub _read_nested { |
172
|
29
|
|
|
29
|
|
33
|
my $self = shift; |
173
|
|
|
|
|
|
|
|
174
|
29
|
|
|
|
|
42
|
my ( $line, $indent ) = $self->_peek; |
175
|
|
|
|
|
|
|
|
176
|
29
|
100
|
|
|
|
118
|
if ( $line =~ /^ -/x ) { |
|
|
50
|
|
|
|
|
|
177
|
12
|
|
|
|
|
33
|
return $self->_read_array($indent); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
elsif ( $line =~ $IS_HASH_KEY ) { |
180
|
17
|
|
|
|
|
44
|
return $self->_read_hash( $line, $indent ); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
else { |
183
|
0
|
|
|
|
|
0
|
die "Unsupported YAMLish syntax: '$line'"; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Parse an array |
188
|
|
|
|
|
|
|
sub _read_array { |
189
|
34
|
|
|
34
|
|
42
|
my ( $self, $limit ) = @_; |
190
|
|
|
|
|
|
|
|
191
|
34
|
|
|
|
|
48
|
my $ar = []; |
192
|
|
|
|
|
|
|
|
193
|
34
|
|
|
|
|
36
|
while (1) { |
194
|
127
|
|
|
|
|
160
|
my ( $line, $indent ) = $self->_peek; |
195
|
|
|
|
|
|
|
last |
196
|
127
|
100
|
66
|
|
|
856
|
if $indent < $limit |
|
|
|
100
|
|
|
|
|
197
|
|
|
|
|
|
|
|| !defined $line |
198
|
|
|
|
|
|
|
|| $line =~ $IS_END_YAML; |
199
|
|
|
|
|
|
|
|
200
|
94
|
50
|
|
|
|
136
|
if ( $indent > $limit ) { |
201
|
0
|
|
|
|
|
0
|
die "Array line over-indented"; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
94
|
100
|
|
|
|
384
|
if ( $line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
205
|
6
|
|
|
|
|
8
|
$indent += length $1; |
206
|
6
|
|
|
|
|
17
|
$line =~ s/-\s+//; |
207
|
6
|
|
|
|
|
12
|
push @$ar, $self->_read_hash( $line, $indent ); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
elsif ( $line =~ /^ - \s* (.+?) \s* $/x ) { |
210
|
77
|
100
|
|
|
|
143
|
die "Unexpected start of YAMLish" if $line =~ /^---/; |
211
|
76
|
|
|
|
|
100
|
$self->_next; |
212
|
76
|
|
|
|
|
114
|
push @$ar, $self->_read_scalar($1); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
elsif ( $line =~ /^ - \s* $/x ) { |
215
|
11
|
|
|
|
|
20
|
$self->_next; |
216
|
11
|
|
|
|
|
25
|
push @$ar, $self->_read_nested; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
elsif ( $line =~ $IS_HASH_KEY ) { |
219
|
0
|
|
|
|
|
0
|
$self->_next; |
220
|
0
|
|
|
|
|
0
|
push @$ar, $self->_read_hash( $line, $indent, ); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
else { |
223
|
0
|
|
|
|
|
0
|
die "Unsupported YAMLish syntax: '$line'"; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
33
|
|
|
|
|
79
|
return $ar; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub _read_hash { |
231
|
44
|
|
|
44
|
|
52
|
my ( $self, $line, $limit ) = @_; |
232
|
|
|
|
|
|
|
|
233
|
44
|
|
|
|
|
40
|
my $indent; |
234
|
44
|
|
|
|
|
47
|
my $hash = {}; |
235
|
|
|
|
|
|
|
|
236
|
44
|
|
|
|
|
43
|
while (1) { |
237
|
103
|
50
|
|
|
|
609
|
die "Badly formed hash line: '$line'" |
238
|
|
|
|
|
|
|
unless $line =~ $HASH_LINE; |
239
|
|
|
|
|
|
|
|
240
|
103
|
|
|
|
|
157
|
my ( $key, $value ) = ( $self->_read_scalar($1), $2 ); |
241
|
103
|
|
|
|
|
134
|
$self->_next; |
242
|
|
|
|
|
|
|
|
243
|
103
|
100
|
|
|
|
139
|
if ( defined $value ) { |
244
|
85
|
|
|
|
|
101
|
$hash->{$key} = $self->_read_scalar($value); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
else { |
247
|
18
|
|
|
|
|
36
|
$hash->{$key} = $self->_read_nested; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
103
|
|
|
|
|
138
|
( $line, $indent ) = $self->_peek; |
251
|
|
|
|
|
|
|
last |
252
|
103
|
100
|
66
|
|
|
565
|
if $indent < $limit |
|
|
|
100
|
|
|
|
|
253
|
|
|
|
|
|
|
|| !defined $line |
254
|
|
|
|
|
|
|
|| $line =~ $IS_END_YAML; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
44
|
|
|
|
|
77
|
return $hash; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
1; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
__END__ |