| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2023-2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 5 | ▶ | use v5.36; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 5 | ▶ | use Object::Pad 0.800; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | role App::perl::distrolint::CheckRole::TreeSitterPerl 0.09; | ||
| 10 | |||
| 11 | 5 | ▶ | use Syntax::Keyword::Match 0.13; # case if |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 5 | ▶ | use File::Slurper qw( read_text ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 5 | ▶ | use List::Util 1.29 qw( min ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | 0 | ▶ | use Text::Treesitter; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Text::Treesitter::Query; | ||
| 18 | use Text::Treesitter::QueryCursor 0.10; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | C<App::perl::distrolint::CheckRole::TreeSitterPerl> - role for checks that parse Perl source code | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | =for highlighter language=perl | ||
| 27 | |||
| 28 | This role provides a number of helper methods for implementing check classes | ||
| 29 | that parse Perl source code, by using F<tree-sitter-perl>. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | my $TSPERL; | ||
| 38 | |||
| 39 | =head2 tree_sitter_perl | ||
| 40 | |||
| 41 | $ts = $check->tree_sitter_perl; | ||
| 42 | |||
| 43 | Returns a singleton L<Text::Treesitter> instance configured for parsing the | ||
| 44 | Perl language, using F<tree-sitter-perl>. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | method tree_sitter_perl () | ||
| 49 | { | ||
| 50 | return $TSPERL //= Text::Treesitter->new( | ||
| 51 | lang_name => "perl", | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | my %TREE_FOR_STRING; | ||
| 56 | |||
| 57 | =head2 parse_perl_string | ||
| 58 | |||
| 59 | $tree = $check->parse_perl_string( $str ); | ||
| 60 | |||
| 61 | Returns a cached L<Text::Treesitter::Tree> instance obtained by parsing the | ||
| 62 | given source string. As multiple checks may wish to inspect the same file, and | ||
| 63 | hence build the same tree, this will be cached and re-returned as necessary. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | method parse_perl_string ( $str ) | ||
| 68 | { | ||
| 69 | return $TREE_FOR_STRING{ $str } //= $self->tree_sitter_perl->parse_string( $str ); | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 parse_perl_file | ||
| 73 | |||
| 74 | $tree = $check->parse_perl_file( $path ); | ||
| 75 | |||
| 76 | A convenient shortcut for reading the text of the given path, then parsing it | ||
| 77 | using L</parse_perl_string>. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | method parse_perl_file( $path ) | ||
| 82 | { | ||
| 83 | return $self->parse_perl_string( read_text $path ); | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 walk_each_statement | ||
| 87 | |||
| 88 | $ok = $check->walk_each_statement( $node, $method, @args ); | ||
| 89 | |||
| 90 | $ok = $self->$method( $child_node, @args ); | ||
| 91 | |||
| 92 | A trampoline method that invokes the method on the given arguments on every | ||
| 93 | statement node found as a descendent of the node passed in. | ||
| 94 | |||
| 95 | Stops and returns false the first time the invoked method returns false, or | ||
| 96 | returns true if the invoked method returns true for every call. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | method _walk_block ( $node, $method, @args ) | ||
| 101 | { | ||
| 102 | foreach my $kid ( $node->child_nodes ) { | ||
| 103 | next if $kid->is_extra; | ||
| 104 | |||
| 105 | $self->walk_each_statement( $kid, $method, @args ) | ||
| 106 | or return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | method walk_each_statement ( $node, $method, @args ) | ||
| 113 | { | ||
| 114 | my $type = $node->is_named ? sprintf( "(%s)", $node->type ) : $node->type; | ||
| 115 | |||
| 116 | match( $type : eq ) { | ||
| 117 | # Containers of statements | ||
| 118 | case("(source_file)"), case("(block_statement)") { | ||
| 119 | $self->_walk_block( $node, $method, @args ) or return 0; | ||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | case("(phaser_statement)") { | ||
| 123 | # Ugh, the grammar didn't name this with a field | ||
| 124 | my ( $block ) = grep { $_->type eq "block" } $node->child_nodes; | ||
| 125 | return $self->walk_each_statement( $block, $method, @args ); | ||
| 126 | } | ||
| 127 | |||
| 128 | case("(subroutine_declaration_statement)"), | ||
| 129 | case("(method_declaration_statement") { | ||
| 130 | my $block = $node->try_child_by_field_name( "body" ) or return 1; | ||
| 131 | $self->_walk_block( $block, $method, @args ) or return 0; | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | # Direct statements | ||
| 136 | case if( $type =~ m/^\(.*_statement\)$/ ) { | ||
| 137 | $self->$method( $node, @args ) | ||
| 138 | or return 0; | ||
| 139 | |||
| 140 | # Certain kinds of statements have a body block | ||
| 141 | if( my $block = $node->try_child_by_field_name( "block" ) ) { | ||
| 142 | $self->_walk_block( $block, $method, @args ) or return 0; | ||
| 143 | } | ||
| 144 | |||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 walk_each_query_match | ||
| 152 | |||
| 153 | $ok = $check->walk_each_query_match( $src, $node, $method, @args ); | ||
| 154 | |||
| 155 | $ok = $self->$method( \%captures, @args ); | ||
| 156 | |||
| 157 | A trampoline method that invokes the method on the given arguments on every | ||
| 158 | match of the given query source against the node passed in. | ||
| 159 | |||
| 160 | Stops and returns false the first time the invoked method returns false, or | ||
| 161 | returns true if the invoked method returns true for every call. | ||
| 162 | |||
| 163 | The invoked method is passed a reference to a hash, containing mappings from | ||
| 164 | capture names to the nodes of the tree that were captured by them. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | my %QUERY_FOR_SRC; | ||
| 169 | |||
| 170 | method walk_each_query_match ( $src, $node, $method, @args ) | ||
| 171 | { | ||
| 172 | my $query = $QUERY_FOR_SRC{ $src } //= Text::Treesitter::Query->new( | ||
| 173 | $self->tree_sitter_perl->lang, $src | ||
| 174 | ); | ||
| 175 | |||
| 176 | my $qc = Text::Treesitter::QueryCursor->new; | ||
| 177 | |||
| 178 | $qc->exec( $query, $node ); | ||
| 179 | |||
| 180 | while( my $captures = $qc->next_match_captures ) { | ||
| 181 | $self->$method( $captures, @args ) or return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 walk_each_scoped_query_match | ||
| 188 | |||
| 189 | $ok = $check->walk_each_scoped_query_match( $src, $node, $method, $context = {}, @args ); | ||
| 190 | |||
| 191 | $ok = $self->$method( \%captures, $context, @args ); | ||
| 192 | |||
| 193 | I<Since version 0.09.> | ||
| 194 | |||
| 195 | A trampoline method that invokes the method on the given arguments on every | ||
| 196 | match of the given query source against the node passed in. Similar to | ||
| 197 | L</walk_each_query_match> except that this version understands the block | ||
| 198 | nesting scope of Perl source code, and maintains a "context" hash scoped | ||
| 199 | alongside it. | ||
| 200 | |||
| 201 | Stops and returns false the first time the invoked method returns false, or | ||
| 202 | returns true if the invoked method returns true for every call. | ||
| 203 | |||
| 204 | The invoked method is passed a reference to a hash, containing mappings from | ||
| 205 | capture names to the nodes of the tree that were captured by them, and another | ||
| 206 | reference to a different hash, containing the current context items. | ||
| 207 | |||
| 208 | Each time a new scope is entered, a shallow-clone of the C<$context> hash is | ||
| 209 | taken, and it is this copy passed into the method for calls within that scope. | ||
| 210 | The method may freely inspect or modify that hash, knowing that any (shallow) | ||
| 211 | modifications made will be undone at the end of the current Perl scope. This | ||
| 212 | makes it useful for tracking state related to Perl parsing. | ||
| 213 | |||
| 214 | The query given in C<$src> is not permitted to use the capture name C<@scope>, | ||
| 215 | as this method will use that for its own purposes. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | method walk_each_scoped_query_match ( $query, $root, $method, $root_context = {}, @args ) | ||
| 220 | { | ||
| 221 | $query = "[(block_statement) (block)] \@scope\n\n" . $query; | ||
| 222 | |||
| 223 | # First build up a flat list of the query matches and note their start points | ||
| 224 | my @captures; | ||
| 225 | $self->walk_each_query_match( $query, $root, sub ( $, $captures ) { | ||
| 226 | my $min_start_byte = min map { $_->start_byte } values %$captures; | ||
| 227 | $captures->{min_start_byte} = $min_start_byte; | ||
| 228 | push @captures, $captures; | ||
| 229 | } ); | ||
| 230 | |||
| 231 | # Then walk the list of matches tracking state as we go | ||
| 232 | # Use __SUB__ recursion to enter nested scopes | ||
| 233 | (sub ( $end_byte, $context ) { | ||
| 234 | CAPTURE: while( @captures ) { | ||
| 235 | last CAPTURE if defined $end_byte and $captures[0]->{min_start_byte} > $end_byte; | ||
| 236 | |||
| 237 | my $c = shift @captures; | ||
| 238 | delete $c->{min_start_byte}; | ||
| 239 | my $node; | ||
| 240 | |||
| 241 | if( $c->{scope} ) { | ||
| 242 | __SUB__->( $c->{scope}->end_byte, { %$context } ) or | ||
| 243 | return 0; | ||
| 244 | } | ||
| 245 | else { | ||
| 246 | $self->$method( $c, $context, @args ) or | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | return 1; | ||
| 252 | })->( undef, $root_context ); | ||
| 253 | } | ||
| 254 | |||
| 255 | =head2 extract_use_module_imports | ||
| 256 | |||
| 257 | @imports = $check->extract_use_module_imports( $node ); | ||
| 258 | |||
| 259 | Given a C<(use_statement)> node, analyse it to determine a list of | ||
| 260 | statically-known import strings and return it. Simple string literals in | ||
| 261 | C<'single quotes'>, C<"double quotes"> or C<qw( quoted word lists )> are | ||
| 262 | parsed fairly simply. Other kinds of expressions are ignored. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | method extract_use_module_imports ( $node ) | ||
| 267 | { | ||
| 268 | $node->type eq "use_statement" or | ||
| 269 | die "Expected a (use_statement) node"; | ||
| 270 | |||
| 271 | # TODO this would be a lot neater if tree-sitter-perl named the imports list | ||
| 272 | |||
| 273 | my @named_kids = $node->field_names_with_child_nodes; | ||
| 274 | |||
| 275 | # First kid is "use" keyword | ||
| 276 | shift @named_kids; shift @named_kids; | ||
| 277 | |||
| 278 | # Second kid really better be the module | ||
| 279 | shift @named_kids eq "module" or die "Expected a 'module:' named node"; shift @named_kids; | ||
| 280 | |||
| 281 | # Might be a version now | ||
| 282 | if( @named_kids and ($named_kids[0]//"") eq "version" ) { | ||
| 283 | shift @named_kids; shift @named_kids; | ||
| 284 | } | ||
| 285 | |||
| 286 | my @imports; | ||
| 287 | |||
| 288 | while( @named_kids ) { | ||
| 289 | my $name = shift @named_kids; | ||
| 290 | my $kid = shift @named_kids; | ||
| 291 | |||
| 292 | last if $kid->type eq ";"; | ||
| 293 | |||
| 294 | push @imports, $self->extract_literal_list( $kid ); | ||
| 295 | } | ||
| 296 | |||
| 297 | return @imports; | ||
| 298 | } | ||
| 299 | |||
| 300 | method extract_literal_list ( $node ) | ||
| 301 | { | ||
| 302 | match( my $type = $node->type : eq ) { | ||
| 303 | case( "string_literal" ) { | ||
| 304 | my $val = $node->text; | ||
| 305 | $val =~ s/^'(.*)'$/$1/; | ||
| 306 | $val =~ s/q\s*.(.*).$/$1/; | ||
| 307 | return $val; | ||
| 308 | } | ||
| 309 | case( "interpolated_string_literal" ) { | ||
| 310 | my $val = $node->text; | ||
| 311 | $val =~ s/^"(.*)"$/$1/; | ||
| 312 | $val =~ s/qq\s*.(.*).$/$1/; | ||
| 313 | # TODO: This check isn't very good; could be improved | ||
| 314 | $val =~ m/\$/ and | ||
| 315 | warn( "Unable to handle interpolation in string literal" ), next; | ||
| 316 | return $val; | ||
| 317 | } | ||
| 318 | case( "quoted_word_list" ) { | ||
| 319 | my $val = $node->text; | ||
| 320 | $val =~ s/qw\s*.(.*).$/$1/; | ||
| 321 | |||
| 322 | return grep { length } split /\s+/, $val; | ||
| 323 | } | ||
| 324 | case( "list_expression" ) { | ||
| 325 | return map { $_->type eq "," ? () : $self->extract_literal_list( $_ ) } $node->child_nodes; | ||
| 326 | } | ||
| 327 | default { | ||
| 328 | warn "Unsure how to handle a ($type) node"; | ||
| 329 | return (); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | =head1 AUTHOR | ||
| 335 | |||
| 336 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | 0x55AA; | ||