Skip to content

Commit

Permalink
start to fail() with typed exceptions from Str.Numeric
Browse files Browse the repository at this point in the history
somewhere along the way it still stringifies though :(
  • Loading branch information
moritz committed Mar 25, 2012
1 parent 06b4eca commit 008dd40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -508,4 +508,24 @@ my class X::Phaser::PrePost is Exception {
}
}

my class X::Str::Numeric is Exception {
has $.source;
has $.pos;
has $.reason;
method source-indicator {
constant marker = chr(0x23CF);
join '', "in '",
$.source.substr(0, $.pos),
marker,
$.source.substr($.pos),
"' (indicated by the ",
marker,
" symbol)",
;
}
method message() {
"Cannot convert string to number: $.reason $.source-indicator";
}
}

# vim: ft=perl6
2 changes: 1 addition & 1 deletion src/core/Failure.pm
Expand Up @@ -39,7 +39,7 @@ my class Failure {


my &fail := -> *@msg {
my $value = @msg.join('');
my $value = @msg == 1 ?? @msg[0] !! @msg.join('');
die $value if $*FATAL;
my Mu $ex := Q:PIR {
# throw and immediately catch an exception, to capture
Expand Down
9 changes: 6 additions & 3 deletions src/core/Str.pm
Expand Up @@ -2,6 +2,7 @@ my class Cursor {... }
my class Range {... }
my class Match {... }
my class Buf {... }
my class X::Str::Numeric { ... }

sub PARROT_ENCODING(Str:D $s) {
my %map = (
Expand Down Expand Up @@ -161,14 +162,16 @@ my class Str does Stringy {
my int $pos = pir::find_not_cclass__Iisii(pir::const::CCLASS_WHITESPACE, $str, 0, $eos);

my $tailfail =
-> { fail "trailing characters after number in conversion"
if nqp::islt_i(
-> { fail(X::Str::Numeric.new(
source => self,
:$pos,
reason => 'trailing characters after number',
)) if nqp::islt_i(
pir::find_not_cclass__Iisii(pir::const::CCLASS_WHITESPACE,
$str, $pos, $eos),
$eos);
0;
};

# objects for managing the parse and results
my Mu $parse;
my $result;
Expand Down

0 comments on commit 008dd40

Please sign in to comment.