PML/Backend/FS.pm

filter: Statementstmt 84.2% 241 / 286 Branchbran 63.0% 82 / 130 Conditioncond 39.0% 16 / 41 MC/DCmcdc 19.2% 5 / 26 Subroutinesub 95.2% 20 / 21 Podpod 8.3% 1 / 12 totaltotal 70.7% 365 / 516 CC 86 SCAR 26.1
CC
86
Cov
74%
ParseFSNode
23.3
read
22.5
PrintFSNode
18.6
CRAP 13.6
« PML/FSFormat.pm ↑ Summary PML/Schema/Constant.pm »
1  
2 ############################################################
3 #
4 # Treex::PML::Backend::FS
5 # =========
6 #
7  
8 package Treex::PML::Backend::FS;
9  
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use vars qw($CheckListValidity $emulatePML);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 $VERSION='2.29'; # version template
17 }
18 9 ▶use Treex::PML::IO qw(open_backend close_backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub DOES {
Subroutine DOES: not called CC=3 SCAR=24.8
Pod undocumented
24 0 my ($self,$role)=@_;
25 0 ▶ if ($role eq 'FSBackend' or $role eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq 'FSBackend' or $role eq __PACKAGE__) { } 0 0 0
Condition0%0/3
$role eq "FSBackend" or $role eq __PACKAGE__
l!l&&r!l&&!r
000
MC/DC0%0/2
$role eq "FSBackend" or $role eq __PACKAGE__
$role eq "FSBackend" $role eq __PACKAGE__
Truth table0%0/3
A or B
A= $role eq "FSBackend"B= $role eq __PACKAGE__
ABresult
1X1
011
000
26 0 return 1;
27 } else {
28 0 return $self->SUPER::DOES($role);
29 }
30 }
31  
32  
33 =pod
34  
35 =head2 NAME
36  
37 Treex::PML::Backend::FS - IO backend for reading/writing FS files.
38  
39 =head1 SYNOPSIS
40  
41 use Treex::PML;
42 Treex::PML::AddBackends(qw(FS))
43  
44 my $document = Treex::PML::Factory->createDocumentFromFile('input.fs');
45 ...
46 $document->save();
47  
48 =head1 DESCRIPTION
49  
50 This module implements a Treex::PML input/output backend which accepts
51 reads/writes documents in the FS format.
52  
53 =head1 REFERENCE
54  
55 =over 4
56  
57 =item Treex::PML::Backend::FS::$emulatePML
58  
59 This variable controls whether a simple PML schema should be created
60 for FS files (default is 1 - yes). Attribute whose name contains one
61 or more slashes is represented as a (possibly nested) structure where
62 each slash represents one level of nesting. Attributes sharing a
63 common name-part followed by a slash are represented as members of
64 the same structure. For example, attributes C<a>, C<b/u/x>, C<b/v/x> and
65 C<b/v/y> result in the following structure:
66  
67 C<{a => value_of_a,
68 b => { u => { x => value_of_a/u/x },
69 v => { x => value_of_a/v/x,
70 y => value_of_a/v/y }
71 }
72 }>
73  
74 In the PML schema emulation mode, it is forbidden to have both C<a>
75 and C<a/b> attributes. In such a case the parser reverts to
76 non-emulation mode.
77  
78 =cut
79  
80 9 $emulatePML=1;
81  
82  
83 4 ▶sub test {
Subroutine test: called CC=5 SCAR=17.0
Pod undocumented
84 4 my ($f,$encoding)=@_;
85 4 ▶ if (ref($f) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $f eq 'ARRAY') { } 0 4 4
elsif (ref $f) { } 2 2 4
86 0 return $f->[0]=~/^@/;
87 } elsif (ref($f)) {
88 2 ▶ binmode $f unless UNIVERSAL::DOES::does($f,'IO::Zlib');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::DOES::does($f, "IO::Zlib") 2 0 2
89 2 my $test = ($f->getline()=~/^@/);
90 2 return $test;
91 } else {
92 2 my $fh = open_backend($f,"r");
93 2 ▶ my $test = $fh && test($fh);
Condition33%1/3
$fh && test($fh)
!ll&&!rl&&r
002
MC/DC0%0/2
$fh && test($fh)
$fh test($fh)
Truth table33%1/3
A && B
A= $fhB= test($fh)
ABresult
0X0
100
111
94 2 close_backend($fh);
95 2 return $test;
96 }
97 }
98  
99  
100 2 ▶sub _fs2members {
Subroutine _fs2members: called CC=6 SCAR=18.8
101 2 my ($fs)=@_;
102 2 my $mbr = {};
103 2 my $defs = $fs->defs;
104 # sort, so that possible short parts go first
105 2 foreach my $attr (sort $fs->attributes) {
106 100 my $m = $mbr;
107 # check that no short attr exists
108 100 my @parts = split /\//,$attr;
109 100 my $short=$parts[0];
110 100 ▶ for (my $i=1;$i<@parts;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @parts) 0 100 100
111 0 ▶ if ($defs->{$short}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defs->{$short}) 0 0 0
112 0 warn "Can't emulate PML schema: attribute name conflict between $short and $attr: falling back to non-emulation mode\n";
113 }
114 0 $short .= '/'.$parts[$i];
115 }
116 100 for my $part (@parts) {
117 100 $m->{structure}{member}{$part}{-name} = $part;
118 100 $m=$m->{structure}{member}{$part};
119 }
120 # allow ``alt'' values concatenated with |
121 100 ▶ if ($fs->isList($attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->isList($attr)) { } 2 98 100
122 $m->{alt} = {
123 2 -flat => 1,
124 choice => [ $fs->listValues($attr) ]
125 };
126 } else {
127 $m->{alt} = {
128 98 -flat => 1,
129 cdata => { format =>'any' }
130 };
131 }
132 }
133 2 return $mbr->{structure}{member};
134 }
135  
136 2 ▶sub read {
Subroutine read: called CC=16 SCAR=31.1
Pod undocumented
137 2 my ($fileref,$fsfile) = @_;
138 2 ▶ return unless ref($fsfile);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsfile 0 2 2
139 2 my $FS = Treex::PML::Factory->createFSFormat();
140 2 ▶ $FS->readFrom($fileref) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$FS->readFrom($fileref) or return 0 2 0 2
141 2 $fsfile->changeFS( $FS );
142  
143 2 my $emu_schema_type;
144 2 ▶ if ($emulatePML) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::Backend::FS::emulatePML) 2 0 2
145 # fake a PML Schema:
146 2 my $members = _fs2members($fsfile->FS);
147 2 $members->{'#childnodes'}={
148 role => '#CHILDNODES',
149 list => {
150 ordered => 1,
151 type => 'fs-node.type',
152 },
153 };
154 2 my $node_type = {
155 name => 'fs-node',
156 role => '#NODE',
157 member => $members,
158 };
159 2 my $schema= Treex::PML::Schema->convert_from_hash({
160 description => 'PML schema generated from FS header',
161 root => { name => 'fs-data',
162 structure => {
163 member => {
164 trees => {
165 -name => 'trees',
166 role => '#TREES',
167 required => 1,
168 list => {
169 ordered => 1,
170 type => 'fs-node.type'
171 }
172 }
173 }
174 }
175 },
176 type => {
177 'fs-node.type' => {
178 -name => 'fs-node.type',
179 structure => $node_type,
180 }
181 }
182 });
183 2 ▶ if (defined($node_type->{member})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node_type->{"member"}) 2 0 2
184 2 $emu_schema_type = $node_type;
185 2 $fsfile->changeMetaData('schema',$schema);
186 }
187 }
188  
189 2 my ($root,$l,@rest);
190 2 $fsfile->changeTrees();
191  
192 # this could give us some speedup.
193 2 my $ordhash;
194 {
195 2 my $i = 0;
196 2 $ordhash = { map { $_ => $i++ } $fsfile->FS->attributes };
197 }
198  
199 2 ▶ while ($l=ReadEscapedLine($fileref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l = ReadEscapedLine($fileref)) 12 2 14
200 12 ▶ if ($l=~/^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^\[/) { } 4 8 12
201 4 $root=ParseFSTree($fsfile->FS,$l,$ordhash,$emu_schema_type);
202 4 ▶ push @{$fsfile->treeList}, $root if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 4 0 4
203 8 } else { push @rest, $l; }
204 }
205 2 $fsfile->changeTail(@rest);
206  
207 #parse Rest
208 2 my @patterns;
209 2 foreach ($fsfile->tail) {
210 8 ▶ if (/^\/\/Tred:Custom-Attribute:(.*\S)\s*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (m[^//Tred:Custom-Attribute:(.*\S)\s*$]) { } 4 4 8
elsif (m[^//Tred:Custom-AttributeCont:(.*\S)\s*$]) { } 0 4 4
elsif (m[^//FS-REQUIRE:\s*(\S+)\s+(\S+)=\"([^\"]+)\"\s*$]) { } 0 4 4
211 4 push @patterns,$1;
212 } elsif (/^\/\/Tred:Custom-AttributeCont:(.*\S)\s*$/) {
213 0 $patterns[$#patterns].="\n".$1;
214 } elsif (/^\/\/FS-REQUIRE:\s*(\S+)\s+(\S+)=\"([^\"]+)\"\s*$/) {
215 0 ▶ my $requires = $fsfile->metaData('fs-require') || $fsfile->changeMetaData('fs-require',[]);
Condition0%0/3
$fsfile->metaData('fs-require') || $fsfile->changeMetaData('fs-require', [])
l!l&&r!l&&!r
000
MC/DC0%0/2
$fsfile->metaData('fs-require') || $fsfile->changeMetaData('fs-require', [])
$fsfile->metaData('fs-require') $fsfile->changeMetaData('fs-require', [])
Truth table0%0/3
A || B
A= $fsfile->metaData('fs-require')B= $fsfile->changeMetaData('fs-require', [])
ABresult
1X1
011
000
216 0 push @$requires,[$2,$3];
217 0 ▶ my $refnames = $fsfile->metaData('refnames') || $fsfile->changeMetaData('refnames',{});
Condition0%0/3
$fsfile->metaData('refnames') || $fsfile->changeMetaData('refnames', {})
l!l&&r!l&&!r
000
MC/DC0%0/2
$fsfile->metaData('refnames') || $fsfile->changeMetaData('refnames', {})
$fsfile->metaData('refnames') $fsfile->changeMetaData('refnames', {})
Truth table0%0/3
A || B
A= $fsfile->metaData('refnames')B= $fsfile->changeMetaData('refnames', {})
ABresult
1X1
011
000
218 0 $refnames->{$1} = $2;
219 }
220 }
221 2 $fsfile->changePatterns(@patterns);
222 2 ▶ unless (@patterns) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@patterns) 0 2 2
223 0 my ($peep)=$fsfile->tail;
224 0 $fsfile->changePatterns( map { "\$\{".$fsfile->FS->atno($_)."\}" }
225 ($peep=~/[,\(]([0-9]+)/g));
226 }
227 $fsfile->changeHint(join "\n",
228 2 ▶ map { /^\/\/Tred:Balloon-Pattern:(.*\S)\s*$/ ? $1 : () } $fsfile->tail);
Branch: 1 branch
BranchTrueFalseTotal
m[^//Tred:Balloon-Pattern:(.*\S)\s*$] ? : 2 6 8
229 2 return 1;
230 }
231  
232  
233 1 ▶sub write {
Subroutine write: called CC=7 SCAR=23.3
Pod undocumented
234 1 my ($fileref,$fsfile) = @_;
235 1 ▶ return unless ref($fsfile);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsfile 0 1 1
236  
237 # print $fileref @{$fsfile->FS->unparsed};
238 {
239 1 my $encoding = $fsfile->encoding;
240 1 ▶ if (defined $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 0 1 1
241 0 print $fileref '@E '."$encoding\n";
242 }
243 }
244 1 $fsfile->FS->writeTo($fileref);
245 1 ▶ PrintFSFile($fileref,
Branch: 1 branch
BranchTrueFalseTotal
ref $fsfile->metaData('schema') ? : 1 0 1
246 $fsfile->FS,
247 $fsfile->treeList,
248 ref($fsfile->metaData('schema')) ? 1 : 0
249 );
250  
251 ## Tredish custom attributes:
252 $fsfile->changeTail(
253 4 (grep { $_!~/\/\/Tred:(?:Custom-Attribute(?:Cont)?|Balloon-Pattern):/ } $fsfile->tail),
254 2 (map {"//Tred:Custom-Attribute:$_\n"}
255 map {
256 2 join "\n//Tred:Custom-AttributeCont:",
257 split /\n/,$_
258 } $fsfile->patterns),
259 1 (map {"//Tred:Balloon-Pattern:$_\n"}
260 split /\n/,$fsfile->hint),
261 );
262 1 print $fileref $fsfile->tail;
263 1 ▶ if (ref($fsfile->metaData('fs-require'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fsfile->metaData("fs-require")) 0 1 1
264 0 ▶ my $refnames = $fsfile->metaData('refnames') || {};
Condition0%0/2
$fsfile->metaData('refnames') || {}
l!l
00
MC/DC0%0/1
$fsfile->metaData('refnames') || {}
$fsfile->metaData('refnames')
Truth table0%0/2
A
A= $fsfile->metaData('refnames')
Aresult
11
00
265 0 foreach my $req ( @{ $fsfile->metaData('fs-require') } ) {
266 0 my ($name) = grep { $refnames->{$_} eq $req->[0] } keys(%$refnames);
267 0 print $fileref "//FS-REQUIRE:$name $req->[0]=\"$req->[1]\"\n";
268 }
269 }
270 1 return 1;
271 }
272  
273 461 ▶sub Print ($$) {
Subroutine Print: called CC=2 SCAR=7.6
Pod undocumented
274 my (
275 461 $output, # filehandle or string
276 $text # text
277 )=@_;
278 461 ▶ if (ref($output) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq 'SCALAR') { } 0 461 461
279 0 $$output.=$text;
280 } else {
281 461 print $output $text;
282 }
283 }
284  
285 1 ▶sub PrintFSFile {
Subroutine PrintFSFile: called CC=2 SCAR=6.9
Pod undocumented
286 1 my ($fh,$fsformat,$trees,$emu_schema)=@_;
287 1 foreach my $tree (@$trees) {
288 2 PrintFSTree($tree,$fsformat,$fh,$emu_schema);
289 }
290 }
291  
292 2 ▶sub PrintFSTree {
Subroutine PrintFSTree: called CC=10 SCAR=23.2
Pod undocumented
293 2 my ($root, # a reference to the root-node
294 $fsformat, # FSFormat object
295 $fh,
296 $emu_schema
297 )=@_;
298  
299 2 ▶ $fh=\*STDOUT unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 2 2
300 2 my $node=$root;
301 2 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 2 0 2
302 14 PrintFSNode($node,$fsformat,$fh,$emu_schema);
303 14 ▶ if ($node->{$Treex::PML::Node::firstson}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$Treex::PML::Node::firstson}) 8 6 14
304 8 Print($fh, "(");
305 8 $node = $node->{$Treex::PML::Node::firstson};
306 8 redo;
307 }
308 6 ▶ while ($node && $node != $root && !($node->{$Treex::PML::Node::rbrother})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $node != $root and not $node->{$Treex::PML::Node::rbrother}) 8 6 14
Condition83%5/6
$node and $node != $root and not $node->{$Treex::PML::Node::rbrother}
!ll&&!rl&&r
248
$node and $node != $root
!ll&&!rl&&r
0212
MC/DC66%2/3
$node and $node != $root and not $node->{$Treex::PML::Node::rbrother}
$node $node != $root not $node->{$Treex::PML::Node::rbrother}
Truth table75%3/4
A and B and not(C)
A= $nodeB= $node != $rootC= not $node->{$Treex::PML::Node::rbrother}
ABCresult
0XX0
10X0
1100
1111
309 8 Print($fh, ")");
310 8 $node = $node->{$Treex::PML::Node::parent};
311 }
312 6 ▶ croak "Error: NULL-node within the node while printing\n" if !$node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 6 6
313 6 ▶ last if ($node == $root || !$node);
Branch: 1 branch
BranchTrueFalseTotal
if $node == $root or not $node 2 4 6
Condition66%2/3
$node == $root or not $node
l!l&&r!l&&!r
204
MC/DC50%1/2
$node == $root or not $node
$node == $root not $node
Truth table66%2/3
A or not(B)
A= $node == $rootB= not $node
ABresult
1X1
011
000
314 4 Print($fh, ",");
315 4 $node = $node->{$Treex::PML::Node::rbrother};
316 4 redo;
317 }
318 2 Print($fh, "\n");
319 }
320  
321 14 ▶sub PrintFSNode {
Subroutine PrintFSNode: called CC=16 SCAR=29.2
Pod undocumented
322 14 my ($node, # a reference to the root-node
323 $fsformat,
324 $output, # output stream
325 $emu_schema
326 )=@_;
327 14 my $v;
328 14 my $lastprinted=1;
329  
330 14 my $defs = $fsformat->defs;
331 14 my $attrs = $fsformat->list;
332 14 my $attr_count = $#$attrs+1;
333  
334 14 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 14 0 14
335 14 Print($output, "[");
336 14 ▶ for (my $n=0; $n<$attr_count; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $attr_count) 700 14 714
337 700 ▶ $v=$emu_schema ? $node->attr($attrs->[$n]) : $node->{$attrs->[$n]};
Branch: 1 branch
BranchTrueFalseTotal
$emu_schema ? : 700 0 700
338 700 ▶ $v=~s/([,\[\]=\\\n])/\\$1/go if (defined($v));
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 172 528 700
339 700 ▶ if (index($defs->{$attrs->[$n]}, " O")>=0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($$defs{$attrs->[$n]}, ' O') != -1) { } 56 644 700
elsif (defined $v and length $v) { } 116 528 644
340 56 ▶ Print($output,",") if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 42 14 56
341 56 ▶ unless ($lastprinted && index($defs->{$attrs->[$n]}," P")>=0) # N could match here too probably
Branch: 1 branch
BranchTrueFalseTotal
unless ($lastprinted and index($$defs{$attrs->[$n]}, " P") != -1) 0 56 56
Condition33%1/3
$lastprinted and index($$defs{$attrs->[$n]}, " P") != -1
!ll&&!rl&&r
0056
MC/DC0%0/2
$lastprinted and index($$defs{$attrs->[$n]}, " P") != -1
$lastprinted index($$defs{$attrs->[$n]}, " P") != -1
Truth table33%1/3
A and B
A= $lastprintedB= index($$defs{$attrs->[$n]}, " P") != -1
ABresult
0X0
100
111
342 0 { Print($output, $attrs->[$n]."="); }
343 56 ▶ $v='-' if ($v eq '' or not defined($v));
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" or not defined $v 0 56 56
Condition33%1/3
$v eq "" or not defined $v
l!l&&r!l&&!r
0056
MC/DC0%0/2
$v eq "" or not defined $v
$v eq "" not defined $v
Truth table33%1/3
A or not(B)
A= $v eq ""B= not defined $v
ABresult
1X1
011
000
344 56 Print($output,$v);
345 56 $lastprinted=1;
346 ▶ } elsif (defined($v) and length($v)) {
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
5280116
MC/DC50%1/2
defined $v and length $v
defined $v length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
347 116 ▶ Print($output,",") if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 116 0 116
348 116 ▶ unless ($lastprinted && index($defs->{$attrs->[$n]}," P")>=0) # N could match here too probably
Branch: 1 branch
BranchTrueFalseTotal
unless ($lastprinted and index($$defs{$attrs->[$n]}, " P") != -1) 81 35 116
Condition66%2/3
$lastprinted and index($$defs{$attrs->[$n]}, " P") != -1
!ll&&!rl&&r
81035
MC/DC50%1/2
$lastprinted and index($$defs{$attrs->[$n]}, " P") != -1
$lastprinted index($$defs{$attrs->[$n]}, " P") != -1
Truth table66%2/3
A and B
A= $lastprintedB= index($$defs{$attrs->[$n]}, " P") != -1
ABresult
0X0
100
111
349 81 { Print($output,$attrs->[$n]."="); }
350 116 Print($output,$v);
351 116 $lastprinted=1;
352 } else {
353 528 $lastprinted=0;
354 }
355 }
356 14 Print($output,"]");
357 } else {
358 0 Print($output,"<<NULL>>");
359 }
360 }
361  
362 =item Treex::PML::Backend::FS::ParseFSTree ($fsformat,$line,$ordhash)
363  
364 Parse a given string (line) in FS format and return the root of the
365 resulting FS tree as a node object.
366  
367 =cut
368  
369 4 ▶sub ParseFSTree {
Subroutine ParseFSTree: called CC=10 SCAR=23.7
Pod documented
370 4 my ($fsformat,$l,$ordhash,$emu_schema_type)=@_;
371 4 ▶ return unless ref($fsformat);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsformat 0 4 4
372 4 my $root;
373 my $curr;
374 4 my $c;
375  
376 4 ▶ unless ($ordhash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ordhash) 0 4 4
377 0 my $i = 0;
378 0 $ordhash = { map { $_ => $i++ } @{$fsformat->list} };
379 }
380  
381 4 ▶ if ($l=~/^\[/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^\[/o) 4 0 4
382 4 $l=~s/&/&amp;/g;
383 4 $l=~s/\\\\/&backslash;/g;
384 4 $l=~s/\\,/&comma;/g;
385 4 $l=~s/\\\[/&lsqb;/g;
386 4 $l=~s/\\]/&rsqb;/g;
387 4 $l=~s/\\=/&eq;/g;
388 4 $l=~s/\\//g;
389 4 $l=~s/\r//g;
390 4 $curr=$root=ParseFSNode($fsformat,\$l,$ordhash,$emu_schema_type); # create Root
391  
392 4 ▶ while ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 40 4 44
393 40 $c = substr($l,0,1);
394 40 $l = substr($l,1);
395 40 ▶ if ( $c eq '(' ) { # Create son (go down)
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 16 24 40
396 16 my $first_son = $curr->{$Treex::PML::Node::firstson} = ParseFSNode($fsformat,\$l,$ordhash,$emu_schema_type);
397 16 $first_son->{$Treex::PML::Node::parent}=$curr;
398 16 $curr=$first_son;
399 16 next;
400 }
401 24 ▶ if ( $c eq ')' ) { # Return to parent (go up)
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 16 8 24
402 16 ▶ croak "Error paring tree" if ($curr eq $root);
Branch: 1 branch
BranchTrueFalseTotal
if $curr eq $root 0 16 16
403 16 $curr=$curr->{$Treex::PML::Node::parent};
404 16 next;
405 }
406 8 ▶ if ( $c eq ',' ) { # Create right brother (go right);
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ",") 8 0 8
407 8 my $rb = $curr->{$Treex::PML::Node::rbrother} = ParseFSNode($fsformat,\$l,$ordhash,$emu_schema_type);
408 8 $rb->set_lbrother( $curr );
409 8 $rb->set_parent( $curr->{$Treex::PML::Node::parent} );
410 8 $curr=$rb;
411 8 next;
412 }
413 0 croak "Unexpected token... `$c'!\n$l\n";
414 }
415 4 ▶ croak "Error: Closing brackets do not lead to root of the tree.\n" if ($curr != $root);
Branch: 1 branch
BranchTrueFalseTotal
if $curr != $root 0 4 4
416 }
417 4 return $root;
418 }
419  
420  
421 28 ▶sub ParseFSNode {
Subroutine ParseFSNode: called CC=16 SCAR=31.5
Pod undocumented
422 28 my ($fsformat,$lr,$ordhash,$emu_schema_type) = @_;
423 28 my $n = 0;
424 28 my $node;
425 28 my @ats=();
426 28 my $pos = 1;
427 28 my $a=0;
428 28 my $v=0;
429 28 my $tmp;
430 my @lv;
431 28 my $nd;
432 28 my $i;
433 28 my $w;
434  
435 28 my $defs = $fsformat->defs;
436 28 my $attrs = $fsformat->list;
437 28 my $attr_count = $#$attrs+1;
438 28 ▶ unless ($ordhash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ordhash) 0 28 28
439 0 my $i = 0;
440 0 $ordhash = { map { $_ => $i++ } @$attrs };
441 }
442  
443 28 ▶ $node = $emu_schema_type
Branch: 1 branch
BranchTrueFalseTotal
$emu_schema_type ? : 28 0 28
444 ? Treex::PML::Factory->createTypedNode($emu_schema_type)
445 : Treex::PML::Factory->createNode();
446 28 ▶ if ($$lr=~/^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$lr =~ /^\[/) { } 28 0 28
447 28 chomp $$lr;
448 28 $i=index($$lr,']');
449 28 $nd=substr($$lr,1,$i-1);
450 28 $$lr=substr($$lr,$i+1);
451 28 @ats=split(',',$nd);
452 28 ▶ while (@ats) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ats) 344 28 372
453 344 $w=shift @ats;
454 344 $i=index($w,'=');
455 344 ▶ if ($i>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) { } 163 181 344
456 163 $a=substr($w,0,$i);
457 163 $v=substr($w,$i+1);
458 163 $tmp=$ordhash->{$a};
459 163 ▶ $n = $tmp if (defined($tmp));
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp 163 0 163
460 } else {
461 181 $v=$w;
462 181 ▶ $n++ while ( $n<$attr_count and $defs->{$attrs->[$n]}!~/ [PNW]/);
Branch: 1 branch
BranchTrueFalseTotal
$n < $attr_count and not $$defs{$attrs->[$n]} =~ / [PNW]/ and do { ++$n } 0 181 181
Condition33%1/3
$n < $attr_count and not $$defs{$attrs->[$n]} =~ / [PNW]/
!ll&&!rl&&r
01810
MC/DC0%0/2
$n < $attr_count and not $$defs{$attrs->[$n]} =~ / [PNW]/
$n < $attr_count not $$defs{$attrs->[$n]} =~ / [PNW]/
Truth table33%1/3
A and not(B)
A= $n < $attr_countB= not $$defs{$attrs->[$n]} =~ / [PNW]/
ABresult
0X0
100
111
463 181 ▶ if ($n>$attr_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $attr_count) 0 181 181
464 0 croak "No more positional attribute $n for value $v at position in:\n".$n."\n";
465 }
466 181 $a=$attrs->[$n];
467 }
468 344 ▶ if ($CheckListValidity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::Backend::FS::CheckListValidity) 0 344 344
469 0 ▶ if ($fsformat->isList($a)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fsformat->isList($a)) 0 0 0
470 0 @lv=$fsformat->listValues($a);
471 0 foreach $tmp (split /\|/,$v) {
472 0 ▶ print("Invalid list value $v of atribute $a no in @lv:\n$nd\n" ) unless (defined(Index(\@lv,$tmp)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined Index(\@lv, $tmp) 0 0 0
473 }
474 }
475 }
476 344 $n++;
477 344 $v=~s/&comma;/,/g;
478 344 $v=~s/&lsqb;/[/g;
479 344 $v=~s/&rsqb;/]/g;
480 344 $v=~s/&eq;/=/g;
481 344 $v=~s/&backslash;/\\/g;
482 344 $v=~s/&amp;/&/g;
483 344 ▶ if ($emu_schema_type and $a=~/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($emu_schema_type and $a =~ m[/]) { } 0 344 344
Condition33%1/3
$emu_schema_type and $a =~ m[/]
!ll&&!rl&&r
03440
MC/DC0%0/2
$emu_schema_type and $a =~ m[/]
$emu_schema_type $a =~ m[/]
Truth table33%1/3
A and B
A= $emu_schema_typeB= $a =~ m[/]
ABresult
0X0
100
111
484 0 $node->set_attr($a,$v);
485 } else {
486 # speed optimized version
487 # $node->setAttribute($a,$v);
488 344 $node->{$a}=$v;
489 }
490 }
491 0 } else { croak $$lr," not node!\n"; }
492 28 return $node;
493 }
494  
495 128 ▶sub ReadLine {
Subroutine ReadLine: called CC=2 SCAR=7.6
Pod undocumented
496 128 my ($handle)=@_;
497 128 local $_;
498 128 ▶ if (ref($handle) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handle eq 'ARRAY') { } 0 128 128
499 0 $_=shift @$handle;
500 128 } else { $_=<$handle>;
501 128 return $_; }
502 0 return $_;
503 }
504  
505 128 ▶sub ReadEscapedLine {
Subroutine ReadEscapedLine: called CC=3 SCAR=11.3
Pod undocumented
506 128 my ($handle)=@_; # file handle or array reference
507 128 my $l="";
508 128 local $_;
509 128 ▶ while ($_=ReadLine($handle)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = ReadLine($handle)) 126 2 128
510 126 ▶ if (s/\\\r*\n?$//og) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\\\r*\n?$//go) 0 126 126
511 0 $l.=$_; next;
512 } # if backslashed eol, concatenate
513 126 $l.=$_;
514 # use Devel::Peek;
515 # Dump($l);
516 126 last; # else we have the whole tree
517 }
518 128 return $l;
519 }
520  
521  
522 =back
523  
524 =cut
525  
526 9 1;
527  
528  
529 __END__
« PML/FSFormat.pm ↑ Summary PML/Schema/Constant.pm »