Permalink
Browse files

tweaks to make => behave properly and some more tests

  • Loading branch information...
1 parent bb9878d commit c404b8ddd857b3874905f6974cd2370ed9423d76 @demerphq committed May 17, 2012
Showing with 37 additions and 18 deletions.
  1. +17 −10 Undump.xs
  2. +10 −5 t/01_undump.t
  3. +10 −3 t/02_error.t
View
@@ -232,6 +232,7 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
char token= TOKEN_ERROR;
char want_key= 0;
char allow_comma= 0;
+ char require_fat_comma= 0;
char stop_char;
char ch;
const char *key;
@@ -263,6 +264,7 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
REPARSE:
while (parse_ptr < parse_end) {
+ /* warn("want_key: %d require_fat_comma: %d allow_comma: %d\n", want_key, require_fat_comma, allow_comma); */
token_start= parse_ptr;
token= TOKEN_ERROR;
ch= *(parse_ptr++);
@@ -274,16 +276,17 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
if ( *parse_ptr != '>' ) {
ERROR(depth,token,token_start,parse_ptr,parse_end,"Encountered assignment '=' or unterminated fat comma '=>'");
}
+ require_fat_comma = 0;
parse_ptr++;
/* fallthrough */
case ',':
/* comma */
- if ( ! allow_comma ) {
- if ( want_key ) {
- ERROR(depth,token,token_start,parse_ptr,parse_end,"unexpected comma when expecting a key");
- } else {
- ERROR(depth,token,token_start,parse_ptr,parse_end,"unexpected comma when expecting a value");
- }
+ if ( require_fat_comma ) {
+ ERROR(depth,token,token_start,parse_ptr,parse_end,"expected fat comma after bareword");
+ }
+ else if ( ! allow_comma ) {
+ ERRORf2(depth,token,token_start,parse_ptr,parse_end,"unexpected %s when expecting a %s",
+ (ch=='=' ? "fat comma" : "comma"),(want_key ? "key" : "value"));
}
allow_comma = 0;
goto REPARSE;
@@ -424,6 +427,11 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
token= TOKEN_BAREWORD;
}
} /* switch */
+ if (require_fat_comma) {
+ ERROR(depth,token,token_start,parse_ptr,parse_end,"expected fat comma after bareword");
+ } else if (allow_comma && token != TOKEN_CLOSE) {
+ ERRORf1(depth,token,token_start,parse_ptr,parse_end,"Expecting comma got %s",token_name[token]);
+ }
SHOW_TOKEN(depth,token,token_start,parse_ptr);
switch (token) {
case TOKEN_BLESS:
@@ -633,13 +641,13 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
grok_len= esc_read - grok_start;
esc_read++; /* skip '}' */
}
- // warn("hex: %.*s\n", grok_len, grok_start);
+ /* warn("hex: %.*s\n", grok_len, grok_start); */
if (grok_len) {
cp= grok_hex((char *)grok_start, &grok_len, &grok_flags, 0);
} else {
ERROR(depth,token,token_start,parse_ptr,parse_end,"empty \\x{} escape?");
}
- // warn("cp: %d\n len: %d flags: %d", cp, grok_len, grok_flags);
+ /* warn("cp: %d\n len: %d flags: %d", cp, grok_len, grok_flags); */
if ( cp < 0x100 ) { /* otherwise it would be in octal */
must_uni= 1;
}
@@ -691,10 +699,9 @@ SV* _undump(pTHX_ const char **parse_start, const char const *parse_end, char ob
}
case TOKEN_BAREWORD:
/* fallthrough */
+ require_fat_comma= 1;
if (want_key) {
DONE_KEY_SIMPLE_break;
- } else {
- ERROR(depth,token,token_start,parse_ptr,parse_end,"got a bareword where it was not expected");
}
if (got) {
ERROR(depth,token,token_start,parse_ptr,parse_end,"Multiple objects in stream?");
View
@@ -12,18 +12,19 @@ our @dump;
push @dump, $_;
}
}
-plan tests => 1 + 2 * @dump;
+plan tests => 1 + 3 * @dump;
pass();
sub dd { return Data::Dumper->new([$_[0]])->Purity(1)->Useqq(1)->Sortkeys(1)->Dump() }
sub check {
my $dump= shift;
my $undumped= dd(my $struct= undump($dump));
+ my $show_diag= !is( $@||undef, undef, "after undump \$\@ was false");
my $evaled= dd(eval($dump));
-
- $dump eq "undef"
+ $show_diag += !($dump eq "undef"
? pass("undumping undef")
- : isnt($struct, undef, "undump succeeded: >>$dump<<");
- return is_string($undumped,$evaled,"undump and eval agree");
+ : isnt($struct, undef, "undump returned something"));
+ $show_diag += !is_string($undumped,$evaled,"undump and eval agree");
+ $show_diag and diag($dump);
}
check($_) for @dump;
@@ -62,6 +63,8 @@ undef
{ foo => 'bar' }
+{ foo => bar => baz => undef }
+
[ 1 ]
[ 1, [ 2 ] ]
@@ -70,6 +73,8 @@ undef
[ 1 , 2 , [ 3 , 4 , { 5 => 6 , 7 => { 8 => [ ] } , 9 => { } } , { }, [ ] ] ]
+[ a => 'b' ]
+
{
foo => 123,
bar => -159.23 ,
View
@@ -28,7 +28,7 @@ __DATA__
{ foo => [ | unterminated ARRAY constructor
-{ foo foo => | got a bareword where it was not expected
+{ foo foo => | expected fat comma after bareword
"foo | unterminated double quoted string
@@ -46,6 +46,13 @@ __DATA__
{ undef => {} } | got an undef when we wanted a key
-[ a => 'b' ] | got a bareword where it was not expected
+{ a,,,b } | expected fat comma after bareword
+
+{ a,1 } | expected fat comma after bareword
+
+{ a => => b } | unexpected fat comma when expecting a value
+
+{ a => x => => b } | unexpected fat comma when expecting a key
+
+$VAR1 | Encountered variable in input. This is not eval - can not undump code
-{ a,,,b } | unexpected comma when expecting a value

0 comments on commit c404b8d

Please sign in to comment.