line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::YAML::Filter::RecDescent; |
2
|
|
|
|
|
|
|
# ABSTRACT: A Parse::RecDescent-based parser for programs |
3
|
|
|
|
|
|
|
$App::YAML::Filter::RecDescent::VERSION = '0.014'; |
4
|
1
|
|
|
1
|
|
409555
|
use App::YAML::Filter::Base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
5
|
1
|
|
|
1
|
|
667
|
use boolean qw( :all ); |
|
1
|
|
|
|
|
1166
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
1432
|
use Parse::RecDescent; |
|
1
|
|
|
|
|
46019
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$::RD_ERRORS = 1; |
9
|
|
|
|
|
|
|
$::RD_WARN = 1; |
10
|
|
|
|
|
|
|
$::RD_HINT = 1; |
11
|
|
|
|
|
|
|
#$::RD_TRACE = 1; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $grammar = q{ |
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
use Data::Dumper; |
16
|
|
|
|
|
|
|
use boolean qw( true false ); |
17
|
|
|
|
|
|
|
sub one { return is_list( $_[0] ) ? $_[0]->[0] : $_[0] } |
18
|
|
|
|
|
|
|
sub is_list { return ref $_[0] eq 'list' } |
19
|
|
|
|
|
|
|
sub flatten { map { is_list( $_ ) ? @$_ : $_ } @_ } |
20
|
|
|
|
|
|
|
sub list { return bless [ flatten( @_ ) ], 'list' } |
21
|
|
|
|
|
|
|
sub empty { return bless {}, 'empty' } |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
program: |
25
|
|
|
|
|
|
|
{ |
26
|
|
|
|
|
|
|
yq::diag( 1, "Program: " . Dumper( \@item ) ); |
27
|
|
|
|
|
|
|
$return = list( @{ $item[1] } ); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
statement: conditional | expr |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
yq::diag( 1, "Statement: " . Dumper( \@item ) ); |
33
|
|
|
|
|
|
|
$return = $item[1]; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
conditional: 'if' expr 'then' expr ( 'else' expr )(?) |
37
|
|
|
|
|
|
|
{ |
38
|
|
|
|
|
|
|
yq::diag( 1, "Conditional: " . Dumper( \@item ) ); |
39
|
|
|
|
|
|
|
$return = list( one( $item[2] ) ? $item[4] : $item[5][0] ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
expr: function_call | hash | array | binop | filter | quote_string | number | word |
43
|
|
|
|
|
|
|
{ |
44
|
|
|
|
|
|
|
$return = list( $item[1] ); |
45
|
|
|
|
|
|
|
yq::diag( 1, "Expr: " . Dumper( \@item ) ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
filter: '.' filter_part(s? /[.]/) |
49
|
|
|
|
|
|
|
{ |
50
|
|
|
|
|
|
|
yq::diag( 1, "Filter: " . Dumper( \@item ) ); |
51
|
|
|
|
|
|
|
my @keys = @{$item[3]}; |
52
|
|
|
|
|
|
|
$return = list( $::document ); |
53
|
|
|
|
|
|
|
for my $key ( @keys ) { |
54
|
|
|
|
|
|
|
yq::diag( 1, "Key: " . Dumper( $key ) ); |
55
|
|
|
|
|
|
|
if ( $key =~ /^\[\]$/ ) { |
56
|
|
|
|
|
|
|
$return = list( @{ $return->[0] } ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif ( $key =~ /^\[(\d+)\]$/ ) { |
59
|
|
|
|
|
|
|
$return = list( $return->[0][ $1 ] ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
elsif ( $key =~ /^\w+$/ ) { |
62
|
|
|
|
|
|
|
$return = list( $return->[0]{ $key } ); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else { |
65
|
|
|
|
|
|
|
die "Invalid filter key '$key'"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
# Make RD commit to this if the filter is just '.' |
69
|
|
|
|
|
|
|
$return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
filter_part: word | '[' number(?) ']' |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
if ( $item[1] eq '[' ) { |
75
|
|
|
|
|
|
|
$return = join "", $item[1], @{$item[2]}, $item[3]; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
|
|
|
|
|
|
$return = $item[1]; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
binop: (filter|quote_string|number|word) op (filter|quote_string|number|word) |
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
yq::diag( 1, "Binop: " . Dumper( [ one($item[1]), $item[2], one($item[3]) ] ) ); |
85
|
|
|
|
|
|
|
my ( $lhs_value, $cond, $rhs_value ) = ( one($item[1]), $item[2], one($item[3]) ); |
86
|
|
|
|
|
|
|
# These operators suppress undef warnings, treating undef as just |
87
|
|
|
|
|
|
|
# another value. Undef will never be treated as '' or 0 here. |
88
|
|
|
|
|
|
|
if ( $cond eq 'eq' ) { |
89
|
|
|
|
|
|
|
$return = defined $lhs_value == defined $rhs_value |
90
|
|
|
|
|
|
|
&& $lhs_value eq $rhs_value ? true : false; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
elsif ( $cond eq 'ne' ) { |
93
|
|
|
|
|
|
|
$return = defined $lhs_value != defined $rhs_value |
94
|
|
|
|
|
|
|
|| $lhs_value ne $rhs_value ? true : false; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
elsif ( $cond eq '==' ) { |
97
|
|
|
|
|
|
|
$return = defined $lhs_value == defined $rhs_value |
98
|
|
|
|
|
|
|
&& $lhs_value == $rhs_value ? true : false; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
elsif ( $cond eq '!=' ) { |
101
|
|
|
|
|
|
|
$return = defined $lhs_value != defined $rhs_value |
102
|
|
|
|
|
|
|
|| $lhs_value != $rhs_value ? true : false; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
# These operators allow undef warnings, since equating undef to 0 or '' |
105
|
|
|
|
|
|
|
# can be a cause of problems. |
106
|
|
|
|
|
|
|
elsif ( $cond eq '>' ) { |
107
|
|
|
|
|
|
|
$return = $lhs_value > $rhs_value ? true : false; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
elsif ( $cond eq '>=' ) { |
110
|
|
|
|
|
|
|
$return = $lhs_value >= $rhs_value ? true : false; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
elsif ( $cond eq '<' ) { |
113
|
|
|
|
|
|
|
$return = $lhs_value < $rhs_value ? true : false; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
elsif ( $cond eq '<=' ) { |
116
|
|
|
|
|
|
|
$return = $lhs_value <= $rhs_value ? true : false; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
$return = list( $return ); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
function_call: function_name arguments(?) |
122
|
|
|
|
|
|
|
{ |
123
|
|
|
|
|
|
|
yq::diag( 1, "FCall: " . Dumper( \@item ) ); |
124
|
|
|
|
|
|
|
my $func = $item[1]; |
125
|
|
|
|
|
|
|
my $args = $item[2]; |
126
|
|
|
|
|
|
|
if ( $func eq 'empty' ) { |
127
|
|
|
|
|
|
|
if ( @$args ) { |
128
|
|
|
|
|
|
|
warn "empty does not take arguments\n"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
$return = list( empty ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
elsif ( $func eq 'select' || $func eq 'grep' ) { |
133
|
|
|
|
|
|
|
if ( !@$args ) { |
134
|
|
|
|
|
|
|
warn "'$func' takes an expression argument"; |
135
|
|
|
|
|
|
|
$return = list( undef ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else { |
138
|
|
|
|
|
|
|
$return = one( $args->[0] ) ? list( $::document ) : list( undef ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
elsif ( $func eq 'group_by' ) { |
142
|
|
|
|
|
|
|
push @{ $::scope->{ group_by }{ one( $args->[0] ) } }, $::document; |
143
|
|
|
|
|
|
|
$return = list(); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ( $func eq 'sort' ) { |
146
|
|
|
|
|
|
|
push @{ $::scope->{ sort } }, [ one( @$args ), $::document ]; |
147
|
|
|
|
|
|
|
$return = list(); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
elsif ( $func eq 'keys' ) { |
150
|
|
|
|
|
|
|
my $value = @$args ? one( $args->[0] ) : $::document; |
151
|
|
|
|
|
|
|
if ( ref $value eq 'HASH' ) { |
152
|
|
|
|
|
|
|
$return = list( [ keys %$value ] ); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
elsif ( ref $value eq 'ARRAY' ) { |
155
|
|
|
|
|
|
|
$return = list( [ 0..$#{ $value } ] ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
else { |
158
|
|
|
|
|
|
|
warn "keys() requires a hash or array"; |
159
|
|
|
|
|
|
|
$return = list( undef ); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
elsif ( $func eq 'length' ) { |
163
|
|
|
|
|
|
|
my $value = @$args ? one( $args->[0] ) : $::document; |
164
|
|
|
|
|
|
|
if ( ref $value eq 'HASH' ) { |
165
|
|
|
|
|
|
|
$return = list( scalar keys %$value ); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
elsif ( ref $value eq 'ARRAY' ) { |
168
|
|
|
|
|
|
|
$return = list( scalar @$value ); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
elsif ( !ref $value ) { |
171
|
|
|
|
|
|
|
$return = list( length $value ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
else { |
174
|
|
|
|
|
|
|
warn "length() requires a hash, array, string, or number"; |
175
|
|
|
|
|
|
|
$return = list( undef ); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
hash: '{' pair(s /,/) '}' |
181
|
|
|
|
|
|
|
{ |
182
|
|
|
|
|
|
|
yq::diag( 1, "Hash: " . Dumper \@item ); |
183
|
|
|
|
|
|
|
$return = {}; |
184
|
|
|
|
|
|
|
for my $i ( @{$item[2]} ) { |
185
|
|
|
|
|
|
|
$return->{ one( $i->[0] ) } = one( $i->[1] ); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
$return = list( $return ); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
array: '[' expr(s /,/) ']' |
191
|
|
|
|
|
|
|
{ |
192
|
|
|
|
|
|
|
yq::diag( 1, "Array: " . Dumper( \@item ) ); |
193
|
|
|
|
|
|
|
$return = [ flatten( @{ $item[2] } ) ]; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
arguments: '(' expr(s /,/) ')' |
197
|
|
|
|
|
|
|
{ |
198
|
|
|
|
|
|
|
$return = list( @{ $item[2] } ); |
199
|
|
|
|
|
|
|
yq::diag( 1, "Args: " . Dumper( \@item ) ); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
pair: key ':' expr |
203
|
|
|
|
|
|
|
{ $return = [ @item[1,3] ] } |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
key: filter | quote_string | word |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
quote_string: quote non_quote quote |
208
|
|
|
|
|
|
|
{ $return = eval join "", @item[1..$#item] } |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
number: binnum | hexnum | octnum | float |
211
|
|
|
|
|
|
|
{ |
212
|
|
|
|
|
|
|
yq::diag( 1, "number: " . Dumper \@item ); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
binnum: /0b[01]+/ |
216
|
|
|
|
|
|
|
{ |
217
|
|
|
|
|
|
|
$return = eval $item[1]; |
218
|
|
|
|
|
|
|
yq::diag( 1, "binnum: " . Dumper \@item ); |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
hexnum: /0x[0-9A-Fa-f]+/ |
222
|
|
|
|
|
|
|
{ |
223
|
|
|
|
|
|
|
$return = eval $item[1]; |
224
|
|
|
|
|
|
|
yq::diag( 1, "hexnum: " . Dumper \@item ); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
octnum: /0o?\d+/ |
228
|
|
|
|
|
|
|
{ |
229
|
|
|
|
|
|
|
$return = eval $item[1]; |
230
|
|
|
|
|
|
|
yq::diag( 1, "octnum: " . Dumper \@item ); |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
float: /-?\d+(?:[.]\d+)?(?:e\d+)?/ |
234
|
|
|
|
|
|
|
{ |
235
|
|
|
|
|
|
|
$return = $item[1]; |
236
|
|
|
|
|
|
|
yq::diag( 1, "float: " . Dumper \@item ); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
word: /\w+/ |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
quote: /(?
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
non_quote: /(?:[^'"]|(?<=\\\\)['"])+/ |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
function_name: "empty" | "select" | "grep" | "group_by" | "keys" | "length" | "sort" |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
op: "eq" | "ne" | "==" | "!=" | ">=" | ">" | "<=" | "<" |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
comb: ',' | '|' |
250
|
|
|
|
|
|
|
}; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
my $parser = Parse::RecDescent->new( $grammar ); |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub is_list { |
255
|
0
|
|
|
0
|
0
|
|
return ref $_[0] eq 'list'; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub filter { |
259
|
0
|
|
|
0
|
0
|
|
my ( $class, $filter, $doc, $scope ) = @_; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
#$yq::VERBOSE = 1; |
262
|
0
|
|
|
|
|
|
my @input = ( $doc ); |
263
|
0
|
|
|
|
|
|
my $output; |
264
|
0
|
|
|
|
|
|
$::scope = $scope; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# We cannot interpret with |, because the right side of the pipe |
267
|
|
|
|
|
|
|
# gets interpreted before the left side can change the document. |
268
|
0
|
|
|
|
|
|
my @pipes = split /\s*[|]\s*/, $filter; |
269
|
0
|
|
|
|
|
|
my @output; |
270
|
0
|
|
|
|
|
|
for my $part ( @pipes ) { |
271
|
0
|
|
|
|
|
|
@output = (); |
272
|
0
|
|
|
|
|
|
for my $input ( @input ) { |
273
|
0
|
|
|
|
|
|
$::document = $input; |
274
|
0
|
|
|
|
|
|
my $output = $parser->program( $part ); |
275
|
|
|
|
|
|
|
#; use Data::Dumper; |
276
|
|
|
|
|
|
|
#; print "Want array: " . wantarray; |
277
|
|
|
|
|
|
|
#; print "OUTPUT: " . Dumper $output; |
278
|
|
|
|
|
|
|
#; print "SCOPE: " . Dumper $scope; |
279
|
0
|
0
|
|
|
|
|
if ( is_list( $output ) ) { |
280
|
0
|
|
|
|
|
|
push @output, @$output; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
else { |
283
|
0
|
|
|
|
|
|
push @output, $output; |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
} |
286
|
0
|
|
|
|
|
|
@input = @output; |
287
|
|
|
|
|
|
|
} |
288
|
0
|
0
|
|
|
|
|
return wantarray ? @output : $output[0]; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1; |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
__END__ |