blib/script/stag-diff.pl untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 198 CC 17 SCAR 45.8
CC
17
Cov
0%
match
182.0
mismatch
12.0
smallstr
6.0
CRAP 97.4
1 #!/usr/local/bin/perl -w
2  
3 # POD docs at bottom of file
4  
5 use strict;
6  
7 use Data::Stag qw(:all);
8 use Getopt::Long;
9  
10 my $parser = "";
11 my $mapf;
12 my $help;
13 my @ignore = ();
14 my @report = ();
15 my $trace;
16 my $verbose;
17 GetOptions(
18 "help|h"=>\$help,
19 "parser|format|p=s"=>\$parser,
20 "ignore|s=s@"=>\@ignore,
21 "report|r=s@"=>\@report,
22 "trace|t"=>\$trace,
23 "verbose|v"=>\$verbose,
24 );
25  
26 our %REPORT = map {$_=>1} @report;
27  
28 if ($help) {
29 system("perldoc $0");
30 exit 0;
31 }
32  
33 my @files = @ARGV;
34 my $file1 = shift @files;
35 if (!@files) {
36 die "you must pass in at least two files";
37 }
38 my $stag1 = Data::Stag->parse($file1, $parser);
39 $stag1->unset($_) foreach @ignore;
40 foreach my $file2 (@files) {
41 my $stag2 = Data::Stag->parse($file2, $parser);
42 $stag2->unset($_) foreach @ignore;
43 my ($match, $reason) = match($stag1, $stag2);
44 if ($match) {
45 printf "SAME: $file1 $file2\n";
46 }
47 else {
48 printf "DIFF: $file1 $file2\n";
49 printf "REASON:\n";
50 showreason($reason);
51 }
52 }
53 exit 0;
54  
55 sub showreason {
56 my $reason = shift;
57 my $indent = shift || 0;
58 print ' ' x $indent;
59 my ($msg, @children) = @$reason;
60 printf $msg;
61 print "\n";
62 showreason($_, $indent+1) foreach @children;
63 }
64  
65 sub match {
66 my $stag1 = shift;
67 my $stag2 = shift;
68 if ($stag1->name ne $stag2->name) {
69 return(0, mismatch("name_mismatch", [$stag1, $stag2]));
70 }
71 trace("comparing %s", $stag1->name, $stag2->name);
72 my $t1 = $stag1->isterminal || 0;
73 my $t2 = $stag2->isterminal || 0;
74 if ($t1 != $t2) {
75 return(0, mismatch("different_node_types", [$stag1, $stag2]));
76 }
77 if ($t1 && $t2) {
78 if ($stag1->data eq $stag2->data) {
79 return 1;
80 }
81 else {
82 return(0, mismatch(sprintf("data_mismatch(%s ne %s)",
83 smallstr($stag1->data),
84 smallstr($stag2->data),
85 ),
86 [$stag1, $stag2]));
87 }
88 }
89 # both nodes nonterminal
90 if ($t1 || $t2) {
91 die "assertion error";
92 }
93  
94 trace(" ..looking at kids\n");
95 my @kids1 = $stag1->kids;
96 my @kids2 = $stag2->kids;
97  
98 # must match exactly
99 if (@kids1 != @kids2) {
100 return(0, mismatch(sprintf("subelement_count_mismatch [%s <=VS=> %s]",
101 names(@kids1), names(@kids2)),
102 [$stag1, $stag2]));
103 }
104 # null always matches
105 if (!@kids1) {
106 # both must be null
107 die "assertion error" unless !@kids2;
108 return 1;
109 }
110 trace(" ..matrix:\n");
111 my @filled = ();
112 for (my $i=0; $i<@kids1; $i++) {
113 my $kid1 = $kids1[$i];
114 my $matched;
115 my @reasons = ();
116 for (my $j=0; $j<@kids2; $j++) {
117 next if $filled[$j];
118 my $kid2 = $kids2[$j];
119 next unless $kid1->name eq $kid2->name;
120 my ($match, $reason) = match($kid1, $kid2);
121 if ($match) {
122 $filled[$j] = 1;
123 $matched = 1;
124 last;
125 }
126 else {
127 push(@reasons, $reason);
128 }
129 }
130 if (!$matched) {
131 my $mismatch =
132 mismatch("no_matching_node", [$kid1]);
133 push(@$mismatch, @reasons);
134 return(0, $mismatch);
135 }
136 }
137 trace(" ..match!\n");
138 return 1;
139 }
140  
141 sub names {
142 join(', ', map {$_->name} @_);
143 }
144  
145 sub mismatch {
146 my $msg = shift;
147 my @stags = @{shift || []};
148 my @names = map {$_->name} @stags;
149 my $reason =
150 sprintf "$msg: %s",
151 join(' AND ', @names);
152 if (grep {$REPORT{$_}} @names) {
153 printf "$reason\n";
154 if ($verbose) {
155 print $_->sxpr foreach @stags;
156 }
157 }
158 return [$reason];
159 }
160  
161 sub smallstr {
162 my $str = shift;
163 return $str if length($str) < 50;
164 return substr($str, 0, 50) ."...";
165 }
166  
167 sub trace {
168 return unless $trace;
169 my $fmt = shift;
170 printf $fmt, @_;
171 print "\n";
172 }
173  
174 __END__