blib/lib/Babble/Grammar.pm

filter: Statementstmt 100% 86 / 86 Branchbran 85.0% 17 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 6 totaltotal 93.1% 123 / 132 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 7 ▶use PPR::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Mu;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶lazy base_grammar_regexp => sub { $PPR::X::GRAMMAR };
Subroutine _build_base_grammar_regexp: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 lazy base_rule_names => sub {
10 35 ▶ my $g = $_[0]->base_grammar_regexp;
Subroutine _build_base_rule_names: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
11 35 +{ map +($_ => 1), $g =~ /\(\?<PerlStd(\w+)>/g };
12 7 };
13  
14 lazy rules => sub {
15 42 ▶ +{ 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 7 };
17  
18 lazy grammar_regexp => sub {
19 86 ▶ my ($self) = @_;
Subroutine _build_grammar_regexp: called CC=6 SCAR=17.9
Subroutine __ANON__: called CC=6 SCAR=17.9
20 86 my @parts;
21 86 foreach my $name (sort keys %{$self->rules}) {
22 8579 my @layers = @{$self->rules->{$name}};
23 8579 foreach my $idx (0..$#layers) {
24 8695 ▶ next unless defined(my $rule = $layers[$idx]);
Branch: 2 branches
BranchTrueFalseTotal
unless defined(my $rule = $layers[$idx]) 8506 189 8695
unless defined(my $rule = $layers[$idx]) 2178 43 2221
25 189 my $layer_name = $self->_rule_name($name, $idx);
26 189 my $define = '(?<'.$layer_name.'>'.$rule.')';
27 189 ▶ $define = '(?<Perl'.$name.'>'.$define.')' if $idx == $#layers;
Branch: 2 branches
BranchTrueFalseTotal
if $idx == $#layers 127 62 189
if $idx == $#layers 29 14 43
28 189 unshift @parts, $define;
29 }
30 }
31 86 my $base_re = $self->base_grammar_regexp;
32 86 ▶ return $base_re unless @parts;
Branch: 2 branches
BranchTrueFalseTotal
unless @parts 27 59 86
unless @parts 7 15 22
33 59 my $define_block = join "\n", '(?(DEFINE)', '', @parts, '', ')';
34 7 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 59 return qr{${define_block} ${base_re}}x;
36 7 };
37  
38 sub _rule_name {
39 372 ▶ my ($self, $name, $index) = @_;
Subroutine _rule_name: called CC=2 SCAR=6.9
40 372 ▶ return 'PerlStd'.$name unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 227 145 372
41 145 return 'PerlWrapper'.$name.'_'.sprintf("%03i", $index);
42 }
43  
44 sub add_rule {
45 74 ▶ my ($self, $name, $rule) = @_;
Subroutine add_rule: called CC=2 SCAR=7.0
Pod undocumented
46 74 ▶ die "Rule ${name} already exists" if exists $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->rules->{$name} 0 74 74
47 74 $self->rules->{$name} = [ $rule ];
48 74 return $self;
49 }
50  
51 sub replace_rule {
52 8 ▶ my ($self, $name, $rule) = @_;
Subroutine replace_rule: called CC=2 SCAR=7.0
Pod undocumented
53 8 ▶ die "Rule ${name} does not exist" unless exists $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->rules->{$name} 0 8 8
54 8 $self->rules->{$name} = [ $rule ];
55 8 return $self;
56 }
57  
58 sub extend_rule {
59 140 ▶ my ($self, $name, $cb) = @_;
Subroutine extend_rule: called CC=2 SCAR=7.0
Pod undocumented
60 140 ▶ die "Rule ${name} does not exist" unless my $r = $self->rules->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $r = $self->rules->{$name} 0 140 140
61 140 my $inner_name = $self->_rule_name($name, $#{$r});
62 140 $self->rules->{$name} = [ @$r, $cb->('(?&'.$inner_name.')') ];
63 140 return $self;
64 }
65  
66 sub augment_rule {
67 66 ▶ my ($self, $name, $extra) = @_;
Subroutine augment_rule: called CC=1 SCAR=0.0
Pod undocumented
68 66 ▶ $self->extend_rule($name, sub { join '|', $extra, $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
69 66 return $self;
70 }
71  
72 sub clone {
73 147 ▶ my ($self) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
74 return ref($self)->new(
75 base_grammar_regexp => $self->base_grammar_regexp,
76 147 rules => { %{$self->rules} },
77 );
78 }
79  
80 sub match {
81 3 ▶ my ($self, $as, $text) = @_;
Subroutine match: called CC=1 SCAR=0.0
Pod undocumented
82 3 require Babble::Match;
83 3 Babble::Match->new(
84 top_rule => $as,
85 text => $text,
86 grammar => $self
87 );
88 }
89  
90 7 1;
« blib/lib/Babble/PluginChain.pm ↑ Summary blib/lib/Babble/Plugin/DefinedOr.pm »