1 #####################################################################
3 # The Perl::Tidy::Debugger class shows line tokenization
5 #####################################################################
7 package Perl::Tidy::Debugger;
10 our $VERSION = '20220217';
14 my ( $class, $filename, $is_encoded_data ) = @_;
17 _debug_file => $filename,
18 _debug_file_opened => 0,
20 _is_encoded_data => $is_encoded_data,
24 sub really_open_debug_file {
27 my $debug_file = $self->{_debug_file};
28 my $is_encoded_data = $self->{_is_encoded_data};
29 my ( $fh, $filename ) =
30 Perl::Tidy::streamhandle( $debug_file, 'w', $is_encoded_data );
32 Perl::Tidy::Warn("can't open $debug_file: $!\n");
34 $self->{_debug_file_opened} = 1;
37 "Use -dump-token-types (-dtt) to get a list of token type codes\n");
41 sub close_debug_file {
44 my $fh = $self->{_fh};
45 if ( $self->{_debug_file_opened} ) {
46 if ( !eval { $self->{_fh}->close(); 1 } ) {
48 # ok, maybe no close function
54 sub write_debug_entry {
56 # This is a debug dump routine which may be modified as necessary
57 # to dump tokens on a line-by-line basis. The output will be written
58 # to the .DEBUG file when the -D flag is entered.
59 my ( $self, $line_of_tokens ) = @_;
61 my $input_line = $line_of_tokens->{_line_text};
63 my $rtoken_type = $line_of_tokens->{_rtoken_type};
64 my $rtokens = $line_of_tokens->{_rtokens};
65 my $rlevels = $line_of_tokens->{_rlevels};
66 my $rslevels = $line_of_tokens->{_rslevels};
67 my $rblock_type = $line_of_tokens->{_rblock_type};
69 my $input_line_number = $line_of_tokens->{_line_number};
70 my $line_type = $line_of_tokens->{_line_type};
74 my $token_str = "$input_line_number: ";
75 my $reconstructed_original = "$input_line_number: ";
76 my $block_str = "$input_line_number: ";
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 $block_str .= "($rblock_type->[$j])";
95 $num = length( $rtokens->[$j] );
96 my $type_str = $rtoken_type->[$j];
98 # be sure there are no blank tokens (shouldn't happen)
99 # This can only happen if a programming error has been made
100 # because all valid tokens are non-blank
101 if ( $type_str eq ' ' ) {
102 $fh->print("BLANK TOKEN on the next line\n");
103 $type_str = $next_char[$i_next];
104 $i_next = 1 - $i_next;
107 if ( length($type_str) == 1 ) {
108 $type_str = $type_str x $num;
110 $token_str .= $type_str;
113 # Write what you want here ...
114 # $fh->print "$input_line\n";
115 # $fh->print "$pattern\n";
116 $fh->print("$reconstructed_original\n");
117 $fh->print("$token_str\n");