R3/Glade.pm

filter: Statementstmt 52.7% 67 / 127 Branchbran 20.0% 6 / 30 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 5 totaltotal 43.5% 87 / 200 CC 22 SCAR 42.0
CC
22
Cov
44%
coro_by_tag
182.0
peak
11.0
glade_common_set
2.1
CRAP 66.7
« R3/SLG.pm ↑ Summary R3/MetaAST.pm »
1 # Marpa::R3 is Copyright (C) 2018, Jeffrey Kegler.
2 #
3 # This module is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl 5.10.1. For more details, see the full text
5 # of the licenses in the directory LICENSES.
6 #
7 # This program is distributed in the hope that it will be
8 # useful, but it is provided "as is" and without any express
9 # or implied warranties. For details, see the full text of
10 # of the licenses in the directory LICENSES.
11  
12 package Marpa::R3::Glade;
13  
14 104 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 104 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 104 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 104 ▶no warnings qw(recursion);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 104 ▶use vars qw($VERSION $STRING_VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 104 $VERSION = '4.001_054';
21 104 $STRING_VERSION = $VERSION;
22 ## no critic(BuiltinFunctions::ProhibitStringyEval)
23 104 $VERSION = eval $VERSION;
24 ## use critic
25  
26 # The code in this file, for now, breaks "the rules". It makes use
27 # of internal methods not documented as part of Libmarpa.
28 # It is intended to create documented Libmarpa methods to underlie
29 # this interface, and rewrite it to use them
30  
31 package Marpa::R3::Internal_Glade;
32  
33 104 ▶use Scalar::Util qw(blessed tainted);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 104 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 104 our $PACKAGE = 'Marpa::R3::Glade';
37  
38 # Set those common args which are at the Perl level.
39 # This is more complicated that it needs to be for the current implementation.
40 # It allows for LHS terminals (implemented in Libmarpa but not allowed by the SLIF).
41 # It also assumes that every or-node which can be constructed from preceding or-nodes
42 # and the input will be present. This is currently the case, but in the future
43 # rules and/or symbols may have extra-syntactic conditions attached making this
44 # assumption false.
45  
46 # Set those common args which are at the Perl level.
47 1 ▶sub glade_common_set {
Subroutine glade_common_set: called CC=2 SCAR=7.2
48 1 my ( $glade, $flat_args ) = @_;
49 1 ▶ if ( my $value = $flat_args->{'trace_file_handle'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $flat_args->{"trace_file_handle"}) 0 1 1
50 0 $glade->[Marpa::R3::Internal_Glade::TRACE_FILE_HANDLE] = $value;
51 }
52 1 my $trace_file_handle =
53 $glade->[Marpa::R3::Internal_Glade::TRACE_FILE_HANDLE];
54 1 delete $flat_args->{'trace_file_handle'};
55 1 return $flat_args;
56 }
57  
58 1 ▶sub Marpa::R3::Internal_Glade::peak {
Subroutine peak: called CC=7 SCAR=23.9
59 1 my ( $asf, @args ) = @_;
60 1 my $glade = bless [], "Marpa::R3::Glade";
61  
62 1 my $end_of_parse;
63  
64 1 my ( $flat_args, $error_message ) = Marpa::R3::flatten_hash_args( \@args );
65 1 ▶ Marpa::R3::exception( sprintf $error_message, '$glade->peak' )
Branch: 1 branch
BranchTrueFalseTotal
unless $flat_args 0 1 1
66 if not $flat_args;
67 1 $flat_args = glade_common_set( $glade, $flat_args );
68  
69 1 my $asf_class = 'Marpa::R3::ASF';
70 1 ▶ if ( not blessed $asf or not $asf->isa($asf_class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($asf) and $asf->isa($asf_class)) 0 1 1
Condition33%1/3
builtin::blessed($asf) and $asf->isa($asf_class)
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($asf) and $asf->isa($asf_class)
builtin::blessed($asf) $asf->isa($asf_class)
Truth table33%1/3
A and B
A= builtin::blessed($asf)B= $asf->isa($asf_class)
ABresult
0X0
100
111
71 0 my $ref_type = ref $asf;
72 0 ▶ my $desc = $ref_type ? "a ref to $ref_type" : 'not a ref';
Branch: 1 branch
BranchTrueFalseTotal
$ref_type ? : 0 0 0
73 0 Marpa::R3::exception(
74 qq{'recognizer' named argument to new() is $desc\n},
75 " It should be a ref to $asf_class\n"
76 );
77 }
78  
79 1 ▶ $glade->[Marpa::R3::Internal_Glade::TRACE_FILE_HANDLE] //=
Condition33%1/3
$glade->[0] //= $asf->[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
$glade->[0] //= $asf->[0]
$glade->[0] $asf->[0]
Truth table33%1/3
A //= B
A= $glade->[0]B= $asf->[0]
ABresult
1X1
011
000
80 $asf->[Marpa::R3::Internal_ASF::TRACE_FILE_HANDLE];
81  
82 1 my $trace_file_handle =
83 $glade->[Marpa::R3::Internal_Glade::TRACE_FILE_HANDLE];
84  
85 1 my $lua = $asf->[Marpa::R3::Internal_ASF::L];
86 1 $glade->[Marpa::R3::Internal_Glade::L] = $lua;
87  
88 my ( $regix ) = $asf->coro_by_tag(
89 ( '@' . __FILE__ . ':' . __LINE__ ),
90 {
91 signature => 's',
92 args => [$flat_args],
93 handlers => {
94 0 ▶ trace => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 my ($msg) = @_;
96 0 say {$trace_file_handle} $msg;
97 0 return 'ok';
98 },
99 }
100 },
101 1 <<'END_OF_LUA');
102 local asf, flat_args = ...
103 _M.wrap(function ()
104 local peak = asf:peak(flat_args)
105 if not peak then return 'ok', -1 end
106 return 'ok', peak.regix
107 end)
108 END_OF_LUA
109  
110 1 ▶ return if $regix < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $regix < 0 0 1 1
111 1 $glade->[Marpa::R3::Internal_Glade::REGIX] = $regix;
112 1 return $glade;
113  
114 }
115  
116 1 ▶sub Marpa::R3::Glade::DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
117 # say STDERR "In Marpa::R3::Glade::DESTROY before test";
118 1 my $glade = shift;
119 1 my $lua = $glade->[Marpa::R3::Internal_Glade::L];
120  
121 # If we are destroying the Perl interpreter, then all the Marpa
122 # objects will be destroyed, including Marpa's Lua interpreter.
123 # We do not need to worry about cleaning up the
124 # recognizer is an orderly manner, because the Lua interpreter
125 # containing the recognizer will be destroyed.
126 # In fact, the Lua interpreter may already have been destroyed,
127 # so this test is necessary to avoid a warning message.
128 1 ▶ return if not $lua;
Branch: 1 branch
BranchTrueFalseTotal
unless $lua 0 1 1
129 # say STDERR "In Marpa::R3::Glade::DESTROY after test";
130  
131 1 my $regix = $glade->[Marpa::R3::Internal_Glade::REGIX];
132 1 $glade->call_by_tag(
133 ('@' . __FILE__ . ':' . __LINE__),
134 <<'END_OF_LUA', '');
135 local glade = ...
136 local regix = glade.regix
137 _M.unregister(_M.registry, regix)
138 END_OF_LUA
139 }
140  
141 # not to be documented
142 2 ▶sub Marpa::R3::Glade::call_by_tag {
Subroutine call_by_tag: called CC=2 SCAR=7.0
Pod undocumented
143 2 my ( $glade, $tag, $codestr, $signature, @args ) = @_;
144 2 my $lua = $glade->[Marpa::R3::Internal_Glade::L];
145 2 my $regix = $glade->[Marpa::R3::Internal_Glade::REGIX];
146  
147 2 my @results;
148 my $eval_error;
149 2 my $eval_ok;
150 {
151 2 local $@;
152 2 $eval_ok = eval {
153 2 @results =
154 $lua->call_by_tag( $regix, $tag, $codestr, $signature, @args );
155 2 return 1;
156 };
157 2 $eval_error = $@;
158 }
159 2 ▶ if ( not $eval_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_ok) 0 2 2
160 0 Marpa::R3::exception($eval_error);
161 }
162 2 return @results;
163 }
164  
165 # not to be documented
166 0 ▶sub Marpa::R3::Glade::coro_by_tag {
Subroutine coro_by_tag: not called CC=13 SCAR=52.0
Pod undocumented
167 0 my ( $glade, $tag, $args, $codestr ) = @_;
168 0 my $lua = $glade->[Marpa::R3::Internal_ASF::L];
169 0 my $regix = $glade->[Marpa::R3::Internal_ASF::REGIX];
170 0 ▶ my $handler = $args->{handlers} // {};
Condition0%0/2
$args->{"handlers"} // {}
l!l
00
MC/DC0%0/1
$args->{"handlers"} // {}
$args->{"handlers"}
Truth table0%0/2
A
A= $args->{"handlers"}
Aresult
11
00
171 0 my $resume_tag = $tag . '[R]';
172 0 ▶ my $signature = $args->{signature} // '';
Condition0%0/2
$args->{"signature"} // ""
l!l
00
MC/DC0%0/1
$args->{"signature"} // ""
$args->{"signature"}
Truth table0%0/2
A
A= $args->{"signature"}
Aresult
11
00
173 0 ▶ my $p_args = $args->{args} // [];
Condition0%0/2
$args->{"args"} // []
l!l
00
MC/DC0%0/1
$args->{"args"} // []
$args->{"args"}
Truth table0%0/2
A
A= $args->{"args"}
Aresult
11
00
174  
175 0 my @results;
176 my $eval_error;
177 0 my $eval_ok;
178 {
179 0 local $@;
180 0 $eval_ok = eval {
181 0 $lua->call_by_tag( $regix, $tag, $codestr, $signature, @{$p_args} );
182 0 my @resume_args = ('');
183 0 my $signature = 's';
184 0 CORO_CALL: while (1) {
185 0 my ( $cmd, $yield_data ) =
186 $lua->call_by_tag( $regix, $resume_tag,
187 'local glade, resume_arg = ...; return _M.resume(resume_arg)',
188 $signature, @resume_args ) ;
189 0 ▶ if (not $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 0 0 0
190 0 @results = @{$yield_data};
191 0 return 1;
192 }
193 0 my $handler = $handler->{$cmd};
194 0 ▶ Marpa::R3::exception(qq{No coro handler for "$cmd"})
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 0 0
195 if not $handler;
196 0 ▶ $yield_data //= [];
Condition0%0/2
$yield_data //= []
l!l
00
MC/DC0%0/1
$yield_data //= []
$yield_data
Truth table0%0/2
A
A= $yield_data
Aresult
11
00
197 0 my ($handler_cmd, $new_resume_args) = $handler->(@{$yield_data});
198 0 ▶ Marpa::R3::exception(qq{Undefined return command from handler for "$cmd"})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handler_cmd 0 0 0
199 if not defined $handler_cmd;
200 0 ▶ if ($handler_cmd eq 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler_cmd eq "ok") 0 0 0
201 0 $signature = 's';
202 0 @resume_args = ($new_resume_args);
203 0 ▶ if (scalar @resume_args < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @resume_args < 1) 0 0 0
204 0 @resume_args = ('');
205 }
206 0 next CORO_CALL;
207 }
208 0 ▶ if ($handler_cmd eq 'sig') {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler_cmd eq "sig") 0 0 0
209 0 @resume_args = @{$new_resume_args};
210 0 $signature = shift @resume_args;
211 0 next CORO_CALL;
212 }
213 0 Marpa::R3::exception(qq{Bad return command ("$handler_cmd") from handler for "$cmd"})
214 }
215 0 return 1;
216 };
217 0 $eval_error = $@;
218 }
219 0 ▶ if ( not $eval_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_ok) 0 0 0
220 # if it's an object, just die
221 0 ▶ die $eval_error if ref $eval_error;
Branch: 1 branch
BranchTrueFalseTotal
if ref $eval_error 0 0 0
222 0 Marpa::R3::exception($eval_error);
223 }
224 0 return @results;
225 }
226  
227 1 ▶sub Marpa::R3::Glade::g1_span {
Subroutine g1_span: called CC=1 SCAR=0.0
Pod undocumented
228 1 my ($glade) = @_;
229  
230 1 my ($g1_start, $g1_length) = $glade->call_by_tag(
231 ('@' . __FILE__ . ':' . __LINE__),
232 <<'END__OF_LUA', '>*' );
233 local glade = ...
234 return glade:g1_span()
235 END__OF_LUA
236 1 return $g1_start, $g1_length;
237 }
238  
239 0 ▶sub Marpa::R3::Glade::dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
240 0 my ($glade) = @_;
241 0 return "";
242 }
243  
244 # not to be documented
245 0 ▶sub Marpa::R3::Glade::regix {
Subroutine regix: not called CC=1 SCAR=6.9
Pod undocumented
246 0 my ( $glade ) = @_;
247 0 my $regix = $glade->[Marpa::R3::Internal_Glade::REGIX];
248 0 return $regix;
249 }
250  
251 104 1;
252  
253 # vim: expandtab shiftwidth=4:
« R3/SLG.pm ↑ Summary R3/MetaAST.pm »