I'm trying to get Boost.Spirit to parse MSVC mangled symbols. These take the form:
?myvolatileStaticMember@myclass@@2HC
which means "volatile int myclass::myvolatileStaticMember".
The "key" to the parse is the double at symbol "@@". Prior to the @@ is the name of the symbol which consists of a C++ identifier followed by zero or more "@" additional identifiers to fully represent the symbol in its absolute namespace hierarchy. After the @@ is the specification of what the identifier is (a variable, a function etc.)
Now, I can get Boost.Spirit to parse either the part preceding the @@ or the part after the @@. I haven't figured out yet how to get Boost.Spirit to find the @@ and feed what comes before that to one custom parser and what comes after it to a different custom parser.
Here's my parser for the part preceding the @@:
// This grammar is for a MSVC mangled identifier
template<typename iterator> struct msvc_name : grammar<iterator, SymbolType(), locals<SymbolType, string>>
{
SymbolTypeDict &typedict;
void name_writer(SymbolType &val, const string &i) const { val.name=i; }
void dependent_writer(SymbolType &val, const string &i) const
{
SymbolTypeDict::const_iterator dt=typedict.find(i);
if(dt==typedict.end())
{
auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Namespace, i)));
dt=_dt.first;
}
val.dependents.push_back(&dt->second);
}
// These work by spreading the building of a templated type over multiple calls using local variables _a and _b
// We accumulate template parameters into _a and accumulate mangled symbolness into _b
void begin_template_dependent_writer(SymbolType &, SymbolType &a, string &b, const string &i) const
{
a=SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Class, i);
b=i;
}
void add_template_constant_dependent_writer(SymbolType &a, string &b, long long constant) const
{
string i("_c"+to_string(constant));
SymbolTypeDict::const_iterator dt=typedict.find(i);
if(dt==typedict.end())
{
auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Constant, to_string(constant))));
dt=_dt.first;
}
a.templ_params.push_back(&dt->second);
b.append(i);
}
void add_template_type_dependent_writer(SymbolType &a, string &b, SymbolTypeType type) const
{
string i("_t"+to_string(static_cast<int>(type)));
SymbolTypeDict::const_iterator dt=typedict.find(i);
if(dt==typedict.end())
{
auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, type)));
dt=_dt.first;
}
a.templ_params.push_back(&dt->second);
b.append(i);
}
void finish_template_dependent_writer(SymbolType &val, SymbolType &a, string &b) const
{
SymbolTypeDict::const_iterator dt=typedict.find(b);
if(dt==typedict.end())
{
auto _dt=typedict.emplace(make_pair(b, a));
dt=_dt.first;
}
val.dependents.push_back(&dt->second);
}
msvc_name(SymbolTypeDict &_typedict) : msvc_name::base_type(start), typedict(_typedict)
{
identifier=+(char_ - '@');
identifier.name("identifier");
template_dependent_identifier=+(char_ - '@');
template_dependent_identifier.name("template_dependent_identifier");
dependent_identifier=+(char_ - '@');
dependent_identifier.name("dependent_identifier");
start = identifier [ boost::phoenix::bind(&msvc_name::name_writer, this, _val, _1) ] >> *(
lit("@@") >> eps
| (("@?$" > template_dependent_identifier [ boost::phoenix::bind(&msvc_name::begin_template_dependent_writer, this, _val, _a, _b, _1) ])
> "@" > +(( "$0" > constant [ boost::phoenix::bind(&msvc_name::add_template_constant_dependent_writer, this, _a, _b, _1) ])
| type [ boost::phoenix::bind(&msvc_name::add_template_type_dependent_writer, this, _a, _b, _1) ])
>> eps [ boost::phoenix::bind(&msvc_name::finish_template_dependent_writer, this, _val, _a, _b) ])
| ("@" > dependent_identifier [ boost::phoenix::bind(&msvc_name::dependent_writer, this, _val, _1) ]))
;
BOOST_SPIRIT_DEBUG_NODE(start);
start.name("msvc_name");
on_error<boost::spirit::qi::fail, iterator>(start,
cerr << boost::phoenix::val("Parsing error: Expected ") << _4 << boost::phoenix::val(" here: \"")
<< boost::phoenix::construct<string>(_3, _2) << boost::phoenix::val("\"") << endl);
}
rule<iterator, SymbolType(), locals<SymbolType, string>> start;
rule<iterator, string()> identifier, template_dependent_identifier, dependent_identifier;
msvc_type type;
msvc_constant<iterator> constant;
};
You'll note the "lit("@@") >> eps" where I'm trying to get it to stop matching once it sees a @@. Now here is the part which is supposed to match a full mangled symbol:
template<typename iterator> struct msvc_symbol : grammar<iterator, SymbolType()>
{
SymbolTypeDict &typedict;
/* The key to Microsoft symbol mangles is the operator '@@' which consists of a preamble
and a postamble. Immediately following the '@@' operator is:
Variable:
3<type><storage class>
Static member variable:
2<type><storage class>
Function:
<near|far><calling conv>[<stor ret>] <return type>[<parameter type>...]<term>Z
<Y |Z ><A|E|G >[<?A|?B|?C|?D>]<MangledToSymbolTypeType...> <@>Z
Member Function:
<protection>[<const>]<calling conv>[<stor ret>] <return type>[<parameter type>...]<term>Z
<A-V >[<A-D> ]<A|E|G >[<?A|?B|?C|?D>]<MangledToSymbolTypeType...> <@>Z
*/
msvc_symbol(SymbolTypeDict &_typedict) : msvc_symbol::base_type(start), typedict(_typedict), name(_typedict), variable(_typedict)
{
start="?" >> name >> ("@@" >> variable);
BOOST_SPIRIT_DEBUG_NODE(start);
on_error<boost::spirit::qi::fail, iterator>(start,
cerr << boost::phoenix::val("Parsing error: Expected ") << _4 << boost::phoenix::val(" here: \"")
<< boost::phoenix::construct<string>(_3, _2) << boost::phoenix::val("\"") << endl);
}
rule<iterator, SymbolType()> start;
msvc_name<iterator> name;
msvc_variable<iterator> variable;
};
So, it matches the "?" easily enough ;). The problem is that it sends everything after the "?" to the msvc_name parser, so instead of the bit from @@ onwards going to msvc_variable and the remainder going to msvc_name, msvc_name consumes everything up to and including @@. This isn't intuitive, as one would have thought that the brackets mean to do that thing first.
Therefore if I replace:
start="?" >> name >> ("@@" >> variable);
with
start="?" >> name >> variable;
... it all works fine.
However I'd really prefer not to do it this way. Ideally, I want Boost.Spirit to split at @@ cleanly in msvc_symbol and "do the right thing" as it were. I'm thinking I'm probably not thinking recursively enough? Either way I'm stumped.
Note: Yes I am aware I can break the string at @@ and run two separate parsers. That isn't what I'm asking - rather, I'm asking how to configure Boost.Spirit to parse the end of a phrase before the beginning.
Note also: I'm aware a skipper could be used to make the @@ whitespace and do the split that way. The problem is that what comes before the @@ is very specific, as is what comes after the @@. Therefore it's not really whitespace.
Many thanks in advance to anyone who can help. From searching Google and Stackoverflow for questions related to this one, overcoming Boost.Spirit's "left-to-right greediness" is a problem for a lot of people.
Niall
It would seem that you can do a number of things:
explicitely disallow the double "@@" where you expect "@". See also
tokenize first (use Spirit Lex?)
Here I show you a working example of the first approach:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
template<typename T> T reversed(T c) { return T(c.rbegin(), c.rend()); }
int main (int argc, char** argv)
{
const std::string input("?myvolatileStaticMember@myclass@@2HC");
auto f = begin(input), l = end(input);
auto identifier = +~qi::char_("@");
auto delimit = qi::lit("@") - "@@";
std::vector<std::string> qualifiedName;
std::string typeId;
if (qi::parse(f,l,
'?' >> identifier % delimit >> "@@" >> +qi::char_,
qualifiedName,
typeId))
{
using namespace boost::spirit::karma;
qualifiedName = reversed(qualifiedName);
std::cout << "Qualified name: " << format(auto_ % "::" << "\n", qualifiedName);
std::cout << "Type indication: '" << typeId << "'\n";
}
}
Output:
Qualified name: myclass::myvolatileStaticMember
Type indication: '2HC'