| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::YAML::Filter::Regex; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: A regex-based parser for programs |
|
3
|
|
|
|
|
|
|
$App::YAML::Filter::Regex::VERSION = '0.013'; |
|
4
|
8
|
|
|
8
|
|
118912
|
use App::YAML::Filter::Base; |
|
|
8
|
|
|
|
|
28
|
|
|
|
8
|
|
|
|
|
73
|
|
|
5
|
8
|
|
|
8
|
|
1508
|
use boolean qw( :all ); |
|
|
8
|
|
|
|
|
1138
|
|
|
|
8
|
|
|
|
|
37
|
|
|
6
|
8
|
|
|
8
|
|
5289
|
use Regexp::Common; |
|
|
8
|
|
|
|
|
19989
|
|
|
|
8
|
|
|
|
|
34
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub empty() { |
|
9
|
2
|
|
|
2
|
0
|
17
|
bless {}, 'empty'; |
|
10
|
|
|
|
|
|
|
} |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
*diag = *yq::diag; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $QUOTE_STRING = $RE{delimited}{-delim=>q{'"}}; |
|
15
|
|
|
|
|
|
|
my $EVAL_NUMS = qr{(?:0b$RE{num}{bin}|0$RE{num}{oct}|0x$RE{num}{hex})}; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Match a document path |
|
18
|
|
|
|
|
|
|
my $FILTER = qr{ |
|
19
|
|
|
|
|
|
|
[.] # entire document |
|
20
|
|
|
|
|
|
|
| |
|
21
|
|
|
|
|
|
|
(?:[.](?:\w+|\[\d*\]))+ # hash/array lookup |
|
22
|
|
|
|
|
|
|
| |
|
23
|
|
|
|
|
|
|
$QUOTE_STRING |
|
24
|
|
|
|
|
|
|
| |
|
25
|
|
|
|
|
|
|
$RE{num}{real}|$EVAL_NUMS |
|
26
|
|
|
|
|
|
|
| |
|
27
|
|
|
|
|
|
|
\w+ # Constant/bareword |
|
28
|
|
|
|
|
|
|
}x; |
|
29
|
|
|
|
|
|
|
my $OP = qr{eq|ne|==|!=|>=?|<=?}; |
|
30
|
|
|
|
|
|
|
my $FUNC_NAME = qr{empty|select|grep|group_by|keys|length|sort}; |
|
31
|
|
|
|
|
|
|
my $EXPR = qr{ |
|
32
|
|
|
|
|
|
|
\{(\s*$FILTER\s*:\s*(?0)\s*(?:,(?-1))*)\} # Hash constructor |
|
33
|
|
|
|
|
|
|
| |
|
34
|
|
|
|
|
|
|
\[(\s*(?0)\s*(?:,(?-1))*)\] # Array constructor |
|
35
|
|
|
|
|
|
|
| |
|
36
|
|
|
|
|
|
|
$FUNC_NAME(?:\(\s*(?0)\s*\))? # Function with optional argument |
|
37
|
|
|
|
|
|
|
| |
|
38
|
|
|
|
|
|
|
$FILTER\s+$OP\s+$FILTER # Binary operator |
|
39
|
|
|
|
|
|
|
| |
|
40
|
|
|
|
|
|
|
$FILTER |
|
41
|
|
|
|
|
|
|
}x; |
|
42
|
|
|
|
|
|
|
my $PIPE = qr{[|]}; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Filter MUST NOT mutate $doc! |
|
45
|
|
|
|
|
|
|
sub filter { |
|
46
|
269
|
|
|
269
|
0
|
131835
|
my ( $class, $filter, $doc, $scope ) = @_; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Pipes: LEFT | RIGHT pipes the output of LEFT to the input of RIGHT |
|
49
|
269
|
100
|
|
|
|
13380
|
if ( $filter =~ $PIPE ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
44
|
my @exprs = split /\s*$PIPE\s*/, $filter; |
|
51
|
3
|
|
|
|
|
5
|
my @in = ( $doc ); |
|
52
|
3
|
|
|
|
|
5
|
for my $expr ( @exprs ) { |
|
53
|
6
|
|
|
|
|
6
|
my @out = (); |
|
54
|
6
|
|
|
|
|
6
|
for my $doc ( @in ) { |
|
55
|
6
|
|
|
|
|
10
|
push @out, $class->filter( $expr, $doc, $scope ); |
|
56
|
|
|
|
|
|
|
} |
|
57
|
6
|
|
|
|
|
11
|
@in = @out; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
3
|
|
|
|
|
7
|
return @in; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
# Hash constructor |
|
62
|
|
|
|
|
|
|
elsif ( $filter =~ /^{/ ) { |
|
63
|
6
|
|
|
|
|
10
|
my %out; |
|
64
|
6
|
|
|
|
|
34
|
my ( $inner ) = $filter =~ /^\{\s*([^\}]+?)\s*\}$/; |
|
65
|
6
|
|
|
|
|
20
|
for my $pair ( split /\s*,\s*/, $inner ) { |
|
66
|
8
|
|
|
|
|
26
|
my ( $key_filter, $value_expr ) = split /\s*:\s*/, $pair; |
|
67
|
8
|
|
|
|
|
30
|
my $key = $class->filter( $key_filter, $doc ); |
|
68
|
8
|
|
|
|
|
15
|
$out{ $key } = $class->filter( $value_expr, $doc ); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
6
|
|
|
|
|
20
|
return \%out; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
# Array constructor |
|
73
|
|
|
|
|
|
|
elsif ( $filter =~ /^\[/ ) { |
|
74
|
1
|
|
|
|
|
2
|
my @out; |
|
75
|
1
|
|
|
|
|
8
|
my ( $inner ) = $filter =~ /^\[\s*([^\]]+?)\s*\]$/; |
|
76
|
1
|
|
|
|
|
7
|
for my $value_expr ( split /\s*,\s*/, $inner ) { |
|
77
|
5
|
|
|
|
|
12
|
push @out, $class->filter( $value_expr, $doc ); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
1
|
|
|
|
|
5
|
return \@out; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
# , does multiple filters, yielding multiple documents |
|
82
|
|
|
|
|
|
|
elsif ( $filter =~ /,/ ) { |
|
83
|
2
|
|
|
|
|
14
|
my @filters = split /\s*,\s*/, $filter; |
|
84
|
2
|
|
|
|
|
6
|
return map { $class->filter( $_, $doc ) } @filters; |
|
|
5
|
|
|
|
|
17
|
|
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
# Function calls |
|
87
|
|
|
|
|
|
|
elsif ( $filter =~ /^($FUNC_NAME)(?:\(\s*($EXPR)\s*\))?$/ ) { |
|
88
|
28
|
|
|
|
|
75
|
my ( $func, $expr ) = ( $1, $2 ); |
|
89
|
28
|
|
100
|
|
|
152
|
diag( 1, "F: $func, ARG: " . ( $expr || '' ) ); |
|
90
|
28
|
100
|
100
|
|
|
190
|
if ( $func eq 'empty' ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
91
|
2
|
50
|
|
|
|
10
|
if ( $expr ) { |
|
92
|
0
|
|
|
|
|
0
|
warn "empty does not take arguments\n"; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
2
|
|
|
|
|
9
|
return empty; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
elsif ( $func eq 'select' || $func eq 'grep' ) { |
|
97
|
2
|
50
|
|
|
|
4
|
if ( !$expr ) { |
|
98
|
0
|
|
|
|
|
0
|
warn "'$func' takes an expression argument"; |
|
99
|
0
|
|
|
|
|
0
|
return empty; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
2
|
50
|
|
|
|
13
|
return $class->filter( $expr, $doc ) ? $doc : empty; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
elsif ( $func eq 'group_by' ) { |
|
104
|
9
|
|
|
|
|
22
|
my $grouping = $class->filter( $expr, $doc ); |
|
105
|
9
|
|
|
|
|
13
|
push @{ $scope->{ group_by }{ $grouping } }, $doc; |
|
|
9
|
|
|
|
|
26
|
|
|
106
|
9
|
|
|
|
|
23
|
return; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
elsif ( $func eq 'sort' ) { |
|
109
|
3
|
|
50
|
|
|
6
|
$expr ||= '.'; |
|
110
|
3
|
|
|
|
|
7
|
my $value = $class->filter( $expr, $doc ); |
|
111
|
3
|
|
|
|
|
3
|
push @{ $scope->{sort} }, [ "$value", $doc ]; |
|
|
3
|
|
|
|
|
9
|
|
|
112
|
3
|
|
|
|
|
8
|
return; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
elsif ( $func eq 'keys' ) { |
|
115
|
5
|
|
100
|
|
|
15
|
$expr ||= '.'; |
|
116
|
5
|
|
|
|
|
10
|
my $value = $class->filter( $expr, $doc ); |
|
117
|
5
|
100
|
|
|
|
14
|
if ( ref $value eq 'HASH' ) { |
|
|
|
50
|
|
|
|
|
|
|
118
|
4
|
|
|
|
|
19
|
return [ keys %$value ]; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
elsif ( ref $value eq 'ARRAY' ) { |
|
121
|
1
|
|
|
|
|
4
|
return [ 0..$#{ $value } ]; |
|
|
1
|
|
|
|
|
4
|
|
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
else { |
|
124
|
0
|
|
|
|
|
0
|
warn "keys() requires a hash or array"; |
|
125
|
0
|
|
|
|
|
0
|
return empty; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
elsif ( $func eq 'length' ) { |
|
129
|
7
|
|
100
|
|
|
16
|
$expr ||= '.'; |
|
130
|
7
|
|
|
|
|
15
|
my $value = $class->filter( $expr, $doc ); |
|
131
|
7
|
100
|
|
|
|
22
|
if ( ref $value eq 'HASH' ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
132
|
3
|
|
|
|
|
10
|
return scalar keys %$value; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
elsif ( ref $value eq 'ARRAY' ) { |
|
135
|
1
|
|
|
|
|
4
|
return scalar @$value; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
elsif ( !ref $value ) { |
|
138
|
3
|
|
|
|
|
11
|
return length $value; |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
else { |
|
141
|
0
|
|
|
|
|
0
|
warn "length() requires a hash, array, string, or number"; |
|
142
|
0
|
|
|
|
|
0
|
return empty; |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
# Hash and array keys to traverse the data structure |
|
147
|
|
|
|
|
|
|
elsif ( $filter =~ /^($FILTER)$/ ) { |
|
148
|
|
|
|
|
|
|
# Extract quoted strings |
|
149
|
170
|
100
|
|
|
|
1439
|
if ( $filter =~ /^(['"])(.+)(\1)$/ ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
150
|
7
|
|
|
|
|
30
|
return $2; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
# Eval numbers to allow bin, hex, and oct |
|
153
|
|
|
|
|
|
|
elsif ( $filter =~ /^$EVAL_NUMS$/ ) { |
|
154
|
|
|
|
|
|
|
## no critic ( ProhibitStringyEval ) |
|
155
|
3
|
|
|
|
|
153
|
return eval $filter; |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
# Constants/barewords do not begin with . |
|
158
|
|
|
|
|
|
|
elsif ( $filter !~ /^[.]/ ) { |
|
159
|
|
|
|
|
|
|
# If it's not a reserved word, it's a string |
|
160
|
|
|
|
|
|
|
# XXX: This is a very poor decision... |
|
161
|
35
|
|
|
|
|
90
|
return $filter; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
125
|
|
|
|
|
332
|
my @keys = split /[.]/, $filter; |
|
164
|
125
|
|
|
|
|
153
|
my $subdoc = $doc; |
|
165
|
125
|
|
|
|
|
284
|
for my $key ( @keys[1..$#keys] ) { |
|
166
|
128
|
100
|
|
|
|
481
|
if ( $key =~ /^\[\]$/ ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
2
|
return @{ $subdoc }; |
|
|
1
|
|
|
|
|
4
|
|
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
elsif ( $key =~ /^\[(\d+)\]$/ ) { |
|
170
|
5
|
|
|
|
|
33
|
$subdoc = $subdoc->[ $1 ]; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
elsif ( $key =~ /^\w+$/ ) { |
|
173
|
122
|
|
|
|
|
300
|
$subdoc = $subdoc->{ $key }; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
else { |
|
176
|
0
|
|
|
|
|
0
|
die "Invalid filter key '$key'"; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
} |
|
179
|
124
|
|
|
|
|
375
|
return $subdoc; |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
# Binary operators |
|
182
|
|
|
|
|
|
|
elsif ( $filter =~ /^($FILTER)\s+($OP)\s+($FILTER)$/ ) { |
|
183
|
54
|
|
|
|
|
145
|
my ( $lhs_filter, $cond, $rhs_filter ) = ( $1, $2, $3 ); |
|
184
|
54
|
|
|
|
|
132
|
my $lhs_value = $class->filter( $lhs_filter, $doc ); |
|
185
|
54
|
|
|
|
|
87
|
my $rhs_value = $class->filter( $rhs_filter, $doc ); |
|
186
|
54
|
|
100
|
|
|
329
|
diag( 1, join " ", "BINOP:", $lhs_value // '', $cond, $rhs_value // '' ); |
|
|
|
|
50
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# These operators suppress undef warnings, treating undef as just |
|
188
|
|
|
|
|
|
|
# another value. Undef will never be treated as '' or 0 here. |
|
189
|
54
|
100
|
|
|
|
193
|
if ( $cond eq 'eq' ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
190
|
11
|
100
|
100
|
|
|
76
|
return defined $lhs_value == defined $rhs_value |
|
191
|
|
|
|
|
|
|
&& $lhs_value eq $rhs_value ? true : false; |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
elsif ( $cond eq 'ne' ) { |
|
194
|
4
|
100
|
66
|
|
|
22
|
return defined $lhs_value != defined $rhs_value |
|
195
|
|
|
|
|
|
|
|| $lhs_value ne $rhs_value ? true : false; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
elsif ( $cond eq '==' ) { |
|
198
|
11
|
100
|
66
|
|
|
67
|
return defined $lhs_value == defined $rhs_value |
|
199
|
|
|
|
|
|
|
&& $lhs_value == $rhs_value ? true : false; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
elsif ( $cond eq '!=' ) { |
|
202
|
4
|
100
|
66
|
|
|
25
|
return defined $lhs_value != defined $rhs_value |
|
203
|
|
|
|
|
|
|
|| $lhs_value != $rhs_value ? true : false; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
# These operators allow undef warnings, since equating undef to 0 or '' |
|
206
|
|
|
|
|
|
|
# can be a cause of problems. |
|
207
|
|
|
|
|
|
|
elsif ( $cond eq '>' ) { |
|
208
|
6
|
100
|
|
|
|
21
|
return $lhs_value > $rhs_value ? true : false; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
elsif ( $cond eq '>=' ) { |
|
211
|
6
|
100
|
|
|
|
21
|
return $lhs_value >= $rhs_value ? true : false; |
|
212
|
|
|
|
|
|
|
} |
|
213
|
|
|
|
|
|
|
elsif ( $cond eq '<' ) { |
|
214
|
6
|
100
|
|
|
|
22
|
return $lhs_value < $rhs_value ? true : false; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
elsif ( $cond eq '<=' ) { |
|
217
|
6
|
100
|
|
|
|
21
|
return $lhs_value <= $rhs_value ? true : false; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
# Conditional (if/then/else) |
|
221
|
|
|
|
|
|
|
# NOTE: If we're capturing using $EXPR, then we _must_ use named captures, |
|
222
|
|
|
|
|
|
|
# because $EXPR has captures in itself |
|
223
|
|
|
|
|
|
|
elsif ( $filter =~ /^if\s+(?$EXPR)\s+then\s+(?$FILTER)(?:\s+else\s+(?$FILTER))?$/ ) { |
|
224
|
7
|
|
|
7
|
|
323012
|
my ( $expr, $true_filter, $false_filter ) = @+{qw( expr true false )}; |
|
|
7
|
|
|
|
|
3013
|
|
|
|
7
|
|
|
|
|
1584
|
|
|
|
5
|
|
|
|
|
60
|
|
|
225
|
5
|
|
|
|
|
50
|
my $expr_value = $class->filter( $expr, $doc ); |
|
226
|
5
|
100
|
|
|
|
125
|
if ( $expr_value ) { |
|
227
|
3
|
|
|
|
|
28
|
return $class->filter( $true_filter, $doc ); |
|
228
|
|
|
|
|
|
|
} |
|
229
|
|
|
|
|
|
|
else { |
|
230
|
2
|
50
|
|
|
|
18
|
return $false_filter ? $class->filter( $false_filter, $doc ) : (); |
|
231
|
|
|
|
|
|
|
} |
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
else { |
|
234
|
0
|
|
|
|
|
|
die "Could not parse filter '$filter'\n"; |
|
235
|
|
|
|
|
|
|
} |
|
236
|
0
|
|
|
|
|
|
return; |
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
1; |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
__END__ |