我有一个解析器用于解析标识符foo, bar, baz
,一个解析器也解析嵌套标识符,foo::bar, foo::bar.baz, foo::bar.baz.baham
它们都解析为相同的 ast 结构,如下所示:
struct identifier : x3::position_tagged{
std::vector <std::string> namespaces;
std::vector <std::string> classes;
std::string identifier;
};
an 的解析器identifier
如下所示:
#define VEC_ATR x3::attr(std::vector<std::string>({})) //ugly hack
auto const identifier_def =
VEC_ATR
>> VEC_ATR
>> id_string;
对于nested_identifier
这样的:
auto const nested_identifier_def =
x3::lexeme[
(+(id_string >> "::") >> +(id_string >> ".") > id_string)
| (+(id_string >> "::") >> VEC_ATR > id_string)
| (VEC_ATR >> +(id_string >> ".") > id_string)
| identifier
];
我知道我为宏感到羞耻。标识符解析器工作正常,但是
nested_identifier
如果我尝试解析像foo::bar::baz
ast 对象这样掉出解析器的东西,它有一个奇怪的行为,它有所有的命名空间,在这种情况下foo
和向量bar
中的两次。namespaces
我在
这里有一个这种奇怪行为的小例子。谁能解释我为什么会发生这种情况,以及如何避免这种情况?