Permalink
Browse files

include line number in error message

  • Loading branch information...
1 parent 8e6f785 commit 998bc6aa872e93f08d286a72f8859d3a3568a4b7 @moritz committed Jan 3, 2010
Showing with 43 additions and 8 deletions.
  1. +43 −8 lib/Mowyw.pm
View
@@ -120,10 +120,26 @@ sub strip_ws {
return $s;
}
+sub escape {
+ my $str = shift;
+ my %esc = (
+ "\\" => '\\\\',
+ "\t" => '\t',
+ "\n" => '\n',
+ );
+ my $re = join '|', map quotemeta, keys %esc;
+ $str =~ s/($re)/$esc{$1}/g;
+ return $str;
+}
+
sub parse_error {
my $message = shift;
my @filenames = @{shift()};
+ my $token = shift;
my $str = "Parse error in file '$filenames[0]': $message\n";
+ if ($token) {
+ $str .= "in line $token->[3] near'" . escape($token->[0]) ."'\n";
+ }
for (@filenames[0..$#filenames]) {
$str .= " ...included from file '$_'\n";
}
@@ -256,8 +272,11 @@ sub p_for {
my $contents = strip_ws(slurp_upto_token($tokens, 'TAG_END', $meta));
my ($iter, $in, $datasource) = split m/\s+/, $contents;
if (!defined $datasource || lc $in ne 'in' ){
- parse_error(q{Can't parse for statement. Syntax is [% for iterator_var in datasource %] ... [% endfor %]},
- $meta->{FILES});
+ parse_error(
+ q{Can't parse for statement. Syntax is [% for iterator_var in datasource %] ... [% endfor %]},
+ $meta->{FILES},
+ $tokens->[0],
+ );
}
my $ds = $meta->{VARS}{$datasource};
if (!$ds || !blessed($ds)){
@@ -282,8 +301,11 @@ sub p_ifvar {
my ($tokens, $meta) = @_;
my $contents = strip_ws(slurp_upto_token($tokens, 'TAG_END', $meta));
if ($contents =~ m/\s/){
- parse_error(q{Parse error in 'ifvar' tag. Syntax is [% ifvar variable %] .. [% endifvar %]},
- $meta->{FILES});
+ parse_error(
+ q{Parse error in 'ifvar' tag. Syntax is [% ifvar variable %] .. [% endifvar %]},
+ $meta->{FILES},
+ $tokens->[0],
+ );
}
my $c = do {
local $meta->{NO_VAR_WARN} = 1;
@@ -488,7 +510,11 @@ sub p_syntaxfile {
p_expect($tokens, "TAG_END", $meta);
my @t = split m/\s+/, $tag_content;
if (scalar @t != 2){
- parse_error("Usage of syntaxfile tag: [[[syntaxfile <filename> <language>", $meta->{FILES});
+ parse_error(
+ "Usage of syntaxfile tag: [[[syntaxfile <filename> <language>",
+ $meta->{FILES},
+ $tokens->[0],
+ );
}
}
@@ -553,10 +579,15 @@ sub p_expect {
shift @$tokens;
return $val;
} else {
- parse_error("Expected '$e_val', got $tokens->[0][1]\n", $meta->{FILES});
+ parse_error("Expected '$e_val', got $tokens->[0][1]\n",
+ $meta->{FILES}, $tokens->[0]);
}
}
- parse_error("Expected token $expect, got $tokens->[0]->[0]\n", $meta->{FILES});
+ parse_error(
+ "Expected token $expect, got $tokens->[0]->[0]\n",
+ $meta->{FILES},
+ $tokens->[0],
+ );
}
@@ -591,7 +622,11 @@ sub parse_tokens {
my ($tag, $prior_tag) = @_;
return sub {
my ($tokens, $meta) = @_;
- parse_error("Unexpected tag '$tag' without prior '$prior_tag'", $meta->{FILES});
+ parse_error(
+ "Unexpected tag '$tag' without prior '$prior_tag'",
+ $meta->{FILES},
+ $tokens->[0],
+ );
}
};

0 comments on commit 998bc6a

Please sign in to comment.