support/cmdline.yp

filter: Statementstmt 72.7% 136 / 187 Branchbran 54.3% 62 / 114 Conditioncond 42.4% 14 / 33 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 75.0% 30 / 40 Podpod 50.0% 1 / 2 totaltotal 62.6% 246 / 393 CC 73 SCAR 29.8
CC
73
Cov
63%
_consume_expression_element
58.5
_is_single_expr
30.0
_is_ref_ok
24.9
CRAP 19.7
« lib/App/GitFind/Base.pm ↑ Summary lib/App/GitFind/FileProcessor.pm »
1 # cmdline.yp: Parse::Yapp file for the command-line parser for App::GitFind
2 # Copyright (c) 2019 Christopher White.
3 # Copyright (c) 2019 D3 Engineering, LLC.
4 # Licensed MIT.
5  
6 #############################################################################
7 # Header
8  
9 %{
10  
11 # Imports {{{1
12  
13 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use App::GitFind::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use App::GitFind::Actions;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Debugging support
22 4 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.0
23 4 ▶ if($App::GitFind::cmdline::SHOW_AST // 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::GitFind::cmdline::SHOW_AST // 0) { } 0 4 4
Condition50%1/2
$App::GitFind::cmdline::SHOW_AST // 0
l!l
04
MC/DC0%0/1
$App::GitFind::cmdline::SHOW_AST // 0
$App::GitFind::cmdline::SHOW_AST
Truth table50%1/2
A
A= $App::GitFind::cmdline::SHOW_AST
Aresult
11
00
24 0 require XXX;
25 0 XXX->import;
26 } else { # !SHOW_AST - make YYY a passthrough
27 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶ *{'App::GitFind::cmdline::YYY'} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
29 78 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 78 78
30 4 };
31 }
32 } #BEGIN
33  
34 4 ▶BEGIN { YYY +{ 'YYY loaded' => 1 } }
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # }}}1
37 # Documentation {{{1
38  
39 =head1 NAME
40  
41 App::GitFind::cmdline - Command-line parser for git-find
42  
43 =head1 SYNOPSIS
44  
45 Generate the .pm file:
46  
47 yapp -m App::GitFind::cmdline -o lib/App/GitFind/cmdline.pm support/cmdline.yp
48  
49 And then:
50  
51 use App::GitFind::cmdline;
52 App::GitFind::cmdline::Parse(\@ARGV);
53  
54 For debugging output, define C<$SHOW_AST> before the C<use> statement:
55  
56 BEGIN { $App::GitFind::cmdline::SHOW_AST = 1; }
57  
58 =head1 FUNCTIONS
59  
60 =cut
61  
62 # }}}1
63 # Helpers for the parser {{{1
64  
65 # Merge any number of hashrefs together and return a hashref
66 21 ▶sub _merge {
Subroutine _merge: called CC=3 SCAR=11.0
67 21 state $merger = Hash::Merge->new('RETAINMENT_PRECEDENT');
68 21 $merger->set_clone_behavior(false); # No cloning
69 21 my $retval = {};
70 21 for(@_) {
71 42 ▶ next unless ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 42 42
72 42 $retval = $merger->merge($retval, $_);
73 }
74 21 return $retval;
75 }
76  
77 # Check for { expr => FOO }
78 0 ▶sub _is_single_expr {
Subroutine _is_single_expr: not called CC=5 SCAR=34.0
79 0 ▶ return false unless @_ eq 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ eq 1 0 0 0
80 0 my $h = $_[0];
81 0 ▶ return false unless ref $h eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 0 0 0
82 0 ▶ return false unless keys(%$h) eq 1;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$h eq 1 0 0 0
83 0 ▶ return false unless (keys %$h)[0] eq 'expr';
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$h)[0] eq "expr" 0 0 0
84 0 return true;
85 }
86  
87 # }}}1
88  
89 %}
90  
91 20 ▶#############################################################################
Subroutine new: called CC=2 SCAR=8.0
Pod undocumented
92 20 # Token and precedence definitions
93 20 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 20 20
94 %token SWITCH # flags controlling the operation of git-find
95 %token REV # a git ref or rev
96  
97 # elements of expressions
98 %token COMMA
99 %token OR
100 %token AND
101 %token NOT
102 %token LPAREN
103 %token RPAREN
104  
105 %token TEST
106 %token ACTION
107  
108 %left COMMA
109 %left OR
110 %left AND
111 %left NOT
112  
113 %%
114  
115 #############################################################################
116 # Rules
117  
118 cmdline:
119 0 ▶ { YYY +{} } # empty
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 7 ▶ | maybeexprplus { YYY $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
121 | switches_and_revs maybeexprplus
122 13 ▶ { YYY _merge($_[1], $_[2]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
123 ;
124  
125 # Switches
126  
127 11 ▶switches_and_revs: SWITCH { YYY +{ switches => {$_[1]=>[true]} } }
Subroutine __ANON__: called CC=1 SCAR=0.0
128 # arrayref so @{{switches}->{foo}} will
129 # always work. That way we can test for
130 # switches specified multiple times.
131  
132 2 ▶ | REV { YYY +{ revs => [$_[1]] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
133 | switches_and_revs SWITCH
134 0 ▶ { YYY _merge($_[1], +{ switches => {$_[2]=>[true]} }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 | switches_and_revs REV
136 8 ▶ { YYY _merge($_[1], +{ revs => [$_[2]] }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
137 ;
138  
139 # Expressions. Since "and", "or", and "not" are keywords in Perl, the
140 # hash keys for our operators are all upper-case.
141 maybeexprplus:
142 7 ▶ { YYY +{} } # empty
Subroutine __ANON__: called CC=1 SCAR=0.0
143 13 ▶ | expr { YYY +{ expr => $_[1] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
144 #{ _is_single_expr($_[1]) ? $_[1] : +{ expr => $_[1] } }
145 0 ▶ | expr switches_and_revs { YYY +{ expr => $_[1], %{$_[2]} } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 # %$_[2]: inline the switches and revs
147 ;
148  
149 # TODO once the parser is solid and the tests are written:
150 # collapse consecutive elements of the same type into flat lists.
151 expr: element
152 2 ▶ | expr COMMA expr { YYY +{ SEQ => [@_[1,3]] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
153 1 ▶ | expr OR expr { YYY +{ OR => [@_[1,3]] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
154 # "OR" because "or" is a Perl keyword
155  
156 # subsequent_expr is used so the parser will know when to reduce.
157 # The rule "expr expr %prec AND" was right-associative
158 # even though explicit-AND was left-associative because
159 # the parser did not start reducing until after anything that
160 # could be an expr. A subsequent_expr cannot start with
161 # a plain expr, so the parser has a reason to reduce the expr.
162 | expr subsequent_expr %prec AND
163 4 ▶ { YYY +{ AND => [@_[1,2]] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
164 0 ▶ | expr AND expr { YYY +{ AND => [@_[1,3]] } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 0 ▶ | NOT expr4 { YYY +{ NOT => $_[2] } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 ▶ | LPAREN expr RPAREN { YYY $_[2] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 ;
168  
169 subsequent_expr: # Everything higher-precedence than AND
170 element
171 0 ▶ | NOT expr4 { YYY +{ NOT => $_[2] } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 ▶ | LPAREN expr RPAREN { YYY $_[2] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 ;
174  
175  
176 element: TEST
177 | ACTION
178 6 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.2
179 6 ▶ $_[0]->YYData->{SAW_NON_PRUNE_ACTION} = true if $_[1] ne 'prune';
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] ne "prune" 6 0 6
180 6 YYY $_[1];
181 }
182 20 ;
183  
184 %%
185 20  
186 #############################################################################
187 # Footer
188  
189 # Helpers for the tokenizer {{{1
190  
191 # Flag a ref as invalid without using regexes.
192 # Implements https://git-scm.com/docs/git-check-ref-format as archived at
193 # https://web.archive.org/web/20190725153529/https://git-scm.com/docs/git-check-ref-format
194  
195 2676 ▶sub _is_ref_ok {
Subroutine _is_ref_ok: called CC=16 SCAR=32.1
196 2676 ▶ my $arg = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2676 0 2676
197  
198 2676 ▶ return false unless defined $arg and length($arg)>0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg and length $arg > 0 0 2676 2676
Condition33%1/3
defined $arg and length $arg > 0
!ll&&!rl&&r
002676
MC/DC0%0/2
defined $arg and length $arg > 0
defined $arg length $arg > 0
Truth table33%1/3
A and B
A= defined $argB= length $arg > 0
ABresult
0X0
100
111
199  
200 #1 - restrictions on slash-separated components
201 2676 ▶ if(index($arg, '/') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($arg, "/") != -1) 40 2636 2676
202 40 ▶ return false if index($arg, '/.') != -1 #internal components
Branch: 1 branch
BranchTrueFalseTotal
if index($arg, "/.") != -1 or index($arg, ".lock/") != -1 or substr($arg, 0, 1) eq "." or substr($arg, -5) eq ".lock" 0 40 40
Condition33%3/9
index($arg, "/.") != -1 or index($arg, ".lock/") != -1 or substr($arg, 0, 1) eq "." or substr($arg, -5) eq ".lock"
l!l&&r!l&&!r
0040
index($arg, "/.") != -1 or index($arg, ".lock/") != -1 or substr($arg, 0, 1) eq "."
l!l&&r!l&&!r
0040
index($arg, "/.") != -1 or index($arg, ".lock/") != -1
l!l&&r!l&&!r
0040
MC/DC0%0/4
index($arg, "/.") != -1 or index($arg, ".lock/") != -1 or substr($arg, 0, 1) eq "." or substr($arg, -5) eq ".lock"
index($arg, "/.") != -1 index($arg, ".lock/") != -1 substr($arg, 0, 1) eq "." substr($arg, -5) eq ".lock"
Truth table20%1/5
A or B or C or D
A= index($arg, "/.") != -1B= index($arg, ".lock/") != -1C= substr($arg, 0, 1) eq "."D= substr($arg, -5) eq ".lock"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
203 || index($arg, '.lock/') != -1
204 || substr($arg, 0, 1) eq '.' #components at start/end
205 || substr($arg, -5) eq '.lock';
206 }
207  
208 # Ignore #2 - assume --allow-onelevel
209  
210 #3
211 2676 ▶ return false if index($arg, '..') != -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($arg, "..") != -1 0 2676 2676
212  
213 #4 - require the caller to check that
214 #5 - require the caller to check that - assume NOT --refspec-pattern
215  
216 #6 - assume NOT --normalize
217 2676 ▶ return false if substr($arg, 0, 1) eq '/'
Branch: 1 branch
BranchTrueFalseTotal
if substr($arg, 0, 1) eq "/" or substr($arg, -1) eq "/" or index($arg, "//") != -1 6 2670 2676
Condition66%4/6
substr($arg, 0, 1) eq "/" or substr($arg, -1) eq "/" or index($arg, "//") != -1
l!l&&r!l&&!r
602670
substr($arg, 0, 1) eq "/" or substr($arg, -1) eq "/"
l!l&&r!l&&!r
062670
MC/DC33%1/3
substr($arg, 0, 1) eq "/" or substr($arg, -1) eq "/" or index($arg, "//") != -1
substr($arg, 0, 1) eq "/" substr($arg, -1) eq "/" index($arg, "//") != -1
Truth table50%2/4
A or B or C
A= substr($arg, 0, 1) eq "/"B= substr($arg, -1) eq "/"C= index($arg, "//") != -1
ABCresult
1XX1
01X1
0011
0000
218 || substr($arg, -1) eq '/'
219 || index($arg, '//') != -1;
220  
221 # #7. Also prohibits ".", which is OK for git-find since it is
222 # fairly ambiguous between a ref/rev and a path.
223 2670 ▶ return false if substr($arg, -1) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($arg, -1) eq "." 21 2649 2670
224  
225 #8
226 2649 ▶ return false if index($arg, '@{') != -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($arg, "\@{") != -1 0 2649 2649
227  
228 #9 ('@') - ignore this one for simplicity in the rev test below.
229  
230 #10 - require the caller to check that
231  
232 # Extra: Prohibit refs that start with '--' since they are arguably
233 # ambiguous with command-line options (and I can't make them work
234 # with git anyway).
235 2649 ▶ return false if substr($arg, 0, 2) eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if substr($arg, 0, 2) eq "--" 1455 1194 2649
236  
237 1194 return true; # It's OK if we got here
238 } #_is_ref_ok()
239  
240 #use re 'debug';
241  
242 # Regex to match a rev or range of revs, i.e., something we should pass to git
243 my $_rev_regex =
244 qr`(?xi) # backtick delimiter because it doesn't occur in the regex text
245 (?&RevRange)
246  
247 (?(DEFINE)
248  
249 (?<RevRange> ^(?:
250 # :/text, :/!-text, :/!!text
251 (?::/ #(?{ print "# saw colon slash\n"; })
252 (?:
253 ![!\-](?:.+) #(?{print "# 4\n";})
254 | [^!].* #(?{print "# 5\u";})
255 )
256 )
257  
258 # :[n:]path. NOTE: we prohibit starting the path with
259 # / if there is no number, in order to disambiguate
260 # the :/ text-search cases.
261 | :\d+:(?:.+) #(?{print "# 2\n";})
262 | :[^/].* #(?{print "# 3\n";})
263  
264 # ^<rev>
265 | \^(?&Rev) #(?{print "# 6\n";})
266  
267 # rev:path
268 | (?&Rev):(?:.+) #(?{print "# 7\n";})
269  
270 # .. and ... differences, including x.., x..., x..y,
271 # and x...y. Also handles the fallthrough
272 # of revrange->rev->ref.
273 | (?&Rev)(?:\.{2,3}(?&Rev)?)?
274 #(?{print "# 8\n";})
275  
276 # ..rev and ...rev
277 | \.{2,3}(?&Rev)
278  
279 # at sign followed by braced item, and possibly
280 # preceded by a REF (not a rev). E.g.,
281 # HEAD@{1}@{1} doesn't work.
282 # refname - at sign - braced item (date, #, branch, "push")
283 | (?&Ref)?\@\{[^\}]+\}
284 #(?{print "# 9\n";})
285  
286 # git-rev-parse "Options for Objects" forms
287 | --all
288 | --(?:branches|tags|remotes)(?:=.+)?
289 | --(?:glob|exclude)=.+
290 | --disambiguate=[0-9a-f]{4,40}
291  
292 # git-rev-parse "Other Options" forms
293 | --(since|after|until|before)=.+
294  
295 )$) # End of RevRange
296  
297 (?<Rev> (?&Ref)(?&RefTrailer)* )
298 # This handles most of the cases.
299 # SHA1s, possibly abbreviated, are refs,
300 # as are git-describe outputs, whence RefTrailer*
301 # instead of RefTrailer+.
302  
303 (?<RefTrailer>
304 # For rev^[#] and rev~[#] forms
305 [~\^]\d*
306  
307 # For rev^{} forms (empty braces OK)
308 | \^\{[^\}]*\}
309  
310 # For rev^[@!] and rev^-n
311 | \^(?: \@ | ! | -\d* )
312 ) # End of RefTrailer
313  
314 (?<Ref>
315 ( \@ # '@' from git-rev-parse
316 | (?:[^\000-\037\177\ ~\^:\\?*\[.@/]
317 # git-check-ref-format #4, #5.
318 # [.@/] are handled below
319 | \.(?!\.) # . ok, but .. prohibited
320 | \@(?!\{) # @ ok, but @{ prohibited
321 | /(?!/) # / ok, but // prohibited
322  
323 )+?
324 )
325 (?(?{ _is_ref_ok($+) })|(?!))
326 # NOTE: $+ used since I couldn't get named capture groups
327 # with either %+ or %- to work
328 ) # End of <Ref>
329  
330 ) #End of (DEFINE)
331  
332 `xi; # End of qr`...` and an extra backtick to unconfuse vim-eyapp: `
333  
334 149 ▶sub _is_valid_rev {
Subroutine _is_valid_rev: called CC=4 SCAR=13.9
335 149 ▶ my $arg = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 149 0 149
336  
337 149 ▶ return false unless defined $arg and length($arg)>0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg and length $arg > 0 2 147 149
Condition100%3/3
defined $arg and length $arg > 0
!ll&&!rl&&r
11147
MC/DC100%2/2
defined $arg and length $arg > 0
defined $arg length $arg > 0
Truth table100%3/3
A and B
A= defined $argB= length $arg > 0
ABresult
0X0
100
111
338 147 return scalar($arg =~ m{$_rev_regex});
339 } #_is_valid_rev()
340  
341 # Get an expression element from the array passed in $_[0].
342 my $ARGTEST_cached = App::GitFind::Actions::ARGTEST();
343 34 ▶sub _consume_expression_element {
Subroutine _consume_expression_element: called CC=18 SCAR=40.7
344 34 my $lrArgv = shift;
345 34 my @retval;
346  
347 #say STDERR "# Trying >>$lrArgv->[0]<<";
348 # TODO find(1) positional options, global options?
349  
350 # Regular options
351 34 ▶ if($lrArgv->[0] =~ $ARGTEST_cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lrArgv->[0] =~ /$ARGTEST_cached/) 20 14 34
352 #say STDERR "# - matched";
353 20 my $arg = $1;
354 20 my %opts = %{App::GitFind::Actions::argdetails($arg)};
355  
356 # Save any non-parser information from the argdetails to be
357 # returned as part of the semantic value.
358 20 my %extras = %opts;
359 20 delete @extras{qw(token nparam)};
360  
361 # No-argument tests or actions
362 20 ▶ unless($opts{nparam}>0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"nparam"} > 0) 20 0 20
363 #say STDERR "# - No parameters";
364 20 shift @$lrArgv;
365 20 return ($opts{token} => { name => $arg, %extras })
366 }
367  
368 # Consume additional arguments up to a regexp
369 0 ▶ if(ref $opts{nparam} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"nparam"} eq "Regexp") 0 0 0
370 #say STDERR "# - parameters until $opts{nparam}";
371 0 ▶ die "Need argument(s) for --$arg" if @$lrArgv == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$lrArgv == 1 0 0 0
372 0 my $lastarg;
373 #say STDERR "Args: ", join ' : ', @$lrArgv;
374 0 for(1..$#$lrArgv) {
375 0 ▶ $lastarg=$_, last if $lrArgv->[$_] =~ $opts{nparam};
Branch: 1 branch
BranchTrueFalseTotal
if $lrArgv->[$_] =~ /$opts{"nparam"}/ 0 0 0
376 }
377 0 ▶ die "--$arg needs an argument terminator matching $opts{nparam}"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lastarg 0 0 0
378 unless defined $lastarg;
379  
380 # Set up to fall through to the numeric-params case
381 0 $opts{nparam} = $lastarg;
382 }
383  
384 # Consume additional positional arguments
385 #say STDERR "# - $opts{nparam} parameters";
386 die "Not enough parameters after --$arg (need $opts{nparam})"
387 0 ▶ unless @$lrArgv >= ($opts{nparam}+1); # +1 for $arg itself
Branch: 1 branch
BranchTrueFalseTotal
unless @$lrArgv >= $opts{"nparam"} + 1 0 0 0
388  
389 # Custom argument validation
390 0 ▶ if($opts{validator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"validator"}) 0 0 0
391 0 my $errmsg = $opts{validator}->(@{$lrArgv}[0..$opts{nparam}]);
392 0 ▶ die "--$arg argument error: $errmsg" if defined($errmsg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $errmsg 0 0 0
393 }
394  
395 @retval = ($opts{token} => {
396 name => $arg,
397 0 params => [ @{$lrArgv}[1..$opts{nparam}] ],
398 %extras,
399 });
400 0 splice @$lrArgv, 0, $opts{nparam}+1;
401 0 return @retval;
402 }
403  
404 # Operators
405 14 my $arg = $lrArgv->[0];
406  
407 14 ▶ @retval = (COMMA => ',') if $arg eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "," 2 12 14
408 14 ▶ @retval = (OR => '-o') if $arg =~ /^(?:-o|--o|-or|--or|\|\|)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^(?:-o|--o|-or|--or|\|\|)$/ 1 13 14
409 14 ▶ @retval = (AND => '-a') if $arg =~ /^(?:-a|--a|-and|--and|&&)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^(?:-a|--a|-and|--and|&&)$/ 0 14 14
410 14 ▶ @retval = (NOT => '!') if $arg =~ /^(?:-not|--not|!|\^)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^(?:-not|--not|!|\^)$/ 0 14 14
411 14 ▶ @retval = (LPAREN => '(') if $arg =~ /^[([]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^[([]$/ 0 14 14
412 14 ▶ @retval = (RPAREN => ')') if $arg =~ /^[])]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^[])]$/ 0 14 14
413  
414 14 ▶ if(@retval) {
Branch: 1 branch
BranchTrueFalseTotal
if (@retval) 3 11 14
415 3 shift @$lrArgv;
416 3 return @retval;
417 }
418  
419 11 return (); # Not an expression element
420 } #_consume_expression_element
421  
422 # Get a switch from the array passed in $_[0], if any.
423 # Removes the switch from the array if successful.
424 # Returns the token on success, and () on failure.
425 # TODO un-bundle switches; handle switches with args.
426 21 ▶sub _consume_switch {
Subroutine _consume_switch: called CC=3 SCAR=11.6
427 21 my $lrArgv = shift;
428 21 ▶ if($lrArgv->[0] =~ /^-([a-zA-z0-9\?])$/) { # non-bundled switch
Branch: 2 branches
BranchTrueFalseTotal
if ($lrArgv->[0] =~ /^-([a-zA-z0-9\?])$/) { } 11 10 21
elsif ($lrArgv->[0] =~ /^--?(help|man|usage|version)$/) { } 0 10 10
429 11 shift @$lrArgv;
430 11 return (SWITCH => $1)
431 } elsif($lrArgv->[0] =~ /^--?(help|man|usage|version)$/) { # long switch
432 0 shift @$lrArgv;
433 0 return (SWITCH => $1);
434 }
435  
436 10 return ();
437 } #_consume_switch()
438  
439 # Consume a rev from the array in $_[0]
440 10 ▶sub _consume_rev {
Subroutine _consume_rev: called CC=2 SCAR=7.2
441 10 my $lrArgv = shift;
442 10 my $arg = $lrArgv->[0];
443 10 ▶ if(_is_valid_rev($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_valid_rev($arg)) 10 0 10
444 10 shift @$lrArgv;
445 10 return (REV => $arg);
446 }
447  
448 0 return ();
449 } #_consume_rev()
450  
451 # }}}1
452 # Tokenizer and error-reporting routine for Parse::Yapp {{{1
453  
454 # The lexer
455 64 ▶sub _next_token {
Subroutine _next_token: called CC=18 SCAR=31.4
456 64 my $parser = shift;
457 64 my $lrArgv = $parser->YYData->{ARGV};
458 64 ▶ return ('', undef) unless @$lrArgv; # EOF
Branch: 1 branch
BranchTrueFalseTotal
unless @$lrArgv 17 47 64
459 47 my @retval; # The eventual token we will return
460  
461 # TODO? in the expression, split trailing commas into their
462 # own arguments
463  
464 # Check for '--'
465 47 ▶ if($lrArgv->[0] eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lrArgv->[0] eq "--") 8 39 47
466 8 $parser->YYData->{ONLY_EXPRESSIONS} = true;
467 8 ▶ return ('', undef) unless @$lrArgv > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lrArgv > 1 3 5 8
468 # We are about to shift, so return EOF if this was the last arg.
469 5 shift(@$lrArgv);
470 }
471  
472 44 ▶ if($parser->YYData->{HAS_DASH_DASH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->YYData->{'HAS_DASH_DASH'}) { } 16 28 44
473 # Split-arg mode: don't look for expressions before '--', or
474 # for switches or refs after '--'.
475 16 ▶ if(!$parser->YYData->{ONLY_EXPRESSIONS}) { # Look for switches/refs
Branch: 1 branch
BranchTrueFalseTotal
if (not $parser->YYData->{'ONLY_EXPRESSIONS'}) { } 10 6 16
476  
477 10 @retval = _consume_switch($lrArgv);
478 10 ▶ return @retval if @retval;
Branch: 1 branch
BranchTrueFalseTotal
if @retval 5 5 10
479  
480 5 @retval = _consume_rev($lrArgv);
481 5 ▶ if(@retval) { # _consume_rev always gives us two elements
Branch: 1 branch
BranchTrueFalseTotal
if (@retval) 5 0 5
482 5 ▶ if($retval[1] eq ']]') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval[1] eq ']]') { } 0 5 5
483 0 ▶ $parser->YYData->{SAW_RR} ||= true;
Condition0%0/2
$parser->YYData->{"SAW_RR"} ||= !!1
l!l
00
MC/DC0%0/1
$parser->YYData->{"SAW_RR"} ||= !!1
$parser->YYData->{"SAW_RR"}
Truth table0%0/2
A
A= $parser->YYData->{"SAW_RR"}
Aresult
11
00
484 } else {
485 5 ▶ $parser->YYData->{SAW_NON_RR} ||= true;
Condition50%1/2
$parser->YYData->{"SAW_NON_RR"} ||= !!1
l!l
05
MC/DC0%0/1
$parser->YYData->{"SAW_NON_RR"} ||= !!1
$parser->YYData->{"SAW_NON_RR"}
Truth table50%1/2
A
A= $parser->YYData->{"SAW_NON_RR"}
Aresult
11
00
486 }
487 5 return @retval;
488 }
489  
490 0 die "I don't understand argument '$lrArgv->[0]' before --";
491  
492 } else { # Look for expressions
493 6 @retval = _consume_expression_element($lrArgv);
494 6 ▶ return @retval if @retval;
Branch: 1 branch
BranchTrueFalseTotal
if @retval 6 0 6
495 0 die "I don't understand argument '$lrArgv->[0]' after --";
496 }
497  
498 } else {
499 # Merged-arg mode: any arg could be anything
500  
501 # Check for expressions. Look for these before checking for refs so
502 # that an expression that happens to look like a ref will be considered
503 # an expression instead of a ref.
504 28 my @retval = _consume_expression_element($lrArgv);
505 28 ▶ return @retval if @retval;
Branch: 1 branch
BranchTrueFalseTotal
if @retval 17 11 28
506  
507 # Next, look for switches. These are after expression elements
508 # so that -a and -o will not be parsed as switches.
509 11 @retval = _consume_switch($lrArgv);
510 11 ▶ return @retval if @retval;
Branch: 1 branch
BranchTrueFalseTotal
if @retval 6 5 11
511  
512 # Last of all, revs.
513 5 @retval = _consume_rev($lrArgv);
514 5 ▶ if(@retval) { # _consume_rev always gives us two elements
Branch: 1 branch
BranchTrueFalseTotal
if (@retval) 5 0 5
515 5 ▶ if($retval[1] eq ']]') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval[1] eq ']]') { } 0 5 5
516 0 ▶ $parser->YYData->{SAW_RR} ||= true;
Condition0%0/2
$parser->YYData->{"SAW_RR"} ||= !!1
l!l
00
MC/DC0%0/1
$parser->YYData->{"SAW_RR"} ||= !!1
$parser->YYData->{"SAW_RR"}
Truth table0%0/2
A
A= $parser->YYData->{"SAW_RR"}
Aresult
11
00
517 } else {
518 5 ▶ $parser->YYData->{SAW_NON_RR} ||= true;
Condition50%1/2
$parser->YYData->{"SAW_NON_RR"} ||= !!1
l!l
05
MC/DC0%0/1
$parser->YYData->{"SAW_NON_RR"} ||= !!1
$parser->YYData->{"SAW_NON_RR"}
Truth table50%1/2
A
A= $parser->YYData->{"SAW_NON_RR"}
Aresult
11
00
519 }
520 5 return @retval;
521 }
522  
523 0 die "I don't understand argument $lrArgv->[0]";
524 }
525  
526 0 die "Unexpected error while processing argument $lrArgv->[0]"; # Shouldn't happen
527 } #_next_token()
528  
529 # Report an error
530 0 ▶sub _report_error {
Subroutine _report_error: not called CC=4 SCAR=30.0
531 0 my $parser = shift;
532 0 ▶ my $got = $parser->YYCurtok || '<end of input>';
Condition0%0/2
$parser->YYCurtok || '<end of input>'
l!l
00
MC/DC0%0/1
$parser->YYCurtok || '<end of input>'
$parser->YYCurtok
Truth table0%0/2
A
A= $parser->YYCurtok
Aresult
11
00
533 0 my $val='';
534 0 ▶ $val = ' (' . $parser->YYCurval . ')' if $parser->YYCurval;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->YYCurval 0 0 0
535 0 die 'Syntax error: could not understand ', $got, $val, "\n";
536 0 ▶ if(ref($parser->YYExpect) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parser->YYExpect eq "ARRAY") 0 0 0
537 0 print 'Expected one of: ', join(',', @{$parser->YYExpect}), "\n";
538 }
539 0 return;
540 } #_report_error()
541  
542 # }}}1
543 # Top-level parse function {{{1
544  
545 =head2 Parse
546  
547 Parse arguments. Usage:
548  
549 my $hrArgs = App::GitFind::cmdline::Parse(\@ARGV);
550  
551 Modifies the C<@ARGV> array.
552  
553 =cut
554  
555 20 ▶sub Parse {
Subroutine Parse: called CC=4 SCAR=14.0
Pod documented
556 20 ▶ my $lrArgv = shift or
Branch: 1 branch
BranchTrueFalseTotal
my $lrArgv = shift() or require Carp, Carp::croak("Parse: Need an argument list") 20 0 20
557 (require Carp, Carp::croak 'Parse: Need an argument list');
558  
559 20 my $parser = __PACKAGE__->new;
560 20 my $hrData = $parser->YYData;
561  
562 # Data we use while parsing
563 20 $hrData->{HAS_DASH_DASH} = !!(scalar grep { $_ eq '--' } @$lrArgv);
564 20 $hrData->{ONLY_EXPRESSIONS} = false; # true once we hit '--'
565 20 $hrData->{ARGV} = $lrArgv;
566  
567 # Data we determine while parsing and return to the caller
568  
569 # Keep track of whether an action other than -prune has been seen.
570 # If not, -print is added automatically.
571 20 $hrData->{SAW_NON_PRUNE_ACTION} = false;
572  
573 # Keep track of the types of rev we've seen (]] or non-]])
574 20 $hrData->{SAW_RR} = false;
575 20 $hrData->{SAW_NON_RR} = false;
576  
577 20 ▶ my $hrRetval = $parser->YYParse(yylex => \&_next_token,
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 20 20
578 yyerror => \&_report_error,
579 (@_ ? (yydebug => $_[0]) : ()),
580 );
581  
582 # Add non-AST data to the retval
583 20 ▶ $hrRetval->{saw_nonprune_action} = $hrData->{SAW_NON_PRUNE_ACTION} if $hrRetval;
Branch: 1 branch
BranchTrueFalseTotal
if $hrRetval 20 0 20
584 20 $hrRetval->{saw_rr} = $hrData->{SAW_RR};
585 20 $hrRetval->{saw_non_rr} = $hrData->{SAW_NON_RR};
586 20 return $hrRetval;
587 } #Parse()
588  
589 # }}}1
590 # Rest of the docs {{{1
591  
592 =head1 AUTHOR
593  
594 Christopher White C<< <cxw@cpan.org> >>
595  
596 =head1 COPYRIGHT
597  
598 MIT
599  
600 =cut
601  
602 # }}}1
603  
604 # vi: set fdm=marker: #
« lib/App/GitFind/Base.pm ↑ Summary lib/App/GitFind/FileProcessor.pm »