1 #####################################################################
3 # The Perl::Tidy::Debugger class shows line tokenization
5 #####################################################################
7 package Perl::Tidy::Debugger;
10 use English qw( -no_match_vars );
11 our $VERSION = '20230309';
13 use constant EMPTY_STRING => q{};
14 use constant SPACE => q{ };
18 my ( $class, $filename, $is_encoded_data ) = @_;
21 _debug_file => $filename,
22 _debug_file_opened => 0,
24 _is_encoded_data => $is_encoded_data,
28 sub really_open_debug_file {
31 my $debug_file = $self->{_debug_file};
32 my $is_encoded_data = $self->{_is_encoded_data};
33 my ( $fh, $filename ) =
34 Perl::Tidy::streamhandle( $debug_file, 'w', $is_encoded_data );
36 Perl::Tidy::Warn("can't open $debug_file: $ERRNO\n");
38 $self->{_debug_file_opened} = 1;
41 "Use -dump-token-types (-dtt) to get a list of token type codes\n");
43 } ## end sub really_open_debug_file
45 sub close_debug_file {
48 if ( $self->{_debug_file_opened} ) {
49 if ( !eval { $self->{_fh}->close(); 1 } ) {
51 # ok, maybe no close function
55 } ## end sub close_debug_file
57 sub write_debug_entry {
59 # This is a debug dump routine which may be modified as necessary
60 # to dump tokens on a line-by-line basis. The output will be written
61 # to the .DEBUG file when the -D flag is entered.
62 my ( $self, $line_of_tokens ) = @_;
64 my $input_line = $line_of_tokens->{_line_text};
66 my $rtoken_type = $line_of_tokens->{_rtoken_type};
67 my $rtokens = $line_of_tokens->{_rtokens};
68 my $rlevels = $line_of_tokens->{_rlevels};
70 my $input_line_number = $line_of_tokens->{_line_number};
71 my $line_type = $line_of_tokens->{_line_type};
75 my $token_str = "$input_line_number: ";
76 my $reconstructed_original = "$input_line_number: ";
78 my $pattern = EMPTY_STRING;
79 my @next_char = ( '"', '"' );
81 unless ( $self->{_debug_file_opened} ) { $self->really_open_debug_file() }
82 my $fh = $self->{_fh};
84 foreach my $j ( 0 .. @{$rtoken_type} - 1 ) {
87 if ( $rtoken_type->[$j] eq 'k' ) {
88 $pattern .= $rtokens->[$j];
91 $pattern .= $rtoken_type->[$j];
93 $reconstructed_original .= $rtokens->[$j];
94 $num = length( $rtokens->[$j] );
95 my $type_str = $rtoken_type->[$j];
97 # be sure there are no blank tokens (shouldn't happen)
98 # This can only happen if a programming error has been made
99 # because all valid tokens are non-blank
100 if ( $type_str eq SPACE ) {
101 $fh->print("BLANK TOKEN on the next line\n");
102 $type_str = $next_char[$i_next];
103 $i_next = 1 - $i_next;
106 if ( length($type_str) == 1 ) {
107 $type_str = $type_str x $num;
109 $token_str .= $type_str;
112 # Write what you want here ...
113 # $fh->print "$input_line\n";
114 # $fh->print "$pattern\n";
115 $fh->print("$reconstructed_original\n");
116 $fh->print("$token_str\n");
119 } ## end sub write_debug_entry