blib/lib/Babble/Grammar.pm

filter: Statementstmt 100% 83 / 83 Branchbran 85.0% 17 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 6 totaltotal 92.8% 117 / 126 CC 15 SCAR 11.0
CC
15
Cov
97%
__ANON__
6.0
_build_grammar_regexp
6.0
add_rule
2.0
CRAP 3.0
« blib/lib/Babble/PluginChain.pm ↑ Summary blib/lib/Babble/Plugin/DefinedOr.pm »
1 package Babble::Grammar;
2  
3 8 ▶use PPR::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 71 ▶lazy base_grammar_regexp => sub { $PPR::X::GRAMMAR };
Subroutine _build_base_grammar_regexp: called CC=1 SCAR=0.0
8  
9 lazy base_rule_names => sub {
10 65 ▶ my $g = $_[0]->base_grammar_regexp;
Subroutine _build_base_rule_names: called CC=1 SCAR=0.0
11 65 +{ map +($_ => 1), $g =~ /\(\?<PerlStd(\w+)>/g };
12 8 };
13  
14 lazy rules => sub {
15 60 ▶ +{ map +($_ => [ undef ]), keys %{ $_[0]->base_rule_names } }
Subroutine _build_rules: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
16 8 };
17  
18 lazy grammar_regexp => sub {
19 129 ▶ my ($self) = @_;
Subroutine _build_grammar_regexp: called CC=6 SCAR=17.9
20 129 my @parts;
21 129 foreach my $name (sort keys %{$self->rules}) {
22 12852 my @layers = @{$self->rules->{$name}};
23 12852 foreach my $idx (0..$#layers) {
24 13018 ▶ next unless defined(my $rule = $layers[$idx]);
Branch: 2 branches
BranchTrueFalseTotal
unless defined(my $rule = $layers[$idx]) 12763 255 13018
unless defined(my $rule = $layers[$idx]) 1485 37 1522
25 255 my $layer_name = $self->_rule_name($name, $idx);
26 255 my $define = '(?<'.$layer_name.'>'.$rule.')';
27 255 ▶ $define = '(?<Perl'.$name.'>'.$define.')' if $idx == $#layers;
Branch: 2 branches
BranchTrueFalseTotal
if $idx == $#layers 174 81 255
if $idx == $#layers 26 11 37
28 255 unshift @parts, $define;
29 }
30 }
31 129 my $base_re = $self->base_grammar_regexp;
32 129 ▶ return $base_re unless @parts;
Branch: 2 branches
BranchTrueFalseTotal
unless @parts 36 93 129
unless @parts 6 9 15
33 93 my $define_block = join "\n", '(?(DEFINE)', '', @parts, '', ')';
34 8 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 # For reasons I don't understand, this stringify is required (RT #126285)
36 93 my $final_re = "${define_block} ${base_re}";
37 93 return qr{$final_re}x;
38 8 };
39  
40 sub _rule_name {
41 476 ▶ my ($self, $name, $index) = @_;
Subroutine _rule_name: called CC=2 SCAR=6.9
42 476 ▶ return 'PerlStd'.$name unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 287 189 476
43 189 return 'PerlWrapper'.$name.'_'.sprintf("%03i", $index);
44 }
45  
46 sub add_rule {
47 90 ▶ my ($self, $name, $rule) = @_;
Subroutine add_rule: called CC=2 SCAR=7.0
Pod undocumented
48 90 ▶ die "Rule ${name} already exists" if exists $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->rules->{$name} 0 90 90
49 90 $self->rules->{$name} = [ $rule ];
50 90 return $self;
51 }
52  
53 sub replace_rule {
54 8 ▶ my ($self, $name, $rule) = @_;
Subroutine replace_rule: called CC=2 SCAR=7.0
Pod undocumented
55 8 ▶ die "Rule ${name} does not exist" unless exists $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->rules->{$name} 0 8 8
56 8 $self->rules->{$name} = [ $rule ];
57 8 return $self;
58 }
59  
60 sub extend_rule {
61 184 ▶ my ($self, $name, $cb) = @_;
Subroutine extend_rule: called CC=2 SCAR=7.0
Pod undocumented
62 184 ▶ die "Rule ${name} does not exist" unless my $r = $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $r = $self->rules->{$name} 0 184 184
63 184 my $inner_name = $self->_rule_name($name, $#{$r});
64 184 $self->rules->{$name} = [ @$r, $cb->('(?&'.$inner_name.')') ];
65 184 return $self;
66 }
67  
68 sub augment_rule {
69 82 ▶ my ($self, $name, $extra) = @_;
Subroutine augment_rule: called CC=1 SCAR=0.0
Pod undocumented
70 82 ▶ $self->extend_rule($name, sub { join '|', $extra, $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
71 82 return $self;
72 }
73  
74 sub clone {
75 191 ▶ my ($self) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
76 return ref($self)->new(
77 base_grammar_regexp => $self->base_grammar_regexp,
78 191 rules => { %{$self->rules} },
79 );
80 }
81  
82 sub match {
83 3 ▶ my ($self, $as, $text) = @_;
Subroutine match: called CC=1 SCAR=0.0
Pod undocumented
84 3 require Babble::Match;
85 3 Babble::Match->new(
86 top_rule => $as,
87 text => $text,
88 grammar => $self
89 );
90 }
91  
92 8 1;
« blib/lib/Babble/PluginChain.pm ↑ Summary blib/lib/Babble/Plugin/DefinedOr.pm »