From 14bc56a71a9d31a2eea2e4f36271366351e8fdc1 Mon Sep 17 00:00:00 2001 From: reneeb Date: Thu, 27 Aug 2020 12:36:50 +0200 Subject: [PATCH 1/2] parse a format (see perldoc -f format) as a single token. Currently PPI parses formats in a wrong way (see also https://github.com/Perl-Critic/Perl-Critic/issues/917) --- lib/PPI/Token.pm | 1 + lib/PPI/Token/Format.pm | 84 +++++++++++++++++++++++++++++++++++++ lib/PPI/Token/Whitespace.pm | 6 ++- t/ppi_token_format.t | 52 +++++++++++++++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 lib/PPI/Token/Format.pm create mode 100644 t/ppi_token_format.t diff --git a/lib/PPI/Token.pm b/lib/PPI/Token.pm index 223a439d..10439f3c 100644 --- a/lib/PPI/Token.pm +++ b/lib/PPI/Token.pm @@ -37,6 +37,7 @@ use PPI::Token::BOM (); use PPI::Token::Whitespace (); use PPI::Token::Comment (); use PPI::Token::Pod (); +use PPI::Token::Format (); use PPI::Token::Number (); use PPI::Token::Number::Binary (); use PPI::Token::Number::Octal (); diff --git a/lib/PPI/Token/Format.pm b/lib/PPI/Token/Format.pm new file mode 100644 index 00000000..f62f51c2 --- /dev/null +++ b/lib/PPI/Token/Format.pm @@ -0,0 +1,84 @@ +package PPI::Token::Format; + +=pod + +=head1 NAME + +PPI::Token::Format - A format for the write function + +=head1 INHERITANCE + + PPI::Token::Pod + isa PPI::Token + isa PPI::Element + +=head1 DESCRIPTION + +A single C object represents a single format section + +=head1 METHODS + +This class provides no additional methods beyond those provided by its +L and L parent classes. + +=cut + +use strict; +use Params::Util qw{_INSTANCE}; +use PPI::Token (); + +# VERSION + +our @ISA = "PPI::Token"; + +##################################################################### +# PPI::Element Methods + +### XS -> PPI/XS.xs:_PPI_Token_Pod__significant 0.900+ +sub significant() { '' } + + + + + +##################################################################### +# Tokenizer Methods + +sub __TOKENIZER__on_line_start { + my $t = $_[1]; + + # Add the line to the token first + $t->{token}->{content} .= $t->{line}; + + # Check the line to see if it is a =cut line + if ( $t->{line} =~ /^\.$/ ) { + # End of the token + $t->_finalize_token; + } + + 0; +} + +1; + +=pod + +=head1 SUPPORT + +See the L in the main module. + +=head1 AUTHOR + +Adam Kennedy Eadamk@cpan.orgE + +=head1 COPYRIGHT + +Copyright 2001 - 2011 Adam Kennedy. + +This program is free software; you can redistribute +it and/or modify it under the same terms as Perl itself. + +The full text of the license can be found in the +LICENSE file included with this module. + +=cut diff --git a/lib/PPI/Token/Whitespace.pm b/lib/PPI/Token/Whitespace.pm index 4ff941a1..83e729a9 100644 --- a/lib/PPI/Token/Whitespace.pm +++ b/lib/PPI/Token/Whitespace.pm @@ -186,7 +186,11 @@ sub __TOKENIZER__on_line_start { # anything to the "use v6..." line. So return as if # we didn't find anything at all. return 1; - } + } elsif ( $line =~ /^\s*format\s*[A-Za-z0-9_]+\s*=\s*$/ ) { + $t->_new_token( 'Format', $line ); + $t->{class} = 'PPI::Token::Format'; + return 0; + } 1; } diff --git a/t/ppi_token_format.t b/t/ppi_token_format.t new file mode 100644 index 00000000..fb214328 --- /dev/null +++ b/t/ppi_token_format.t @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# Unit testing for PPI::Token::Format + +use lib 't/lib'; +use PPI::Test::pragmas; +use Test::More tests => 4; + +use PPI; + + +use lib 't/lib'; +use Helper 'check_with'; + +my $Document = PPI::Document->new(\<<'END_PERL'); +#!/usr/bin/env perl +use strict; + +format BYDEPTH_TOP = +Top disk utilization in @*, @* level(s) deep + $BASEPATH, $DEPTH +================================================================================ + +subpath disk utilization +------- ---------------- +. + +__END__ + +=head1 SYNOPSIS + + ⋮ + -d, --depth DEPTH[,...] displays disk usage DEPTH levels into hierarchy + (default: 2); separate multiple DEPTHs with commas + --[no-]by-depth [suppresses] displays usage by depth in hierarchy + -q, --[no-]quiet suppress progress messages (implied for '-r') + FILE is the output of a previous 'find DIR -printf ...' + invocation, as described below; use '-' for stdin + +=cut +END_PERL + +isa_ok( $Document, 'PPI::Document' ); +my $formats = $Document->find('Token::Format'); +is( scalar @{$formats}, 1, 'Found the 1 format' ); + +my $uses = $Document->find('Statement::Include'); +is( scalar @{$uses}, 1, 'Found the 1 include' ); + +my $pods = $Document->find('Token::Pod'); +is( scalar @{$pods}, 1, 'Found the 1 pod section' ); + From 7f77c674600f4ba91ca5e4137b813b331f1210e5 Mon Sep 17 00:00:00 2001 From: reneeb Date: Wed, 21 Oct 2020 08:07:07 +0200 Subject: [PATCH 2/2] formats are significant --- lib/PPI/Token/Format.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/PPI/Token/Format.pm b/lib/PPI/Token/Format.pm index f62f51c2..6c07079f 100644 --- a/lib/PPI/Token/Format.pm +++ b/lib/PPI/Token/Format.pm @@ -35,7 +35,7 @@ our @ISA = "PPI::Token"; # PPI::Element Methods ### XS -> PPI/XS.xs:_PPI_Token_Pod__significant 0.900+ -sub significant() { '' } +sub significant() { 1 }