Pattern line numbers

I use the Perl Template Toolkit to create C files. I really want to be able to include #line directives in my C code so that error messages from the C compiler send me to the right place (template file) and not to the wrong place (template output) . However, I do not know how to do this. The only result I got from Google is an unanswered message on the Template Toolkit mailing list.

I can imagine a painful decision, for example, to read the template file myself and add line numbers, but does anyone have a trick or even a reasonable way to get the line numbers of the source file in the Template Toolkit?

+5
source share
4 answers

It seems to me that the Template :: Parser location method returns a suitable #line directive, but there is no built-in function that I see to include it in the output. You will need to expand the Template Toolkit to do this.

+2
source

Since the number and the "file name" are completely composed (no matter what you want) in the #line directive, I suggest putting #line directives in the template using a slightly different context.

Instead of independently counting the lines in the template, what you could do even using the template pre-processor. I would "come up" with file names for different sections of the template and line numbers with small numbers that you can count.

"... 2 div id = 'topleft'"

0

This is not like Template :: Toolkit natively supports this. One thing you could do is make your program also record the matching of the generated strings in their strings in the corresponding template so that you can view the errors with errors with a simple script.

0
source

Brute force solution:

#!/usr/local/bin/perl
use warnings;
use strict;
my $file = "something.c";
open my $input, "<", $file or die $!;
while (<$input>) {
    print "#line $. \"$file\"\n";
    print;
}
close $input or die $!;

Better yet, check [%on the line and print #linewhere necessary.

0
source

All Articles