Pegex/Compiler.pm

filter: Statementstmt 87.2% 96 / 110 Branchbran 78.1% 25 / 32 Conditioncond 60.0% 6 / 10 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 88.2% 15 / 17 Podpod 54.5% 6 / 11 totaltotal 80.8% 148 / 183 CC 26 SCAR 16.5
CC
26
Cov
84%
combinate_object
10.4
combinate_re
6.4
perl_regexes
6.0
CRAP 5.2
« Pegex/Regex.pm ↑ Summary Pegex/Receiver.pm »
1 package Pegex::Compiler;
2 17 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 17 ▶use Pegex::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Pegex::Pegex::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Pegex::Pegex::AST;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Pegex::Grammar::Atoms;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 has tree => ();
10  
11 90 ▶sub compile {
Subroutine compile: called CC=2 SCAR=8.0
Pod documented
12 90 my ($self, $grammar, @rules) = @_;
13  
14 # Global request to use the Pegex bootstrap compiler
15 90 ▶ if ($Pegex::Bootstrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pegex::Bootstrap) 0 90 90
16 0 require Pegex::Bootstrap;
17 0 $self = Pegex::Bootstrap->new;
18 }
19  
20 0 @rules = map { s/-/_/g; $_ } @rules;
21  
22 90 $self->parse($grammar);
23 76 $self->combinate(@rules);
24 76 $self->native;
25  
26 76 return $self;
27 }
28  
29 202 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
30 202 my ($self, $input) = @_;
31  
32 202 my $parser = Pegex::Parser->new(
33 grammar => Pegex::Pegex::Grammar->new,
34 receiver => Pegex::Pegex::AST->new,
35 );
36  
37 202 $self->{tree} = $parser->parse($input);
38  
39 188 return $self;
40 }
41  
42 #-----------------------------------------------------------------------------#
43 # Combination
44 #-----------------------------------------------------------------------------#
45 17 has _tree => ();
46  
47 76 ▶sub combinate {
Subroutine combinate: called CC=4 SCAR=14.0
Pod documented
48 76 my ($self, @rule) = @_;
49 76 ▶ if (not @rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rule) 76 0 76
50 76 ▶ if (my $rule = $self->{tree}->{'+toprule'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rule = $self->{'tree'}{'+toprule'}) { } 76 0 76
51 76 @rule = ($rule);
52 }
53 else {
54 0 return $self;
55 }
56 }
57 $self->{_tree} = {
58 76 map {($_, $self->{tree}->{$_})} grep { /^\+/ } keys %{$self->{tree}}
59 };
60 76 for my $rule (@rule) {
61 76 $self->combinate_rule($rule);
62 }
63 76 $self->{tree} = $self->{_tree};
64 76 delete $self->{_tree};
65 76 return $self;
66 }
67  
68 270 ▶sub combinate_rule {
Subroutine combinate_rule: called CC=2 SCAR=6.9
Pod undocumented
69 270 my ($self, $rule) = @_;
70 270 ▶ return if exists $self->{_tree}->{$rule};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_tree"}{$rule} 41 229 270
71  
72 229 my $object = $self->{_tree}->{$rule} = $self->{tree}->{$rule};
73 229 $self->combinate_object($object);
74 }
75  
76 467 ▶sub combinate_object {
Subroutine combinate_object: called CC=10 SCAR=23.4
Pod undocumented
77 467 my ($self, $object) = @_;
78 467 ▶ if (exists $object->{'.rgx'}) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $object->{'.rgx'}) { } 176 291 467
elsif (exists $object->{'.ref'}) { } 197 94 291
elsif (exists $object->{'.any'}) { } 9 85 94
elsif (exists $object->{'.all'}) { } 85 0 85
elsif (exists $object->{'.err'}) { } 0 0 0
79 176 $self->combinate_re($object);
80 }
81 elsif (exists $object->{'.ref'}) {
82 197 my $rule = $object->{'.ref'};
83 197 ▶ if (exists $self->{tree}{$rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'tree'}{$rule}) { } 189 8 197
84 189 $self->combinate_rule($rule);
85 }
86 else {
87 8 ▶ if (my $regex = (Pegex::Grammar::Atoms::atoms)->{$rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = Pegex::Grammar::Atoms::atoms()->{$rule}) 5 3 8
88 5 $self->{tree}{$rule} = { '.rgx' => $regex };
89 5 $self->combinate_rule($rule);
90 }
91 }
92 }
93 elsif (exists $object->{'.any'}) {
94 9 for my $elem (@{$object->{'.any'}}) {
95 18 $self->combinate_object($elem);
96 }
97 }
98 elsif (exists $object->{'.all' }) {
99 85 for my $elem (@{$object->{'.all'}}) {
100 220 $self->combinate_object($elem);
101 }
102 }
103 elsif (exists $object->{'.err' }) {
104 }
105 else {
106 0 require YAML::PP;
107 0 die "Can't combinate:\n" . YAML::PP
108 ->new(schema => ['Core', 'Perl'])
109 ->dump_string($object);
110 }
111 }
112  
113 176 ▶sub combinate_re {
Subroutine combinate_re: called CC=6 SCAR=18.5
Pod undocumented
114 176 my ($self, $regexp) = @_;
115 176 my $atoms = Pegex::Grammar::Atoms->atoms;
116 176 my $re = $regexp->{'.rgx'};
117 176 while (1) {
118 0 $re =~ s[(?<!\\)(~+)]['<ws' . length($1) . '>']ge;
119 269 $re =~ s[<([\w\-]+)>][
120 93 (my $key = $1) =~ s/-/_/g;
121 $self->{tree}->{$key} and (
122 $self->{tree}->{$key}{'.rgx'} or
123 die "'$key' not defined as a single RE"
124 )
125 93 ▶ or $atoms->{$key}
Condition60%6/10
$self->{'tree'}{$key} && ($self->{'tree'}{$key}{'.rgx'} || die("'${key}' not defined as a single RE")) || $atoms->{$key} || die("'${key}' not defined in the grammar")
l!l
930
$self->{"tree"}{$key} and $self->{"tree"}{$key}{".rgx"} || die("'${key}' not defined as a single RE") or $atoms->{$key}
l!l&&r!l&&!r
5880
$self->{"tree"}{$key} and $self->{"tree"}{$key}{".rgx"} || die("'${key}' not defined as a single RE")
!ll&&!rl&&r
8805
$self->{"tree"}{$key}{".rgx"} or die "'${key}' not defined as a single RE"
l!l
50
MC/DC0%0/3
$self->{'tree'}{$key} && ($self->{'tree'}{$key}{'.rgx'} || die("'${key}' not defined as a single RE")) || $atoms->{$key} || die("'${key}' not defined in the grammar")
$self->{"tree"}{$key} $self->{"tree"}{$key}{".rgx"} $atoms->{$key}
Truth table40%2/5
A and B or C
A= $self->{"tree"}{$key}B= $self->{"tree"}{$key}{".rgx"}C= $atoms->{$key}
ABCresult
11X1
0X11
1011
0X00
1000
126 or die "'$key' not defined in the grammar"
127 ]e;
128 269 ▶ last if $re eq $regexp->{'.rgx'};
Branch: 1 branch
BranchTrueFalseTotal
if $re eq $regexp->{".rgx"} 176 93 269
129 93 $regexp->{'.rgx'} = $re;
130 }
131 }
132  
133 #-----------------------------------------------------------------------------#
134 # Compile to native Perl regexes
135 #-----------------------------------------------------------------------------#
136 76 ▶sub native {
Subroutine native: called CC=1 SCAR=0.0
Pod undocumented
137 76 my ($self) = @_;
138 76 $self->perl_regexes($self->{tree});
139 76 return $self;
140 }
141  
142 1019 ▶sub perl_regexes {
Subroutine perl_regexes: called CC=6 SCAR=17.9
Pod undocumented
143 1019 my ($self, $node) = @_;
144 1019 ▶ if (ref($node) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 543 476 1019
elsif (ref $node eq 'ARRAY') { } 94 382 476
145 543 ▶ if (exists $node->{'.rgx'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{'.rgx'}) { } 176 367 543
146 176 my $re = $node->{'.rgx'};
147 176 $node->{'.rgx'} = qr/\G$re/;
148 }
149 else {
150 367 for (keys %$node) {
151 705 $self->perl_regexes($node->{$_});
152 }
153 }
154 }
155 elsif (ref($node) eq 'ARRAY') {
156 94 $self->perl_regexes($_) for @$node;
157 }
158 }
159  
160 #-----------------------------------------------------------------------------#
161 # Serialization formatter methods
162 #-----------------------------------------------------------------------------#
163 0 ▶sub to_yaml {
Subroutine to_yaml: not called CC=1 SCAR=6.9
Pod documented
164 0 require YAML::PP;
165 0 my $self = shift;
166 0 YAML::PP
167 ->new(schema => ['Core', 'Perl'])
168 ->dump_string($self->tree);
169 }
170  
171 0 ▶sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
Pod documented
172 0 require JSON::PP;
173 0 my $self = shift;
174 0 JSON::PP
175 ->new
176 ->utf8
177 ->canonical
178 ->pretty
179 ->encode($self->tree);
180 }
181  
182 2 ▶sub to_perl {
Subroutine to_perl: called CC=2 SCAR=6.9
Pod documented
183 2 my $self = shift;
184 2 require Data::Dumper;
185 17 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
186 2 $Data::Dumper::Terse = 1;
187 2 $Data::Dumper::Indent = 1;
188 2 $Data::Dumper::Sortkeys = 1;
189 2 my $perl = Data::Dumper::Dumper($self->tree);
190 2 $perl =~ s/\?\^u?:/?-xism:/g; # the "u" is perl 5.14-18 equiv of /u
191 2 $perl =~ s!(\.rgx.*?qr/)\(\?-xism:(.*)\)(?=/)!$1$2!g;
192 2 $perl =~ s!/u$!/!gm; # perl 5.20+ put /u, older perls don't understand
193 2 ▶ die "to_perl failed with non compatible regex in:\n$perl"
Branch: 1 branch
BranchTrueFalseTotal
if $perl =~ /\?\^/ 0 2 2
194 if $perl =~ /\?\^/;
195 2 return $perl;
196 }
197  
198 17 1;
« Pegex/Regex.pm ↑ Summary Pegex/Receiver.pm »