blib/lib/Data/SExpression.pm

filter: Statementstmt 100% 101 / 101 Branchbran 100% 38 / 38 Conditioncond 88.2% 15 / 17 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 20 / 20 Podpod 88.8% 8 / 9 totaltotal 97.4% 190 / 195 CC 27 SCAR 13.2
CC
27
Cov
99%
_fold_lists
8.0
new_symbol
5.0
read
4.0
CRAP 3.7
« blib/lib/Data/SExpression/Symbol.pm ↑ Summary blib/lib/Data/SExpression/Cons.pm »
1 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Data::SExpression;
5  
6 8 our $VERSION = '0.41';
7  
8 =head1 NAME
9  
10 Data::SExpression -- Parse Lisp S-Expressions into perl data
11 structures.
12  
13 =head1 SYNOPSIS
14  
15 use Data::SExpression;
16  
17 my $ds = Data::SExpression->new;
18  
19 $ds->read("(foo bar baz)"); # [\*::foo, \*::bar, \*::baz]
20  
21 my @sexps;
22 my $sexp;
23 while(1) {
24 eval {
25 ($sexp, $text) = $ds->read($text);
26 };
27 last if $@;
28 push @sexps, $sexp;
29 }
30  
31 $ds = Data::SExpression->new({fold_alists => 1});
32  
33 $ds->read("((top . 4) (left . 5))"); # {\*::top => 4, \*::left => 5}
34  
35 =cut
36  
37 8 ▶use base qw(Class::Accessor::Fast Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 __PACKAGE__->follow_best_practice;
39 8 __PACKAGE__->mk_ro_accessors(qw(parser symbol_case use_symbol_class fold_dashes fold_lists fold_alists));
40  
41 8 our @EXPORT_OK = qw(cons consp scalarp);
42  
43 8 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use Data::SExpression::Cons;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use Data::SExpression::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use Data::SExpression::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49  
50 sub cons ($$);
51 sub consp ($);
52 sub scalarp ($);
53  
54  
55 =head1 METHODS
56  
57 =head2 new [\%args]
58  
59 Returns a new Data::SExpression object. Possibly args are:
60  
61 =over 4
62  
63 =item fold_lists
64  
65 If true, fold lisp lists (e.g. "(1 2 3)") into Perl listrefs, e.g. [1, 2, 3]
66  
67 Defaults to true.
68  
69 =item fold_alists
70  
71 If true, fold lisp alists into perl hashrefs. e.g.
72  
73 C<"((fg . red) (bg . black) (weight . bold))">
74  
75 would become
76  
77 {
78 \*fg => \*red,
79 \*bg => \*black,
80 \*weight => \*bold
81 }
82  
83 Alists will only be folded if they are a list of conses, all of which
84 have scalars as both their C<car> and C<cdr> (See
85 L<Data::SExpression::Cons/scalarp>)
86  
87 This option implies L</fold_lists>
88  
89 Defaults to false.
90  
91 =item symbol_case
92  
93 Can be C<"up">, C<"down">, or C<undef>, to fold symbol case to
94 uppercase, lowercase, or to leave as-is.
95  
96 Defaults to leaving symbols alone.
97  
98 =item use_symbol_class
99  
100 If true, symbols become instances of L<Data::SExpression::Symbol>
101 instead of globrefs.
102  
103 Defaults to false
104  
105 =item fold_dashes
106  
107 If true, dash characters in symbols (C<->) will be folded to the more
108 perlish underscore, C<_>. This is especially convenient when symbols
109 are being converted to globrefs.
110  
111 Defaults to false.
112  
113 =back
114  
115 =cut
116  
117 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
118 12 my $class = shift;
119 12 ▶ my $args = shift || {};
Condition100%2/2
shift() || {}
l!l
75
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
120  
121 12 my $parser = Data::SExpression::Parser->new;
122  
123 12 ▶ $args->{fold_lists} = 1 if $args->{fold_alists};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"fold_alists"} 1 11 12
124  
125 12 my $self = {
126 fold_lists => 1,
127 fold_alists => 0,
128 symbol_case => 0,
129 use_symbol_class => 0,
130 fold_dashes => 0,
131 %$args,
132 parser => $parser,
133 };
134
135 12 bless($self, $class);
136  
137 12 $parser->set_handler($self);
138  
139 12 return $self;
140 }
141  
142 =head2 read STRING
143  
144 Parse an SExpression from the start of STRING, or die if the parse
145 fails.
146  
147 In scalar context, returns the expression parsed as a perl data
148 structure; In list context, also return the part of STRING left
149 unparsed. This means you can read all the expressions in a string
150 with:
151  
152 my @sexps;
153 my $sexp;
154 while(1) {
155 eval {
156 ($sexp, $text) = $ds->read($text);
157 };
158 last if $@;
159 push @sexps, $sexp;
160 }
161  
162  
163 This method converts Lisp SExpressions into perl data structures by
164 the following rules:
165  
166 =over 4
167  
168 =item Numbers and Strings become perl scalars
169  
170 Lisp differentiates between the types; perl doesn't.
171  
172 =item Symbols become globrefs in main::
173  
174 This means they become something like \*main::foo, or \*::foo for
175 short. To convert from a string to a symbol, you can use
176 L<Symbol/qualify_to_ref>, with C<"main"> as the package.
177  
178 But see L</use_symbol_class> if you'd prefer to get back objects.
179  
180 =item Conses become Data::SExpression::Cons objects
181  
182 See L<Data::SExpression::Cons> for how to deal with these. See also
183 the C<fold_lists> and C<fold_alists> arguments to L</new>.
184  
185 If C<fold_lists> is false, the Lisp empty list C<()> becomes the perl
186 C<undef>. With C<fold_lists>, it turns into C<[]> as you would expect.
187  
188 =item Quotation is parsed as in scheme
189  
190 This means that "'foo" is parsed like "(quote foo)", "`foo" like
191 "(quasiquote foo)", and ",foo" like "(unquote foo)".
192  
193 =back
194  
195 =cut
196  
197 54 ▶sub read {
Subroutine read: called CC=4 SCAR=13.9
Pod documented
198 54 my $self = shift;
199 54 my $string = shift;
200  
201 54 $self->get_parser->set_input($string);
202
203 54 my $value = $self->get_parser->parse;
204  
205 53 ▶ $value = $self->_fold_lists($value) if $self->get_fold_lists;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_fold_lists 41 12 53
206 53 ▶ $value = $self->_fold_alists($value) if $self->get_fold_alists;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_fold_alists 5 48 53
207  
208 53 my $unparsed = $self->get_parser->unparsed_input;
209  
210 53 ▶ return wantarray ? ($value, $unparsed) : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 49 53
211 }
212  
213 123 ▶sub _fold_lists {
Subroutine _fold_lists: called CC=8 SCAR=20.8
214 123 my $self = shift;
215 123 my $thing = shift;
216  
217 123 ▶ if(!defined($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $thing) 5 118 123
218 5 $thing = [];
219 123 ▶ } if(consp $thing) {
Branch: 1 branch
BranchTrueFalseTotal
if (&consp($thing)) 41 82 123
220 # Recursively fold the car
221 41 $thing->set_car($self->_fold_lists($thing->car));
222  
223 # Unroll the cdr-folding, since recursing over really long
224 # lists will net us warnings
225 41 ▶ if(consp $thing->cdr || !defined($thing->cdr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&consp(scalar $thing->cdr) or not defined $thing->cdr) 33 8 41
Condition100%3/3
&consp(scalar $thing->cdr) or not defined $thing->cdr
l!l&&r!l&&!r
3038
MC/DC100%2/2
&consp(scalar $thing->cdr) or not defined $thing->cdr
&consp(scalar $thing->cdr) not defined $thing->cdr
Truth table100%3/3
A or not(B)
A= &consp(scalar $thing->cdr)B= not defined $thing->cdr
ABresult
1X1
011
000
226 33 my $cdr = $thing->cdr;
227 33 my $array;
228 33 ▶ while(consp $cdr) {
Branch: 1 branch
BranchTrueFalseTotal
if (&consp($cdr)) 42 33 75
229 42 $cdr = $cdr->cdr;
230 }
231 33 ▶ if(defined($cdr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cdr) 1 32 33
232 # We hit the end of the chain, and found something other
233 # than nil. This is an improper list.
234 1 return $thing;
235 }
236
237 32 $array = [$thing->car];
238 32 $cdr = $thing->cdr;
239 32 ▶ while(defined $cdr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cdr) 41 32 73
240 41 push @$array, $self->_fold_lists($cdr->car);
241 41 $cdr = $cdr->cdr;
242 }
243 32 return $array;
244 }
245 }
246  
247 90 return $thing;
248 }
249  
250 13 ▶sub for_all(&@) {$_[0]() or return 0 foreach (@_[1..$#_]); 1;}
Subroutine for_all: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->() or return 0 3 12 15
251  
252 37 ▶sub _fold_alists {
Subroutine _fold_alists: called CC=4 SCAR=13.9
253 37 my $self = shift;
254 37 my $thing = shift;
255  
256 #Assume $thing has already been list-folded
257  
258 37 ▶ if(ref($thing) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 13 24 37
elsif (&consp($thing)) { } 2 22 24
259 13 ▶ if( for_all {consp $_ && scalarp $_->car && scalarp $_->cdr} @{$thing} ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
if (&for_all(sub { &consp($_) and &scalarp(scalar $_->car) and &scalarp(scalar $_->cdr); } , @{$thing;})) { } 1 12 13
&consp($_) and &scalarp(scalar $_->car) and &scalarp(scalar $_->cdr) 3 12 15
Condition100%3/3
&consp($_) and &scalarp(scalar $_->car)
!ll&&!rl&&r
1113
MC/DC100%2/2
&consp($_) and &scalarp(scalar $_->car)
&consp($_) &scalarp(scalar $_->car)
Truth table100%3/3
A and B
A= &consp($_)B= &scalarp(scalar $_->car)
ABresult
0X0
100
111
260 1 return {map {$_->car => $_ -> cdr} @{$thing}};
261 } else {
262 12 return [map {$self->_fold_alists($_)} @{$thing}];
263 }
264 } elsif(consp $thing) {
265 2 return cons($self->_fold_alists($thing->car),
266 $self->_fold_alists($thing->cdr));
267 } else {
268 22 return $thing;
269 }
270 }
271  
272 =head1 LISP-LIKE CONVENIENCE FUNCTIONS
273  
274 These are all generic methods to make operating on cons's easier in
275 perl. You can ask for any of these in the export list, e.g.
276  
277 use Data::SExpression qw(cons consp);
278  
279 =head2 cons CAR CDR
280  
281 Convenience method for Data::SExpression::Cons->new(CAR, CDR)
282  
283 =cut
284  
285 116 ▶sub cons ($$) {
Subroutine cons: called CC=1 SCAR=0.0
Pod documented
286 116 my ($car, $cdr) = @_;
287 116 return Data::SExpression::Cons->new($car, $cdr);
288 }
289  
290 =head2 consp THING
291  
292 Returns true iff C<THING> is a reference to a
293 C<Data::SExpression::Cons>
294  
295 =cut
296  
297 278 ▶sub consp ($) {
Subroutine consp: called CC=2 SCAR=6.9
Pod documented
298 278 my $thing = shift;
299 278 ▶ return ref($thing) && UNIVERSAL::isa($thing, 'Data::SExpression::Cons');
Condition100%3/3
ref $thing && UNIVERSAL::isa($thing, 'Data::SExpression::Cons')
!ll&&!rl&&r
6495119
MC/DC100%2/2
ref $thing && UNIVERSAL::isa($thing, 'Data::SExpression::Cons')
ref $thing UNIVERSAL::isa($thing, 'Data::SExpression::Cons')
Truth table100%3/3
A && B
A= ref $thingB= UNIVERSAL::isa($thing, 'Data::SExpression::Cons')
ABresult
0X0
100
111
300 }
301  
302 =head2 scalarp THING
303  
304 Returns true iff C<THING> is a scalar -- i.e. a string, symbol, or
305 number
306  
307 =cut
308  
309 7 ▶sub scalarp ($) {
Subroutine scalarp: called CC=3 SCAR=11.4
Pod documented
310 7 my $thing = shift;
311 7 ▶ return !ref($thing) ||
Condition66%4/6
!ref($thing) || ref $thing eq 'GLOB' || ref $thing eq 'Data::SExpression::Symbol'
l!l&&r!l&&!r
601
not ref $thing or ref $thing eq "GLOB"
l!l&&r!l&&!r
061
MC/DC33%1/3
!ref($thing) || ref $thing eq 'GLOB' || ref $thing eq 'Data::SExpression::Symbol'
not ref $thing ref $thing eq "GLOB" ref $thing eq 'Data::SExpression::Symbol'
Truth table50%2/4
not(A) or B || C
A= not ref $thingB= ref $thing eq "GLOB"C= ref $thing eq 'Data::SExpression::Symbol'
ABCresult
1XX1
01X1
0011
0000
312 ref($thing) eq "GLOB" ||
313 ref($thing) eq 'Data::SExpression::Symbol';;
314 }
315  
316 =head1 Data::SExpression::Parser callbacks
317  
318 These are for internal use only, and are used to generate the data
319 structures returned by L</read>.
320  
321 =head2 new_cons CAR CDR
322  
323 Returns a new cons with the given CAR and CDR
324  
325 =cut
326  
327 114 ▶sub new_cons {
Subroutine new_cons: called CC=1 SCAR=0.0
Pod documented
328 114 my ($self, $car, $cdr) = @_;
329 114 return cons($car, $cdr);
330 }
331  
332 =head2 new_symbol NAME
333  
334 Returns a new symbol with the given name
335  
336 =cut
337  
338 68 ▶sub new_symbol {
Subroutine new_symbol: called CC=5 SCAR=16.1
Pod documented
339 68 my ($self, $name) = @_;
340 68 ▶ if($self->get_symbol_case eq 'up') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_symbol_case eq 'up') { } 2 66 68
elsif ($self->get_symbol_case eq 'down') { } 2 64 66
341 2 $name = uc $name;
342 } elsif($self->get_symbol_case eq 'down') {
343 2 $name = lc $name;
344 }
345  
346 68 ▶ if($self->get_fold_dashes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_fold_dashes) 1 67 68
347 1 $name =~ tr/-/_/;
348 }
349  
350 68 ▶ if($self->get_use_symbol_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_use_symbol_class) { } 1 67 68
351 1 return Data::SExpression::Symbol->new($name);
352 } else {
353 67 return Symbol::qualify_to_ref($name, 'main');
354 }
355 }
356  
357 =head2 new_string CONTENT
358  
359 Returns a new string with the given raw content
360  
361 =cut
362  
363 12 ▶sub new_string {
Subroutine new_string: called CC=1 SCAR=0.0
Pod documented
364 12 my ($self, $content) = @_;
365  
366 12 $content =~ s/\\"/"/g;
367  
368 12 return $content;
369 }
370  
371 =head1 BUGS
372  
373 None known, but there are probably a few. Please reports bugs via
374 rt.cpan.org by sending mail to:
375  
376 L<bug-Data-SExpression@rt.cpan.org>
377  
378  
379 =head1 AUTHOR
380  
381 Nelson Elhage <nelhage@mit.edu>
382  
383 =cut
384  
385 8 1;
386  
« blib/lib/Data/SExpression/Symbol.pm ↑ Summary blib/lib/Data/SExpression/Cons.pm »