Reader/Dot.pm untested

filter: Statementstmt 0.0% 0 / 293 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 373 CC 15 SCAR 36.0
CC
15
Cov
0%
new
90.0
_Lexer
20.0
_set_attribute_hash
12.0
CRAP 36.7
1 ####################################################################
2 #
3 # This file was generated using Parse::Yapp version 1.02.
4 #
5 # Don't edit this file, use source file instead.
6 #
7 # ANY CHANGE MADE HERE WILL BE LOST !
8 #
9 ####################################################################
10 package Graph::Reader::Dot;
11 $Graph::Reader::Dot::VERSION = '2.10';
12 use strict;
13 use warnings;
14  
15 use parent 'Parse::Yapp::Driver';
16  
17  
18  
19 sub new {
20 my($class)=shift;
21 ref($class)
22 and $class=ref($class);
23  
24 my($self)=$class->SUPER::new( yyversion => '1.02',
25 yystates =>
26 [
27 {#State 0
28 ACTIONS => {
29 'STRICT' => 1,
30 'GRAPH' => 2,
31 'DIGRAPH' => 5
32 },
33 GOTOS => {
34 'Graph' => 3,
35 'GraphType' => 4
36 }
37 },
38 {#State 1
39 ACTIONS => {
40 'STRICT' => 1,
41 'GRAPH' => 2,
42 'DIGRAPH' => 5
43 },
44 GOTOS => {
45 'GraphType' => 6
46 }
47 },
48 {#State 2
49 DEFAULT => -6
50 },
51 {#State 3
52 ACTIONS => {
53 '' => 7
54 }
55 },
56 {#State 4
57 ACTIONS => {
58 'QUOT' => 9,
59 'NUMBER' => 8,
60 'ID' => 10
61 },
62 GOTOS => {
63 'IdNumQuot' => 11
64 }
65 },
66 {#State 5
67 DEFAULT => -5
68 },
69 {#State 6
70 DEFAULT => -4
71 },
72 {#State 7
73 DEFAULT => -0
74 },
75 {#State 8
76 DEFAULT => -31
77 },
78 {#State 9
79 DEFAULT => -30
80 },
81 {#State 10
82 DEFAULT => -32
83 },
84 {#State 11
85 ACTIONS => {
86 "{" => 12
87 }
88 },
89 {#State 12
90 ACTIONS => {
91 'GRAPH' => 13,
92 'EDGE' => 17,
93 'NODE' => 19,
94 'NUMBER' => 8,
95 'QUOT' => 9,
96 "{" => 22,
97 'SUBGRAPH' => 24,
98 'ID' => 10
99 },
100 DEFAULT => -2,
101 GOTOS => {
102 'AttrStmt' => 15,
103 'NodeId' => 14,
104 'StmtList' => 16,
105 'IdNumQuot' => 18,
106 'OptStmtList' => 20,
107 'Stmt' => 21,
108 'Attr' => 23,
109 'Subgraph' => 25,
110 'EdgeStmt' => 26,
111 'NodeStmt' => 27,
112 'NodeIdSubgraph' => 28
113 }
114 },
115 {#State 13
116 ACTIONS => {
117 "[" => 29
118 },
119 DEFAULT => -33,
120 GOTOS => {
121 'OptAttrList' => 30
122 }
123 },
124 {#State 14
125 ACTIONS => {
126 "[" => 29
127 },
128 DEFAULT => -26,
129 GOTOS => {
130 'OptAttrList' => 31
131 }
132 },
133 {#State 15
134 ACTIONS => {
135 ";" => 32
136 },
137 DEFAULT => -39,
138 GOTOS => {
139 'OptSemicolon' => 33
140 }
141 },
142 {#State 16
143 ACTIONS => {
144 'GRAPH' => 13,
145 'EDGE' => 17,
146 'NODE' => 19,
147 'NUMBER' => 8,
148 'QUOT' => 9,
149 "{" => 22,
150 'SUBGRAPH' => 24,
151 'ID' => 10
152 },
153 DEFAULT => -3,
154 GOTOS => {
155 'AttrStmt' => 15,
156 'NodeId' => 14,
157 'IdNumQuot' => 18,
158 'Stmt' => 34,
159 'Attr' => 23,
160 'Subgraph' => 25,
161 'EdgeStmt' => 26,
162 'NodeStmt' => 27,
163 'NodeIdSubgraph' => 28
164 }
165 },
166 {#State 17
167 ACTIONS => {
168 "[" => 29
169 },
170 DEFAULT => -33,
171 GOTOS => {
172 'OptAttrList' => 35
173 }
174 },
175 {#State 18
176 ACTIONS => {
177 ":" => 36,
178 "=" => 37
179 },
180 DEFAULT => -21
181 },
182 {#State 19
183 ACTIONS => {
184 "[" => 29
185 },
186 DEFAULT => -33,
187 GOTOS => {
188 'OptAttrList' => 38
189 }
190 },
191 {#State 20
192 ACTIONS => {
193 "}" => 39
194 }
195 },
196 {#State 21
197 DEFAULT => -10
198 },
199 {#State 22
200 DEFAULT => -7,
201 GOTOS => {
202 'BeginScope' => 41,
203 'WrappedStmtList' => 40
204 }
205 },
206 {#State 23
207 ACTIONS => {
208 ";" => 32
209 },
210 DEFAULT => -39,
211 GOTOS => {
212 'OptSemicolon' => 42
213 }
214 },
215 {#State 24
216 ACTIONS => {
217 'QUOT' => 9,
218 'NUMBER' => 8,
219 'ID' => 10
220 },
221 GOTOS => {
222 'IdNumQuot' => 43
223 }
224 },
225 {#State 25
226 ACTIONS => {
227 ";" => 32
228 },
229 DEFAULT => -25,
230 GOTOS => {
231 'OptSemicolon' => 44
232 }
233 },
234 {#State 26
235 ACTIONS => {
236 ";" => 32
237 },
238 DEFAULT => -39,
239 GOTOS => {
240 'OptSemicolon' => 45
241 }
242 },
243 {#State 27
244 ACTIONS => {
245 ";" => 32
246 },
247 DEFAULT => -39,
248 GOTOS => {
249 'OptSemicolon' => 46
250 }
251 },
252 {#State 28
253 ACTIONS => {
254 'EDGEOP' => 47,
255 "[" => 29
256 },
257 DEFAULT => -33,
258 GOTOS => {
259 'OptAttrList' => 48
260 }
261 },
262 {#State 29
263 ACTIONS => {
264 'QUOT' => 9,
265 'NUMBER' => 8,
266 "]" => 52,
267 'ID' => 10
268 },
269 GOTOS => {
270 'Attr' => 51,
271 'IdNumQuot' => 49,
272 'AttrList' => 50
273 }
274 },
275 {#State 30
276 DEFAULT => -17
277 },
278 {#State 31
279 DEFAULT => -20
280 },
281 {#State 32
282 DEFAULT => -40
283 },
284 {#State 33
285 DEFAULT => -12
286 },
287 {#State 34
288 DEFAULT => -11
289 },
290 {#State 35
291 DEFAULT => -19
292 },
293 {#State 36
294 ACTIONS => {
295 'QUOT' => 9,
296 'NUMBER' => 8,
297 'ID' => 10
298 },
299 GOTOS => {
300 'IdNumQuot' => 53
301 }
302 },
303 {#State 37
304 ACTIONS => {
305 'QUOT' => 9,
306 'NUMBER' => 8,
307 'ID' => 10
308 },
309 GOTOS => {
310 'IdNumQuot' => 54
311 }
312 },
313 {#State 38
314 DEFAULT => -18
315 },
316 {#State 39
317 ACTIONS => {
318 ";" => 32
319 },
320 DEFAULT => -39,
321 GOTOS => {
322 'OptSemicolon' => 55
323 }
324 },
325 {#State 40
326 ACTIONS => {
327 "}" => 56
328 }
329 },
330 {#State 41
331 ACTIONS => {
332 "{" => 22,
333 'QUOT' => 9,
334 'NUMBER' => 8,
335 'GRAPH' => 13,
336 'SUBGRAPH' => 24,
337 'ID' => 10,
338 'EDGE' => 17,
339 'NODE' => 19
340 },
341 GOTOS => {
342 'AttrStmt' => 15,
343 'NodeId' => 14,
344 'StmtList' => 57,
345 'IdNumQuot' => 18,
346 'Stmt' => 21,
347 'Attr' => 23,
348 'Subgraph' => 25,
349 'EdgeStmt' => 26,
350 'NodeStmt' => 27,
351 'NodeIdSubgraph' => 28
352 }
353 },
354 {#State 42
355 DEFAULT => -16
356 },
357 {#State 43
358 ACTIONS => {
359 "{" => 58
360 },
361 DEFAULT => -29
362 },
363 {#State 44
364 DEFAULT => -15
365 },
366 {#State 45
367 DEFAULT => -14
368 },
369 {#State 46
370 DEFAULT => -13
371 },
372 {#State 47
373 ACTIONS => {
374 "{" => 22,
375 'QUOT' => 9,
376 'NUMBER' => 8,
377 'SUBGRAPH' => 24,
378 'ID' => 10
379 },
380 GOTOS => {
381 'NodeId' => 59,
382 'Subgraph' => 61,
383 'EdgeStmt' => 62,
384 'IdNumQuot' => 60,
385 'NodeIdSubgraph' => 28
386 }
387 },
388 {#State 48
389 DEFAULT => -24
390 },
391 {#State 49
392 ACTIONS => {
393 "=" => 37
394 }
395 },
396 {#State 50
397 ACTIONS => {
398 "," => 64,
399 "]" => 65
400 },
401 DEFAULT => -41,
402 GOTOS => {
403 'OptComma' => 63
404 }
405 },
406 {#State 51
407 DEFAULT => -37
408 },
409 {#State 52
410 DEFAULT => -34
411 },
412 {#State 53
413 DEFAULT => -22
414 },
415 {#State 54
416 DEFAULT => -38
417 },
418 {#State 55
419 DEFAULT => -1
420 },
421 {#State 56
422 DEFAULT => -27
423 },
424 {#State 57
425 ACTIONS => {
426 'GRAPH' => 13,
427 'EDGE' => 17,
428 'NODE' => 19,
429 'NUMBER' => 8,
430 'QUOT' => 9,
431 "{" => 22,
432 'SUBGRAPH' => 24,
433 'ID' => 10
434 },
435 DEFAULT => -8,
436 GOTOS => {
437 'EndScope' => 66,
438 'AttrStmt' => 15,
439 'NodeId' => 14,
440 'IdNumQuot' => 18,
441 'Stmt' => 34,
442 'Attr' => 23,
443 'Subgraph' => 25,
444 'EdgeStmt' => 26,
445 'NodeStmt' => 27,
446 'NodeIdSubgraph' => 28
447 }
448 },
449 {#State 58
450 DEFAULT => -7,
451 GOTOS => {
452 'BeginScope' => 41,
453 'WrappedStmtList' => 67
454 }
455 },
456 {#State 59
457 DEFAULT => -26
458 },
459 {#State 60
460 ACTIONS => {
461 ":" => 36
462 },
463 DEFAULT => -21
464 },
465 {#State 61
466 DEFAULT => -25
467 },
468 {#State 62
469 DEFAULT => -23
470 },
471 {#State 63
472 ACTIONS => {
473 'QUOT' => 9,
474 'NUMBER' => 8,
475 'ID' => 10
476 },
477 GOTOS => {
478 'Attr' => 68,
479 'IdNumQuot' => 49
480 }
481 },
482 {#State 64
483 DEFAULT => -42
484 },
485 {#State 65
486 DEFAULT => -35
487 },
488 {#State 66
489 DEFAULT => -9
490 },
491 {#State 67
492 ACTIONS => {
493 "}" => 69
494 }
495 },
496 {#State 68
497 DEFAULT => -36
498 },
499 {#State 69
500 DEFAULT => -28
501 }
502 ],
503 yyrules =>
504 [
505 [#Rule 0
506 '$start', 2, undef
507 ],
508 [#Rule 1
509 'Graph', 6,
510 sub
511 #line 16 "Graph_Reader_Dot.yp"
512 {
513 # add the graph attributes...
514 my $r = $_[0]->YYData->{DefAttr}->[-1]->{Graph};
515 my $g = $_[0]->{GRAPH};
516 for my $attr (keys %{$r}) {
517 my $value = $r->{$attr};
518 $g->set_graph_attribute($attr,$value);
519 }
520 $g->set_graph_attribute('name', $_[2] ); # set name, will be reused by Graph::Writer::Dot
521 }
522 ],
523 [#Rule 2
524 'OptStmtList', 0,
525 sub
526 #line 28 "Graph_Reader_Dot.yp"
527 { #empty
528 return undef;
529 }
530 ],
531 [#Rule 3
532 'OptStmtList', 1, undef
533 ],
534 [#Rule 4
535 'GraphType', 2,
536 sub
537 #line 35 "Graph_Reader_Dot.yp"
538 {
539 return $_[2]; # dunno what to do with the strict anyway...
540 }
541 ],
542 [#Rule 5
543 'GraphType', 1, undef
544 ],
545 [#Rule 6
546 'GraphType', 1,
547 sub
548 #line 41 "Graph_Reader_Dot.yp"
549 {
550 &{$_[0]->YYData->{Options}->{Carp}}( "!graph will be treated as digraph" ); # see below for expl.
551 return $_[1];
552 }
553 ],
554 [#Rule 7
555 'BeginScope', 0,
556 sub
557 #line 48 "Graph_Reader_Dot.yp"
558 { # empty
559 # mah: use some clone() function?
560 # mah: for optimization should do lazy copying... (i.e. COW, copy-on-write)
561 my $n = {};
562 $n->{Graph} = { %{$_[0]->YYData->{DefAttr}->[-1]->{Graph}} };
563 $n->{Node} = { %{$_[0]->YYData->{DefAttr}->[-1]->{Node}} };
564 $n->{Edge} = { %{$_[0]->YYData->{DefAttr}->[-1]->{Edge}} };
565 push @{$_[0]->YYData->{DefAttr}}, $n;
566 }
567 ],
568 [#Rule 8
569 'EndScope', 0,
570 sub
571 #line 59 "Graph_Reader_Dot.yp"
572 { # empty
573 pop @{$_[0]->YYData->{DefAttr}};
574 }
575 ],
576 [#Rule 9
577 'WrappedStmtList', 3,
578 sub
579 #line 64 "Graph_Reader_Dot.yp"
580 {
581 return $_[2];
582 }
583 ],
584 [#Rule 10
585 'StmtList', 1,
586 sub
587 #line 69 "Graph_Reader_Dot.yp"
588 {
589 return $_[1];
590 }
591 ],
592 [#Rule 11
593 'StmtList', 2,
594 sub
595 #line 73 "Graph_Reader_Dot.yp"
596 {
597 for my $k ( keys %{$_[2]} ) { # merge the hashes
598 $_[1]->{$k}++;
599 }
600 return $_[1];
601 }
602 ],
603 [#Rule 12
604 'Stmt', 2,
605 sub
606 #line 81 "Graph_Reader_Dot.yp"
607 {
608 return {};
609 }
610 ],
611 [#Rule 13
612 'Stmt', 2, undef
613 ],
614 [#Rule 14
615 'Stmt', 2,
616 sub
617 #line 87 "Graph_Reader_Dot.yp"
618 {
619 return $_[1]->[2]; # only pass on the cumulative node set
620 }
621 ],
622 [#Rule 15
623 'Stmt', 2, undef
624 ],
625 [#Rule 16
626 'Stmt', 2,
627 sub
628 #line 93 "Graph_Reader_Dot.yp"
629 { # graph / subgraph attribute (i.e. same as graph [bla=3];)
630 my $r = $_[0]->YYData->{DefAttr}->[-1]->{Graph};
631 $r->{$_[1]->[0]} = $_[1]->[1];
632  
633 return {}; # no node returned...
634 }
635 ],
636 [#Rule 17
637 'AttrStmt', 2,
638 sub
639 #line 101 "Graph_Reader_Dot.yp"
640 {
641 _merge_hash( $_[0]->YYData->{DefAttr}->[-1]->{Graph}, $_[2] );
642 }
643 ],
644 [#Rule 18
645 'AttrStmt', 2,
646 sub
647 #line 105 "Graph_Reader_Dot.yp"
648 { # note: those will only apply to newly created nodes...
649 _merge_hash( $_[0]->YYData->{DefAttr}->[-1]->{Node}, $_[2] );
650 }
651 ],
652 [#Rule 19
653 'AttrStmt', 2,
654 sub
655 #line 109 "Graph_Reader_Dot.yp"
656 { # note: those will only apply to newly created edges...
657 _merge_hash( $_[0]->YYData->{DefAttr}->[-1]->{Edge}, $_[2] );
658 }
659 ],
660 [#Rule 20
661 'NodeStmt', 2,
662 sub
663 #line 114 "Graph_Reader_Dot.yp"
664 {
665 my $g = $_[0]->{GRAPH};
666 unless( $g->has_vertex($_[1]) ) {
667 $g->add_vertex($_[1]);
668 # default node attribute only apply to *new* nodes (as in dot)
669 # btw, that implies also, that the order is important in dot files for the attribute values...
670 if( $_[0]->YYData->{Options}->{UseNodeAttr} ) {
671 _set_attribute_hash( $g, $_[0]->YYData->{DefAttr}->[-1]->{Node}, $_[1] );
672 }
673 };
674 _set_attribute_hash( $g, $_[2], $_[1] );
675 return { $_[1] => 1 };
676 }
677 ],
678 [#Rule 21
679 'NodeId', 1, undef
680 ],
681 [#Rule 22
682 'NodeId', 3,
683 sub
684 #line 131 "Graph_Reader_Dot.yp"
685 {
686 &{$_[0]->YYData->{Options}->{Carp}}( "!cannot correctly process subnodes" );
687 return $_[1];
688 }
689 ],
690 [#Rule 23
691 'EdgeStmt', 3,
692 sub
693 #line 142 "Graph_Reader_Dot.yp"
694 {
695 my $g = $_[0]->{GRAPH};
696 for my $u (keys %{$_[1]}) {
697 for my $v (keys %{ @{$_[3]}[0] } ) {
698 # add non-existent nodes... (should make a separate loop for efficiency)
699 if( $_[0]->YYData->{Options}->{UseNodeAttr} ) {
700 unless ( $g->has_vertex($u) ) {
701 $g->add_vertex($u); # important
702 _set_attribute_hash( $g, $_[0]->YYData->{DefAttr}->[-1]->{Node}, $u );
703 }
704 unless( $g->has_vertex($v) ) {
705 $g->add_vertex($v); # important
706 _set_attribute_hash( $g, $_[0]->YYData->{DefAttr}->[-1]->{Node}, $v );
707 }
708 }
709 $g->add_edge($u,$v);
710 _set_attribute_hash($g, $_[3]->[1], $u, $v );
711 if( $_[0]->YYData->{Options}->{UseEdgeAttr} ) {
712 _set_attribute_hash($g, $_[0]->YYData->{DefAttr}->[-1]->{Edge}, $u, $v );
713 }
714 }
715 }
716 for my $u (keys %{$_[1]}) { # update cumulative node hash
717 $_[3]->[2]->{$u}++;
718 }
719 return [$_[1],$_[3]->[1], $_[3]->[2]];
720 }
721 ],
722 [#Rule 24
723 'EdgeStmt', 2,
724 sub
725 #line 170 "Graph_Reader_Dot.yp"
726 {
727 return [$_[1],$_[2],$_[1]]; # mah: not copying $_[1] is dangerous but works at the moment
728 # (it requires the other routines to keep the order of (a) make edges and (b) update cumulative nodes)
729 }
730 ],
731 [#Rule 25
732 'NodeIdSubgraph', 1, undef
733 ],
734 [#Rule 26
735 'NodeIdSubgraph', 1,
736 sub
737 #line 178 "Graph_Reader_Dot.yp"
738 {
739 return { $_[1] => 1 }
740 }
741 ],
742 [#Rule 27
743 'Subgraph', 3,
744 sub
745 #line 185 "Graph_Reader_Dot.yp"
746 { # anonymous subgraph
747 return $_[2];
748 }
749 ],
750 [#Rule 28
751 'Subgraph', 5,
752 sub
753 #line 189 "Graph_Reader_Dot.yp"
754 { # named subgraph
755 # have to store the nodeset somewhere...
756 if( defined $_[0]->YYData->{Subgraphs}->{$_[2]} ) {
757 # check for name clash for subgraph
758 die "?subgraph '$_[2]' has been doubly defined\n";
759 } else {
760 # *copy* the subgraphs nodes for later use
761 # mah: note: assumptions is, that the outside may (and in fact will) modify hash contents
762 $_[0]->YYData->{Subgraphs}->{$_[2]} = { %{$_[4]} };
763 }
764 return $_[4];
765 }
766 ],
767 [#Rule 29
768 'Subgraph', 2,
769 sub
770 #line 202 "Graph_Reader_Dot.yp"
771 { # subgraph reference (mah: what does it do?)
772 if( !defined $_[0]->YYData->{Subgraphs}->{$_[2]} ) {
773 # check for missing name
774 die "?subgraph '$_[2]' has not been defined\n";
775 } else {
776 # hand out copy...
777 return +{ %{$_[0]->YYData->{Subgraphs}->{$_[2]}} };
778 }
779 }
780 ],
781 [#Rule 30
782 'IdNumQuot', 1,
783 sub
784 #line 213 "Graph_Reader_Dot.yp"
785 {
786 return substr $_[1],1,-1;
787 }
788 ],
789 [#Rule 31
790 'IdNumQuot', 1, undef
791 ],
792 [#Rule 32
793 'IdNumQuot', 1, undef
794 ],
795 [#Rule 33
796 'OptAttrList', 0,
797 sub
798 #line 223 "Graph_Reader_Dot.yp"
799 { # may be empty
800 return {};
801 }
802 ],
803 [#Rule 34
804 'OptAttrList', 2,
805 sub
806 #line 227 "Graph_Reader_Dot.yp"
807 {
808 return {};
809 }
810 ],
811 [#Rule 35
812 'OptAttrList', 3,
813 sub
814 #line 231 "Graph_Reader_Dot.yp"
815 {
816 return $_[2];
817 }
818 ],
819 [#Rule 36
820 'AttrList', 3,
821 sub
822 #line 236 "Graph_Reader_Dot.yp"
823 {
824 my ($k,$v) = @{$_[3]};
825 $_[1]->{$k} = $v;
826 return $_[1];
827 }
828 ],
829 [#Rule 37
830 'AttrList', 1,
831 sub
832 #line 242 "Graph_Reader_Dot.yp"
833 {
834 return { @{$_[1]} }; # pull it into a hash reference
835 }
836 ],
837 [#Rule 38
838 'Attr', 3,
839 sub
840 #line 247 "Graph_Reader_Dot.yp"
841 {
842 return [$_[1],$_[3]];
843 }
844 ],
845 [#Rule 39
846 'OptSemicolon', 0,
847 sub
848 #line 252 "Graph_Reader_Dot.yp"
849 { #empty
850 return ';' # just for the sake of completeness...
851 }
852 ],
853 [#Rule 40
854 'OptSemicolon', 1,
855 sub
856 #line 256 "Graph_Reader_Dot.yp"
857 {
858 }
859 ],
860 [#Rule 41
861 'OptComma', 0,
862 sub
863 #line 260 "Graph_Reader_Dot.yp"
864 { # empty
865 return ','; # for the sake of completeness...
866 }
867 ],
868 [#Rule 42
869 'OptComma', 1,
870 sub
871 #line 264 "Graph_Reader_Dot.yp"
872 {
873 }
874 ]
875 ],
876 @_);
877 bless($self,$class);
878 }
879  
880 #line 267 "Graph_Reader_Dot.yp"
881  
882  
883 sub _merge_hash {
884 my ($dst,$src) = @_;
885 # merge keys and values of %{$src} in %{$dst}
886 for ( keys %$src ) {
887 $dst->{$_} = $src->{$_};
888 }
889 }
890  
891 sub _set_attribute_hash {
892 my $g = shift;
893 my $h = shift;
894 local $_;
895  
896 # @_ contains the destination... (graph, node, edge)
897 for (keys %$h ) {
898 if (@_ == 0) {
899 $g->set_graph_attribute(@_,$_,$h->{$_});
900 } elsif (@_ == 1) {
901 $g->set_vertex_attribute(@_,$_,$h->{$_});
902 } else {
903 $g->set_edge_attribute(@_,$_,$h->{$_});
904 }
905 }
906 }
907  
908 # lexer starts here:
909  
910 # build regexp for reserved words:
911 my @reserved = qw(
912 strict digraph graph edge node subgraph
913 );
914 my $reserved_re = qr{\b(@{[join '|', @reserved]})\b}oi;
915  
916 sub _Error {
917 exists $_[0]->YYData->{ERRMSG} and do {
918 print $_[0]->YYData->{ERRMSG};
919 delete $_[0]->YYData->{ERRMSG};
920 return;
921 };
922 print "\$_[0]->YYCurtok " . $_[0]->YYCurtok."\n";
923 print "\$_[0]->YYCurval " . $_[0]->YYCurval."\n";
924 print "\@\$_[0]->YYExpect " . (join " ", $_[0]->YYExpect )."\n";
925 print "\$_[0]->YYLexer " . $_[0]->YYLexer."\n";
926 print "substr(\$_[0]->YYData->{INPUT},0,21) " . substr($_[0]->YYData->{INPUT},0,21) . "...\n";
927 print "Syntax error.\n";
928 }
929  
930 sub _Lexer {
931 my($parser)=shift;
932 my $fh = $parser->{FILE};
933  
934 $parser->YYData->{INPUT} =~ s:^//.*::; # must be at beginning of string, this ensures it is unquoted (string are only single line)
935 if( $parser->YYData->{INPUT} eq '' ) {
936 do {
937 return ('',undef) if( $fh->eof );
938 $_ = <$fh>;
939 chomp;
940 s/^\s*//;
941 if( m:/\*: ) { # kill c-style comments
942 # TODO scan for eof...
943 while( ! s:/\*.*\*/::s ) {
944 $_ .= <$fh>;
945 }
946 chomp;
947 }
948 } while( m:^//: || $_ eq '' ); # skip comment & empty lines
949 $parser->YYData->{INPUT} = $_;
950 }
951  
952 $parser->YYData->{INPUT} =~ s/^($reserved_re)\s*// and return uc $1; # reserved word
953 $parser->YYData->{INPUT} =~ s/^(-[->])\s*// and return( 'EDGEOP', $1 ); # edge operator (directed or undirected)
954 $parser->YYData->{INPUT} =~ s/^([_a-zA-Z][._a-zA-Z0-9]*)\s*// and return( 'ID',$1 ); # identifier
955 $parser->YYData->{INPUT} =~ s/^(-?[0-9]*\.[0-9]*|-?[0-9]+)\s*// and return( 'NUMBER',$1 ); # number
956 $parser->YYData->{INPUT} =~ s/^(\"(?:\\\"|[^\"])*\")\s*// and return( 'QUOT', $1 ); # quoted string
957 $parser->YYData->{INPUT} =~ s/^(.)\s*//s and return($1,$1); # any char
958 }
959  
960 use Graph::Reader;
961 use vars qw(@ISA $UseNodeAttr $UseEdgeAttr);
962  
963 @ISA = qw(Parse::Yapp::Driver Graph::Reader); # this will override setting from yapp
964  
965 sub _init {
966 my $self = shift;
967 $self->SUPER::_init();
968 }
969  
970 sub _read_graph {
971 my $self = shift;
972 my $graph = shift;
973 my $FILE = shift;
974  
975 $self->{CONTEXT} = [];
976 $self->{GRAPH} = $graph;
977 $self->{FILE} = $FILE;
978 # initialize parse data structures...
979 undef $self->YYData->{Subgraphs}; # will contain node sets for every name subgraph
980 # clear default attribs for current scope:
981 $self->YYData->{DefAttr} = [{Graph=>{}, Node=>{}, Edge=>{}}];
982 $self->YYData->{Options}->{UseNodeAttr} = $UseNodeAttr;
983 $self->YYData->{Options}->{UseEdgeAttr} = $UseEdgeAttr;
984 $self->YYData->{Options}->{Carp} = \&Carp::carp;
985 $self->YYData->{Options}->{Croak} = \&Carp::croak;
986 # ^ now that's a workaround for not being able to declare Carp early enough, coz of Yapp restrictions...
987  
988 # the following kills a warning from the test regression suite:
989 $self->YYData->{INPUT} = '' unless defined $self->YYData->{INPUT};
990  
991 $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error );
992  
993 return 1;
994 }
995  
996 1;
997  
998 =head1 NAME
999  
1000 Graph::Reader::Dot - class for reading a Graph instance from Dot format
1001  
1002 =head1 SYNOPSIS
1003  
1004 use Graph::Reader::Dot;
1005 use Graph;
1006  
1007 $reader = Graph::Reader::Dot->new();
1008 $graph = $reader->read_graph('mygraph.dot');
1009  
1010 =head1 DESCRIPTION
1011  
1012 B<Graph::Reader::Dot> is a class for reading in a directed graph
1013 in the file format used by the I<dot> tool (part of the AT+T graphviz
1014 package).
1015  
1016 B<Graph::Reader::Dot> is a subclass of B<Graph::Reader>,
1017 which defines the generic interface for Graph reader classes.
1018  
1019 =head1 METHODS AND CONFIGURATION
1020  
1021 =head2 C<new()>
1022  
1023 Constructor - generate a new reader instance.
1024  
1025 $reader = Graph::Reader::Dot->new();
1026  
1027 This doesn't take any arguments.
1028  
1029 =head2 C<read_graph()>
1030  
1031 Read a graph from a file:
1032  
1033 $graph = $reader->read_graph( $file );
1034  
1035 The C<$file> argument can be either a filename
1036 or a filehandle of a previously opened file.
1037  
1038 =head2 C<$Graph::Reader::Dot::UseNodeAttr>
1039  
1040 Controls, if implicit node attributes given by the dot directive C<node[]> will be merged into (new) nodes.
1041 Setting it to C<0> or C<undef> (default) will not disable this feature.
1042 Setting it to any other value will enable this feature.
1043  
1044 =head2 C<$Graph::Reader::Dot::UseEdgeAttr>
1045  
1046 Controls, if implicit edge attributes given by the dot directive C<edge[]> will be merged into edges.
1047 Setting it to C<0> or C<undef> (default) will not disable this feature.
1048 Setting it to any other value will enable this feature.
1049  
1050 =head1 RESTRICTIONS
1051  
1052 =over 4
1053  
1054 =item *
1055  
1056 Default (graph) attributes in subgraphs (i.e. inside C<{}>) are not processed.
1057  
1058 =item *
1059  
1060 Sub nodes as used by dot's C<record> node shape are supported.
1061  
1062 =item *
1063  
1064 Undirected graphs will be treated as directed graphs.
1065 This means that the C<--> edge operator works as the C<-E<gt>> edge operator.
1066  
1067 =item *
1068  
1069 Be aware that you are loosing scope information on writing back the graph.
1070  
1071 =item *
1072  
1073 Multiple C<node[]> or C<edge[]> statements in the same scope are not correctly supported.
1074  
1075 =back
1076  
1077 =head1 SEE ALSO
1078  
1079 =over 4
1080  
1081 =item http://www.graphviz.org/
1082  
1083 The home page for the AT+T graphviz toolkit that
1084 includes the dot tool.
1085  
1086 =item Graph::Reader
1087  
1088 The base class for B<Graph::Reader::Dot>.
1089  
1090 =item Graph::Writer::Dot
1091  
1092 Used to serialise a Graph instance in Dot format.
1093  
1094 =item Graph
1095  
1096 Jarkko Hietaniemi's classes for representing directed graphs.
1097  
1098 =item Parse::Yapp
1099  
1100 Another base class for B<Graph::Reader::Dot>.
1101 The B<Parse::Yapp> module comes with the following copyright notice:
1102  
1103 The Parse::Yapp module and its related modules and shell
1104 scripts are copyright (c) 1998-1999 Francois Desarmenien,
1105 France. All rights reserved.
1106  
1107 You may use and distribute them under the terms of either
1108 the GNU General Public License or the Artistic License, as
1109 specified in the Perl README file.
1110  
1111 If you use the "standalone parser" option so people don't
1112 need to install Parse::Yapp on their systems in order to
1113 run you software, this copyright noticed should be
1114 included in your software copyright too, and the copyright
1115 notice in the embedded driver should be left untouched.
1116  
1117 =back
1118  
1119 =head1 AUTHOR
1120  
1121 Mark A. Hillebrand E<lt>mah@wjpserver.cs.uni-sb.deE<gt>
1122  
1123 =head1 COPYRIGHT
1124  
1125 Copyright (c) 2001 by Mark A. Hillebrand. All rights reserved.
1126  
1127 This module is free software; you can redistribute it and/or modify
1128 it under the same terms as Perl itself.
1129  
1130 =cut
1131  
1132  
1133 1;