Permalink
Browse files

Support parsing the empty list '()'.

Reported-By: Chia-liang Kao <clkao@clkao.org>
  • Loading branch information...
1 parent d81a9fa commit 753c5e8effc3351c1b01658e2209247a42c249f7 @nelhage committed Jul 12, 2009
Showing with 43 additions and 5 deletions.
  1. +6 −3 lib/Data/SExpression.pm
  2. +1 −0 lib/Data/SExpression/Parser.yp
  3. +25 −1 t/03-lists.t
  4. +11 −1 t/04-folding.t
View
@@ -182,6 +182,9 @@ But see L</use_symbol_class> if you'd prefer to get back objects.
See L<Data::SExpression::Cons> for how to deal with these. See also
the C<fold_lists> and C<fold_alists> arguments to L</new>.
+If C<fold_lists> is false, the Lisp empty list C<()> becomes the perl
+C<undef>. With C<fold_lists>, it turns into C<[]> as you would expect.
+
=item Quotation is parsed as in scheme
This means that "'foo" is parsed like "(quote foo)", "`foo" like
@@ -199,8 +202,6 @@ sub read {
my $value = $self->get_parser->parse;
- croak("SExp Parse error") unless defined($value);
-
$value = $self->_fold_lists($value) if $self->get_fold_lists;
$value = $self->_fold_alists($value) if $self->get_fold_alists;
@@ -213,7 +214,9 @@ sub _fold_lists {
my $self = shift;
my $thing = shift;
- if(consp $thing) {
+ if(!defined($thing)) {
+ $thing = [];
+ } if(consp $thing) {
# Recursively fold the car
$thing->set_car($self->_fold_lists($thing->car));
@@ -32,6 +32,7 @@ list_interior:
expression '.' expression { $_[0]->handler->new_cons($_[1], $_[3]) }
| expression list_interior { $_[0]->handler->new_cons($_[1], $_[2]) }
| expression { $_[0]->handler->new_cons($_[1], undef) }
+ | { undef }
;
View
@@ -8,7 +8,7 @@ Test the parsing of lists, without folding.
=cut
-use Test::More tests => 8;
+use Test::More tests => 12;
use Test::Deep;
use Symbol;
@@ -123,3 +123,27 @@ cmp_deeply(
cdr => undef
))
);
+
+# Reported by clkao, 2009-07-08
+# Data::SExpression <= 0.37 don't handle the empty list
+
+is(scalar $ds->read('()'), undef);
+
+cmp_deeply(
+ scalar $ds->read('(())'),
+ methods(car => undef,
+ cdr => undef));
+
+cmp_deeply(
+ scalar $ds->read('(1 ())'),
+ methods(car => 1,
+ cdr => methods(
+ car => undef,
+ cdr => undef)));
+
+cmp_deeply(
+ scalar $ds->read('(() ())'),
+ methods(car => undef,
+ cdr => methods(
+ car => undef,
+ cdr => undef)));
View
@@ -8,7 +8,7 @@ Test the folding of Lisp lists and alists into perl lists and hashes.
=cut
-use Test::More tests => 8;
+use Test::More tests => 12;
use Test::Deep;
use Data::SExpression;
@@ -38,6 +38,16 @@ cmp_deeply(
],
"Read an alist");
+# Reported by clkao, 2009-07-08
+# Data::SExpression <= 0.37 don't handle the empty list
+
+cmp_deeply(scalar $ds->read('()'), [], "Folded the empty list");
+
+cmp_deeply(scalar $ds->read('(())'), [[]]);
+
+cmp_deeply(scalar $ds->read('(1 ())'), [1, []]);
+
+cmp_deeply(scalar $ds->read('(() ())'), [[],[]]);
$ds = Data::SExpression->new({fold_alists => 1});

0 comments on commit 753c5e8

Please sign in to comment.