tools/gen_grammar.pl

filter: Statementstmt 95.7% 89 / 93 Branchbran 82.7% 48 / 58 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 87.1% 156 / 179 CC 36 SCAR 30.1
CC
36
Cov
89%
create_rule_production
29.1
get_text
7.2
has_sibling
2.0
CRAP 20.4
« blib/lib/XML/XSH2/LibXMLCompat.pm ↑ Summary tools/gen_pod.pl »
1 #!/usr/bin/perl
2  
3 # $Id: gen_grammar.pl,v 2.1 2004-12-02 19:26:40 pajas Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶if ($ARGV[0]=~q(^(-h|--help)?$)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] =~ /^(-h|--help)?$/) 0 1 1
9 0 print <<EOF;
10 Generates RecDescent grammar from RecDescentXML source.
11  
12 Usage: $0 <source.xml>
13  
14 EOF
15 0 exit;
16 }
17  
18 1 my $parser=XML::LibXML->new();
19 1 $parser->load_ext_dtd(1);
20 1 $parser->validation(1);
21 1 $parser->complete_attributes(1);
22 1 my $doc=$parser->parse_file($ARGV[0]);
23  
24 1 my $dom=$doc->getDocumentElement();
25 1 my ($rules)=$dom->findnodes('./rules');
26 1 my ($preamb)=$dom->findnodes('./preamb');
27 1 my ($postamb)=$dom->findnodes('./postamb');
28  
29 1 print "# This file was automatically generated from $ARGV[0] on \n# ",scalar(localtime),"\n";
30  
31 1 print get_text($preamb,1);
32  
33 1 foreach my $r ($rules->findnodes('./rule[@inline!="yes" and production]')) {
34 49 print "\n ",$r->getAttribute('id'),":\n\t ";
35 49 print join("\n\t |",create_productions($r)),"\n";
36 }
37 1 print get_text($postamb,1);
38 1 print "\n";
39 1 exit;
40  
41 ## ================================================
42  
43 sub strip_space {
44 127 ▶ my ($text)=@_;
Subroutine strip_space: called CC=1 SCAR=0.0
45 127 $text=~s/^\s*//;
46 127 $text=~s/\s*$//;
47 127 return $text;
48 }
49  
50 sub get_text {
51 204 ▶ my ($node,$no_strip)=@_;
Subroutine get_text: called CC=7 SCAR=19.7
52 204 my $text="";
53 204 foreach ($node->childNodes()) {
54 213 ▶ if ($_->nodeType() == XML::LibXML::XML_ELEMENT_NODE &&
Branch: 2 branches
BranchTrueFalseTotal
if ($_->nodeType == 1 and $_->nodeName eq 'lineinfo') { } 26 187 213
elsif ($_->nodeType == 3 or $_->nodeType == 4) { } 187 0 187
Condition66%2/3
$_->nodeType == 1 and $_->nodeName eq "lineinfo"
!ll&&!rl&&r
187026
MC/DC50%1/2
$_->nodeType == 1 and $_->nodeName eq "lineinfo"
$_->nodeType == 1 $_->nodeName eq "lineinfo"
Truth table66%2/3
A and B
A= $_->nodeType == 1B= $_->nodeName eq "lineinfo"
ABresult
0X0
100
111
55 $_->nodeName() eq 'lineinfo') {
56 # $text.='0,0,0,$XML::XSH2::Functions::SCRIPT'
57 26 $text.='$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT';
58 ▶ } elsif ($_->nodeType() == XML::LibXML::XML_TEXT_NODE ||
Condition66%2/3
$_->nodeType == 3 or $_->nodeType == 4
l!l&&r!l&&!r
18520
MC/DC0%0/2
$_->nodeType == 3 or $_->nodeType == 4
$_->nodeType == 3 $_->nodeType == 4
Truth table66%2/3
A or B
A= $_->nodeType == 3B= $_->nodeType == 4
ABresult
1X1
011
000
59 $_->nodeType() == XML::LibXML::XML_CDATA_SECTION_NODE) {
60 187 $text.=$_->getData();
61 }
62 }
63 204 ▶ return $no_strip ? $text : strip_space($text);
Branch: 1 branch
BranchTrueFalseTotal
$no_strip ? : 77 127 204
64 }
65  
66 sub find_rule {
67 20 ▶ my ($r)=@_;
Subroutine find_rule: called CC=1 SCAR=0.0
68 20 return $r->findnodes('id("'.$r->getAttribute('ref').'")');
69 }
70  
71 sub create_productions {
72 73 ▶ my ($rule)=@_;
Subroutine create_productions: called CC=2 SCAR=6.9
73 return map {
74 73 ▶ $_->nodeName() eq 'production' ?
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeName eq 'production' ? : 109 20 129
75 create_rule_production($rule,$_) : create_productions(find_rule($_))
76 }
77 $rule->findnodes('production|ruleref');
78 }
79  
80 sub has_sibling {
81 22 ▶ my ($node)=@_;
Subroutine has_sibling: called CC=2 SCAR=7.0
82 22 ▶ return 0 unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 22 22
83 22 my$value=
84 $node->find(
85 '(following-sibling::*[name()!="action" and name()!="directive"]
86 or
87 not(following-sibling::*) and
88 (parent::production/parent::group/following-sibling::*[name()!="action"
89 and name()!="directive"])
90 )');
91 22 return $value;
92 # $node=$node->nextSibling();
93 # while ($node) {
94 # return 1 if ($node->nodeType == XML::LibXML::XML_ELEMENT_NODE
95 # and
96 # $node->nodeName ne 'action'
97 # and
98 # $node->nodeName ne 'directive'
99 # );
100 # $node=$node->nextSibling();
101 # }
102 # return 0;
103 }
104  
105 sub create_rule_production {
106 109 ▶ my ($rule,$prod)=@_;
Subroutine create_rule_production: called CC=28 SCAR=33.7
107 109 my $result;
108 my $name;
109 109 foreach my $item ($prod->childNodes()) {
110 761 ▶ next unless $item->nodeType == XML::LibXML::XML_ELEMENT_NODE;
Branch: 1 branch
BranchTrueFalseTotal
unless $item->nodeType == 1 432 329 761
111 329 $name=$item->nodeName();
112 329 ▶ if ($name eq 'lookahead') {
Branch: 9 branches
BranchTrueFalseTotal
if ($name eq 'lookahead') { } 0 329 329
elsif ($name eq 'regexp') { } 52 277 329
elsif ($name eq 'directive') { } 60 217 277
elsif ($name eq 'ruleref') { } 100 117 217
elsif ($name eq 'string') { } 15 102 117
elsif ($name eq 'action') { } 75 27 102
elsif ($name eq 'group') { } 4 23 27
elsif ($name eq 'selfref') { } 22 1 23
elsif ($name eq 'paramlist') { } 1 0 1
113 0 ▶ $result.=' ...' . ($item->getAttribute('negative') eq 'yes' ? '!' : '');
Branch: 1 branch
BranchTrueFalseTotal
$item->getAttribute('negative') eq 'yes' ? : 0 0 0
114 } elsif ($name eq 'regexp') {
115 52 $result.=" /".get_text($item)."/".$item->getAttribute('mod');
116 } elsif ($name eq 'directive') {
117 60 my $text=get_text($item);
118 60 my $type=$item->getAttribute('type');
119 60 ▶ $type='error?' if ($type eq 'error-if-committed');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "error-if-committed" 1 59 60
120 60 $result.=" <".$type;
121 60 ▶ $result.=":$text" if ($text ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $text ne "" 13 47 60
122 60 $result.=">";
123 } elsif ($name eq 'ruleref') {
124 100 $result.=" ".$item->getAttribute('ref');
125 100 ▶ if ($item->getAttribute('rep') ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->getAttribute("rep") ne "") 23 77 100
126 23 $result.="(".$item->getAttribute('rep').")";
127 }
128 100 ▶ if ($item->getAttribute('arguments') ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->getAttribute("arguments") ne "") 0 100 100
129 0 $result.="[".$item->getAttribute('arguments')."]";
130 }
131 } elsif ($name eq 'string') {
132 15 $result.=" '".get_text($item)."'";
133 } elsif ($name eq 'action') {
134 75 $result.="\n\t\t{ ".get_text($item,1)." }\n \t";
135 } elsif ($name eq 'group') {
136 4 ▶ $result.="\n\t " unless $result eq "";
Branch: 1 branch
BranchTrueFalseTotal
unless $result eq "" 1 3 4
137 4 $result.="("
138 . join("\n\t |",create_productions($item))
139 . "\n\t )";
140 4 ▶ if ($item->getAttribute('rep') ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->getAttribute("rep") ne "") 1 3 4
141 1 $result.="(".$item->getAttribute('rep').")";
142 }
143 } elsif ($name eq 'selfref') {
144 $result.=' /('
145 29 ▶ .join("|", map { $_->getAttribute('regexp') ne "" ?
Branch: 1 branch
BranchTrueFalseTotal
$_->getAttribute('regexp') ne '' ? : 0 29 29
146 $_->getAttribute('regexp') :
147 $_->getAttribute('name')
148 }
149 $rule->findnodes('ancestor-or-self::rule'),
150 22 ▶ grep {defined($_)} $rule->findnodes("./aliases/alias"))
Branch: 1 branch
BranchTrueFalseTotal
$item->getAttribute('sep') ne 'no' && has_sibling($item) ? : 20 2 22
Condition66%2/3
$item->getAttribute("sep") ne "no" and has_sibling($item)
!ll&&!rl&&r
0220
MC/DC50%1/2
$item->getAttribute("sep") ne "no" and has_sibling($item)
$item->getAttribute("sep") ne "no" has_sibling($item)
Truth table66%2/3
A and B
A= $item->getAttribute("sep") ne "no"B= has_sibling($item)
ABresult
0X0
100
111
151 . ')'
152 . (($item->getAttribute('sep') ne 'no' and has_sibling($item)) ? '\b/' : '/');
153 } elsif ($name eq 'paramlist') {
154 1 $result.="\n\t (( ";
155 1 my $count = 0;
156 1 foreach my $param ($item->childNodes()) {
157 next unless
158 15 ▶ $param->nodeType == XML::LibXML::XML_ELEMENT_NODE
Branch: 1 branch
BranchTrueFalseTotal
unless $param->nodeType == 1 and $param->nodeName eq "param" 8 7 15
Condition66%2/3
$param->nodeType == 1 and $param->nodeName eq "param"
!ll&&!rl&&r
807
MC/DC50%1/2
$param->nodeType == 1 and $param->nodeName eq "param"
$param->nodeType == 1 $param->nodeName eq "param"
Truth table66%2/3
A and B
A= $param->nodeType == 1B= $param->nodeName eq "param"
ABresult
0X0
100
111
159 and $param->nodeName eq 'param';
160 7 ▶ $result.="\n\t | " if $count++;
Branch: 1 branch
BranchTrueFalseTotal
if $count++ 6 1 7
161 7 my $name=$param->getAttribute('name');
162 7 my $short=$param->getAttribute('short');
163 7 my $arg=$param->getAttribute('argument');
164 7 my $type=$param->getAttribute('type');
165 7 my $prefix=$param->getAttribute('prefix');
166 7 my $shortprefix=$param->getAttribute('shortprefix');
167 7 $result.='/'.$prefix.$name;
168 7 ▶ $result.='|'.$shortprefix.$short if $short ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $short ne "" 7 0 7
169 7 $result.='/';
170 7 ▶ if ($arg ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne '') { } 6 1 7
171 6 $result.=' '.$arg." { [ '$type/$name' => \$item[2] ] }";
172 } else {
173 1 $result.=" { [ '/$name' => 1 ] }";
174 }
175 }
176 1 $result.="\n\t )(s?) { [ map { \@\$_ } \@{\$item[1]} ] }\n\t )\n";
177 #(( /:fold\s/ { [":fold" => 1] }
178 # | /:encoding\s/ expression { [":encoding" => $item[2]] }
179 # )(s)
180 # { [ map { @$_ } @{$item[1]} ] })
181 }
182 }
183
184 109 return $result;
185 }
186  
« blib/lib/XML/XSH2/LibXMLCompat.pm ↑ Summary tools/gen_pod.pl »