I wrote a code to analyize javascript code lexeme with c++. Below is the corresponding function. For reference, Kind
is a enum class that contains kinds of lex, and definition of a function toKind
is below of the function analyzeLexeme
.
auto analyzeLexeme(std::wstring & source_code) -> std::vector<Token> {
using std::__1::wregex;
using std::regex_search;
using std::vector;
using std::wsmatch;
using std::wstring;
using std::string;
using std::tuple;
using std::map;
wsmatch matched;
Kind result_kind;
map<string, bool> context = { { "in_tmplt", false }, };
auto result = vector<Token>();
auto reSearch = [&source_code, &matched] (const wregex & re) {
return regex_search(source_code, matched, re);
};
source_code += L'\0';
for (; source_code[0]; source_code = matched.suffix()) {
if (reSearch(kReWhiteSpace)) continue;
result.push_back({(
reSearch(kReNumLiteral) ? [] { return Kind::NumberLiteral; } :
reSearch(kReStrLiteral) ? [] { return Kind::StringLiteral; } :
reSearch(kReTmpltHdLiteral) && !context["in_tmplt"] ?
[&] {
context["in_tmplt"] = true;
return Kind::TemplateHeadLiteral;
} :
reSearch(kReTmpltTlLiteral) && context["in_tmplt"] ?
[&] {
context["in_tmplt"] = false;
return Kind::TemplateTailLiteral;
} :
reSearch(kReTmpltMdLiteral) && context["in_tmplt"] ? [] { return Kind::TemplateMiddleLiteral; } :
reSearch(kReTmpltLiteral) ? [] { return Kind::TemplateLiteral; } :
reSearch(kReIdentifierKyword) ? [&] { return toKind(matched.str(), Kind::Identifier); } :
reSearch(kReOperatorBracket) ? [&] { return toKind(matched.str()); } :
[] { return Kind::Unknown; }
)(),
matched.str(),
});
if (result.back().kind == Kind::Unknown) {
std::wcerr << L"[ERR] : Unknown Token : " << source_code.substr(0, 40) << std::endl;
// fwprintf(stderr, L"[ERR] : Unknown Token : %s", source_code.substr(0, 40).c_str());
exit(1);
}
}
return result;
}
// str_to_kind is a map(string to kind).
auto toKind(std::wstring str, Kind default_kind) -> Kind {
return str_to_kind.count(str) ? str_to_kind.at(str) : default_kind;
}
Error is occurred here(reSearch(kReOperatorBracket) ? [&] { return toKind(matched.str()); }
). And reason is error: incompatible operand types ('(lambda at ~/projects/lexer.cc:69:72)' and '(lambda at ~/projects/lexer.cc:70:17)')
.
I do not understand it because return type of toKind
is Kind, so lambda function([&] { return toKind(matched.str())
) must return Kind value, and lambda function([] { return Kind::Unknown; }
) also has no choice but to return a value of type Kind.
At first I thought g++ couldn't interpret my code (in python, sometimes the interpreter can't interpret it if I write down the line I shouldn't write down). However, after many attempts, I found that it was not the main cause either.
Also, if the code was compiled, I would run gdb to debug it, but I couldn't do this either because it was a problem with the compilation process.