[libtype-tiny-perl] 11/27: Type::Parser improvements

Jonas Smedegaard js at alioth.debian.org
Fri Aug 9 21:13:11 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libtype-tiny-perl.

commit efa9168e8605a4e318623fe20ed8ada461d20844
Author: Toby Inkster <mail at tobyinkster.co.uk>
Date:   Wed Jul 31 21:37:22 2013 +0100

    Type::Parser improvements
---
 lib/Type/Parser.pm |    7 ++-
 t/parser.t         |  132 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+), 1 deletion(-)

diff --git a/lib/Type/Parser.pm b/lib/Type/Parser.pm
index dac4e22..5893d6d 100644
--- a/lib/Type/Parser.pm
+++ b/lib/Type/Parser.pm
@@ -347,6 +347,7 @@ Evaluate: {
 	package # hide from CPAN
 	Type::Parser::TokenStream;
 	
+	use Scalar::Util qw(looks_like_number);
 	use Text::Balanced qw(extract_quotelike);
 	
 	sub new
@@ -463,7 +464,7 @@ Evaluate: {
 			return bless([ Type::Parser::QUOTELIKE, $quotelike ], "Type::Parser::Token"),;
 		}
 		
-		if ($self->{remaining} =~ /^([\w:]+)/sm)
+		if ($self->{remaining} =~ /^([+-]?[\w:.+]+)/sm)
 		{
 			my $spelling = $1;
 			substr($self->{remaining}, 0, length $spelling) = "";
@@ -472,6 +473,10 @@ Evaluate: {
 			{
 				return bless([ Type::Parser::CLASS, $spelling ], "Type::Parser::Token"),;
 			}
+			elsif (looks_like_number($spelling))
+			{
+				return bless([ Type::Parser::STRING, $spelling ], "Type::Parser::Token"),;
+			}
 			elsif ($self->{remaining} =~ /^\s*=>/sm) # peek ahead
 			{
 				return bless([ Type::Parser::STRING, $spelling ], "Type::Parser::Token"),;
diff --git a/t/parser.t b/t/parser.t
index dd89baf..5cbb939 100644
--- a/t/parser.t
+++ b/t/parser.t
@@ -132,4 +132,136 @@ note "Tail retention";
 my ($ast, $remaining) = parse("ArrayRef   [DateTime::]  |HashRef[ Int|\tDateTime::]|CodeRef monkey nuts ");
 is($remaining, " monkey nuts ", "remainder is ok");
 
+note "Parsing edge cases";
+is_deeply(
+	scalar parse('Xyzzy[Foo]'),
+	{
+		'type' => 'parameterized',
+		'base' => {
+			'type' => 'primary',
+			'token' => bless( [
+				'TYPE',
+				'Xyzzy'
+			], 'Type::Parser::Token' ),
+		},
+		'params' => {
+			'type' => 'list',
+			'list' => [
+				{
+					'type' => 'primary',
+					'token' => bless( [
+						'TYPE',
+						'Foo'
+					], 'Type::Parser::Token' ),
+				}
+			],
+		},
+	},
+	'Xyzzy[Foo] - parameter is treated as a type constraint'
+);
+is_deeply(
+	scalar parse('Xyzzy["Foo"]'),
+	{
+		'type' => 'parameterized',
+		'base' => {
+			'type' => 'primary',
+			'token' => bless( [
+				'TYPE',
+				'Xyzzy'
+			], 'Type::Parser::Token' ),
+		},
+		'params' => {
+			'type' => 'list',
+			'list' => [
+				{
+					'type' => 'primary',
+					'token' => bless( [
+						'QUOTELIKE',
+						'"Foo"'
+					], 'Type::Parser::Token' ),
+				}
+			],
+		},
+	},
+	'Xyzzy["Foo"] - parameter is treated as a string'
+);
+is_deeply(
+	scalar parse('Xyzzy[-100]'),
+	{
+		'type' => 'parameterized',
+		'base' => {
+			'type' => 'primary',
+			'token' => bless( [
+				'TYPE',
+				'Xyzzy'
+			], 'Type::Parser::Token' ),
+		},
+		'params' => {
+			'type' => 'list',
+			'list' => [
+				{
+					'type' => 'primary',
+					'token' => bless( [
+						'STRING',
+						'-100'
+					], 'Type::Parser::Token' ),
+				}
+			],
+		},
+	},
+	'Xyzzy[-100] - parameter is treated as a string'
+);
+is_deeply(
+	scalar parse('Xyzzy[200]'),
+	{
+		'type' => 'parameterized',
+		'base' => {
+			'type' => 'primary',
+			'token' => bless( [
+				'TYPE',
+				'Xyzzy'
+			], 'Type::Parser::Token' ),
+		},
+		'params' => {
+			'type' => 'list',
+			'list' => [
+				{
+					'type' => 'primary',
+					'token' => bless( [
+						'STRING',
+						'200'
+					], 'Type::Parser::Token' ),
+				}
+			],
+		},
+	},
+	'Xyzzy[200] - parameter is treated as a string'
+);
+is_deeply(
+	scalar parse('Xyzzy[+20.0]'),
+	{
+		'type' => 'parameterized',
+		'base' => {
+			'type' => 'primary',
+			'token' => bless( [
+				'TYPE',
+				'Xyzzy'
+			], 'Type::Parser::Token' ),
+		},
+		'params' => {
+			'type' => 'list',
+			'list' => [
+				{
+					'type' => 'primary',
+					'token' => bless( [
+						'STRING',
+						'+20.0'
+					], 'Type::Parser::Token' ),
+				}
+			],
+		},
+	},
+	'Xyzzy[+20.0] - parameter is treated as a string'
+);
+
 done_testing;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libtype-tiny-perl.git



More information about the Pkg-perl-cvs-commits mailing list