带有模板工具包的行号
我正在使用 Perl 模板工具包生成 C 文件。我非常希望能够在我的 C 代码中包含 #line 指令,以便来自 C 编译器的错误消息将我发送到正确的位置(模板文件)而不是错误的位置(模板输出)。但是,我不知道该怎么做。我从 Google 得到的唯一结果是 Template Toolkit 邮件列表上有一条未回复的消息。
我可以想象一个痛苦的解决方案,例如自己读取模板文件并添加行号,但是有人有技巧甚至明智的方法来获取 Template Toolkit 中原始文件的行号吗?
I am using the Perl Template Toolkit to generate C files. I dearly 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 (the template file) rather than the wrong place (the template output). However, I don't know how to do this. The only result I got from Google was an unanswered message on the Template Toolkit mailing list.
I can imagine a tortured solution like reading the template file myself and adding line numbers, but does anyone have a trick or even a sensible method of getting the line numbers of the original file in Template Toolkit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,Template::Parser 的 location 方法返回一个合适的 #line 指令,但我看到没有任何内置函数将其包含在输出中。您必须扩展 Template Toolkit 才能做到这一点。
Looks to me like Template::Parser's location method returns a suitable #line directive, but there isn't any built in function I see to include that in the output. You'd have to extend Template Toolkit to make it do so.
由于数字和“文件名”是在 #line 指令中完全组成的(无论你想要什么),
我建议使用稍微不同的上下文将 #line 指令放入模板中。
您可以自己计算模板中的行数,甚至可以使用模板预处理器。我会为模板的不同部分“发明”文件名,并用可以数的小数字对这些行进行编号。
然后错误消息可能会显示“... in line 2 of div id='topleft'”
Since number and "filename" are totally made up (whatever you want) in the #line directive,
I suggest putting #line directives in the template using a slightly different context.
Rather than counting lines in the template yourself, which you could do, even using a template pre-processor. I would 'invent' file names for the different sections of the template and number thos lines with small numbers that you can count.
Then an error message might say "... in line 2 of div id='topleft'"
看起来 Template::Toolkit 本身并不支持这一点。您可以做的一件事是让您的程序也编写生成的行到相应模板中的行的映射,以便您可以使用简单的脚本查找错误。
It doesn't look like Template::Toolkit natively supports this. One thing that you could do is make your program also write a mapping of generated lines to their lines in the appropriate template, so that you can look the errors up the errors with a simple script.
暴力解决方案:
更好的是,测试行上的
[%
],并仅在必要时打印#line
。A brute-force solution:
Even better, test for
[%
on the line and only print#line
s where necessary.