Identifier Parsing - Redux
The ink hadn’t dried1 on my Identifier Parsing post when I realized that there was indeed a better way to handle multiple keywords.
In that post I stated that a symbols<T>
parser would not help because it suffered the same problem as lit()
. Which is true.
What I missed was that, of course, you could use the same trick with symbols as you did with lit()
to make it work.
Like this:
symbols<int>symtab;
symtabs.add("var")("func");
auto const reserved = lexeme[symtab >> !alnum];
auto const ident = lexeme[ +alnum - reserved ];
That does what we need.
And, we can fix up our lambda to automatically register new keywords.
auto mkkw = [](std::string kw) {
symtab.add(kw);
return lexeme[x3::lit(kw) >> !alnum];
};
Now, we can happily make up keywords and keep the rest of the parser in sync.
I will place a V4 in the Github repository.
Yea, I know. Work with me. ↩︎