使用antlr和line指令的C ++代码插入 [英] C++ Code insertion using antlr and the line directive
问题描述
我正在使用antlr将自定义语言转换为C ++代码.
用这种语言,用户可以在$code...$endcode
指令之间嵌入C ++代码段,这些指令原样插入到翻译后的C ++代码中.
I am using antlr to translate a custom language to C++ code.
In this language, user can embed C++ code snippets between $code...$endcode
directives, that gets inserted into the translated C++code as-is.
我有以下问题:当代码片段中有错误时,我希望编译器指向源文件,而不是指向翻译后的C ++代码.
I have the following problem: When there is an error in the code snippet I would like the compiler to point to the source file rather than the translated C++ code.
我尝试如下使用行指令,但是没有用:
I tried using line directives as follows, but it didn't work:
"foo.custom_laguage"
1 $code
2 ...some c++ code...
3 $endcode
获取翻译为
"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line __LINE__ __FILE__
这不起作用,我认为是因为#line指令会修改稍后由__LINE__
宏写入的内容.如何在翻译后的C ++代码中将行号设置回 actual 行号? antlr如何做到这一点?
This doesn't work, I think because #line directive modifies what gets written by the __LINE__
macro later. How can I set line number back to the actual line number in the translated C++ code? How does antlr do this?
这是我想要自动生成的代码的样子:
Here is what I want the auto-generated code to look like:
"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line 44 "auto-generated.cpp" //actual line in this file
45 ..some more C++ code ...
编辑:我刚刚发现有一种方法可以通过使用#line default
指令以C#语言实现:
http://msdn.microsoft.com/en-us/library/34dk387t.aspx
但是找不到与C ++类似的东西
edit: I just found out that there is a way to do this in C# language by using a #line default
directive:
http://msdn.microsoft.com/en-us/library/34dk387t.aspx
But couldn't find anything similar for C++
推荐答案
这个问题尚不清楚,但是您是自己给source-line
和source-file
生成#line
指令的吗?对不起,我对Antlr不熟悉.
It's not clear from the question, but you're generating the #line
directives yourself given source-line
and source-file
? I'm sorry, I'm not familiar with Antlr.
实际上,#line __LINE__ __FILE__
除了将__LINE__
宏分配给自身外,什么也不做.
Indeed, #line __LINE__ __FILE__
does nothing but assign the __LINE__
macro to itself.
由于预处理器的评估语义,您不能轻松地将__LINE__
的数值分配给宏. (您只能定义一个新的宏以字面意义映射到__LINE__
宏,并返回其 current 值.)但是,为什么需要这样做呢?除非Antlr本身使用__LINE__
宏,否则您无需将其还原为以前的值.
Because of the evaluation semantics of the preprocessor, you can't easily assign the numeric value of __LINE__
to a macro. (You can only define a new macro to map to the macro __LINE__
literally, returning its current value.) But why do you need that? Unless Antlr itself uses the __LINE__
macro, you don't need to restore it to its previous value.
如果遇到问题,最直接的解决方案是将原始C ++代码放入单独的include
文件中,并放弃内联嵌入.为了防止头文件扩散,您可以使用类似
If it is an issue, the most straightforward solution would be to put the raw C++ code in separate include
files and forgo the inline embedding. To prevent proliferation of header files, you could use a construct like
$code
#define USE_SNIPPET_FOO
#include "snippets.h"
$endcode
$code
#define USE_SNIPPET_BAR
#include "snippets.h"
$endcode
在标头中,还有一种反向标头保护:
and in the header, a kind of reverse header guard:
#ifdef USE_SNIPPET_FOO
#undef USE_SNIPPET_FOO
class foo {};
#elif defined USE_SNIPPET_BAR
#undef USE_SNIPPET_BAR
class bar {};
#else
#error no snippet selected
#endif
这篇关于使用antlr和line指令的C ++代码插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!