Production/Printer.pm

filter: Statementstmt 95.3% 203 / 213 Branchbran 85.0% 51 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.6% 41 / 42 Podpod 100% 1 / 1 totaltotal 93.6% 296 / 316 CC 32 SCAR 9.6
CC
32
Cov
93%
_run
12.0
_rhsValue
5.0
_condsForOneId
4.0
CRAP 2.6
↑ Summary Production/Parser.pm »
1 #
2 # This file is part of Soar-Production
3 #
4 # This software is copyright (c) 2012 by Nathan Glenn.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Soar::Production::Printer;
10 # ABSTRACT: Print Soar productions
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '0.03'; # VERSION
15  
16 3 ▶use Soar::Production::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 OK => [qw(tree_to_text)]
20 3 );
21  
22 #default behavior is to read the input Soar file and output another one; worthless except for testing
23 3 ▶_run(shift) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 3 3
24  
25 #pass in a Soar grammar file name. Parse the file, then reconstruct it and print to STDOUT.
26 0 ▶sub _run {
Subroutine _run: not called CC=3 SCAR=24.8
27 0 my ($file) = @_;
28  
29 0 my $parser = Soar::Production::Parser->new();
30 0 my $trees = $parser->productions(file => $file, parse => 1);
31 0 ▶ croak "parse failure\n" if ( $#$trees == -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $trees->$#* == -1 0 0 0
32  
33 0 my $text = tree_to_text($$trees[0]);
34 0 ▶ my $tree = $parser->parse_text($text)
Branch: 1 branch
BranchTrueFalseTotal
my $tree = $parser->parse_text($text) or croak("illegal production printed") 0 0 0
35 or croak 'illegal production printed';
36  
37 0 print $text;
38 0 return;
39 }
40  
41 822 ▶sub tree_to_text{
Subroutine tree_to_text: called CC=1 SCAR=0.0
Pod documented
42 822 my ($tree) = @_;
43  
44 #traverse tree and construct the Soar production text
45 822 my $text = 'sp {';
46  
47 822 $text .= _name( $tree->{name} );
48 822 $text .= _doc( $tree->{doc} );
49 822 $text .= _flags( $tree->{flags} );
50  
51 822 $text .= _LHS( $tree->{LHS} );
52 822 $text .= "\n-->\n\t";
53 822 $text .= _RHS( $tree->{RHS} );
54 822 $text .= "\n}";
55  
56 822 return $text;
57 }
58  
59 822 ▶sub _name {
Subroutine _name: called CC=1 SCAR=0.0
60 822 my $name = shift;
61 822 return $name . "\n\t";;
62 }
63  
64 822 ▶sub _doc {
Subroutine _doc: called CC=2 SCAR=6.9
65 822 my $doc = shift;
66 822 ▶ if(defined $doc){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) 48 774 822
67 48 return '"' . $doc . '"' . "\n\t";
68 }
69 774 return '';
70 }
71  
72 822 ▶sub _flags {
Subroutine _flags: called CC=2 SCAR=6.9
73 822 my $flags = shift;
74 822 my $text = '';
75 822 for my $flag (@$flags){
76 104 $text .= ':' . $flag . "\n\t";
77 }
78 822 return $text;
79 }
80  
81 822 ▶sub _LHS {
Subroutine _LHS: called CC=1 SCAR=0.0
82 822 my $LHS = shift;
83 return join "\n\t",
84 822 map { _condition($_) } @{ $LHS->{conditions} };
85 }
86  
87 2271 ▶sub _condition {
Subroutine _condition: called CC=2 SCAR=6.9
88 2271 my $condition = shift;
89 2271 my $text = '';
90  
91 $text .= '-'
92 2271 ▶ if($condition->{negative} eq 'yes');
Branch: 1 branch
BranchTrueFalseTotal
if $condition->{"negative"} eq "yes" 38 2233 2271
93  
94 2271 $text .= _positive_condition( $condition->{condition} );
95  
96 2271 return $text;
97 }
98  
99 2271 ▶sub _positive_condition {
Subroutine _positive_condition: called CC=2 SCAR=6.9
100 2271 my $condition = shift;
101  
102 return _conjunction( $condition->{conjunction} )
103 2271 ▶ if($condition->{conjunction});
Branch: 1 branch
BranchTrueFalseTotal
if $condition->{"conjunction"} 22 2249 2271
104  
105 2249 return _condsForOneId($condition);
106 }
107  
108 2249 ▶sub _condsForOneId {
Subroutine _condsForOneId: called CC=4 SCAR=13.9
109 2249 my $condsForOneId = shift;
110 2249 my $text = '(';
111 my ($type, $idTest, $attrValueTests) =
112 (
113 $condsForOneId->{condType},
114 $condsForOneId->{idTest},
115 $condsForOneId->{attrValueTests}
116 2249 );
117  
118 2249 ▶ $text .= $type
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 841 1408 2249
119 if(defined $type);
120  
121 2249 ▶ $text .= ' ' . _test($idTest)
Branch: 1 branch
BranchTrueFalseTotal
if defined $idTest 2249 0 2249
122 if(defined $idTest);
123  
124 2249 ▶ if($#$attrValueTests != -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($attrValueTests->$#* != -1) 2249 0 2249
125 2249 $text .= ' ';
126 2249 $text .= join ' ', map { _attrValueTests($_) } @$attrValueTests;
127 }
128  
129 2249 $text .= ')';
130 2249 return $text;
131 }
132  
133 12087 ▶sub _test {
Subroutine _test: called CC=2 SCAR=6.9
134 12087 my $test = shift;
135  
136 12087 ▶ if(exists $test->{conjunctiveTest}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test->{"conjunctiveTest"}) 221 11866 12087
137 return _conjunctiveTest(
138 221 $test->{conjunctiveTest} );
139 }
140  
141 11866 return _simpleTest( $test->{simpleTest} );
142 }
143  
144 221 ▶sub _conjunctiveTest {
Subroutine _conjunctiveTest: called CC=1 SCAR=0.0
145 221 my $conjTest = shift;
146 221 my $text = '{';
147 $text .= join ' ',
148 221 map { _simpleTest($_) } @$conjTest;
149 221 $text .= '}';
150 221 return $text;
151 }
152  
153 12364 ▶sub _simpleTest {
Subroutine _simpleTest: called CC=3 SCAR=11.0
154 12364 my $test = shift;
155 return _disjunctionTest($test->{disjunctionTest})
156 12364 ▶ if( exists $test->{disjunctionTest} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $test->{"disjunctionTest"} 83 12281 12364
157 return _relationalTest($test->{relationTest} )
158 12281 ▶ if( exists $test->{relationTest} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $test->{"relationTest"} 330 11951 12281
159 11951 return _singleTest($test);
160 }
161  
162 83 ▶sub _disjunctionTest {
Subroutine _disjunctionTest: called CC=1 SCAR=0.0
163 83 my $test = shift;
164 83 my $text = '<< ';
165 83 $text .= join ' ', map { _constant($_) } @$test;
166 83 $text .= ' >>';
167 83 return $text;
168 }
169  
170 330 ▶sub _relationalTest {
Subroutine _relationalTest: called CC=1 SCAR=0.0
171 330 my $test = shift;
172  
173 330 my $text = _relation( $test->{relation} );
174 330 $text .= ' ';
175 330 $text .= _singleTest( $test->{test} );
176  
177 330 return $text;
178 }
179  
180 330 ▶sub _relation {
Subroutine _relation: called CC=1 SCAR=0.0
181 330 my $relation = shift;
182 330 return $relation;
183 }
184  
185 12281 ▶sub _singleTest {
Subroutine _singleTest: called CC=2 SCAR=6.9
186 12281 my $test = shift;
187 return _variable($test->{variable})
188 12281 ▶ if( exists $test->{variable} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $test->{"variable"} 5744 6537 12281
189 6537 return _constant($test);
190 }
191  
192 4624 ▶sub _attrValueTests {
Subroutine _attrValueTests: called CC=3 SCAR=11.0
193 4624 my $attrValuetests = shift;
194 my ($negative, $attrs, $values) =
195 (
196 $attrValuetests->{negative},
197 $attrValuetests->{attrs},
198 $attrValuetests->{values}
199 4624 );
200 4624 my $text = '';
201 4624 ▶ $text .= '-'
Branch: 1 branch
BranchTrueFalseTotal
if $negative eq "yes" 116 4508 4624
202 if($negative eq 'yes');
203 4624 $text .= _attTest($attrs);
204  
205 4624 ▶ if($#$values != -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($values->$#* != -1) 4518 106 4624
206 4518 $text .= ' ';
207 4518 $text .= join ' ', map { _valueTest($_) } @$values;
208 }
209 4624 return $text;
210 }
211  
212 4624 ▶sub _attTest {
Subroutine _attTest: called CC=1 SCAR=0.0
213 4624 my $attTest = shift;
214 4624 my $text = '^';
215 4624 $text .= join '.', map { _test($_) } @$attTest;
216 4624 return $text;
217 }
218  
219 4732 ▶sub _valueTest {
Subroutine _valueTest: called CC=3 SCAR=11.2
220 4732 my $valueTest = shift;
221 4732 my $text = '';
222  
223 4732 ▶ if(exists $valueTest->{test}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $valueTest->{'test'}) { } 4732 0 4732
224 4732 $text = _test( $valueTest->{test} );
225 }else{
226 #condsForOneId
227 0 $text = _condsForOneId($valueTest->{conds});
228 }
229  
230 $text .= '+'
231 4732 ▶ if($valueTest->{'+'} eq 'yes');
Branch: 1 branch
BranchTrueFalseTotal
if $valueTest->{"+"} eq "yes" 41 4691 4732
232  
233 4732 return $text
234 }
235  
236 22 ▶sub _conjunction {
Subroutine _conjunction: called CC=1 SCAR=0.0
237 22 my $conjunction = shift;
238 22 my $text = '{';
239 22 $text .= join "\n\t", map { _condition($_) } @$conjunction;
240 22 $text .= '}';
241 22 return $text;
242 }
243  
244 822 ▶sub _RHS {
Subroutine _RHS: called CC=2 SCAR=6.9
245 822 my $RHS = shift;
246 822 my $text = '';
247 822 for my $action (@$RHS){
248 2472 $text .= _action($action);
249 2472 $text .= "\n\t";
250 }
251 822 return $text;
252 }
253  
254 2472 ▶sub _action {
Subroutine _action: called CC=2 SCAR=6.9
255 2472 my $action = shift;
256 2472 ▶ if(exists $action->{funcCall}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $action->{"funcCall"}) 230 2242 2472
257 230 return _funcCall($action->{funcCall});
258 }
259  
260 2242 my $text = '(';
261 2242 $text .= _variable($action->{variable});
262 2242 $text .= ' ';
263 $text .= join ' ',
264 2242 map {_attrValueMake($_)} @{ $action->{attrValueMake} };
265 2242 $text .= ')';
266 2242 return $text;
267 }
268  
269 5133 ▶sub _attrValueMake {
Subroutine _attrValueMake: called CC=2 SCAR=6.9
270 5133 my $attrValueMake = shift;
271 my ($attr, $valueMake) =
272 5133 ($attrValueMake->{attr}, $attrValueMake->{valueMake});
273  
274 5133 my $text = _attr($$attr[0]);
275 5133 ▶ if($#$attr != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->$#* != 0) 15 5118 5133
276 15 $text .= '.';
277 $text .= join '.',
278 15 map { _variableOrSymConstant($_) } @$attr[1..$#$attr];
279 }
280  
281 5133 $text .= ' ';
282 5133 $text .= join ' ', map{_valueMake($_)} @$valueMake;
283  
284 5133 return $text;
285 }
286  
287 5133 ▶sub _attr {
Subroutine _attr: called CC=1 SCAR=0.0
288 5133 my $attr = shift;
289 5133 return '^' . _variableOrSymConstant($attr);
290 }
291  
292 5148 ▶sub _variableOrSymConstant {
Subroutine _variableOrSymConstant: called CC=2 SCAR=6.9
293 5148 my $vOs = shift;
294 return _variable($vOs->{variable})
295 5148 ▶ if(exists $vOs->{variable});
Branch: 1 branch
BranchTrueFalseTotal
if exists $vOs->{"variable"} 51 5097 5148
296 5097 return _symConstant($vOs);
297  
298 }
299  
300 6257 ▶sub _valueMake {
Subroutine _valueMake: called CC=1 SCAR=0.0
301 6257 my $valueMake = shift;
302 my ($rhsValue, $preferences) =
303 6257 ($valueMake->{rhsValue}, $valueMake->{preferences});
304 6257 my $text = _rhsValue($rhsValue);
305 #there will always be at least one preference; '+' is default
306 6257 $text .= ' ';
307 6257 $text .= join ',', map { _preference($_) } @$preferences;
308 6257 return $text;
309 }
310  
311 6369 ▶sub _preference {
Subroutine _preference: called CC=2 SCAR=6.9
312 6369 my $preference = shift;
313 6369 my $text = $preference->{value};
314 6369 ▶ if($preference->{type} eq 'binary'){
Branch: 1 branch
BranchTrueFalseTotal
if ($preference->{"type"} eq "binary") 13 6356 6369
315 13 $text .= ' ' . _rhsValue( $preference->{compareTo} );
316 }
317 6369 return $text;
318 }
319  
320 #variable | constant | "(crlf)" | funcCall
321 7514 ▶sub _rhsValue {
Subroutine _rhsValue: called CC=5 SCAR=16.2
322 7514 my $rhsValue = shift;
323  
324 7514 ▶ return '(crlf)'
Branch: 1 branch
BranchTrueFalseTotal
if $rhsValue eq "(crlf)" 195 7319 7514
325 if($rhsValue eq '(crlf)');
326  
327 7319 ▶ if(exists $rhsValue->{variable}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhsValue->{"variable"}) 3605 3714 7319
328 3605 return _variable($rhsValue->{variable});
329 }
330 3714 ▶ if(exists $rhsValue->{constant}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhsValue->{"constant"}) 3609 105 3714
331 3609 return _constant($rhsValue);
332 }
333 105 ▶ if(exists $rhsValue->{function}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhsValue->{"function"}) 105 0 105
334 105 return _funcCall($rhsValue);
335 }
336 0 return $rhsValue;
337 }
338  
339 #(write |Hello World| |hello again|)
340 335 ▶sub _funcCall {
Subroutine _funcCall: called CC=2 SCAR=6.9
341 335 my $funcCall = shift;
342  
343 my ($name, $args) =
344 335 (_funcName($funcCall->{function}), $funcCall->{args});
345 335 my $text = '(' . $name;
346 335 ▶ if($#$args != -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->$#* != -1) 303 32 335
347 303 $text .= ' ';
348 303 $text .= join ' ', map {_rhsValue($_)} @$args;
349 }
350 335 return $text . ')';
351 }
352  
353 # arithmetic operator (+ - * /) or a symConstant, being the name of some function
354 335 ▶sub _funcName {
Subroutine _funcName: called CC=2 SCAR=6.9
355 335 my $funcName = shift;
356  
357 335 ▶ if(ref $funcName eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $funcName eq "HASH") 233 102 335
358 233 return _symConstant($funcName);
359 }
360 102 return $funcName;
361 }
362  
363 11642 ▶sub _variable {
Subroutine _variable: called CC=1 SCAR=0.0
364 11642 my $variable = shift;
365 11642 return '<' . $variable . '>'
366 }
367  
368 10336 ▶sub _constant {
Subroutine _constant: called CC=3 SCAR=11.0
369 10336 my $constant = shift;
370 10336 my ($type, $value) = ($constant->{type}, $constant->{constant});
371  
372 10336 ▶ return _symConstant($value) if($type eq 'sym');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sym" 7882 2454 10336
373 2454 ▶ return _int($value) if($type eq 'int');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "int" 2405 49 2454
374 49 return _float($value);#only other type is 'float'
375 }
376  
377 49 ▶sub _float {
Subroutine _float: called CC=1 SCAR=0.0
378 49 my $float = shift;
379 49 return $float;
380 }
381  
382 2405 ▶sub _int {
Subroutine _int: called CC=1 SCAR=0.0
383 2405 my $int = shift;
384 2405 return $int;
385 }
386  
387 #either string or quoted
388 13212 ▶sub _symConstant {
Subroutine _symConstant: called CC=2 SCAR=6.9
389 13212 my $symConstant = shift;
390 13212 my ($type, $value) = ($symConstant->{type}, $symConstant->{value});
391 13212 ▶ return _string($value) if($type eq 'string');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 12574 638 13212
392 638 return _quoted($value);
393 }
394  
395 12574 ▶sub _string {
Subroutine _string: called CC=1 SCAR=0.0
396 12574 return shift;
397 }
398  
399 638 ▶sub _quoted {
Subroutine _quoted: called CC=1 SCAR=0.0
400 638 my $text = shift;
401  
402 #escape vertical bars
403 638 $text =~ s/\|/\\|/g;
404 638 return '|' . $text . '|';
405 }
406  
407 3 1;
408  
409 __END__
↑ Summary Production/Parser.pm »