我想解析依赖于宏的代码,这些宏在解析之前不应被替换。
解析后我想:
- 漂亮地打印代码
- 提取一些信息
因此,我认为我需要一种方法来以某种方式“保留”宏,同时仍然扩展它以满足语法。
一些类似于我需要的示例代码:
在 SomeFile 中定义了一些宏:
// other code...
// macro definitions look like this:
// a bunch of #define statements inside {}
// So every "{" start a macro scope (except inside strings of course)
{
#define ASSERT if ((
#define IS_TRUE )==true) throw new AssertionFailed();
#define IS_FALSE )==false) throw new AssertionFailed();
#define MULTI_LINE_MACRO they \
always end in \
backslashes except the last line
}
// other code...
在我要解析的文件中:
// similar to "#include" in C
import SomeFile, SomeOtherFile ;
function Myfunction : void
ASSERT 1 == 1 IS_TRUE
end_function
例如,我想将其转换为以下内容:
import SomeFile, SomeOtherFile ;
function Myfunction : void
ASSERT
1 == 1
IS_TRUE
end_function
而且我还想检查宏是否使用正确(对于一些“已知”宏):
import SomeFile, SomeOtherFile ;
function Myfunction : void
// this should not be considered ok although it will compile fine
// It should be checked that ASSERT is followed by an expression followed by IS_TRUE
ASSERT 1 == 1 ));
// ^
// error should be here
end_function
理想情况下,它应该可以在宏定义源文件中配置哪些限制适用,因此如果可以动态进行检查将是一个优势。
{
#define ASSERT if ((
#define IS_TRUE )==true) throw new AssertionFailed();
#define IS_FALSE )==false) throw new AssertionFailed();
// ideally I could define some semantic constraints here:
//@@rule:: assertion : ASSERT expression (IS_TRUE|IS_FALSE) => statements
//this should place the "assertion" rule in the statements section of the grammar
}
// other code...