Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
method invocation can now follows a bare block
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package Rubyish::Syntax::MethodCallWithBlock; | ||
use strict; | ||
use warnings; | ||
use 5.010; | ||
|
||
use B::Hooks::Parser; | ||
use B::Hooks::EndOfScope; | ||
use B::Generate; | ||
|
||
sub inject_close_paren { | ||
my $linestr = B::Hooks::Parser::get_linestr; | ||
my $offset = B::Hooks::Parser::get_linestr_offset; | ||
substr($linestr, $offset, 0) = ');'; | ||
B::Hooks::Parser::set_linestr($linestr); | ||
} | ||
|
||
sub block_checker { | ||
my ($op) = shift; | ||
my $linestr = B::Hooks::Parser::get_linestr; | ||
my $offset = B::Hooks::Parser::get_linestr_offset; | ||
my $code = substr($linestr, $offset); | ||
return unless $code ~~ /^->(?<method_name>\w+)(\((?<method_args>.*)\))?\s+{/; | ||
$code = "->$+{method_name}($+{method_args}, sub { BEGIN { B::Hooks::EndOfScope::on_scope_end(\\&Rubyish::Syntax::MethodCallWithBlock::inject_close_paren); } "; | ||
substr($linestr, $offset) = $code; | ||
B::Hooks::Parser::set_linestr($linestr); | ||
} | ||
|
||
sub import { | ||
my ($class) = @_; | ||
my $caller = caller; | ||
B::Hooks::Parser::setup(); | ||
my $linestr = B::Hooks::Parser::get_linestr(); | ||
my $offset = B::Hooks::Parser::get_linestr_offset(); | ||
substr($linestr, $offset, 0) = 'use B::Hooks::EndOfScope(); use B::OPCheck const => check => \&Rubyish::Syntax::MethodCallWithBlock::block_checker;'; | ||
B::Hooks::Parser::set_linestr($linestr); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env perl -w | ||
use strict; | ||
use Test::More; | ||
|
||
use Rubyish::Syntax::MethodCallWithBlock; | ||
|
||
sub Foo::bar { | ||
my ($self, $arg, $block) = @_; | ||
$block->($arg); | ||
} | ||
|
||
Foo->bar(42) { | ||
pass "Foo->bar(): block is called"; | ||
} | ||
pass " no syntax error"; | ||
|
||
my $foo = bless {}, "Foo"; | ||
|
||
$foo->bar(42) { | ||
pass '$foo->bar(): block is called'; | ||
} | ||
pass " no syntax error"; | ||
|
||
done_testing; |