blib/lib/Tree/Term.pm

filter: Statementstmt 98.6% 356 / 361 Branchbran 83.1% 133 / 160 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 98.3% 60 / 61 Podpod 63.6% 28 / 44 totaltotal 91.5% 595 / 650 CC 104 SCAR 18.7
CC
104
Cov
93%
reduce
20.0
parseExpression
11.0
flat
11.0
CRAP 6.5
↑ Summary
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/
2 #-------------------------------------------------------------------------------
3 # Create a parse tree from an array of terms representing an expression.
4 # Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2021
5 #-------------------------------------------------------------------------------
6 # podDocumentation
7 package Tree::Term;
8 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our $VERSION = 20210827; # Version
10 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(dump ddx pp);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use feature qw(say state current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #D1 Parse # Create a parse tree from an array of terms representing an expression.
18 1 my $stack = undef; # Stack of lexical items
19 1 my $expression = undef; # Expression being parsed
20 1 my $position = undef; # Position in expression
21 1 our %tested; # Pairs of lexical items (b, a) such that 'b' is observed to follow 'a' in a test.
22 1 our %follows; # Pairs of lexical items (b, a) such that 'b' is observed to follow 'a' in a test without causing a syntax error.
23 1 our %first; # Lexical elements that can come first
24 1 our %last; # Lexical elements that can come last
25  
26 sub new($) #P Create a new term from the indicated number of items on top of the stack
27 309 ▶ {my ($count) = @_; # Number of terms
Subroutine new: called CC=4 SCAR=13.9
Pod documented
28  
29 309 ▶ @$stack >= $count or confess "Stack underflow";
Branch: 1 branch
BranchTrueFalseTotal
@$stack >= $count or confess("Stack underflow") 309 0 309
30  
31 309 my ($operator, @operands) = splice @$stack, -$count; # Remove lexical items from stack
32  
33 309 ▶ my $t = genHash(__PACKAGE__, # Description of a term in the expression.
Branch: 1 branch
BranchTrueFalseTotal
@operands ? : 183 126 309
34 operands => @operands ? [@operands] : undef, # Operands to which the operator will be applied.
35 operator => $operator, # Operator to be applied to one or more operands.
36 up => undef, # Parent term if this is a sub term.
37 );
38  
39 309 $_->up = $t for grep {ref $_} @operands; # Link to parent if possible
40  
41 309 push @$stack, $t; # Save newly created term on the stack
42 }
43  
44 sub LexicalCode($$$$) #P Lexical code definition
45 9 ▶ {my ($letter, $next, $name, $short) = @_; # Letter used to refer to the lexical item, letters of items that can follow this lexical item, descriptive name of lexical item, short name
Subroutine LexicalCode: called CC=1 SCAR=0.0
Pod documented
46 9 genHash(q(Tree::Term::LexicalCode), # Lexical item codes.
47 letter => $letter, # Letter code used to refer to the lexical item.
48 next => $next, # Letters codes of items that can follow this lexical item.
49 name => $name, # Descriptive name of lexical item.
50 short => $short, # Short name of lexical item.
51 );
52 }
53  
54 1 my $LexicalCodes = genHash(q(Tree::Term::Codes), # Lexical item codes.
55 a => LexicalCode('a', 'bpv', q(assignment operator), qq(assign)), # Infix operator with priority 2 binding right to left typically used in an assignment.
56 b => LexicalCode('b', 'bBpsv', q(opening parenthesis), qq(OpenBracket)), # Opening parenthesis.
57 B => LexicalCode('B', 'aBdqs', q(closing parenthesis), qq(CloseBracket)), # Closing parenthesis.
58 d => LexicalCode('d', 'bpv', q(dyadic operator), qq(dyad)), # Infix operator with priority 3 binding left to right typically used in arithmetic.
59 p => LexicalCode('p', 'bpv', q(prefix operator), qq(prefix)), # Monadic prefix operator.
60 q => LexicalCode('q', 'aBdqs', q(suffix operator), qq(suffix)), # Monadic suffix operator.
61 s => LexicalCode('s', 'bBpsv', q(semi-colon), qq(semiColon)), # Infix operator with priority 1 binding left to right typically used to separate statements.
62 t => LexicalCode('t', 'aBdqs', q(term), qq(term)), # A term in the expression.
63 v => LexicalCode('v', 'aBdqs', q(variable), qq(variable)), # A variable in the expression.
64 );
65  
66 1 my $first = 'bpsv'; # First element
67 1 my $last = 'Bqsv'; # Last element
68  
69 sub LexicalStructure() # Return the lexical codes and their relationships in a data structure so this information can be used in other contexts.
70 4 ▶ {genHash(q(Tree::Term::LexicalStructure), # Lexical item codes.
Subroutine LexicalStructure: called CC=1 SCAR=0.0
Pod documented
71 codes => $LexicalCodes, # Code describing each lexical item
72 first => $first, # Lexical items we can start with
73 last => $last, # Lexical items we can end with
74 );
75 }
76  
77 sub type($) #P Type of term
78 3796 ▶ {my ($s) = @_; # Term to test
Subroutine type: called CC=2 SCAR=6.9
Pod documented
79 3796 ▶ return 't' if ref $s; # Term on top of stack
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 216 3580 3796
80 3580 substr($s, 0, 1); # Something other than a term defines its type by its first letter
81 }
82  
83 sub expandElement($) #P Describe a lexical element
84 195 ▶ {my ($e) = @_; # Element to expand
Subroutine expandElement: called CC=1 SCAR=0.0
Pod documented
85 195 my $x = $LexicalCodes->{type $e}->name; # Expansion
86 195 "'$x': $e"
87 }
88  
89 sub expandCodes($) #P Expand a string of codes
90 64 ▶ {my ($e) = @_; # Codes to expand
Subroutine expandCodes: called CC=1 SCAR=0.0
Pod documented
91 64 my @c = map {qq('$_')} sort map {$LexicalCodes->{$_}->name} split //, $e; # Codes for next possible items
92 64 my $c = pop @c;
93 64 my $t = join ', ', @c;
94 64 "$t or $c"
95 }
96  
97 sub expected($) #P String of next possible lexical items
98 60 ▶ {my ($s) = @_; # Lexical item
Subroutine expected: called CC=1 SCAR=0.0
Pod documented
99 60 my $e = expandCodes $LexicalCodes->{type $s}->next; # Codes for next possible items
100 60 "Expected: $e"
101 }
102  
103 sub unexpected($$$) #P Complain about an unexpected element
104 56 ▶ {my ($element, $unexpected, $position) = @_; # Last good element, unexpected element, position
Subroutine unexpected: called CC=2 SCAR=6.9
Pod documented
105 56 my $j = $position + 1;
106 56 my $E = expandElement $unexpected;
107 56 my $X = expected $element;
108  
109 my sub de($) # Extract an error message and die
110 56 ▶ {my ($message) = @_; # Message
Subroutine de: called CC=1 SCAR=0.0
111 56 $message =~ s(\n) ( )gs;
112 56 die "$message\n";
113 }
114  
115 56 ▶ de <<END if ref $element;
Branch: 1 branch
BranchTrueFalseTotal
if ref $element 9 47 56
116 Unexpected $E following term ending at position $j.
117 $X.
118 END
119 47 my $S = expandElement $element;
120 47 de <<END;
121 Unexpected $E following $S at position $j.
122 $X.
123 END
124 }
125  
126 sub syntaxError(@) # Check the syntax of an expression without parsing it. Die with a helpful message if an error occurs. The helpful message will be slightly different from that produced by L<parse> as it cannot contain information from the non existent parse tree.
127 86 ▶ {my (@expression) = @_; # Expression to parse
Subroutine syntaxError: called CC=10 SCAR=23.0
Pod documented
128 86 my @e = @_;
129  
130 86 ▶ return '' unless @e; # An empty string is valid
Branch: 1 branch
BranchTrueFalseTotal
unless @e 0 86 86
131  
132 my sub test($$$) # Test a transition
133 317 ▶ {my ($current, $following, $position) = @_; # Current element, following element, position
Subroutine test: called CC=2 SCAR=6.9
Pod undocumented
134 317 my $n = $LexicalCodes->{type $current}->next; # Elements expected next
135 317 ▶ return if index($n, type $following) > -1; # Transition allowed
Branch: 1 branch
BranchTrueFalseTotal
if index($n, &type($following)) != -1 292 25 317
136 25 unexpected $current, $following, $position - 1; # Complain about the unexpected element
137 }
138  
139 my sub testFirst # Test first transition
140 78 ▶ {return if index($first, type $e[0]) > -1; # Transition allowed
Subroutine testFirst: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if index($first, &type($e[0])) != -1 74 4 78
141 4 my $E = expandElement $e[0];
142 4 my $C = expandCodes $first;
143 4 die <<END;
144 Expression must start with $C, not $E.
145 END
146 }
147  
148 my sub testLast($$) # Test last transition
149 49 ▶ {my ($current, $position) = @_; # Current element, position
Subroutine testLast: called CC=2 SCAR=6.9
150 49 ▶ return if index($last, type $current) > -1; # Transition allowed
Branch: 1 branch
BranchTrueFalseTotal
if index($last, &type($current)) != -1 48 1 49
151 1 my $C = expandElement $current;
152 1 my $E = expected $current;
153 1 die <<END;
154 $E after final $C.
155 END
156 }
157  
158 86 if (1) # Test parentheses
159 86 {my @b;
160 86 for my $i(keys @e) # Each element
161 417 {my $e = $e[$i];
162 417 ▶ if (type($e) eq 'b') # Open
Branch: 2 branches
BranchTrueFalseTotal
if (&type($e) eq 'b') { } 54 363 417
elsif (&type($e) eq 'B') { } 49 314 363
163 54 {push @b, [$i, $e];
164 }
165 elsif (type($e) eq 'B') # Close
166 49 ▶ {if (@b > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@b > 0) { } 47 2 49
167 47 {my ($h, $a) = pop(@b)->@*;
168 47 my $j = $i + 1;
169 47 my $g = $h + 1;
170 47 ▶ die <<END if substr($a, 1) ne substr($e, 1); # Close fails to match
Branch: 1 branch
BranchTrueFalseTotal
if substr($a, 1) ne substr($e, 1) 0 47 47
171 Parenthesis mismatch between $a at position $g and $e at position $j.
172 END
173 }
174 else # No corresponding open
175 2 {my $j = $i + 1;
176 2 ▶ my $E = $i ? expected($e[$i-1]) : testFirst; # What we might have had instead
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 1 1 2
177 1 die <<END;
178 Unexpected closing parenthesis $e at position $j. $E.
179 END
180 }
181 }
182 }
183 84 ▶ if (@b > 0) # Closing parentheses at end
Branch: 1 branch
BranchTrueFalseTotal
if (@b > 0) 7 77 84
184 7 {my ($h, $a) = pop(@b)->@*;
185 7 my $g = $h + 1;
186 7 die <<END;
187 No closing parenthesis matching $a at position $g.
188 END
189 }
190 }
191  
192 77 if (1) # Test transitions
193 77 {testFirst $e[0]; # First transition
194 74 test $e[$_-1], $e[$_], $_+1 for 1..$#e; # Each element beyond the first
195 49 testLast $e[-1], scalar @e; # Final transition
196 }
197 }
198  
199 BEGIN # Generate recognition routines.
200 1 ▶ {for my $t(qw(abdps bst t))
Subroutine BEGIN: called CC=5 SCAR=16.2
201 3 {my $c = <<'END';
202 sub check_XXXX() #P Check that the top of the stack has one of XXXX
203 {$tested {type $$expression[$position]}{type $$expression[$position-1]}++; # Check that one lexical item has been seen to follow after another
204 if (index("XXXX", type($$stack[-1])) > -1) # Check type allowed
205 {$follows{type $$expression[$position]}{type $$expression[$position-1]}++; # Shows that one lexical item can possibly follow after another in some circumstances
206 return 1; # Type allowed
207 }
208 unexpected $$stack[-1], $$expression[$position], $position; # Complain about an unexpected type
209 }
210 END
211 3 $c =~ s(XXXX) ($t)gs;
212 3 ▶ eval $c; $@ and confess "$@\n";
Subroutine check_abdps: called CC=2 SCAR=6.9
Subroutine check_bst: called CC=2 SCAR=6.9
Subroutine check_t: called CC=2 SCAR=6.9
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
$@ and confess("$@\n") 0 3 3
if (index("abdps", &type($stack->[-1])) != -1) 124 9 133
if (index("bst", &type($stack->[-1])) != -1) 125 7 132
if (index("t", &type($stack->[-1])) != -1) 101 15 116
213 }
214  
215 1 for my $t(qw(ads b B bpsv bst d p s v)) # Test various sets of items
216 9 {my $c = <<'END';
217 sub test_XXXX($) #P Check that we have XXXX
218 {my ($item) = @_; # Item to test
219 !ref($item) and index('XXXX', substr($item, 0, 1)) > -1
220 }
221 END
222 9 $c =~ s(XXXX) ($t)gs;
223 0 ▶ eval $c; $@ and confess "$@\n";
Subroutine test_B: called CC=2 SCAR=7.2
Subroutine test_ads: called CC=2 SCAR=7.2
Subroutine test_b: called CC=2 SCAR=7.2
Subroutine test_bpsv: called CC=2 SCAR=7.2
Subroutine test_bst: not called CC=2 SCAR=7.2
Subroutine test_d: called CC=2 SCAR=7.2
Subroutine test_p: called CC=2 SCAR=7.2
Subroutine test_s: called CC=2 SCAR=7.2
Subroutine test_v: called CC=2 SCAR=7.2
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 10 branches
BranchTrueFalseTotal
$@ and confess("$@\n") 0 9 9
not ref $item and index("B", substr($item, 0, 1)) != -1 68 76 144
not ref $item and index("ads", substr($item, 0, 1)) != -1 49 0 49
not ref $item and index("b", substr($item, 0, 1)) != -1 188 30 218
not ref $item and index("bpsv", substr($item, 0, 1)) != -1 86 0 86
not ref $item and index("bst", substr($item, 0, 1)) != -1 0 0 0
not ref $item and index("d", substr($item, 0, 1)) != -1 11 0 11
not ref $item and index("p", substr($item, 0, 1)) != -1 172 12 184
not ref $item and index("s", substr($item, 0, 1)) != -1 139 12 151
not ref $item and index("v", substr($item, 0, 1)) != -1 82 0 82
224 }
225 }
226  
227 sub test_t($) #P Check that we have a term
228 321 ▶ {my ($item) = @_; # Item to test
Subroutine test_t: called CC=1 SCAR=0.0
Pod documented
229 321 ref $item
230 }
231  
232 sub reduce($) #P Reduce the stack at the specified priority
233 322 ▶ {my ($priority) = @_; # Priority
Subroutine reduce: called CC=20 SCAR=30.0
Pod documented
234 #lll "Reduce at $priority: ", scalar(@s), "\n", dump([@s]);
235  
236 322 ▶ if (@$stack >= 3) # term infix-operator term
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack >= 3) 163 159 322
237 163 {my ($l, $d, $r) = ($$stack[-3], $$stack[-2], $$stack[-1]); # Left infix right
238  
239 163 ▶ if (test_t($l)) # Parse out infix operator expression
Branch: 1 branch
BranchTrueFalseTotal
if (&test_t($l)) 71 92 163
240 71 ▶ {if (test_t($r))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_t($r)) 60 11 71
241 60 ▶ {if ($priority == 1 ? test_ads($d) : test_d($d)) # Amount of reduction
Branch: 2 branches
BranchTrueFalseTotal
if ($priority == 1 ? &test_ads($d) : &test_d($d)) 53 7 60
$priority == 1 ? : 49 11 60
242 53 {pop @$stack for 1..3;
243 53 push @$stack, $d, $l, $r;
244 53 new 3;
245 53 return 1;
246 }
247 }
248 }
249  
250 110 ▶ if (test_b($l)) # Parse parenthesized term keeping the opening parenthesis
Branch: 1 branch
BranchTrueFalseTotal
if (&test_b($l)) 64 46 110
251 64 ▶ {if (test_B($r))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_B($r)) 40 24 64
252 40 ▶ {if (test_t($d))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_t($d)) 39 1 40
253 39 {pop @$stack for 1..3;
254 39 push @$stack, "$l$r", $d;
255 39 new 2;
256 39 return 1;
257 }
258 }
259 }
260 }
261  
262 230 ▶ if (@$stack >= 2) # Convert an empty pair of parentheses to an empty term
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack >= 2) 108 122 230
263 108 {my ($l, $r) = ($$stack[-2], $$stack[-1]);
264 108 ▶ if (test_b($l)) # Empty pair of parentheses
Branch: 1 branch
BranchTrueFalseTotal
if (&test_b($l)) 67 41 108
265 67 ▶ {if (test_B($r))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_B($r)) 5 62 67
266 5 {pop @$stack for 1..2;
267 5 push @$stack, "$l$r";
268 5 new 1;
269 5 return 1;
270 }
271 }
272 103 ▶ if (test_s($l)) # Semi-colon, close implies remove unneeded semi
Branch: 1 branch
BranchTrueFalseTotal
if (&test_s($l)) 13 90 103
273 13 ▶ {if (test_B($r))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_B($r)) 11 2 13
274 11 {pop @$stack for 1..2;
275 11 push @$stack, $r;
276 11 return 1;
277 }
278 }
279 92 ▶ if (test_p($l)) # Prefix, term
Branch: 1 branch
BranchTrueFalseTotal
if (&test_p($l)) 11 81 92
280 11 ▶ {if (test_t($r))
Branch: 1 branch
BranchTrueFalseTotal
if (&test_t($r)) 11 0 11
281 11 {new 2;
282 11 return 1;
283 }
284 }
285 }
286  
287 0 # No move made
288 203 }
289  
290 sub reduce1() #P Reduce the stack at priority 1
291 291 ▶ {reduce 1;
Subroutine reduce1: called CC=1 SCAR=0.0
Pod documented
292 }
293  
294 sub reduce2() #P Reduce the stack at priority 2
295 31 ▶ {reduce 2;
Subroutine reduce2: called CC=1 SCAR=0.0
Pod documented
296 }
297  
298 sub pushElement() #P Push an element
299 306 ▶ {push @$stack, $$expression[$position];
Subroutine pushElement: called CC=1 SCAR=0.0
Pod documented
300 }
301  
302 sub accept_a() #P Assign
303 32 ▶ {check_t;
Subroutine accept_a: called CC=2 SCAR=6.9
Pod documented
304 27 ▶ 1 while reduce2;
Branch: 1 branch
BranchTrueFalseTotal
&reduce2() and do { '???' } 4 27 31
305 27 pushElement;
306 }
307  
308 sub accept_b() #P Open
309 27 ▶ {check_abdps;
Subroutine accept_b: called CC=1 SCAR=0.0
Pod documented
310 24 pushElement;
311 }
312  
313 sub accept_B() #P Closing parenthesis
314 48 ▶ {check_bst;
Subroutine accept_B: called CC=3 SCAR=11.0
Pod documented
315 45 ▶ 1 while reduce1;
Branch: 1 branch
BranchTrueFalseTotal
&reduce1() and do { '???' } 7 45 52
316 45 pushElement;
317 45 ▶ 1 while reduce1;
Branch: 1 branch
BranchTrueFalseTotal
&reduce1() and do { '???' } 81 45 126
318 45 check_bst;
319 }
320  
321 sub accept_d() #P Infix but not assign or semi-colon
322 28 ▶ {check_t;
Subroutine accept_d: called CC=1 SCAR=0.0
Pod documented
323 23 pushElement;
324 }
325  
326 sub accept_p() #P Prefix
327 35 ▶ {check_abdps;
Subroutine accept_p: called CC=1 SCAR=0.0
Pod documented
328 32 pushElement;
329 }
330  
331 sub accept_q() #P Post fix
332 56 ▶ {check_t;
Subroutine accept_q: called CC=1 SCAR=0.0
Pod documented
333 51 my $p = pop @$stack;
334 51 pushElement;
335 51 push @$stack, $p;
336 51 new 2;
337 }
338  
339 sub accept_s() #P Semi colon
340 39 ▶ {check_bst;
Subroutine accept_s: called CC=3 SCAR=11.0
Pod documented
341 36 ▶ if (!test_t($$stack[-1])) # Insert an empty element between two consecutive semicolons
Branch: 1 branch
BranchTrueFalseTotal
unless (&test_t($stack->[-1])) 12 24 36
342 12 {push @$stack, 'empty1';
343 12 new 1;
344 }
345 36 ▶ 1 while reduce1;
Branch: 1 branch
BranchTrueFalseTotal
&reduce1() and do { '???' } 15 36 51
346 36 pushElement;
347 }
348  
349 sub accept_v() #P Variable
350 71 ▶ {check_abdps;
Subroutine accept_v: called CC=3 SCAR=11.0
Pod documented
351 68 pushElement;
352 68 new 1;
353 68 ▶ new 2 while @$stack >= 2 and test_p($$stack[-2]); # Check for preceding prefix operators
Branch: 1 branch
BranchTrueFalseTotal
@$stack >= 2 and &test_p($stack->[-2]) and do { &new(2) } 29 68 97
Condition100%3/3
@$stack >= 2 and &test_p($stack->[-2])
!ll&&!rl&&r
56329
MC/DC100%2/2
@$stack >= 2 and &test_p($stack->[-2])
@$stack >= 2 &test_p($stack->[-2])
Truth table100%3/3
A and B
A= @$stack >= 2B= &test_p($stack->[-2])
ABresult
0X0
100
111
354 }
355 # Action on each lexical item
356 1 my $Accept = # Dispatch the action associated with the lexical item
357 {a => \&accept_a, # Assign
358 b => \&accept_b, # Open
359 B => \&accept_B, # Closing parenthesis
360 d => \&accept_d, # Infix but not assign or semi-colon
361 p => \&accept_p, # Prefix
362 q => \&accept_q, # Post fix
363 s => \&accept_s, # Semi colon
364 v => \&accept_v, # Variable
365 };
366  
367 sub parseExpression() #P Parse an expression.
368 86 ▶ {if (@$expression)
Subroutine parseExpression: called CC=11 SCAR=24.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@$expression) { } 86 0 86
369 86 {my $e = $$expression[$position = 0];
370  
371 86 my $E = expandElement $e;
372 86 ▶ die <<END =~ s(\n) ( )gsr =~ s(\s+\Z) (\n)gsr if !test_bpsv($e);
Branch: 1 branch
BranchTrueFalseTotal
unless &test_bpsv($e) 4 82 86
373 Expression must start with 'opening parenthesis', 'prefix
374 operator', 'semi-colon' or 'variable', not $E.
375 END
376 82 ▶ if (test_v($e)) # Single variable
Branch: 1 branch
BranchTrueFalseTotal
if (&test_v($e)) { } 34 48 82
377 34 {push @$stack, $e;
378 34 new 1;
379 }
380 else
381 48 ▶ {if (test_s($e)) # Semi
Branch: 1 branch
BranchTrueFalseTotal
if (&test_s($e)) 7 41 48
382 7 {push @$stack, 'empty2';
383 7 new 1;
384 }
385 48 push @$stack, $e;
386 }
387 }
388 else # Empty expression
389 0 {return undef;
390 }
391  
392 82 for(1..$#$expression) # Each input element
393 336 {$$Accept{substr($$expression[$position = $_], 0, 1)}(); # Dispatch the action associated with the lexical item
394 }
395  
396 51 ▶ if (index($last, type $$expression[-1]) == -1) # Check for incomplete expression
Branch: 1 branch
BranchTrueFalseTotal
if (index($last, &type($expression->[-1])) == -1) 1 50 51
397 1 {my $C = expandElement $$expression[-1];
398 1 my $E = expected $$expression[-1];
399 1 die <<END;
400 $E after final $C.
401 END
402 }
403  
404 50 ▶ pop @$stack while @$stack > 1 and $$stack[-1] =~ m(s); # Remove any trailing semi colons
Branch: 1 branch
BranchTrueFalseTotal
@$stack > 1 and $stack->[-1] =~ /s/u and do { pop @$stack } 16 50 66
Condition100%3/3
@$stack > 1 and $stack->[-1] =~ /s/u
!ll&&!rl&&r
391116
MC/DC100%2/2
@$stack > 1 and $stack->[-1] =~ /s/u
@$stack > 1 $stack->[-1] =~ /s/u
Truth table100%3/3
A and B
A= @$stack > 1B= $stack->[-1] =~ /s/u
ABresult
0X0
100
111
405 50 ▶ 1 while reduce1; # Final reductions
Branch: 1 branch
BranchTrueFalseTotal
&reduce1() and do { '???' } 12 50 62
406  
407 50 ▶ if (@$stack != 1) # Incomplete expression
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack != 1) 1 49 50
408 1 {my $E = expected $$expression[-1];
409 1 die "Incomplete expression. $E.\n";
410 }
411  
412 49 $first{type $$expression[ 0]}++; # Capture valid first and last lexical elements
413 49 $last {type $$expression[-1]}++;
414  
415 49 $$stack[0] # The resulting parse tree
416 } # parseExpression
417  
418 sub parse(@) # Parse an expression.
419 86 ▶ {my (@expression) = @_; # Expression to parse
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
420 86 my $s = $stack;
421 86 $stack = []; # Clear the current stack - the things we do to speed things up.
422 86 my $x = $expression;
423 86 $expression = \@expression; # Clear the current expression
424 86 my $p = $position;
425 86 $position = 0; # Clear the current parse position
426  
427 86 my $e = eval {parseExpression};
428 86 my $r = $@; # Save any error message
429 86 $stack = $s; $expression = $x; $position = $p; # Restore the stack and expression being parsed
430 86 ▶ die $r if $r; # Die again if we died the last time
Branch: 1 branch
BranchTrueFalseTotal
if $r 37 49 86
431  
432 49 $e # Otherwise return the parse tree
433 } # parse
434  
435 #D1 Validate # Validating is the same as parsing except we do not start at the beginning, instead we start at any lexical element and proceed a few steps from there.
436  
437 sub validPair($$) # Confirm that the specified pair of lexical elements can occur as a sequence.
438 4 ▶ {my ($A, $B) = @_; # First element, second element
Subroutine validPair: called CC=3 SCAR=11.0
Pod documented
439 4 my $a = type $A;
440 4 my $b = type $B;
441 4 ▶ if (my $l = $$LexicalCodes{$a})
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $LexicalCodes->{$a}) 4 0 4
442 4 ▶ {return 1 if (index $l->next, $b) > -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($l->next, $b) != -1 3 1 4
443 }
444 undef
445 1 }
446  
447 #D1 Print # Print a parse tree to make it easy to visualize its structure.
448  
449 sub depth($) #P Depth of a term in an expression.
450 558 ▶ {my ($term) = @_; # Term
Subroutine depth: called CC=2 SCAR=6.9
Pod documented
451 558 my $d = 0;
452 558 ▶ for(my $t = $term; $t; $t = $t->up) {++$d}
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 2216 558 2774
453 558 $d
454 }
455  
456 sub listTerms($) #P List the terms in an expression in post order
457 47 ▶ {my ($expression) = @_; # Root term
Subroutine listTerms: called CC=1 SCAR=0.0
Pod documented
458 47 my @t; # Terms
459  
460 sub # Recurse through terms
461 279 ▶ {my ($e) = @_; # Term
Subroutine __ANON__: called CC=5 SCAR=16.1
462 279 my $o = $e->operands;
463 279 ▶ return unless $e; # Operator
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 279 279
464 279 ▶ if (my @o = $o ? grep {ref $_} @$o : ()) # Operands
Branch: 2 branches
BranchTrueFalseTotal
if (my(@o) = $o ? grep({ref $_;} @$o) : ()) { } 179 100 279
$o ? : 179 100 279
465 179 {my ($p, @p) = @o;
466 179 __SUB__->($p); # First operand
467 179 push @t, $e; # Operator
468 179 __SUB__->($_) for @p; # Second and subsequent operands
469 }
470 else # No operands
471 100 {push @t, $e; # Operator
472 }
473 47 } ->($expression);
474  
475 @t
476 47 }
477  
478 sub flat($@) # Print the terms in the expression as a tree from left right to make it easier to visualize the structure of the tree.
479 47 ▶ {my ($expression, @title) = @_; # Root term, optional title
Subroutine flat: called CC=11 SCAR=24.0
Pod documented
480 47 my @t = $expression->listTerms; # Terms in expression in post order
481 47 my @s; # Print
482  
483 my sub align # Align the ends of the lines
484 161 ▶ {my $L = 0; # Longest line
Subroutine align: called CC=5 SCAR=16.1
485 161 for my $s(@s)
486 1079 ▶ {my $l = length $s; $L = $l if $l > $L;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $L 274 805 1079
487 }
488  
489 161 for my $i(keys @s) # Pad to longest
490 1079 {my $s = $s[$i] =~ s/\s+\Z//rs;
491 1079 my $l = length($s);
492 1079 ▶ if ($l < $L)
Branch: 1 branch
BranchTrueFalseTotal
if ($l < $L) 850 229 1079
493 850 {my $p = ' ' x ($L - $l);
494 850 $s[$i] = $s . $p;
495 }
496 }
497 };
498  
499 47 for my $t(@t) # Initialize output rectangle
500 279 ▶ {$s[$_] //= '' for 0..$t->depth;
Condition100%2/2
$s[$_] //= ""
l!l
1147240
MC/DC100%1/1
$s[$_] //= ""
$s[$_]
Truth table100%2/2
A
A= $s[$_]
Aresult
11
00
501 }
502  
503 47 for my $t(@t) # Traverse tree
504 279 {my $d = $t->depth;
505 279 my $p = $t->operator; # Operator
506 279 my $P = $p =~ s(\A\w+?_) ()gsr; # Remove leading type character if followed by underscore as this make for clearer results
507  
508 279 ▶ align if $p =~ m(\A(a|d|s)); # Shift over for some components
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ /\A(a|d|s)/u 53 226 279
509  
510 279 $s[$d] .= " $P"; # Describe operator or operand with type component removed if requested
511 279 ▶ align if $p !~ m(\A(p|q|v)); # Vertical for some components
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /\A(p|q|v)/u 108 171 279
512 }
513  
514 47 ▶ shift @s while @s and $s[ 0] =~ m(\A\s*\Z)s; # Remove leading blank lines
Branch: 1 branch
BranchTrueFalseTotal
@s and $s[0] =~ /\A\s*\Z/su and do { shift @s } 47 47 94
Condition66%2/3
@s and $s[0] =~ /\A\s*\Z/su
!ll&&!rl&&r
04747
MC/DC50%1/2
@s and $s[0] =~ /\A\s*\Z/su
@s $s[0] =~ /\A\s*\Z/su
Truth table66%2/3
A and B
A= @sB= $s[0] =~ /\A\s*\Z/su
ABresult
0X0
100
111
515  
516 47 for(@s) # Clean up trailing blanks so that tests are not affected by spurious white space mismatches
517 193 {s/\s+\n/\n/gs; s/\s+\Z//gs;
518 }
519  
520 47 ▶ unshift @s, join(' ', @title) if @title; # Add title
Branch: 1 branch
BranchTrueFalseTotal
if @title 0 47 47
521  
522 47 join "\n", @s, '';
523 }
524  
525 #D
526 #-------------------------------------------------------------------------------
527 # Export - eeee
528 #-------------------------------------------------------------------------------
529  
530 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
531  
532 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
533  
534 1 @ISA = qw(Exporter);
535 1 @EXPORT = qw();
536 1 @EXPORT_OK = qw(
537 );
538 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
539  
540 # podDocumentation
541 =pod
542  
543 =encoding utf-8
544  
545 =head1 Name
546  
547 Tree::Term - Create a parse tree from an array of terms representing an expression.
548  
549 =head1 Synopsis
550  
551 The expression to L<parse> is presented as an array of words, the first letter
552 of each word indicates its lexical role as in:
553  
554 my @e = qw(
555  
556 v_sub a_is v_array as
557 v1 d_== v2 a_then v3 d_plus v4 a_else
558 v5 d_== v6 a_then v7 d_minus v8 a_else v9 d_times b v10 a_+ v11 B);
559  
560 Where:
561  
562 a assign - infix operator with priority 2 binding right to left
563 b open - open parenthesis
564 B close - close parenthesis
565 d dyad - infix operator with priority 3 binding left to right
566 p prefix - monadic prefix operator
567 q suffix - monadic suffix operator
568 s semi-colon - infix operator with priority 1 binding left to right
569 v variable - a variable in the expression
570  
571 The results of parsing the expression can be printed with L<flat> which
572 provides a left to right representation of the parse tree.
573  
574 is_deeply parse(@e)->flat, <<END;
575 is
576 sub as
577 array then
578 == else
579 v1 v2 plus then
580 v3 v4 == else
581 v5 v6 minus times
582 v7 v8 v9 +
583 v10 v11
584 END
585  
586 =head1 Description
587  
588 Create a parse tree from an array of terms representing an expression.
589  
590  
591 Version 20210724.
592  
593  
594 The following sections describe the methods in each functional area of this
595 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
596  
597  
598  
599 =head1 Parse
600  
601 Create a parse tree from an array of terms representing an expression.
602  
603 =head2 LexicalStructure()
604  
605 Return the lexical codes and their relationships in a data structure so this information can be used in other contexts.
606  
607  
608 B<Example:>
609  
610  
611  
612 is_deeply LexicalStructure, # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
613  
614  
615  
616 =head2 syntaxError(@expression)
617  
618 Check the syntax of an expression without parsing it. Die with a helpful message if an error occurs. The helpful message will be slightly different from that produced by L<parse|https://en.wikipedia.org/wiki/Parsing> as it cannot contain information from the non existent parse tree.
619  
620 Parameter Description
621 1 @expression Expression to parse
622  
623 B<Example:>
624  
625  
626 if (1)
627  
628 {eval {syntaxError(qw(v1 p1))}; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
629  
630 ok -1 < index $@, <<END =~ s({a}) ( )gsr;
631 Unexpected 'prefix operator': p1 following 'variable': v1 at position 2.
632 Expected: 'assignment operator', 'closing parenthesis',
633 'dyadic operator', 'semi-colon' or 'suffix operator'.
634 END
635 }
636  
637  
638 =head2 parse(@expression)
639  
640 Parse an expression.
641  
642 Parameter Description
643 1 @expression Expression to parse
644  
645 B<Example:>
646  
647  
648 ok T [qw(v_sub a_is v_array as v1 d_== v2 a_then v3 d_plus v4 a_else v5 d_== v6 a_then v7 d_minus v8 a_else v9 d_times b v10 a_+ v11 B)], <<END;
649 is
650 sub as
651 array then
652 == else
653 v1 v2 plus then
654 v3 v4 == else
655 v5 v6 minus times
656 v7 v8 v9 +
657 v10 v11
658 END
659 }
660  
661 if (1) {
662 ok validPair('B', 'd');
663 ok validPair('b', 'B');
664 ok validPair('v', 'a');
665 ok !validPair('v', 'v');
666  
667  
668 =head1 Validate
669  
670 Validating is the same as parsing except we do not start at the beginning, instead we start at any lexical element and proceed a few steps from there.
671  
672 =head2 validPair($A, $B)
673  
674 Confirm that the specified pair of lexical elements can occur as a sequence.
675  
676 Parameter Description
677 1 $A First element
678 2 $B Second element
679  
680 B<Example:>
681  
682  
683  
684 ok validPair('B', 'd'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
685  
686  
687 ok validPair('b', 'B'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
688  
689  
690 ok validPair('v', 'a'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
691  
692  
693 ok !validPair('v', 'v'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
694  
695  
696  
697 =head1 Print
698  
699 Print a parse tree to make it easy to visualize its structure.
700  
701 =head2 flat($expression, @title)
702  
703 Print the terms in the expression as a tree from left right to make it easier to visualize the structure of the tree.
704  
705 Parameter Description
706 1 $expression Root term
707 2 @title Optional title
708  
709 B<Example:>
710  
711  
712  
713 my @e = qw(v1 a2 v3 d4 v5 s6 v8 a9 v10);
714  
715  
716 is_deeply parse(@e)->flat, <<END; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
717  
718 s6
719 a2 a9
720 v1 d4 v8 v10
721 v3 v5
722 END
723 }
724  
725 ok T [qw(v1 a2 v3 s s s v4 a5 v6 s s)], <<END;
726 s
727 s empty1
728 s a5
729 s empty1 v4 v6
730 a2 empty1
731 v1 v3
732 END
733  
734 ok T [qw(b B)], <<END;
735 empty1
736 END
737  
738 ok T [qw(b b B B)], <<END;
739 empty1
740 END
741  
742 ok T [qw(b b v1 B B)], <<END;
743 v1
744 END
745  
746 ok T [qw(b b v1 a2 v3 B B)], <<END;
747 a2
748 v1 v3
749 END
750  
751 ok T [qw(b b v1 a2 v3 d4 v5 B B)], <<END;
752 a2
753 v1 d4
754 v3 v5
755 END
756  
757 ok T [qw(p1 v1)], <<END;
758 p1
759 v1
760 END
761  
762 ok T [qw(p2 p1 v1)], <<END;
763 p2
764 p1
765 v1
766 END
767  
768 ok T [qw(v1 q1)], <<END;
769 q1
770 v1
771 END
772  
773 ok T [qw(v1 q1 q2)], <<END;
774 q2
775 q1
776 v1
777 END
778  
779 ok T [qw(p2 p1 v1 q1 q2)], <<END;
780 q2
781 q1
782 p2
783 p1
784 v1
785 END
786  
787 ok T [qw(p2 p1 v1 q1 q2 d3 p4 p3 v2 q3 q4)], <<END;
788 d3
789 q2 q4
790 q1 q3
791 p2 p4
792 p1 p3
793 v1 v2
794 END
795  
796 ok T [qw(p2 p1 v1 q1 q2 d3 p4 p3 v2 q3 q4 d4 p6 p5 v3 q5 q6 s)], <<END;
797 d3
798 q2 d4
799 q1 q4 q6
800 p2 q3 q5
801 p1 p4 p6
802 v1 p3 p5
803 v2 v3
804 END
805  
806 ok T [qw(b s B)], <<END;
807 empty1
808 END
809  
810 ok T [qw(b s s B)], <<END;
811 s
812 empty1 empty1
813 END
814  
815  
816 if (1) {
817  
818 my @e = qw(b b p2 p1 v1 q1 q2 B d3 b p4 p3 v2 q3 q4 d4 p6 p5 v3 q5 q6 B s B s);
819  
820  
821 is_deeply parse(@e)->flat, <<END; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
822  
823 d3
824 q2 d4
825 q1 q4 q6
826 p2 q3 q5
827 p1 p4 p6
828 v1 p3 p5
829 v2 v3
830 END
831  
832 }
833  
834 ok T [qw(b b v1 B s B s)], <<END;
835 v1
836 END
837  
838 ok T [qw(v1 q1 s)], <<END;
839 q1
840 v1
841 END
842  
843 ok T [qw(b b v1 q1 q2 B q3 q4 s B q5 q6 s)], <<END;
844 q6
845 q5
846 q4
847 q3
848 q2
849 q1
850 v1
851 END
852  
853 ok T [qw(p1 p2 b v1 B)], <<END;
854 p1
855 p2
856 v1
857 END
858  
859 ok T [qw(v1 d1 p1 p2 v2)], <<END;
860 d1
861 v1 p1
862 p2
863 v2
864 END
865  
866 ok T [qw(p1 p2 b p3 p4 b p5 p6 v1 d1 v2 q1 q2 B q3 q4 s B q5 q6 s)], <<END;
867 q6
868 q5
869 p1
870 p2
871 q4
872 q3
873 p3
874 p4
875 d1
876 p5 q2
877 p6 q1
878 v1 v2
879 END
880  
881 ok T [qw(p1 p2 b p3 p4 b p5 p6 v1 a1 v2 q1 q2 B q3 q4 s B q5 q6 s)], <<END;
882 q6
883 q5
884 p1
885 p2
886 q4
887 q3
888 p3
889 p4
890 a1
891 p5 q2
892 p6 q1
893 v1 v2
894 END
895  
896 ok T [qw(b v1 B d1 b v2 B)], <<END;
897 d1
898 v1 v2
899 END
900  
901 ok T [qw(b v1 B q1 q2 d1 b v2 B)], <<END;
902 d1
903 q2 v2
904 q1
905 v1
906 END
907  
908 ok T [qw(v1 s)], <<END;
909 v1
910 END
911  
912 ok T [qw(v1 s s)], <<END;
913 s
914 v1 empty1
915 END
916  
917 ok T [qw(v1 s b s B)], <<END;
918 s
919 v1 empty1
920 END
921  
922 ok T [qw(v1 s b b s s B B)], <<END;
923 s
924 v1 s
925 empty1 empty1
926 END
927  
928 ok T [qw(b v1 s B s s)], <<END;
929 s
930 v1 empty1
931 END
932  
933 ok T [qw(v1 a b1 b2 v2 B2 B1 s)], <<END;
934 a
935 v1 v2
936 END
937  
938 ok T [qw(v1 a1 b1 v2 a2 b2 v3 B2 B1 s)], <<END;
939 a1
940 v1 a2
941 v2 v3
942 END
943  
944 ok T [qw(v1 a1 p1 v2)], <<END;
945 a1
946 v1 p1
947 v2
948 END
949  
950 ok T [qw(b1 v1 q1 q2 B1)], <<END;
951 q2
952 q1
953 v1
954 END
955  
956 ok T [qw(b1 v1 q1 q2 s B1)], <<END;
957 q2
958 q1
959 v1
960 END
961  
962 ok T [qw(p1 b1 v1 B1 q1)], <<END;
963 q1
964 p1
965 v1
966 END
967  
968 ok T [qw(b1 v1 B1 a1 v2)], <<END;
969 a1
970 v1 v2
971 END
972  
973 ok T [qw(v1 q1 a1 v2)], <<END;
974 a1
975 q1 v2
976 v1
977 END
978  
979 ok T [qw(s1 p1 v1)], <<END;
980 s1
981 empty2 p1
982 v1
983 END
984  
985 ok E <<END;
986 a
987 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'assignment operator': a.
988 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'assignment operator': a.
989 END
990  
991 ok E <<END;
992 B
993 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'closing parenthesis': B.
994 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'closing parenthesis': B.
995 END
996  
997 ok E <<END;
998 d1
999 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'dyadic operator': d1.
1000 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'dyadic operator': d1.
1001 END
1002  
1003 ok E <<END;
1004 p1
1005 Expected: 'opening parenthesis', 'prefix operator' or 'variable' after final 'prefix operator': p1.
1006 Expected: 'opening parenthesis', 'prefix operator' or 'variable' after final 'prefix operator': p1.
1007 END
1008  
1009 ok E <<END;
1010 q1
1011 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'suffix operator': q1.
1012 Expression must start with 'opening parenthesis', 'prefix operator', 'semi-colon' or 'variable', not 'suffix operator': q1.
1013 END
1014  
1015 ok E <<END;
1016 s
1017  
1018  
1019 END
1020  
1021 ok E <<END;
1022 v1
1023  
1024  
1025 END
1026  
1027 ok E <<END;
1028 b v1
1029 Incomplete expression. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1030 No closing parenthesis matching b at position 1.
1031 END
1032  
1033 ok E <<END;
1034 b v1 B B
1035 Unexpected 'closing parenthesis': B following 'closing parenthesis': B at position 4. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1036 Unexpected closing parenthesis B at position 4. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1037 END
1038  
1039 ok E <<END;
1040 v1 d1 d2 v2
1041 Unexpected 'dyadic operator': d2 following 'dyadic operator': d1 at position 3. Expected: 'opening parenthesis', 'prefix operator' or 'variable'.
1042 Unexpected 'dyadic operator': d2 following 'dyadic operator': d1 at position 3. Expected: 'opening parenthesis', 'prefix operator' or 'variable'.
1043 END
1044  
1045 ok E <<END;
1046 v1 p1
1047 Unexpected 'prefix operator': p1 following term ending at position 2. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1048 Unexpected 'prefix operator': p1 following 'variable': v1 at position 2. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1049 END
1050  
1051 ok E <<END;
1052 b1 B1 v1
1053 Unexpected 'variable': v1 following term ending at position 3. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1054 Unexpected 'variable': v1 following 'closing parenthesis': B1 at position 3. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1055 END
1056  
1057 ok E <<END;
1058 b1 B1 p1 v1
1059 Unexpected 'prefix operator': p1 following term ending at position 3. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1060 Unexpected 'prefix operator': p1 following 'closing parenthesis': B1 at position 3. Expected: 'assignment operator', 'closing parenthesis', 'dyadic operator', 'semi-colon' or 'suffix operator'.
1061 END
1062  
1063 if (1)
1064 {eval {syntaxError(qw(v1 p1))};
1065 ok -1 < index $@, <<END =~ s({a}) ( )gsr;
1066 Unexpected 'prefix operator': p1 following 'variable': v1 at position 2.
1067 Expected: 'assignment operator', 'closing parenthesis',
1068 'dyadic operator', 'semi-colon' or 'suffix operator'.
1069 END
1070  
1071  
1072  
1073 =head1 Hash Definitions
1074  
1075  
1076  
1077  
1078 =head2 Tree::Term Definition
1079  
1080  
1081 Description of a term in the expression.
1082  
1083  
1084  
1085  
1086 =head3 Output fields
1087  
1088  
1089 =head4 operands
1090  
1091 Operands to which the operator will be applied.
1092  
1093 =head4 operator
1094  
1095 Operator to be applied to one or more operands.
1096  
1097 =head4 up
1098  
1099 Parent term if this is a sub term.
1100  
1101  
1102  
1103 =head2 Tree::Term::Codes Definition
1104  
1105  
1106 Lexical item codes.
1107  
1108  
1109  
1110  
1111 =head3 Output fields
1112  
1113  
1114 =head4 B
1115  
1116 Closing parenthesis.
1117  
1118 =head4 a
1119  
1120 Infix operator with priority 2 binding right to left typically used in an assignment.
1121  
1122 =head4 b
1123  
1124 Opening parenthesis.
1125  
1126 =head4 d
1127  
1128 Infix operator with priority 3 binding left to right typically used in arithmetic.
1129  
1130 =head4 p
1131  
1132 Monadic prefix operator.
1133  
1134 =head4 q
1135  
1136 Monadic suffix operator.
1137  
1138 =head4 s
1139  
1140 Infix operator with priority 1 binding left to right typically used to separate statements.
1141  
1142 =head4 t
1143  
1144 A term in the expression.
1145  
1146 =head4 v
1147  
1148 A variable in the expression.
1149  
1150  
1151  
1152 =head2 Tree::Term::LexicalCode Definition
1153  
1154  
1155 Lexical item codes.
1156  
1157  
1158  
1159  
1160 =head3 Output fields
1161  
1162  
1163 =head4 letter
1164  
1165 Letter code used to refer to the lexical item.
1166  
1167 =head4 name
1168  
1169 Descriptive name of lexical item.
1170  
1171 =head4 next
1172  
1173 Letters codes of items that can follow this lexical item.
1174  
1175  
1176  
1177 =head2 Tree::Term::LexicalStructure Definition
1178  
1179  
1180 Lexical item codes.
1181  
1182  
1183  
1184  
1185 =head3 Output fields
1186  
1187  
1188 =head4 codes
1189  
1190 Code describing each lexical item
1191  
1192 =head4 first
1193  
1194 Lexical items we can start with
1195  
1196 =head4 last
1197  
1198 Lexical items we can end with
1199  
1200  
1201  
1202 =head1 Private Methods
1203  
1204 =head2 new($count)
1205  
1206 Create a new term from the indicated number of items on top of the stack
1207  
1208 Parameter Description
1209 1 $count Number of terms
1210  
1211 =head2 LexicalCode($letter, $next, $name)
1212  
1213 Lexical code definition
1214  
1215 Parameter Description
1216 1 $letter Letter used to refer to the lexical item
1217 2 $next Letters of items that can follow this lexical item
1218 3 $name Descriptive name of lexical item
1219  
1220 =head2 type($s)
1221  
1222 Type of term
1223  
1224 Parameter Description
1225 1 $s Term to test
1226  
1227 =head2 expandElement($e)
1228  
1229 Describe a lexical element
1230  
1231 Parameter Description
1232 1 $e Element to expand
1233  
1234 =head2 expandCodes($e)
1235  
1236 Expand a string of codes
1237  
1238 Parameter Description
1239 1 $e Codes to expand
1240  
1241 =head2 expected($s)
1242  
1243 String of next possible lexical items
1244  
1245 Parameter Description
1246 1 $s Lexical item
1247  
1248 =head2 unexpected($element, $unexpected, $position)
1249  
1250 Complain about an unexpected element
1251  
1252 Parameter Description
1253 1 $element Last good element
1254 2 $unexpected Unexpected element
1255 3 $position Position
1256  
1257 =head2 check_XXXX()
1258  
1259 Check that the top of the stack has one of XXXX
1260  
1261  
1262 =head2 test_XXXX($item)
1263  
1264 Check that we have XXXX
1265  
1266 Parameter Description
1267 1 $item Item to test
1268  
1269 =head2 test_t($item)
1270  
1271 Check that we have a term
1272  
1273 Parameter Description
1274 1 $item Item to test
1275  
1276 =head2 reduce($priority)
1277  
1278 Reduce the stack at the specified priority
1279  
1280 Parameter Description
1281 1 $priority Priority
1282  
1283 =head2 reduce1()
1284  
1285 Reduce the stack at priority 1
1286  
1287  
1288 =head2 reduce2()
1289  
1290 Reduce the stack at priority 2
1291  
1292  
1293 =head2 pushElement()
1294  
1295 Push an element
1296  
1297  
1298 =head2 accept_a()
1299  
1300 Assign
1301  
1302  
1303 =head2 accept_b()
1304  
1305 Open
1306  
1307  
1308 =head2 accept_B()
1309  
1310 Closing parenthesis
1311  
1312  
1313 =head2 accept_d()
1314  
1315 Infix but not assign or semi-colon
1316  
1317  
1318 =head2 accept_p()
1319  
1320 Prefix
1321  
1322  
1323 =head2 accept_q()
1324  
1325 Post fix
1326  
1327  
1328 =head2 accept_s()
1329  
1330 Semi colon
1331  
1332  
1333 =head2 accept_v()
1334  
1335 Variable
1336  
1337  
1338 =head2 parseExpression()
1339  
1340 Parse an expression.
1341  
1342  
1343 =head2 depth($term)
1344  
1345 Depth of a term in an expression.
1346  
1347 Parameter Description
1348 1 $term Term
1349  
1350 =head2 listTerms($expression)
1351  
1352 List the terms in an expression in post order
1353  
1354 Parameter Description
1355 1 $expression Root term
1356  
1357  
1358 =head1 Index
1359  
1360  
1361 1 L<accept_a|/accept_a> - Assign
1362  
1363 2 L<accept_B|/accept_B> - Closing parenthesis
1364  
1365 3 L<accept_b|/accept_b> - Open
1366  
1367 4 L<accept_d|/accept_d> - Infix but not assign or semi-colon
1368  
1369 5 L<accept_p|/accept_p> - Prefix
1370  
1371 6 L<accept_q|/accept_q> - Post fix
1372  
1373 7 L<accept_s|/accept_s> - Semi colon
1374  
1375 8 L<accept_v|/accept_v> - Variable
1376  
1377 9 L<check_XXXX|/check_XXXX> - Check that the top of the stack has one of XXXX
1378  
1379 10 L<depth|/depth> - Depth of a term in an expression.
1380  
1381 11 L<expandCodes|/expandCodes> - Expand a string of codes
1382  
1383 12 L<expandElement|/expandElement> - Describe a lexical element
1384  
1385 13 L<expected|/expected> - String of next possible lexical items
1386  
1387 14 L<flat|/flat> - Print the terms in the expression as a tree from left right to make it easier to visualize the structure of the tree.
1388  
1389 15 L<LexicalCode|/LexicalCode> - Lexical code definition
1390  
1391 16 L<LexicalStructure|/LexicalStructure> - Return the lexical codes and their relationships in a data structure so this information can be used in other contexts.
1392  
1393 17 L<listTerms|/listTerms> - List the terms in an expression in post order
1394  
1395 18 L<new|/new> - Create a new term from the indicated number of items on top of the stack
1396  
1397 19 L<parse|/parse> - Parse an expression.
1398  
1399 20 L<parseExpression|/parseExpression> - Parse an expression.
1400  
1401 21 L<pushElement|/pushElement> - Push an element
1402  
1403 22 L<reduce|/reduce> - Reduce the stack at the specified priority
1404  
1405 23 L<reduce1|/reduce1> - Reduce the stack at priority 1
1406  
1407 24 L<reduce2|/reduce2> - Reduce the stack at priority 2
1408  
1409 25 L<syntaxError|/syntaxError> - Check the syntax of an expression without parsing it.
1410  
1411 26 L<test_t|/test_t> - Check that we have a term
1412  
1413 27 L<test_XXXX|/test_XXXX> - Check that we have XXXX
1414  
1415 28 L<type|/type> - Type of term
1416  
1417 29 L<unexpected|/unexpected> - Complain about an unexpected element
1418  
1419 30 L<validPair|/validPair> - Confirm that the specified pair of lexical elements can occur as a sequence.
1420  
1421 =head1 Installation
1422  
1423 This module is written in 100% Pure Perl and, thus, it is easy to read,
1424 comprehend, use, modify and install via B<cpan>:
1425  
1426 sudo cpan install Tree::Term
1427  
1428 =head1 Author
1429  
1430 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
1431  
1432 L<http://www.appaapps.com|http://www.appaapps.com>
1433  
1434 =head1 Copyright
1435  
1436 Copyright (c) 2016-2021 Philip R Brenan.
1437  
1438 This module is free software. It may be used, redistributed and/or modified
1439 under the same terms as Perl itself.
1440  
1441 =cut
1442  
1443  
1444  
1445 # Tests and documentation
1446  
1447 sub test
1448 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=16.7
Pod undocumented
1449 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
1450 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
1451 1 my $s = eval "join('', <${p}::DATA>)";
1452 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
1453 0 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine E: called CC=7 SCAR=20.4
Subroutine T: called CC=3 SCAR=11.4
Pod undocumented
Pod undocumented
Branch: 6 branches
BranchTrueFalseTotal
unless index($@, $parse) != -1 0 39 39
unless index($@, $syntax) != -1 0 39 39
if ($e) 0 39 39
if -e $log 0 0 0
if -e $log 0 46 46
unless $r 0 46 46
Condition50%2/4
$@ // ""
l!l
390
$@ // ""
l!l
390
MC/DC0%0/1
$@ // ""
$@
MC/DC0%0/1
$@ // ""
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
Truth table50%1/2
A
A= $@
Aresult
11
00
1454 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
1455 1 1
1456 }
1457  
1458 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
1459  
1460 1 1;
1461 # podDocumentation
1462 __DATA__
↑ Summary