| 1 | package Babble::Grammar; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 12 | ▶ | use PPR::X; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 12 | ▶ | use Mu; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 12 | ▶ | use strictures 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 12 | ▶ | use Babble::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 32 | ▶ | lazy base_grammar_regexp => sub { $PPR::X::GRAMMAR }; | ||||||||||||||||||
|
Subroutine
_build_base_grammar_regexp: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 71 | ▶ | lazy base_rule_names => sub { | ||||||||||||||||||
|
Subroutine
_build_base_rule_names: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 71 | my $g = $_[0]->base_grammar_regexp; | |||||||||||||||||||
| 13 | 71 | +{ map +($_ => 1), $g =~ /\(\?<PerlStd(\w+)>/g }; | |||||||||||||||||||
| 14 | 12 | }; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 14 | ▶ | lazy rules => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_rules: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 14 | +{ map +($_ => [ undef ]), keys %{ $_[0]->base_rule_names } } | |||||||||||||||||||
| 18 | 12 | }; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # global cache of compiled grammar regexps | ||||||||||||||||||||
| 21 | 12 | my %COMPILE_CACHE; | |||||||||||||||||||
| 22 | 411 | ▶ | lazy grammar_regexp => sub { | ||||||||||||||||||
|
Subroutine
_build_grammar_regexp: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 23 | 411 | my ($self) = @_; | |||||||||||||||||||
| 24 | 411 | my @parts; | |||||||||||||||||||
| 25 | 411 | foreach my $name (sort keys %{$self->rules}) { | |||||||||||||||||||
| 26 | 41114 | my @layers = @{$self->rules->{$name}}; | |||||||||||||||||||
| 27 | 41114 | foreach my $idx (0..$#layers) { | |||||||||||||||||||
| 28 | 41822 | ▶ | next unless defined(my $rule = $layers[$idx]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 1143 | my $layer_name = $self->_rule_name($name, $idx); | |||||||||||||||||||
| 30 | 1143 | my $define = '(?<'.$layer_name.'>'.$rule.')'; | |||||||||||||||||||
| 31 | 1143 | ▶ | $define = '(?<Perl'.$name.'>'.$define.')' if $idx == $#layers; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 1143 | unshift @parts, $define; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | 411 | my $base_re = $self->base_grammar_regexp; | |||||||||||||||||||
| 36 | 411 | ▶ | return $base_re unless @parts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 402 | my $define_block = join "\n", '(?(DEFINE)', '', @parts, '', ')'; | |||||||||||||||||||
| 38 | # This stringify is required for Perl v5.18 - v5.28 | ||||||||||||||||||||
| 39 | # (RT #126285, RT #144248). | ||||||||||||||||||||
| 40 | 402 | my $final_re = "${define_block} ${base_re}"; | |||||||||||||||||||
| 41 | 402 | my $_re; | |||||||||||||||||||
| 42 | return Babble::Config::CACHE_RE ? $COMPILE_CACHE{$final_re} : $_re ||= | ||||||||||||||||||||
| 43 | 402 | ▶ | do { | ||||||||||||||||||
|
Condition66%2/3
$COMPILE_CACHE{$final_re} ||= do {
'???';
my $re = qr/$final_re/x;
$re
}
MC/DC0%0/2
$COMPILE_CACHE{$final_re} ||= do {
'???';
my $re = qr/$final_re/x;
$re
}
$COMPILE_CACHE{$final_re}
do {
'???';
my $re = qr/$final_re/x;
$re
}
Truth table66%2/3
A ||= B
A= $COMPILE_CACHE{$final_re}B= do {
'???';
my $re = qr/$final_re/x;
$re
}
| |||||||||||||||||||||
| 44 | 49 | warn "Cache miss grammar_regexp: ${define_block}\n" if Babble::Config::CACHE_RE && Babble::Config::DEBUG_CACHE_MISS; | |||||||||||||||||||
| 45 | 12 | ▶ | use re 'eval'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 49 | my $re = qr{$final_re}x; | |||||||||||||||||||
| 47 | 12 | ▶ | no re 'eval'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 49 | $re; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 12 | }; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 1741 | ▶ | sub _rule_name { | ||||||||||||||||||
|
Subroutine
_rule_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 53 | 1741 | my ($self, $name, $index) = @_; | |||||||||||||||||||
| 54 | 1741 | ▶ | return 'PerlStd'.$name unless $index; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 712 | return 'PerlWrapper'.$name.'_'.sprintf("%03i", $index); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 294 | ▶ | sub add_rule { | ||||||||||||||||||
|
Subroutine
add_rule: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 59 | 294 | my ($self, $name, $rule) = @_; | |||||||||||||||||||
| 60 | 294 | ▶ | die "Rule ${name} already exists" if exists $self->rules->{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 294 | $self->rules->{$name} = [ $rule ]; | |||||||||||||||||||
| 62 | 294 | return $self; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 10 | ▶ | sub replace_rule { | ||||||||||||||||||
|
Subroutine
replace_rule: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 66 | 10 | my ($self, $name, $rule) = @_; | |||||||||||||||||||
| 67 | 10 | ▶ | die "Rule ${name} does not exist" unless exists $self->rules->{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 10 | $self->rules->{$name} = [ $rule ]; | |||||||||||||||||||
| 69 | 10 | return $self; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 598 | ▶ | sub extend_rule { | ||||||||||||||||||
|
Subroutine
extend_rule: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 73 | 598 | my ($self, $name, $cb) = @_; | |||||||||||||||||||
| 74 | 598 | ▶ | die "Rule ${name} does not exist" unless my $r = $self->rules->{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 598 | my $inner_name = $self->_rule_name($name, $#{$r}); | |||||||||||||||||||
| 76 | 598 | $self->rules->{$name} = [ @$r, $cb->('(?&'.$inner_name.')') ]; | |||||||||||||||||||
| 77 | 598 | return $self; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 281 | ▶ | sub augment_rule { | ||||||||||||||||||
|
Subroutine
augment_rule: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 81 | 281 | my ($self, $name, $extra) = @_; | |||||||||||||||||||
| 82 | 281 | ▶ | $self->extend_rule($name, sub { join '|', $extra, $_[0] }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 83 | 281 | return $self; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 606 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 87 | 606 | my ($self) = @_; | |||||||||||||||||||
| 88 | return ref($self)->new( | ||||||||||||||||||||
| 89 | base_grammar_regexp => $self->base_grammar_regexp, | ||||||||||||||||||||
| 90 | 606 | rules => { %{$self->rules} }, | |||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 7 | ▶ | sub match { | ||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 95 | 7 | my ($self, $as, $text) = @_; | |||||||||||||||||||
| 96 | 7 | require Babble::Match; | |||||||||||||||||||
| 97 | 7 | Babble::Match->new( | |||||||||||||||||||
| 98 | top_rule => $as, | ||||||||||||||||||||
| 99 | text => $text, | ||||||||||||||||||||
| 100 | grammar => $self | ||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 12 | 1; | |||||||||||||||||||