Neh*_*kar 5 c++ translation antlr
我正在使用 antlr 将自定义语言转换为 C++ 代码。在这种语言中,用户可以在$code...$endcode指令之间嵌入 C++ 代码片段,按原样插入到翻译后的 C++ 代码中。
我有以下问题:当代码片段中有错误时,我希望编译器指向源文件而不是翻译后的 C++ 代码。
我尝试按如下方式使用行指令,但没有奏效:
"foo.custom_laguage"
1 $code
2 ...some c++ code...
3 $endcode
Run Code Online (Sandbox Code Playgroud)
被翻译成
"auto-generated.cpp"
42 #line 2 "foo.custom_language"
43 ...some c++ code...
44 #line __LINE__ __FILE__
Run Code Online (Sandbox Code Playgroud)
这不起作用,我认为是因为 #line 指令会修改__LINE__宏稍后写入的内容。如何将行号设置回已翻译的 C++ 代码中的实际行号?antlr 是如何做到这一点的?
这是我希望自动生成的代码的样子:
"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 ...
Run Code Online (Sandbox Code Playgroud)
编辑: 我刚刚发现有一种方法可以通过使用#line default指令在 C# 语言中做到这一点:http :
//msdn.microsoft.com/en-us/library/34dk387t.aspx
但找不到任何类似的 C++
从问题中尚不清楚,但您正在生成#line自己给出的指令,source-line并且source-file?抱歉,我对 Antlr 不熟悉。
实际上,#line __LINE__ __FILE__除了将宏分配给自身之外什么也不做__LINE__。
由于预处理器的求值语义,您无法轻松地将 的数值分配__LINE__给宏。(您只能定义一个新宏来__LINE__按字面映射到该宏,并返回其当前值。)但是为什么需要它呢?除非Antlr本身使用该__LINE__宏,否则您不需要将其恢复为之前的值。
如果这是一个问题,最直接的解决方案是将原始 C++ 代码放在单独的include文件中并放弃内联嵌入。为了防止头文件的扩散,您可以使用类似的结构
$code
#define USE_SNIPPET_FOO
#include "snippets.h"
$endcode
$code
#define USE_SNIPPET_BAR
#include "snippets.h"
$endcode
Run Code Online (Sandbox Code Playgroud)
在标头中,有一种反向标头保护:
#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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
581 次 |
| 最近记录: |