karupanerura / TOML-Parser

simple toml parser
Other
15 stars 6 forks source link

TOML::Parser 0.05 does not recognize some dates from the TOML examples. #4

Closed biztos closed 8 years ago

biztos commented 9 years ago

This may or may not be a "TOML 0.4 thing" but I have a proof and a suggested patch.

In short, of these three dates, only the first is parsed, the other two generate errors:

date1 = 1979-05-27T07:32:00Z
date2 = 1979-05-27T00:32:00-07:00
date3 = 1979-05-27T00:32:00.999999-07:00

I guess I should make a pull request for this but in case I don't get around to it, here's my proof and suggested change. I haven't tested it thoroughly so please don't assume it works.

# TOML::Parser 0.05 does not recognize some dates from the TOML examples.
#
# Suggested patch is below, past the __END__.

use strict;
use warnings;

use Test::More tests => 3;

BEGIN {
    use_ok('TOML::Parser') or die "failed to load target class";
}

# Examples from: https://github.com/toml-lang/toml#user-content-datetime
# (As of 2015-08-14 anyway.)

my $toml = <<'_TOML';
date1 = 1979-05-27T07:32:00Z
date2 = 1979-05-27T00:32:00-07:00
date3 = 1979-05-27T00:32:00.999999-07:00
_TOML

my $parser = TOML::Parser->new();
note $toml;
my $data = eval { $parser->parse($toml) };
is( $@, '', "parsed without error" );
is_deeply(
    $data,
    {   date1 => '1979-05-27T07:32:00Z',
        date2 => '1979-05-27T00:32:00-07:00',
        date3 => '1979-05-27T00:32:00.999999-07:00'
    },
    "parsed correctly"
);

__END__
--- Tokenizer-orig.pm   2015-08-14 12:34:02.000000000 -0700
+++ Tokenizer.pm    2015-08-14 12:33:11.000000000 -0700
@@ -42,7 +42,23 @@
         array_of_table => qr{\[\[([^.\s\\\]]+(?:\.[^.\s\\\]]+)*)\]\]},
         key            => qr{([^\s]+)\s*=},
         value          => {
-            datetime => qr{([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)},
+            datetime => qr{
+                (
+                    [0-9]{4}- # YYYY
+                    [0-9]{2}- # MM
+                    [0-9]{2}  # DD
+                    T
+                    [0-9]{2}: # HH
+                    [0-9]{2}: # MI
+                    [0-9]{2}  # SS
+                    (?:[.][0-9]+)? # seconds can be a float
+                    (?:
+                        Z
+                        |
+                        -[0-9]{2}:[0-9]{2} # TZ
+                    )
+                )
+            }x,
             float    => qr{(-?[0-9]*\.[0-9]+)},
             integer  => qr{(-?[0-9]+)},
             boolean  => qr{(true|false)},
karupanerura commented 9 years ago

Thank you for reporting. This module current support version is v0.2.0. (https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.2.0.md#datetime) I'll be support it on #2.

karupanerura commented 8 years ago

Done. You can try on feature/0.4 branch.

karupanerura commented 8 years ago

merged

karupanerura commented 8 years ago

Released it as version 0.6 to CPAN.