I have a parser for parsing an Identifier like foo, bar, baz
and one for parsing also nested identifiers like foo::bar, foo::bar.baz, foo::bar.baz.baham
They both parse into the same ast struct, which looks like this:
struct identifier : x3::position_tagged{
std::vector <std::string> namespaces;
std::vector <std::string> classes;
std::string identifier;
};
The parser for an identifier
looks like this:
#define VEC_ATR x3::attr(std::vector<std::string>({})) //ugly hack
auto const identifier_def =
VEC_ATR
>> VEC_ATR
>> id_string;
and for the nested_identifier
like this:
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
];
I know shame on me for the macro.
The identifier parser works fine, but the
nested_identifier
has a strange behaviour
if I try to parse something like foo::bar::baz
the ast objects which falls out of the parser, has all the namespaces, in this case foo
and bar
twice in the namespaces
vector.
I have a small example of this strange behaviour
here.
Can anybody explain me why this happens, and how I can avoid this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…