Skip to content

Commit e750f69

Browse files
author
Chris White
committed
Bugfix to line-number counting
1 parent 2408d23 commit e750f69

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

lib/XML/Axk/Core.pm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ files.
126126
my $length_delta = 0; # how much to adjust pos($text) by
127127
#say "Old pos: ", $oldpos;
128128

129-
# Get line number in the original file
130-
my $orig_lineno = 1 + ( () = substr($text, 0, $idxes[0]) =~ /\n/g );
129+
# Get line number in the present, possibly modified, text
130+
my $curr_lineno = 1 + ( () = substr($text, 0, $idxes[0]) =~ /\n/g );
131131
# Thanks to ikegami, http://www.perlmonks.org/?node_id=968352
132-
$orig_lineno -= $lines_added;
132+
$curr_lineno -= $lines_added;
133133

134134
# Ln must be followed by whitespace and a newline.
135135
# This is to keep the line numbering vaguely consistent.
136136
my ($removed) = (substr($text, $idxes[1]) =~ s/\A(\h*\n)//);
137137

138138
unless($removed) {
139139
# Tell the caller where in the source file the problem is
140-
eval "#line $orig_lineno \"$fn\"\n" .
140+
eval "#line $curr_lineno \"$fn\"\n" .
141141
"die(\"L$lang indicator must be on its own line\");";
142142
die $@;
143143
}
@@ -149,10 +149,10 @@ files.
149149

150150
# End an existing capture if we're switching languages
151151
if($curr_trailer) {
152-
$replacement .= "\n$curr_trailer\n" .
153-
"#line $orig_lineno \"$fn\"\n";
152+
$replacement .= "$curr_trailer\n" .
153+
"#line $curr_lineno \"$fn\"\n";
154154
$curr_trailer='';
155-
++$lines_added;
155+
$lines_added += 2;
156156
}
157157

158158
# Does this language parse the source text itself?
@@ -171,7 +171,7 @@ files.
171171
$curr_trailer =
172172
"AXK_EMBEDDED_SOURCE_DO_NOT_TYPE_THIS_YOURSELF_OR_ELSE";
173173

174-
my $following_lineno = $orig_lineno+1;
174+
my $following_lineno = $curr_lineno+1;
175175
# Number of first line of the text in that language
176176
$replacement .=
177177
"use XML::Axk::L$lang \"$fn\", $following_lineno, " .

lib/XML/Axk/L0.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ sub import {
2323
);
2424
my $class = shift;
2525
my ($fn, $lineno, $source_text) = @_;
26-
say "Got source text at $fn:$lineno:\n-----------------\n$source_text\n-----------------";
26+
say "Got source text len ", length($source_text), " at $fn:$lineno:\n-----------------\n$source_text\n-----------------";
2727
} #import()
2828

2929
#}}}1

t/04-languages.t

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!perl
2+
# 04-languages.t: Multi-language support
3+
4+
use 5.018;
5+
use strict;
6+
use warnings;
7+
use Test::More; # tests=>27;
8+
use Capture::Tiny 'capture_stdout';
9+
use File::Spec;
10+
use XML::Axk::App;
11+
12+
sub localpath {
13+
state $voldir = [File::Spec->splitpath(__FILE__)];
14+
return File::Spec->catpath($voldir->[0], $voldir->[1], shift)
15+
}
16+
17+
# Inline script =================================================== {{{1
18+
{
19+
my $out = capture_stdout
20+
{ XML::Axk::App::Main(['--no-input', '-e',
21+
"say __LINE__;\nL1\nL0\nL1\nL0\nL0\nL1\nL1\nsay __LINE__;" ])
22+
};
23+
like($out, qr/1\n9\n\Z/, 'line number counting works');
24+
}
25+
26+
# }}}1
27+
28+
done_testing();
29+
30+
# vi: set ts=4 sts=4 sw=4 et ai fdm=marker fdl=1: #

0 commit comments

Comments
 (0)