Skip to content

Commit 4c1a78c

Browse files
committed
parse prototypes as Quote::Literal
1 parent 5b1b885 commit 4c1a78c

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for Perl extension PPI
22

33
{{$NEXT}}
4+
Summary:
5+
- Parse prototypes as literal quotes, enables parens and newlines in protos
6+
47
Details:
58
- Wrapped most Document->new calls in tests with automatic checks
69

lib/PPI/Token/Prototype.pm

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,7 @@ use PPI::Token ();
5151

5252
our $VERSION = '1.277';
5353

54-
our @ISA = "PPI::Token";
55-
56-
sub __TOKENIZER__on_char {
57-
my $class = shift;
58-
my $t = shift;
59-
60-
# Suck in until we find the closing paren (or the end of line)
61-
pos $t->{line} = $t->{line_cursor};
62-
die "regex should always match" if $t->{line} !~ m/\G(.*?\n?(?:\)|$))/gc;
63-
$t->{token}->{content} .= $1;
64-
$t->{line_cursor} += length $1;
65-
66-
# Shortcut if end of line
67-
return 0 unless $1 =~ /\)$/;
68-
69-
# Found the closing paren
70-
$t->_finalize_token->__TOKENIZER__on_char( $t );
71-
}
54+
our @ISA = "PPI::Token::Quote::Literal";
7255

7356
=pod
7457

lib/PPI/Token/_QuoteEngine/Full.pm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ my %QUOTES = (
4343
# used yet, since I'm not sure on the context differences between
4444
# this and the trinary operator, but it's here for completeness.
4545
'?' => { operator => undef, braced => 0, separator => '?', _sections => 1, modifiers => 1 },
46+
47+
# parse prototypes as a literal quote
48+
'(' => { operator => undef, braced => 1, separator => undef, _sections => 1, },
4649
);
4750

4851

@@ -70,9 +73,8 @@ sub new {
7073
$self->{modifiers} = {} if $self->{modifiers};
7174

7275
# Handle the special < base
73-
if ( $init eq '<' ) {
74-
$self->{sections}->[0] = Clone::clone( $SECTIONS{'<'} );
75-
}
76+
$self->{sections}[0] = Clone::clone $SECTIONS{'<'} if $init eq '<';
77+
$self->{sections}[0] = Clone::clone $SECTIONS{'('} if $init eq '(';
7678

7779
$self;
7880
}

t/ppi_token_prototype.t

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lib 't/lib';
66
use PPI::Test::pragmas;
7-
use Test::More tests => 104 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );
7+
use Test::More tests => 120 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );
88

99
use PPI ();
1010
use Helper 'safe_new';
@@ -34,7 +34,10 @@ PARSING: {
3434
## invalid chars in prototype
3535
check_w_subs \@sub_patterns, '(!-=|)', '(!-=|)', '!-=|';
3636
## perl refuses to compile this
37-
check_w_subs \@sub_patterns, '(()', '(()', '(';
37+
check_w_subs \@sub_patterns, '(()', '(()', '(', 1;
38+
check_w_subs \@sub_patterns, '((a))', '((a))', '(a)';
39+
check_w_subs \@sub_patterns, #
40+
"(\n(\na\n)\n)", "(\n(\na\n)\n)", "(a)";
3841
}
3942

4043
sub check_w_subs {
@@ -46,7 +49,7 @@ sub check_w_subs {
4649
sub check {
4750
local $Test::Builder::Level = $Test::Builder::Level + 1;
4851
my ( $name, $block, $code_prototype, $expected_content,
49-
$expected_prototype )
52+
$expected_prototype, $tail )
5053
= @_;
5154
my $desc = my $code = "$name$code_prototype$block";
5255
$desc =~ s/\n/\\n/g;
@@ -60,9 +63,10 @@ sub check {
6063
$all_prototypes = [] if !ref $all_prototypes;
6164
is scalar(@$all_prototypes), 1, "got exactly one prototype";
6265
my $prototype_obj = $all_prototypes->[0];
63-
is $prototype_obj, $expected_content,
66+
is $prototype_obj, $expected_content . ( $tail ? $block : "" ),
6467
"prototype object content matches";
65-
is $prototype_obj->prototype, $expected_prototype,
68+
is $prototype_obj->prototype,
69+
$expected_prototype . ( $tail ? ")$block" : "" ),
6670
"prototype characters match";
6771
};
6872
return;

t/ppi_token_quote_literal.t

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@
44

55
use lib 't/lib';
66
use PPI::Test::pragmas;
7-
use Test::More tests => 20 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );
7+
use Test::More tests => 23 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );
88
use B qw( perlstring );
99

1010
use PPI ();
1111
use Helper 'safe_new';
1212

13-
1413
STRING: {
15-
my $Document = safe_new \"print q{foo}, q!bar!, q <foo>;";
14+
my $Document = safe_new \"print q{foo}, q!bar!, q <foo>, q((foo));";
1615
my $literal = $Document->find('Token::Quote::Literal');
17-
is( scalar(@$literal), 3, '->find returns three objects' );
16+
is( scalar(@$literal), 4, '->find returns three objects' );
1817
isa_ok( $literal->[0], 'PPI::Token::Quote::Literal' );
1918
isa_ok( $literal->[1], 'PPI::Token::Quote::Literal' );
2019
isa_ok( $literal->[2], 'PPI::Token::Quote::Literal' );
21-
is( $literal->[0]->string, 'foo', '->string returns as expected' );
22-
is( $literal->[1]->string, 'bar', '->string returns as expected' );
23-
is( $literal->[2]->string, 'foo', '->string returns as expected' );
20+
isa_ok( $literal->[3], 'PPI::Token::Quote::Literal' );
21+
is( $literal->[0]->string, 'foo', '->string returns as expected' );
22+
is( $literal->[1]->string, 'bar', '->string returns as expected' );
23+
is( $literal->[2]->string, 'foo', '->string returns as expected' );
24+
is( $literal->[3]->string, '(foo)', '->string returns as expected' );
2425
}
2526

26-
2727
LITERAL: {
28-
my $Document = safe_new \"print q{foo}, q!bar!, q <foo>;";
28+
my $Document = safe_new \"print q{foo}, q!bar!, q <foo>, q((foo));";
2929
my $literal = $Document->find('Token::Quote::Literal');
30-
is( $literal->[0]->literal, 'foo', '->literal returns as expected' );
31-
is( $literal->[1]->literal, 'bar', '->literal returns as expected' );
32-
is( $literal->[2]->literal, 'foo', '->literal returns as expected' );
30+
is( $literal->[0]->literal, 'foo', '->literal returns as expected' );
31+
is( $literal->[1]->literal, 'bar', '->literal returns as expected' );
32+
is( $literal->[2]->literal, 'foo', '->literal returns as expected' );
33+
is( $literal->[3]->literal, '(foo)', '->literal returns as expected' );
3334
}
3435

3536
test_statement(

0 commit comments

Comments
 (0)