blib/lib/Data/Stag/IndentParser.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 125 CC 11 SCAR 43.5
CC
11
Cov
0%
parse_fh
132.0
fmtstr
2.0
pop_to_level
2.0
CRAP 77.3
1 # $Id: IndentParser.pm,v 1.4 2008/06/03 17:31:15 cmungall Exp $
2 #
3 # Copyright (C) 2002 Chris Mungall <cjm@fruitfly.org>
4 #
5 # See also - http://stag.sourceforge.net
6 #
7 # This module is free software.
8 # You may distribute this module under the same terms as perl itself
9  
10 package Data::Stag::IndentParser;
11  
12 =head1 NAME
13  
14 Data::Stag::IndentParser.pm - parses stag indent format into stag events
15  
16 =head1 SYNOPSIS
17  
18 =cut
19  
20 =head1 DESCRIPTION
21  
22  
23 =head1 AUTHOR
24  
25 =cut
26  
27 use Exporter;
28 use Carp;
29 use FileHandle;
30 use strict;
31 use base qw(Data::Stag::BaseGenerator Exporter);
32  
33 use vars qw($VERSION);
34 $VERSION="0.14";
35  
36 sub fmtstr {
37 return 'indent';
38 }
39  
40 our $elt_re = '^([\w\-\+\?\*\:]+)\s*(.*)';
41  
42 sub parse_fh {
43 my $self = shift;
44 my $fh = shift;
45 my @stack = ();
46  
47 while(<$fh>) {
48 chomp;
49 s/\\\#/MAGIC_WORD_HASH/g;
50 s/\#.*//;
51 s/MAGIC_WORD_HASH/\#/g;
52  
53 # remove trailing ws
54 s/\s*$//;
55 next unless $_;
56  
57 # get indent level
58 /(\s*)(.*)/s;
59  
60 my ($indent_txt, $elt) = ($1, $2);
61 my $indent = length($indent_txt);
62 if ($elt =~ /^$elt_re/) {
63 $elt = $1;
64 my $rest = $2;
65 my @atts = ();
66 if ($rest =~ /^\s*\[(.*)/) {
67 $rest = $1;
68 while ($rest =~ /^\s*([\w:]+)\s*=\s*(.*)/) {
69 push(@atts, $1);
70 $rest = $2;
71 if ($rest =~ /(.*?)([\s?\]].*)/) {
72 push(@atts, $1);
73 $rest = $2;
74 }
75 else {
76 confess("expected SPC | ], found $rest in $_");
77 }
78 }
79 if ($rest =~ /^\s*\]\s*(.*)/) {
80 $rest = $1;
81 }
82 else {
83 confess("expected ] | TAG=VAL, found $rest");
84 }
85 }
86  
87 $self->pop_to_level($indent, \@stack);
88 $self->start_event($elt);
89 if (@atts) {
90 $self->start_event('@');
91 while (my ($tag,$val) = splice(@atts,0,2)) {
92 $self->event($tag,$val);
93 }
94 $self->end_event('@');
95 }
96 if ($rest =~ /^\s*\"(.*)/) {
97 my $buf = $1;
98 my $str = '';
99 my $done = 0;
100 while (!$done) {
101 while (my $c = substr($buf,0,1)) {
102 $buf = substr($buf,1);
103 if ($c eq '"') {
104 $done = 1;
105 last;
106 }
107 $str .= $c;
108 }
109 if (!$done) {
110 $buf = <$fh>;
111 if (!$buf) {
112 confess("expected \", found EOF");
113 }
114 }
115 }
116 $self->evbody($str);
117 }
118 push(@stack, [$indent, $elt]);
119 }
120 else {
121 confess("expected INDENT-ELEMENT, found $_");
122 }
123 }
124 $fh->close;
125 $self->pop_to_level(0, \@stack);
126 return;
127 }
128  
129 sub pop_to_level {
130 my $self = shift;
131 my $indent = shift;
132 my $stack = shift;
133  
134 while (scalar(@$stack) &&
135 $stack->[-1]->[0] >= $indent) {
136 $self->end_event($stack->[-1]->[1]);
137 pop(@$stack);
138 }
139 return;
140 }
141  
142 1;