blib/lib/Data/Stag/SxprParser.pm

filter: Statementstmt 86.4% 83 / 96 Branchbran 70.6% 41 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 2 totaltotal 80.0% 132 / 165 CC 30 SCAR 34.6
CC
30
Cov
81%
parse_fh
40.0
fmtstr
2.0
BEGIN
1.0
CRAP 31.8
« blib/lib/Data/Stag/StagImpl.pm ↑ Summary blib/lib/Data/Stag/IndentWriter.pm »
1 # $Id: SxprParser.pm,v 1.22 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::SxprParser;
11  
12 =head1 NAME
13  
14 SxprParser.pm - parses Stag S-expression format
15  
16 =head1 SYNOPSIS
17  
18 =cut
19  
20 =head1 DESCRIPTION
21  
22  
23 =head1 AUTHOR
24  
25 =cut
26  
27 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Data::Stag qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use base qw(Data::Stag::BaseGenerator Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 $VERSION="0.14";
36  
37 0 ▶sub fmtstr {
Subroutine fmtstr: not called CC=1 SCAR=6.9
Pod undocumented
38 0 return 'sxpr';
39 }
40  
41 7 ▶sub parse_fh {
Subroutine parse_fh: called CC=30 SCAR=36.9
Pod undocumented
42 7 my $self = shift;
43 7 my $fh = shift;
44 7 my $line_no = 0;
45 7 my $state = "init";
46 7 my $txt = '';
47  
48 # c: comment ;
49 # q: quote (double quote only)
50 # o: opening tag - after round bracket, before whitespace or ()
51 # 0: body, or awaiting open or close
52 7 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 105 7 112
53 105 $line_no++;
54  
55 105 ▶ while (length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 1956 105 2061
56 1956 my $c = substr($line,0,1,'');
57 1956 ▶ if ($state eq 'init') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "init") 11 1945 1956
58 11 ▶ if ($c eq '(') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '(') { } 7 4 11
elsif ($c eq q[']) { } 4 0 4
elsif ($c =~ /\s/) { } 0 0 0
elsif ($c eq ';') { } 0 0 0
59 # at start - do nothing
60 7 $state = 0;
61 }
62 elsif ($c eq "'") {
63 # leading quote is allowed [list constructor in lisp]
64 # (good for editing in emacs)
65 4 next;
66 0 $state = 0;
67 }
68 elsif ($c =~ /\s/) {
69 0 next;
70 }
71 elsif ($c eq ';') {
72 0 $state = 'c';
73 }
74 else {
75 0 $self->throw("Line: $line_no\n$line\nExpected \"(\" at start of file");
76 }
77 }
78 1952 ▶ $state ne 'init' || $self->throw("assertion error: state=$state");
Branch: 1 branch
BranchTrueFalseTotal
$state ne "init" or $self->throw("assertion error: state=$state") 1952 0 1952
79
80 1952 ▶ if ($state eq 'c') { # comment
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "c") 0 1952 1952
81 # newline is the only char that can break out of comments
82 0 ▶ if ($c eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\n") 0 0 0
83 0 $state = 0;
84 }
85 0 next;
86 }
87 1952 ▶ $state ne 'c' || $self->throw("assertion error: state=$state");
Branch: 1 branch
BranchTrueFalseTotal
$state ne "c" or $self->throw("assertion error: state=$state") 1952 0 1952
88
89 1952 ▶ if ($state eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "q") 465 1487 1952
90 465 ▶ if ($c eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq '"') { } 62 403 465
91 62 $state = 0;
92 }
93 else {
94 403 $txt .= $c;
95 }
96 465 next;
97 }
98 1487 ▶ $state ne 'q' || $self->throw("assertion error: state=$state");
Branch: 1 branch
BranchTrueFalseTotal
$state ne "q" or $self->throw("assertion error: state=$state") 1487 0 1487
99
100 1487 ▶ if ($c eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"") 62 1425 1487
101 62 $state = 'q';
102 62 next;
103 }
104 1425 ▶ $state ne 'q' || $self->throw("assertion error: state=$state");
Branch: 1 branch
BranchTrueFalseTotal
$state ne "q" or $self->throw("assertion error: state=$state") 1425 0 1425
105
106 1425 ▶ if ($c eq ';') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ";") 0 1425 1425
107 # can only open comments when NOT in quotes
108 0 $state = 'c';
109 0 next;
110 }
111
112 1425 ▶ if ($c eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 110 1315 1425
113 110 ▶ if ($state eq 'o') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "o") 6 104 110
114 6 ▶ if (!$txt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txt) 0 6 6
115 0 $self->throw("Line: $line_no\b$line\ndouble open brackets (( not allowed!");
116 }
117 6 $self->start_event($txt);
118 6 $txt = '';
119 }
120 110 $state = 'o';
121 110 next;
122 }
123 1315 ▶ if ($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 110 1205 1315
124 110 ▶ if ($state eq 'o') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'o') { } 1 109 110
125 1 ▶ if (!$txt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txt) 0 1 1
126 0 $self->throw("Line: $line_no\b$line\n () not allowed!");
127 }
128 1 $self->start_event($txt);
129 1 $txt = '';
130 1 $self->end_event;
131 }
132 else {
133 109 ▶ if ($txt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt) 66 43 109
134 66 $self->evbody($txt);
135 }
136 109 $txt = '';
137 109 $self->end_event;
138 }
139 110 $state=0;
140 110 next;
141 }
142 1205 ▶ if ($state eq 'o') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "o") 759 446 1205
143 759 ▶ if ($c =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) { } 103 656 759
144 # reached last char of start event name
145 103 $self->start_event($txt);
146 103 $txt = '';
147 103 $state = 0;
148 103 next;
149 }
150 else {
151 656 $txt .= $c;
152 656 next;
153 }
154 }
155 446 ▶ $state == 0 || $self->throw("assertion error: state=$state");
Branch: 1 branch
BranchTrueFalseTotal
$state == 0 or $self->throw("assertion error: state=$state") 446 0 446
156 446 ▶ if ($c =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\s/) 442 4 446
157 442 next;
158 }
159 4 $txt .= $c;
160 4 next;
161 }
162 }
163 7 ▶ if ($txt =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /\S/) 0 7 7
164 0 $self->throw("text at end: $txt");
165 }
166 }
167  
168 4 1;
« blib/lib/Data/Stag/StagImpl.pm ↑ Summary blib/lib/Data/Stag/IndentWriter.pm »