Skip to content

Commit

Permalink
Type::Parser should support 0x123ABC when parsing integers; closes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyink committed May 2, 2021
1 parent d051aba commit 63f57ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Type/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $VERSION =~ tr/_//d;
sub TYPE () { "TYPE" }
sub QUOTELIKE () { "QUOTELIKE" }
sub STRING () { "STRING" }
sub HEXNUM () { "HEXNUM" }
sub CLASS () { "CLASS" }
sub L_BRACKET () { "L_BRACKET" }
sub R_BRACKET () { "R_BRACKET" }
Expand Down Expand Up @@ -120,6 +121,10 @@ Evaluate: {
return $node->{token}->spelling;
}

if ( $node->{type} eq "primary" and $node->{token}->type eq HEXNUM ) {
return hex( $node->{token}->spelling );
}

if ( $node->{type} eq "primary" and $node->{token}->type eq TYPE ) {
my $t = $node->{token}->spelling;
my $r =
Expand Down Expand Up @@ -248,6 +253,7 @@ Evaluate: {
if ( $type eq Type::Parser::TYPE
or $type eq Type::Parser::QUOTELIKE
or $type eq Type::Parser::STRING
or $type eq Type::Parser::HEXNUM
or $type eq Type::Parser::CLASS )
{
return { type => "primary", token => $tokens->eat };
Expand Down Expand Up @@ -434,6 +440,9 @@ Evaluate: {
if ( $spelling =~ /::$/sm ) {
return bless( [ Type::Parser::CLASS, $spelling ], "Type::Parser::Token" );
}
elsif ( $spelling =~ /^[+-]?0x[0-9A-Fa-f]+$/sm ) {
return bless( [ Type::Parser::HEXNUM, $spelling ], "Type::Parser::Token" );
}
elsif ( looks_like_number( $spelling ) ) {
return bless( [ Type::Parser::STRING, $spelling ], "Type::Parser::Token" );
}
Expand Down Expand Up @@ -532,6 +541,8 @@ The following constants correspond to values returned by C<< $token->type >>.
=item C<< STRING >>
=item C<< HEXNUM >>
=item C<< CLASS >>
=item C<< L_BRACKET >>
Expand Down
7 changes: 7 additions & 0 deletions t/20-unit/Type-Parser/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ types_equal("Int[]", Int, "empty parameterization against non-parameterizable ty
types_equal("Tuple[]", Tuple[], "empty parameterization against parameterizble type");
types_equal("ArrayRef[]", ArrayRef, "empty parameterization against parameterizable type");
types_equal("ArrayRef[Int]", ArrayRef[Int], "parameterized type");
types_equal("Overload[15]", Overload[15], "numeric parameter (decimal integer)");
types_equal("Overload[0x0F]", Overload[15], "numeric parameter (hexadecimal integer)");
types_equal("Overload[0x0f]", Overload[15], "numeric parameter (hexadecimal integer, lowercase)");
types_equal("Overload[1.5]", Overload[1.5], "numeric parameter (float)");
types_equal("Ref['HASH']", Ref['HASH'], "string parameter (singles)");
types_equal("Ref[\"HASH\"]", Ref['HASH'], "string parameter (doubles)");
types_equal("Ref[q(HASH)]", Ref['HASH'], "string parameter (q)");
types_equal("Ref[qq(HASH)]", Ref['HASH'], "string parameter (qq)");
types_equal("StrMatch[qr{foo}]", StrMatch[qr{foo}], "regexp parameter");

# No, Overload[15] doesn't make much sense, but it's one of the few types in
# Types::Standard that accept pretty much any list of strings as parameters.

note "Unions";
types_equal("Int|HashRef", Int|HashRef);
types_equal("Int|HashRef|ArrayRef", Int|HashRef|ArrayRef);
Expand Down

0 comments on commit 63f57ad

Please sign in to comment.