| 1 | package Text::MicroMason::LineNumbers; | ||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | ###################################################################### | ||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | sub read { | ||||||||||||||||||||||||||||
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 7 | 10 | my ( $self, $src_type, $src_data ) = @_; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 10 | $self->{ last_read_file } = "unrecognized source $src_type"; | |||||||||||||||||||||||||||||
| 10 | 10 | $self->{ last_read_line } = 1; | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 10 | $self->NEXT( 'read', $src_type, $src_data ) | |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub read_file { | ||||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 16 | 3 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | 3 | $self->{ last_read_file } = $file; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 3 | $self->NEXT( 'read_file', $file ) | |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub read_handle { | ||||||||||||||||||||||||||||
|
Subroutine
read_handle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 24 | 0 | my ( $self, $handle ) = @_; | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 0 | my ( $caller_file, $caller_line ) = $self->_get_external_caller(); | |||||||||||||||||||||||||||||
| 27 | 0 | $self->{ last_read_file } = "file handle template (compiled at $caller_file line $caller_line)"; | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | 0 | $self->NEXT( 'read_handle', $handle ) | |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | sub read_text { | ||||||||||||||||||||||||||||
|
Subroutine
read_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 33 | 4 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 4 | my ( $caller_file, $caller_line ) = $self->_get_external_caller(); | |||||||||||||||||||||||||||||
| 36 | 4 | $self->{ last_read_file } = "text template (compiled at $caller_file line $caller_line)"; | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 4 | $self->NEXT( 'read_text', $text ) | |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | sub read_inline { | ||||||||||||||||||||||||||||
|
Subroutine
read_inline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 42 | 3 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 3 | my ( $caller_file, $caller_line ) = $self->_get_external_caller(); | |||||||||||||||||||||||||||||
| 45 | 3 | $self->{ last_read_file } = $caller_file; | |||||||||||||||||||||||||||||
| 46 | 3 | $self->{ last_read_line } = $caller_line; | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | 3 | $self->NEXT( 'read_text', $text ) | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | sub _get_external_caller { | ||||||||||||||||||||||||||||
|
Subroutine
_get_external_caller: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 52 | 7 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 53 | 7 | my ( @caller, $call_level ); | |||||||||||||||||||||||||||||
| 54 | 7 | ▶ | do { @caller = caller( ++ $call_level ) } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0])
MC/DC100%2/2
$caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0])
$caller[0] =~ /^Text::MicroMason/
$self->isa($caller[0])
Truth table100%3/3
A or B
A= $caller[0] =~ /^Text::MicroMason/B= $self->isa($caller[0])
| |||||||||||||||||||||||||||||||
| 55 | while ( $caller[0] =~ /^Text::MicroMason/ or $self->isa($caller[0]) ); | ||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | return ( $caller[1] || $0, $caller[2] ); | ||||||||||||||||||||||||||||
|
Condition33%1/3
$caller[1] || $0
MC/DC0%0/2
$caller[1] || $0
$caller[1]
$0
Truth table33%1/3
A || B
A= $caller[1]B= $0
| |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||
| 59 | ###################################################################### | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | sub lex { | ||||||||||||||||||||||||||||
|
Subroutine
lex: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 62 | 10 | my $self = shift; | |||||||||||||||||||||||||||||
| 63 | 10 | local $_ = "$_[0]"; | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | my $lexer = $self->can('lex_token') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 66 | or $self->croak_msg('Unable to lex_token(); must select a syntax mixin'); | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 10 | ▶ | my $filename = $self->{ last_read_file } || 'unknown source'; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'last_read_file'} || 'unknown source'
MC/DC0%0/1
$self->{'last_read_file'} || 'unknown source'
$self->{'last_read_file'}
Truth table50%1/2
A
A= $self->{'last_read_file'}
| |||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | my $linenum = $self->{ last_read_line } || 1; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'last_read_line'} || 1
MC/DC0%0/1
$self->{'last_read_line'} || 1
$self->{'last_read_line'}
Truth table50%1/2
A
A= $self->{'last_read_line'}
| |||||||||||||||||||||||||||||||
| 70 | 10 | my $last_pos = 0; | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 10 | my @tokens; | |||||||||||||||||||||||||||||
| 73 | 10 | ▶ | until ( /\G\z/gc ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 74 | 38 | ▶ | my @parsed = &$lexer( $self ) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
(my(@parsed) = &$lexer($self)) || /\G ( .{0,20} ) /cgsx && die("MicroMason parsing halted at '$1'\n")
/\G ( .{0,20} ) /cgsx and die "MicroMason parsing halted at '$1'\n"
MC/DC0%0/2
(my(@parsed) = &$lexer($self)) || /\G ( .{0,20} ) /cgsx && die("MicroMason parsing halted at '$1'\n")
(my(@parsed) = &$lexer($self))
/\G ( .{0,20} ) /cgsx
Truth table33%1/3
A || B
A= (my(@parsed) = &$lexer($self))B= /\G ( .{0,20} ) /cgsx
| |||||||||||||||||||||||||||||||
| 75 | or /\G ( .{0,20} ) /gcxs && die "MicroMason parsing halted at '$1'\n"; | ||||||||||||||||||||||||||||||
| 76 | 38 | push @tokens, 'line_num' => ( $linenum - 1 ) . qq{ "$filename"}; | |||||||||||||||||||||||||||||
| 77 | 38 | push @tokens, @parsed; | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | # Update the current line number by counting newlines in the text | ||||||||||||||||||||||||||||||
| 80 | # we've parsed since the last time through the loop. | ||||||||||||||||||||||||||||||
| 81 | 38 | ▶ | my $new_pos = pos($_) || 0; | ||||||||||||||||||||||||||||
|
Condition50%1/2
pos $_ || 0
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
| |||||||||||||||||||||||||||||||
| 82 | 38 | $linenum += ( substr($_, $last_pos, $new_pos - $last_pos) =~ tr[\n][] ); | |||||||||||||||||||||||||||||
| 83 | 38 | $last_pos = $new_pos; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 10 | return @tokens; | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 10 | ▶ | sub assembler_rules { | ||||||||||||||||||||||||||||
|
Subroutine
assembler_rules: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 90 | 10 | my $self = shift; | |||||||||||||||||||||||||||||
| 91 | ( | ||||||||||||||||||||||||||||||
| 92 | 10 | $self->NEXT('assembler_rules', @_), | |||||||||||||||||||||||||||||
| 93 | line_num_token => 'perl # line TOKEN', | ||||||||||||||||||||||||||||||
| 94 | ) | ||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | ###################################################################### | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | ###################################################################### | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | Text::MicroMason::LineNumbers - Report errors at correct source code line numbers | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | This mixin class associates each token in a template with the line | ||||||||||||||||||||||||||||||
| 112 | number on which it was found, and then inserts special comments in the | ||||||||||||||||||||||||||||||
| 113 | generated Perl code that preserve that original source file and line | ||||||||||||||||||||||||||||||
| 114 | number information. | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | This should facilitate debugging, by making it easier to match up run- | ||||||||||||||||||||||||||||||
| 117 | time errors with the template code that produced them. | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | To turn this behavior on, just add "-LineNumbers" to your MicroMason | ||||||||||||||||||||||||||||||
| 120 | creation call: | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | my $mason = Text::MicroMason->new( qw( -LineNumbers ) ); | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | =head2 Public Methods | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | These methods are called from within the normal flow of MicroMason | ||||||||||||||||||||||||||||||
| 128 | functionality, and you do not need to invoke them directly. | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | =over 4 | ||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | =item read() | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | Clears the variables used to store the file name and first line of a | ||||||||||||||||||||||||||||||
| 135 | template, so that they can be set by the methods below. | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | =item read_file() | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | Saves the source file name before invoking the standard behavior for this method. | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | $mason->compile( file => $filename ); | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | =item read_handle() | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | Saves the caller's file name before invoking the standard behavior for this method. | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | $mason->compile( handle => $filename ); | ||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||
| 149 | =item read_text() | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | Saves the caller's file name before invoking the standard behavior for this method. | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | $mason->compile( text => $filename ); | ||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | =item read_inline() | ||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | This is similar to read_text, except it adjusts the line numbering to | ||||||||||||||||||||||||||||||
| 158 | reflect a template that's embdded as a literal text in the Perl code. | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | $mason->compile( inline => q{ | ||||||||||||||||||||||||||||||
| 161 | My template text goes here. | ||||||||||||||||||||||||||||||
| 162 | } ); | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | =item lex() | ||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | Identical to the lex() method provided by the Base class, except that it | ||||||||||||||||||||||||||||||
| 167 | also inserts a stream of line-number-setting comments into the to-be- | ||||||||||||||||||||||||||||||
| 168 | compiled Perl code that attempt to re-synchronize the | ||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | =item assembler_rules() | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | Maps the "line_num" token to a perl line number comment. | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | =head2 Private Methods | ||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||
| 179 | =over 4 | ||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||
| 181 | =item _get_external_caller() | ||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | Returns the source file and line number of the first item in the | ||||||||||||||||||||||||||||||
| 184 | function call stack that is not a Text::MicroMason package. | ||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | For an overview of this templating framework, see L<Text::MicroMason>. | ||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | This is a mixin class intended for use with L<Text::MicroMason::Base>. | ||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||
| 195 | For distribution, installation, support, copyright and license | ||||||||||||||||||||||||||||||
| 196 | information, see L<Text::MicroMason::Docs::ReadMe>. | ||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||